1 17 package com.jahia.opentools.advancedapprunner; 18 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.InputStreamReader ; 22 import java.util.Properties ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 26 32 public class SysEnvPropertiesProvider { 33 34 private Properties envProperties; 35 36 private boolean loaded; 37 private Object LOCK = new Object (); 38 private static SysEnvPropertiesProvider singletonInstance = null; 39 40 private SysEnvPropertiesProvider() { 41 loaded = false; 42 } 43 44 public static SysEnvPropertiesProvider getInstance() { 45 if (singletonInstance == null) { 46 singletonInstance = new SysEnvPropertiesProvider(); 47 } 48 return singletonInstance; 49 } 50 51 private void loadEnvironment() { 52 try { 53 envProperties = getEnvVars(); 54 } catch (Exception exc) { 55 envProperties = new Properties (); 56 } 57 } 58 59 public String getProperty(String name) { 60 checkLoaded(); 61 return envProperties.getProperty(name); 62 } 63 64 public void setProperty(String name, String value) { 65 checkLoaded(); 66 envProperties.setProperty(name, value); 67 } 68 69 private void checkLoaded() { 70 if (!loaded) { 71 synchronized (LOCK) { 72 if (!loaded) { 73 loadEnvironment(); 74 loaded = true; 75 } 76 } 77 } 78 } 79 80 100 public String matchLongestProperty(String input) { 101 if (input == null) { 102 return null; 103 } 104 checkLoaded(); 105 if (envProperties.size() == 0) { 106 return null; 107 } 108 if (envProperties.size() == 1) { 109 String singleProperty = (String ) envProperties.keySet().iterator().next(); 110 if (input.indexOf(singleProperty) != -1) { 111 return singleProperty; 112 } else { 113 return null; 114 } 115 } 116 ArrayList matchCandidates = new ArrayList (envProperties.keySet()); 117 int curLen = 1; 118 while (matchCandidates.size() > 1) { 119 String partialInput = input.substring(0, curLen); 120 Iterator candidatesIter = matchCandidates.iterator(); 121 ArrayList newCandidates = new ArrayList (); 122 while (candidatesIter.hasNext()) { 123 String curCandidateName = (String ) candidatesIter.next(); 124 if (curCandidateName.startsWith(partialInput)) { 125 newCandidates.add(curCandidateName); 126 } 127 } 128 matchCandidates = newCandidates; 129 curLen++; 130 } 131 if (matchCandidates.size() == 0) { 132 return null; 133 } 134 return (String ) matchCandidates.get(0); 135 } 136 137 147 public String replaceEnvVariables(String input) { 148 int curPos = 0; 149 String result = new String (input); 150 int envStartPos = result.indexOf("$", curPos); 151 while (envStartPos != -1) { 152 curPos = envStartPos; 153 String propertyName = matchLongestProperty(result.substring(envStartPos+1)); 156 if (propertyName != null) { 157 String leftOfProperty = result.substring(0, envStartPos); 158 String rightOfProperty = result.substring(envStartPos + 159 1 + propertyName.length()); 160 result = leftOfProperty + getProperty(propertyName) + rightOfProperty; 161 } else { 162 curPos++; 163 } 164 165 envStartPos = result.indexOf("$", curPos); 167 } 168 return result; 169 } 170 171 172 private static Properties getEnvVars() throws IOException { 173 Process p = null; 174 Properties envVars = new Properties (); 175 Runtime r = Runtime.getRuntime(); 176 String os = System.getProperty("os.name").toLowerCase(); 177 if (os.indexOf("windows") > -1) { 179 if (os.indexOf("windows 9") > -1) { 180 p = r.exec("command.com /c set"); 182 } else { 183 p = r.exec("cmd.exe /c set"); 185 } 186 } else { 187 p = r.exec("sh -l -c env"); 189 } 190 BufferedReader br = new BufferedReader 191 (new InputStreamReader (p.getInputStream())); 192 String line; 193 while ( (line = br.readLine()) != null) { 194 int idx = line.indexOf('='); 195 if (idx > 0) { 196 String key = line.substring(0, idx); 197 String value = line.substring(idx + 1); 198 envVars.setProperty(key, value); 199 } else { 200 } 202 } 203 return envVars; 204 } 205 } 206 | Popular Tags |