1 28 29 package org.jruby.environment; 30 31 import java.io.BufferedReader ; 32 import java.io.IOException ; 33 import java.security.AccessControlException ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 import java.util.Set ; 38 39 import org.jruby.Ruby; 40 41 public class OSEnvironment { 42 43 44 49 void handleException(Exception e) { 50 throw (OSEnvironmentReaderExcepton) 51 new OSEnvironmentReaderExcepton().initCause(e); 52 } 53 54 68 public Map getEnvironmentVariableMap(Ruby runtime) { 69 Map envs = null; 70 71 if (runtime.getInstanceConfig().getEnvironment() != null) { 72 return getAsMapOfRubyStrings(runtime, runtime.getInstanceConfig().getEnvironment().entrySet()); 73 } 74 75 try { 77 String jrubyEnvMethod = System.getProperty("jruby.env.method"); 78 79 IOSEnvironmentReader reader; 80 81 if (jrubyEnvMethod == null || jrubyEnvMethod.length() < 1) { 82 reader = getAccessibleOSEnvironment(runtime, OSEnvironmentReaderFromJava5SystemGetenv.class.getName()); 84 if (reader == null) { 86 reader = getAccessibleOSEnvironment(runtime, OSEnvironmentReaderFromRuntimeExec.class.getName()); 87 } else { 89 } 91 } else { 92 runtime.getWarnings().warn("Getting environment variables using command line defined method: " + jrubyEnvMethod); 94 reader = getAccessibleOSEnvironment(runtime, jrubyEnvMethod); 95 } 96 97 envs = null; 98 if (reader != null) { 99 Map variables = null; 100 variables = reader.getVariables(runtime); 101 envs = getAsMapOfRubyStrings(runtime, variables.entrySet()); 102 } 103 } catch (AccessControlException accessEx) { 104 envs = new HashMap (); 106 } 107 108 return envs; 109 110 } 111 112 117 public Map getSystemPropertiesMap(Ruby runtime) { 118 try { 119 return getAsMapOfRubyStrings(runtime, System.getProperties().entrySet()); 120 } catch (AccessControlException accessEx) { 121 return new HashMap (); 123 } 124 } 125 126 127 private static IOSEnvironmentReader getAccessibleOSEnvironment(Ruby runtime, String classname) { 128 IOSEnvironmentReader osenvironment = null; 129 try { 130 osenvironment = (IOSEnvironmentReader)Class.forName(classname).newInstance(); 131 } catch (Exception e) { 132 runtime.getWarnings().warn(e.getMessage()); 134 } 135 136 if (osenvironment != null && osenvironment.isAccessible(runtime)) { 137 return osenvironment; 138 } 139 return null; 140 } 141 142 143 144 private static Map getAsMapOfRubyStrings(Ruby runtime, Set entrySet) { 145 Map envs = new HashMap (); 146 for (Iterator iter = entrySet.iterator(); iter.hasNext();) { 147 Map.Entry entry = (Map.Entry ) iter.next(); 148 envs.put(runtime.newString((String )entry.getKey()),runtime.newString((String )entry.getValue())); 149 } 150 return envs; 151 } 152 153 154 159 Map getVariablesFrom(BufferedReader reader) { 160 Map envs = new HashMap (); 161 try { 162 String line, envVarName, envVarValue; 163 int equalsPos; 164 while ((line = reader.readLine()) != null) { 165 equalsPos = line.indexOf('='); 166 if (equalsPos >= 1) { 167 envVarName = line.substring(0, equalsPos); 168 envVarValue = line.substring(equalsPos + 1); 169 envs.put(envVarName, envVarValue); 170 } 171 } 172 } catch (IOException e) { 173 envs = null; 174 handleException(e); 175 } finally { 176 try { 177 reader.close(); 178 } catch (IOException e) { 179 envs = null; 180 handleException(e); 181 } 182 } 183 return envs; 184 } 185 186 187 } 188
| Popular Tags
|