KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > VariableManager


1 package org.jzonic.jlo;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.InputStreamReader JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Properties JavaDoc;
8 /**
9  * This class manages all variables for all log configurations.
10  * Every handler or formatter can use this class to replace
11  * any variables that are used inside of the parameters.
12  * Please refer to the specific handler or formatter to see
13  * which parameters support the use of variables.
14  *
15  * @author Andreas Mecky
16  * @author Terry Dye
17  */

18 public class VariableManager {
19     
20     private static VariableManager vm = null;
21     private HashMap JavaDoc varMapping;
22     private Properties JavaDoc envVariables;
23      
24     private VariableManager() {
25         varMapping = new HashMap JavaDoc();
26         envVariables = getEnvVars();
27     }
28     
29     /**
30      * This method will return an instance of the VariableManager
31      *
32      * @return the one and only instance of the VariableManager
33      */

34     public static VariableManager getInstance() {
35         if ( vm == null ) {
36             vm = new VariableManager();
37         }
38         return vm;
39     }
40     
41     /**
42      * This method adds a variable for a given configuration.
43      *
44      * @param varName name of the variable
45      * @param varValue the value for this variable
46      * @param configName the configuration to which this variable belongs
47      */

48     public void addVariable(String JavaDoc varName,String JavaDoc varValue,String JavaDoc configName) {
49         HashMap JavaDoc vars = (HashMap JavaDoc)varMapping.get(configName);
50         if ( vars == null ) {
51             vars = new HashMap JavaDoc();
52         }
53         vars.put(varName,varValue);
54         varMapping.put(configName,vars);
55     }
56     
57     /**
58      * This method will replace all variables inside one String that
59      * are in this configuration. A variable is used as ${name}.
60      * This occurance will be replaced with the specific value
61      * or will be left as is if the variable name is not found
62      * in the specific configuration. This method calls replaceEnvVars
63      * at the end to replace environment variables.
64      *
65      * @param text the line of text which contains variables
66      * @param configName the name of the configuration
67      * @return a String where all variables are replaced with their values
68      */

69     public String JavaDoc replaceVariables(String JavaDoc text,String JavaDoc configName) {
70         HashMap JavaDoc vars = (HashMap JavaDoc)varMapping.get(configName);
71         if ( vars != null && vars.size() > 0 ) {
72             Iterator JavaDoc it = vars.keySet().iterator();
73             int pos = 0;
74             // walk through all variables
75
while ( it.hasNext() ) {
76                 String JavaDoc currentKey = (String JavaDoc) it.next();
77                 String JavaDoc value = (String JavaDoc) vars.get(currentKey);
78                 currentKey = "${" + currentKey + "}";
79                 pos = text.indexOf(currentKey);
80                 // check if we have found a variable
81
while (pos != -1) {
82                     // cut the line into 2 pieces and put in the
83
// value of the variable
84
String JavaDoc firstPart = text.substring(0, pos);
85                     String JavaDoc 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     /**
96      * This method returns the number of variables
97      * for one specific configuration. If the configuration
98      * does not exist it will return 0;
99      *
100      * @param configName the name of the configuration
101      * @return the number of variables
102      */

103     public int getVariableCount(String JavaDoc configName) {
104         if ( varMapping.containsKey(configName) ) {
105             return ((HashMap JavaDoc)varMapping.get(configName)).size();
106         }
107         else {
108             return 0;
109         }
110     }
111     
112     public String JavaDoc replaceSystemVar(String JavaDoc text) {
113         if ( text != null ) {
114             int idx = text.indexOf("${system:");
115             // walk through the text and replace it
116
while ( idx != -1 ) {
117                 String JavaDoc firstPart = text.substring(0, idx);
118                 String JavaDoc envVar = text.substring(idx+9,text.indexOf("}"));
119                 String JavaDoc secondPart = text.substring(idx + envVar.length()+10);
120                 String JavaDoc 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     /**
132      * This method replace all occurences ${env:xxx} with
133      * the value of an environment variable. If the env-variable
134      * does not exist then the part is not converted. The
135      * method gets called from replaceVariables
136      *
137      * @param text the line of text that will be processed
138      * @return a String with the replaced env-variables.
139      */

140     public String JavaDoc replaceEnvVar(String JavaDoc text) {
141         if ( text != null ) {
142             int idx = text.indexOf("${env:");
143             // walk through the text and replace it
144
while ( idx != -1 ) {
145                 String JavaDoc firstPart = text.substring(0, idx);
146                 String JavaDoc envVar = text.substring(idx+6,text.indexOf("}"));
147                 String JavaDoc secondPart = text.substring(idx + envVar.length()+7);
148                 String JavaDoc 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     // Thanx to http://www.rgagnon.com/howto.html for
160
// this implementation.
161
private Properties JavaDoc getEnvVars() {
162         Process JavaDoc p = null;
163         Properties JavaDoc envVars = new Properties JavaDoc();
164         try {
165         Runtime JavaDoc r = Runtime.getRuntime();
166         String JavaDoc OS = System.getProperty("os.name").toLowerCase();
167         // System.out.println(OS);
168
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             // thanks to JuanFran for the xp fix!
175
p = r.exec( "cmd.exe /c set" );
176         }
177         else {
178             // our last hope, we assume Unix (thanks to H. Ware for the fix)
179
p = r.exec( "env" );
180         }
181         BufferedReader JavaDoc br = new BufferedReader JavaDoc
182         ( new InputStreamReader JavaDoc( p.getInputStream() ) );
183         String JavaDoc line;
184         while( (line = br.readLine()) != null ) {
185             int idx = line.indexOf( '=' );
186             String JavaDoc key = line.substring( 0, idx );
187             String JavaDoc value = line.substring( idx+1 );
188             envVars.setProperty( key, value );
189             //System.out.println( key + " = " + value );
190
}
191         }
192         catch (Exception JavaDoc e) {
193             // we do not care here. Just no env vars for the user. Sorry.
194
}
195         return envVars;
196     }
197     
198 }
199
Popular Tags