1 14 package org.compiere.util; 15 16 import java.util.*; 17 import java.io.*; 18 19 25 public class EnvLoader 26 { 27 private static final boolean DEBUG = false; 28 29 36 public static boolean load (String prefix) 37 { 38 Properties prop = getEnv(prefix); 39 if (prop == null) 40 return false; 41 Object [] pp = prop.keySet().toArray(); 43 for (int i = 0; i < pp.length; i++) 44 { 45 String key = pp[i].toString(); 46 String value = prop.getProperty(key); 47 System.setProperty(key, value); 48 } 49 if (DEBUG) 50 Log.printProperties(System.getProperties(), "System with Environment", false); 51 return true; 52 } 54 59 public static Properties getEnv (String prefix) 60 { 61 String cmd = "cmd /c set"; if (!System.getProperty("os.name", "").startsWith("Win")) 63 cmd = "set"; String result = execCommand (cmd); 65 if (result == null || result.length() == 0) 66 return null; 67 if (prefix == null) 69 prefix = ""; 70 return parseEnv (result, prefix); 71 } 73 76 private static String execCommand (String command) 77 { 78 Process cmd; 79 try 80 { 81 cmd = Runtime.getRuntime().exec(command); 82 } 83 catch (Exception e) 84 { 85 System.err.println("-- Error executing command: " + command + " - " + e.toString()); 86 return null; 87 } 88 if (DEBUG) 89 System.out.println("** Command executed: " + command); 90 91 StringBuffer bufOut = new StringBuffer (); 92 StringBuffer bufErr = new StringBuffer (); 93 try 94 { 95 InputStream in = cmd.getInputStream(); 96 InputStream err = cmd.getErrorStream(); 97 int c; 99 while ((c = in.read()) != -1) 100 bufOut.append((char)c); 101 in.close(); 102 while ((c = err.read()) != -1) 104 bufErr.append((char)c); 105 err.close(); 106 } 107 catch (Exception e) 108 { 109 System.err.println("-- Error reading output: " + e.toString()); 110 return null; 111 } 112 if (DEBUG) 113 { 114 System.out.println("** Command result: " + bufOut.toString()); 115 System.out.println("** Command error: " + bufErr.toString()); 116 } 117 return bufOut.toString(); 118 } 120 123 private static Properties parseEnv (String input, String prefix) 124 { 125 Properties prop = new Properties (); 126 String separator = System.getProperty("line.separator", "\n"); 128 StringTokenizer st = new StringTokenizer(input, separator); 129 while (st.hasMoreTokens()) 130 { 131 String s = st.nextToken(); 132 int pos = s.indexOf("="); if (pos > 0) 135 prop.setProperty(prefix + s.substring(0, pos), s.substring(pos+1)); 136 } 137 if (DEBUG) 138 System.out.println("** Loaded " + prop.size() + " Properties"); 139 return prop; 140 } 142 } | Popular Tags |