001 // This file is part of the program FRYSK. 002 // 003 // Copyright 2008 Red Hat Inc. 004 // 005 // FRYSK is free software; you can redistribute it and/or modify it 006 // under the terms of the GNU General Public License as published by 007 // the Free Software Foundation; version 2 of the License. 008 // 009 // FRYSK is distributed in the hope that it will be useful, but 010 // WITHOUT ANY WARRANTY; without even the implied warranty of 011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 // General Public License for more details. 013 // 014 // You should have received a copy of the GNU General Public License 015 // along with FRYSK; if not, write to the Free Software Foundation, 016 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 017 // 018 // In addition, as a special exception, Red Hat, Inc. gives You the 019 // additional right to link the code of FRYSK with code not covered 020 // under the GNU General Public License ("Non-GPL Code") and to 021 // distribute linked combinations including the two, subject to the 022 // limitations in this paragraph. Non-GPL Code permitted under this 023 // exception must only link to the code of FRYSK through those well 024 // defined interfaces identified in the file named EXCEPTION found in 025 // the source code files (the "Approved Interfaces"). The files of 026 // Non-GPL Code may instantiate templates or use macros or inline 027 // functions from the Approved Interfaces without causing the 028 // resulting work to be covered by the GNU General Public 029 // License. Only Red Hat, Inc. may make changes or additions to the 030 // list of Approved Interfaces. You must obey the GNU General Public 031 // License in all respects for all of the FRYSK code and other code 032 // used in conjunction with FRYSK except the Non-GPL Code covered by 033 // this exception. If you modify this file, you may extend this 034 // exception to your version of the file, but you are not obligated to 035 // do so. If you do not wish to provide this exception without 036 // modification, you must delete this exception statement from your 037 // version and license this file solely under the GPL without 038 // exception. 039 040 package frysk.proc; 041 042 import java.util.HashMap; 043 import java.util.Map.Entry; 044 import java.util.Iterator; 045 import java.util.Set; 046 047 /** 048 * The environment vector. 049 */ 050 public class Environ { 051 private final HashMap environ; 052 053 /** 054 * Create a new empty environment. 055 */ 056 public Environ() { 057 environ = new HashMap(); 058 } 059 /** 060 * Create a new environment populated by the existing environ. 061 */ 062 public Environ(String[] environ) { 063 this(); 064 put(environ); 065 } 066 067 /** 068 * Return the environ as a string array. 069 */ 070 public String[] toStringArray() { 071 Set entries = environ.entrySet(); 072 String[] env = new String[environ.size()]; 073 int j = 0; 074 for (Iterator i = entries.iterator(); i.hasNext(); ) { 075 Entry e = (Entry)i.next(); 076 String name = (String)e.getKey(); 077 String value = (String)e.getValue(); 078 env[j++] = name + "=" + value; 079 } 080 return env; 081 } 082 083 /** 084 * Get an environment variable. 085 * @param name is the environment variable name. 086 * @return the value of the variable. 087 */ 088 public String get(String name) { 089 return (String)environ.get(name); 090 } 091 092 /** 093 * Put the variable into the environ set with the provided value. 094 * @param name is the environment variable name. 095 * @param value is the environment variable value. 096 */ 097 public void put(String name, String value) { 098 environ.put(name, value); 099 } 100 101 /** 102 * Decode then add an environment variable. 103 * @param name is the variable=value pair. 104 */ 105 public void put(String name) { 106 String[] member = name.split("="); 107 if (member.length == 2) { 108 environ.put(member[0], member[1]); 109 } else { 110 environ.put(member[0], ""); 111 } 112 } 113 114 /** 115 * Put all elements of the the ENVIRON array into the ENVIRON set. 116 */ 117 public void put(String[] environ) { 118 for (int i = 0; i < environ.length; i++) { 119 put(environ[i]); 120 } 121 } 122 123 /** 124 * Delete the entry. 125 */ 126 public void remove(String name) { 127 environ.remove(name); 128 } 129 }