1 28 29 package org.jruby.environment; 30 31 import java.lang.reflect.InvocationTargetException ; 32 import java.lang.reflect.Method ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 import java.util.Vector ; 37 38 import org.jruby.Ruby; 39 40 class OSEnvironmentReaderFromApacheAnt implements IOSEnvironmentReader { 41 42 43 protected Map getProcEnvironmentMethodV() { 44 45 Method getProcEnvironment = getProcEnvironmentMethod(); 46 Vector v = new Vector (); 47 48 HashMap map = new HashMap (); 49 50 51 if (getProcEnvironment != null) { 52 try { 53 v = (Vector ) getProcEnvironment.invoke(null, (Object []) null); 54 } catch (IllegalArgumentException e) { 55 return map; 56 } catch (IllegalAccessException e) { 57 return map; 58 } catch (InvocationTargetException e) { 59 return map; 60 } 61 } 62 63 String line; 64 int equalsPos; 65 66 for (java.util.Enumeration e = v.elements(); e.hasMoreElements();) { 67 line = (String ) e.nextElement(); 68 equalsPos = line.indexOf('='); 69 if (equalsPos >= 1) { 70 map.put(line.substring(0, equalsPos), line.substring(equalsPos + 1)); 71 } 72 } 73 74 75 return map; 76 77 } 78 79 80 protected Method getProcEnvironmentMethod() { 81 82 Method getProcEnvironment; 83 Class c; 84 85 try { 86 c = Class.forName("org.apache.tools.ant.taskdefs.Execute"); 87 } catch (ClassNotFoundException e) { 88 return null; 89 } 90 91 try { 92 getProcEnvironment = c.getMethod("getProcEnvironment", (Class []) null); 93 } catch (SecurityException e) { 94 return null; 95 } catch (NoSuchMethodException e) { 96 return null; 97 } 98 99 return getProcEnvironment; 100 101 } 102 103 104 105 106 109 public boolean isAccessible(Ruby runtime) { 110 return getProcEnvironmentMethod() != null; 111 } 112 113 116 public Map getVariables(Ruby runtime) { 117 return getProcEnvironmentMethodV(); 118 } 119 120 public static void main(String [] args) { 121 OSEnvironmentReaderFromApacheAnt getenv = new OSEnvironmentReaderFromApacheAnt(); 122 Map envs = getenv.getVariables(null); 123 for (Iterator i = envs.entrySet().iterator(); i.hasNext();) { 124 Map.Entry entry = (Map.Entry ) i.next(); 125 System.out.println(entry.getKey() + ":" + entry.getValue()); 126 } 127 System.out.println(); 128 } 129 } 130
| Popular Tags
|