1 28 29 package org.jruby.environment; 30 31 import java.lang.reflect.Method ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 import org.jruby.Ruby; 36 37 class OSEnvironmentReaderFromJava5SystemGetenv implements IOSEnvironmentReader { 38 39 40 44 protected Method getSystemGetenvMethod() { 45 Method getenvMethod = null; 46 try { 47 getenvMethod = java.lang.System .class.getMethod("getenv", (Class []) null); 48 if (!getenvMethod.getReturnType().equals(java.util.Map .class)) { 49 getenvMethod = null; 51 } 52 } catch (NoSuchMethodException e) { 53 getenvMethod = null; 55 } catch (Exception e) { 56 getenvMethod = null; 58 } 59 return getenvMethod; 60 } 61 62 65 public boolean isAccessible(Ruby runtime) { 66 return getSystemGetenvMethod() != null; 67 } 68 69 72 public Map getVariables(Ruby runtime) { 73 Map returnMap = null; 74 Method getenvMethod = getSystemGetenvMethod(); 75 try { 76 if (getenvMethod != null) { 77 returnMap = (Map ) getenvMethod.invoke(null, (Object []) null); 78 } 79 } catch (Exception e) { 80 new OSEnvironment().handleException(e); 81 } 82 return returnMap; 83 } 84 85 public static void main(String [] args) { 86 OSEnvironmentReaderFromJava5SystemGetenv getenv = new OSEnvironmentReaderFromJava5SystemGetenv(); 87 Map envs = getenv.getVariables(null); 88 for (Iterator i = envs.entrySet().iterator(); i.hasNext();) { 89 Map.Entry entry = (Map.Entry ) i.next(); 90 System.out.println(entry.getKey() + ":" + entry.getValue()); 91 } 92 System.out.println(); 93 } 94 } 95 | Popular Tags |