1 10 package org.mmbase.util.externalprocess; 11 12 import java.io.ByteArrayOutputStream ; 13 import java.io.IOException ; 14 import java.util.Properties ; 15 import java.util.StringTokenizer ; 16 import java.util.Vector ; 17 18 27 public class EnvironmentReader { 28 private static Properties envVars = null; 29 private static Vector rawVars = null; 30 31 35 public static Properties getEnvVars() { 36 37 if (null != envVars) 38 return envVars; 39 40 envVars = new Properties (); 41 rawVars = new Vector (32); 42 String lineSeparator = System.getProperty("line.separator"); 43 44 String command = getEnvCommand(); 45 46 CommandLauncher launcher = new CommandLauncher("EnvironmentReader"); 47 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 48 try { 49 launcher.execute(command); 50 launcher.waitAndRead(outputStream, null); 51 52 String envStr = outputStream.toString(); 53 StringTokenizer strTokens = new StringTokenizer (envStr, lineSeparator); 54 55 String line; 56 while (strTokens.hasMoreTokens()) { 57 line = strTokens.nextToken(); 58 59 rawVars.add(line); 60 int idx = line.indexOf('='); 61 if (idx != -1) { 62 String key = line.substring(0, idx); 63 String value = line.substring(idx + 1); 64 envVars.setProperty(key, value); 65 } else { 66 envVars.setProperty(line, ""); 67 } 68 } 69 } catch (ProcessException e) {} finally { 70 try { 71 if (outputStream != null) { 72 outputStream.close(); 73 } 74 } catch (IOException e) {} 75 } 76 rawVars.trimToSize(); 77 return envVars; 78 } 79 80 84 public static String getEnvCommand() { 85 String OS = System.getProperty("os.name").toLowerCase(); 86 if (OS.indexOf("windows 9") > -1) { 87 return "command.com /c set"; 88 } else { 89 if ((OS.indexOf("nt") > -1) || (OS.indexOf("windows 2000") > -1) || (OS.indexOf("windows xp") > -1)) { 90 return "cmd.exe /c set"; 91 } else { 92 throw new UnsupportedOperationException ("OS not supported yet"); 93 } 94 } 95 } 96 97 102 public static String getEnvVar(String key) { 103 Properties p = getEnvVars(); 104 return p.getProperty(key); 105 } 106 107 111 public static String [] getRawEnvVars() { 112 getEnvVars(); 113 return (String [])rawVars.toArray(new String [0]); 114 } 115 } 116 | Popular Tags |