1 10 11 package org.mule.util; 12 13 import java.io.BufferedReader ; 14 import java.io.InputStreamReader ; 15 import java.lang.reflect.Method ; 16 import java.util.Arrays ; 17 import java.util.Collections ; 18 import java.util.HashMap ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import org.apache.commons.lang.ArrayUtils; 23 import org.apache.commons.lang.StringUtils; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 public class SystemUtils extends org.apache.commons.lang.SystemUtils 29 { 30 protected static final Log logger = LogFactory.getLog(SystemUtils.class); 31 32 private static final String [] UNIX_ENV_PREFIXES = new String []{"declare -", "typeset -"}; 35 36 42 public static synchronized Map getenv() 43 { 44 Map env = Collections.EMPTY_MAP; 45 46 try 47 { 48 if (SystemUtils.IS_JAVA_1_4) 49 { 50 env = getenvJDK14(); 52 } 53 else 54 { 55 Class target = System .class; 58 Method envMethod = target.getMethod("getenv", ArrayUtils.EMPTY_CLASS_ARRAY); 59 env = (Map )envMethod.invoke(target, (Class [])null); 60 } 61 } 62 catch (Exception ex) 63 { 64 logger.error("Could not access OS environment: ", ex); 65 } 66 67 return env; 68 } 69 70 private static Map getenvJDK14() throws Exception 71 { 72 Map env = new HashMap (); 73 Process process = null; 74 75 try 76 { 77 boolean isUnix = true; 78 String command; 79 80 if (SystemUtils.IS_OS_WINDOWS) 81 { 82 command = "cmd /c set"; 83 isUnix = false; 84 } 85 else 86 { 87 command = "env"; 88 } 89 90 process = Runtime.getRuntime().exec(command); 91 BufferedReader br = new BufferedReader (new InputStreamReader (process.getInputStream())); 92 93 String line; 94 while ((line = br.readLine()) != null) 95 { 96 for (int prefix = 0; prefix < UNIX_ENV_PREFIXES.length; prefix++) 97 { 98 if (line.startsWith(UNIX_ENV_PREFIXES[prefix])) 99 { 100 line = line.substring(UNIX_ENV_PREFIXES[prefix].length()); 101 } 102 } 103 104 int index = -1; 105 if ((index = line.indexOf('=')) > -1) 106 { 107 String key = line.substring(0, index).trim(); 108 String value = line.substring(index + 1).trim(); 109 if (isUnix && value.length() > 1 && (value.startsWith("\"") || value.startsWith("'"))) 111 { 112 value = value.substring(1, value.length() - 1); 113 } 114 env.put(key, value); 115 } 116 else 117 { 118 env.put(line, StringUtils.EMPTY); 119 } 120 } 121 } 122 catch (Exception e) 123 { 124 throw e; } 126 finally 127 { 128 if (process != null) 129 { 130 process.destroy(); 131 } 132 } 133 134 return env; 135 } 136 137 public static String getenv(String name) 138 { 139 return (String )SystemUtils.getenv().get(name); 140 } 141 142 public static boolean isSunJDK() 143 { 144 return SystemUtils.JAVA_VM_VENDOR.toUpperCase().indexOf("SUN") != -1; 145 } 146 147 public static boolean isIbmJDK() 148 { 149 return SystemUtils.JAVA_VM_VENDOR.toUpperCase().indexOf("IBM") != -1; 150 } 151 152 158 public static String getCommandLineOption(String option, String args[]) 159 { 160 List options = Arrays.asList(args); 161 if (options.contains(option)) 162 { 163 int i = options.indexOf(option); 164 if (i < options.size() - 1) 165 { 166 return options.get(i + 1).toString(); 167 } 168 } 169 return null; 170 } 171 } 172 | Popular Tags |