1 37 90 package net.sourceforge.cruisecontrol.util; 91 92 import java.io.BufferedReader ; 93 import java.io.InputStreamReader ; 94 import java.util.ArrayList ; 95 import java.util.Enumeration ; 96 import java.util.List ; 97 import java.util.Properties ; 98 99 import org.apache.log4j.Logger; 100 101 108 public class OSEnvironment { 109 110 private static final Logger LOG = Logger.getLogger(OSEnvironment.class); 111 112 115 private Properties variables = new Properties (); 116 117 124 public OSEnvironment() { 125 parse(); 126 } 127 128 132 private void parse() { 133 134 String command; 135 136 String os = System.getProperty("os.name").toLowerCase(); 138 if (os.indexOf("windows 9") > -1) { 139 command = "command.com /c set"; 140 } else if ( 141 (os.indexOf("nt") > -1) 142 || (os.indexOf("windows 200") > -1) 143 || (os.indexOf("windows xp") > -1) 144 || (os.indexOf("os/2") > -1)) { 145 command = "cmd.exe /c set"; 146 } else { 147 command = "env"; 149 } 150 151 try { 153 Process p = Runtime.getRuntime().exec(command); 154 155 BufferedReader stdoutStream = new BufferedReader (new InputStreamReader ( 157 p.getInputStream())); 158 BufferedReader stderrStream = new BufferedReader (new InputStreamReader ( 159 p.getErrorStream())); 160 161 String line; 163 String key = null; 164 while ((line = stdoutStream.readLine()) != null) { 165 int idx = line.indexOf('='); 166 String value; 167 if (idx == -1) { 168 if (key == null) { 169 continue; 170 } 171 value = variables.getProperty(key); 173 value += "\n" + line; 174 } else { 175 key = line.substring(0, idx); 176 value = line.substring(idx + 1); 177 } 178 variables.setProperty(key, value); 179 } 180 181 stdoutStream.close(); 183 stderrStream.close(); 184 185 } catch (Exception e) { 186 LOG.error("Failed to parse the OS environment.", e); 187 } 188 } 189 190 191 202 public String getVariable(String variable) { 203 return variables.getProperty(variable); 204 } 205 206 207 218 public String getVariable(String variable, String defaultValue) { 219 return variables.getProperty(variable, defaultValue); 220 } 221 222 223 235 public String getVariableIgnoreCase(String variable) { 236 Enumeration keys = variables.keys(); 237 while (keys.hasMoreElements()) { 238 String key = (String ) keys.nextElement(); 239 if (key.equalsIgnoreCase(variable)) { 240 return variables.getProperty(key); 241 } 242 } 243 return null; 244 } 245 246 247 255 public void add(String variable, String value) { 256 variables.setProperty(variable, value); 257 } 258 259 260 271 public List getEnvironment() { 272 List env = new ArrayList (); 273 Enumeration keys = variables.keys(); 274 while (keys.hasMoreElements()) { 275 String key = (String ) keys.nextElement(); 276 env.add(key + "=" + variables.getProperty(key)); 277 } 278 return env; 279 } 280 281 293 public String [] toArray() { 294 List list = getEnvironment(); 295 return (String []) list.toArray(new String [list.size()]); 296 } 297 298 299 305 public String toString() { 306 return variables.toString(); 307 } 308 } 309 | Popular Tags |