1 package org.jzonic.jlo; 2 3 import java.io.BufferedReader ; 4 import java.io.InputStreamReader ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.Properties ; 8 18 public class VariableManager { 19 20 private static VariableManager vm = null; 21 private HashMap varMapping; 22 private Properties envVariables; 23 24 private VariableManager() { 25 varMapping = new HashMap (); 26 envVariables = getEnvVars(); 27 } 28 29 34 public static VariableManager getInstance() { 35 if ( vm == null ) { 36 vm = new VariableManager(); 37 } 38 return vm; 39 } 40 41 48 public void addVariable(String varName,String varValue,String configName) { 49 HashMap vars = (HashMap )varMapping.get(configName); 50 if ( vars == null ) { 51 vars = new HashMap (); 52 } 53 vars.put(varName,varValue); 54 varMapping.put(configName,vars); 55 } 56 57 69 public String replaceVariables(String text,String configName) { 70 HashMap vars = (HashMap )varMapping.get(configName); 71 if ( vars != null && vars.size() > 0 ) { 72 Iterator it = vars.keySet().iterator(); 73 int pos = 0; 74 while ( it.hasNext() ) { 76 String currentKey = (String ) it.next(); 77 String value = (String ) vars.get(currentKey); 78 currentKey = "${" + currentKey + "}"; 79 pos = text.indexOf(currentKey); 80 while (pos != -1) { 82 String firstPart = text.substring(0, pos); 85 String secondPart = text.substring(pos + currentKey.length()); 86 text = firstPart + value + secondPart; 87 pos = text.indexOf(currentKey); 88 } 89 } 90 } 91 text = replaceSystemVar(text); 92 return replaceEnvVar(text); 93 } 94 95 103 public int getVariableCount(String configName) { 104 if ( varMapping.containsKey(configName) ) { 105 return ((HashMap )varMapping.get(configName)).size(); 106 } 107 else { 108 return 0; 109 } 110 } 111 112 public String replaceSystemVar(String text) { 113 if ( text != null ) { 114 int idx = text.indexOf("${system:"); 115 while ( idx != -1 ) { 117 String firstPart = text.substring(0, idx); 118 String envVar = text.substring(idx+9,text.indexOf("}")); 119 String secondPart = text.substring(idx + envVar.length()+10); 120 String value = System.getProperty(envVar); 121 if ( value == null ) { 122 value = "${system:"+envVar+"}"; 123 } 124 text = firstPart + value + secondPart; 125 idx = text.indexOf("${system:",idx+1); 126 } 127 } 128 return text; 129 } 130 131 140 public String replaceEnvVar(String text) { 141 if ( text != null ) { 142 int idx = text.indexOf("${env:"); 143 while ( idx != -1 ) { 145 String firstPart = text.substring(0, idx); 146 String envVar = text.substring(idx+6,text.indexOf("}")); 147 String secondPart = text.substring(idx + envVar.length()+7); 148 String value = envVariables.getProperty(envVar); 149 if ( value == null ) { 150 value = "${env:"+envVar+"}"; 151 } 152 text = firstPart + value + secondPart; 153 idx = text.indexOf("${env:",idx+1); 154 } 155 } 156 return text; 157 } 158 159 private Properties getEnvVars() { 162 Process p = null; 163 Properties envVars = new Properties (); 164 try { 165 Runtime r = Runtime.getRuntime(); 166 String OS = System.getProperty("os.name").toLowerCase(); 167 if (OS.indexOf("windows 9") > -1) { 169 p = r.exec( "command.com /c set" ); 170 } 171 else if ( (OS.indexOf("nt") > -1) 172 || (OS.indexOf("windows 2000") > -1 173 || (OS.indexOf("windows xp") > -1) ) ) { 174 p = r.exec( "cmd.exe /c set" ); 176 } 177 else { 178 p = r.exec( "env" ); 180 } 181 BufferedReader br = new BufferedReader 182 ( new InputStreamReader ( p.getInputStream() ) ); 183 String line; 184 while( (line = br.readLine()) != null ) { 185 int idx = line.indexOf( '=' ); 186 String key = line.substring( 0, idx ); 187 String value = line.substring( idx+1 ); 188 envVars.setProperty( key, value ); 189 } 191 } 192 catch (Exception e) { 193 } 195 return envVars; 196 } 197 198 } 199 | Popular Tags |