001: package org.jzonic.jlo;
002:
003: import java.io.BufferedReader;
004: import java.io.InputStreamReader;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.Properties;
008:
009: /**
010: * This class manages all variables for all log configurations.
011: * Every handler or formatter can use this class to replace
012: * any variables that are used inside of the parameters.
013: * Please refer to the specific handler or formatter to see
014: * which parameters support the use of variables.
015: *
016: * @author Andreas Mecky
017: * @author Terry Dye
018: */
019: public class VariableManager {
020:
021: private static VariableManager vm = null;
022: private HashMap varMapping;
023: private Properties envVariables;
024:
025: private VariableManager() {
026: varMapping = new HashMap();
027: envVariables = getEnvVars();
028: }
029:
030: /**
031: * This method will return an instance of the VariableManager
032: *
033: * @return the one and only instance of the VariableManager
034: */
035: public static VariableManager getInstance() {
036: if (vm == null) {
037: vm = new VariableManager();
038: }
039: return vm;
040: }
041:
042: /**
043: * This method adds a variable for a given configuration.
044: *
045: * @param varName name of the variable
046: * @param varValue the value for this variable
047: * @param configName the configuration to which this variable belongs
048: */
049: public void addVariable(String varName, String varValue,
050: String configName) {
051: HashMap vars = (HashMap) varMapping.get(configName);
052: if (vars == null) {
053: vars = new HashMap();
054: }
055: vars.put(varName, varValue);
056: varMapping.put(configName, vars);
057: }
058:
059: /**
060: * This method will replace all variables inside one String that
061: * are in this configuration. A variable is used as ${name}.
062: * This occurance will be replaced with the specific value
063: * or will be left as is if the variable name is not found
064: * in the specific configuration. This method calls replaceEnvVars
065: * at the end to replace environment variables.
066: *
067: * @param text the line of text which contains variables
068: * @param configName the name of the configuration
069: * @return a String where all variables are replaced with their values
070: */
071: public String replaceVariables(String text, String configName) {
072: HashMap vars = (HashMap) varMapping.get(configName);
073: if (vars != null && vars.size() > 0) {
074: Iterator it = vars.keySet().iterator();
075: int pos = 0;
076: // walk through all variables
077: while (it.hasNext()) {
078: String currentKey = (String) it.next();
079: String value = (String) vars.get(currentKey);
080: currentKey = "${" + currentKey + "}";
081: pos = text.indexOf(currentKey);
082: // check if we have found a variable
083: while (pos != -1) {
084: // cut the line into 2 pieces and put in the
085: // value of the variable
086: String firstPart = text.substring(0, pos);
087: String secondPart = text.substring(pos
088: + currentKey.length());
089: text = firstPart + value + secondPart;
090: pos = text.indexOf(currentKey);
091: }
092: }
093: }
094: text = replaceSystemVar(text);
095: return replaceEnvVar(text);
096: }
097:
098: /**
099: * This method returns the number of variables
100: * for one specific configuration. If the configuration
101: * does not exist it will return 0;
102: *
103: * @param configName the name of the configuration
104: * @return the number of variables
105: */
106: public int getVariableCount(String configName) {
107: if (varMapping.containsKey(configName)) {
108: return ((HashMap) varMapping.get(configName)).size();
109: } else {
110: return 0;
111: }
112: }
113:
114: public String replaceSystemVar(String text) {
115: if (text != null) {
116: int idx = text.indexOf("${system:");
117: // walk through the text and replace it
118: while (idx != -1) {
119: String firstPart = text.substring(0, idx);
120: String envVar = text.substring(idx + 9, text
121: .indexOf("}"));
122: String secondPart = text.substring(idx
123: + envVar.length() + 10);
124: String value = System.getProperty(envVar);
125: if (value == null) {
126: value = "${system:" + envVar + "}";
127: }
128: text = firstPart + value + secondPart;
129: idx = text.indexOf("${system:", idx + 1);
130: }
131: }
132: return text;
133: }
134:
135: /**
136: * This method replace all occurences ${env:xxx} with
137: * the value of an environment variable. If the env-variable
138: * does not exist then the part is not converted. The
139: * method gets called from replaceVariables
140: *
141: * @param text the line of text that will be processed
142: * @return a String with the replaced env-variables.
143: */
144: public String replaceEnvVar(String text) {
145: if (text != null) {
146: int idx = text.indexOf("${env:");
147: // walk through the text and replace it
148: while (idx != -1) {
149: String firstPart = text.substring(0, idx);
150: String envVar = text.substring(idx + 6, text
151: .indexOf("}"));
152: String secondPart = text.substring(idx
153: + envVar.length() + 7);
154: String value = envVariables.getProperty(envVar);
155: if (value == null) {
156: value = "${env:" + envVar + "}";
157: }
158: text = firstPart + value + secondPart;
159: idx = text.indexOf("${env:", idx + 1);
160: }
161: }
162: return text;
163: }
164:
165: // Thanx to http://www.rgagnon.com/howto.html for
166: // this implementation.
167: private Properties getEnvVars() {
168: Process p = null;
169: Properties envVars = new Properties();
170: try {
171: Runtime r = Runtime.getRuntime();
172: String OS = System.getProperty("os.name").toLowerCase();
173: // System.out.println(OS);
174: if (OS.indexOf("windows 9") > -1) {
175: p = r.exec("command.com /c set");
176: } else if ((OS.indexOf("nt") > -1)
177: || (OS.indexOf("windows 2000") > -1 || (OS
178: .indexOf("windows xp") > -1))) {
179: // thanks to JuanFran for the xp fix!
180: p = r.exec("cmd.exe /c set");
181: } else {
182: // our last hope, we assume Unix (thanks to H. Ware for the fix)
183: p = r.exec("env");
184: }
185: BufferedReader br = new BufferedReader(
186: new InputStreamReader(p.getInputStream()));
187: String line;
188: while ((line = br.readLine()) != null) {
189: int idx = line.indexOf('=');
190: String key = line.substring(0, idx);
191: String value = line.substring(idx + 1);
192: envVars.setProperty(key, value);
193: //System.out.println( key + " = " + value );
194: }
195: } catch (Exception e) {
196: // we do not care here. Just no env vars for the user. Sorry.
197: }
198: return envVars;
199: }
200:
201: }
|