Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 265   Methods: 12
NCLOC: 189   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
VariableManager.java 80,8% 93% 100% 89,9%
coverage coverage
 1    package org.jconfig;
 2   
 3    import java.io.BufferedReader;
 4    import java.io.IOException;
 5    import java.io.InputStreamReader;
 6    import java.util.HashMap;
 7    import java.util.Map;
 8    import java.util.Properties;
 9    import java.util.Vector;
 10    /**
 11    * This class manages all variables for all log configurations.
 12    * Every handler or formatter can use this class to replace
 13    * any variables that are used inside of the parameters.
 14    * Please refer to the specific handler or formatter to see
 15    * which parameters support the use of variables.
 16    *
 17    * @author Andreas Mecky
 18    * @author Terry Dye
 19    */
 20    public class VariableManager {
 21   
 22    private static VariableManager vm = null;
 23    private HashMap varMapping;
 24    private Properties envVariables;
 25    // a HashMap containing a Vector for every config
 26    // with the immutable proerpties that were included
 27    private HashMap includedProperties;
 28   
 29  1 private VariableManager() {
 30  1 varMapping = new HashMap();
 31  1 includedProperties = new HashMap();
 32    }
 33   
 34    /**
 35    * This method will return an instance of the VariableManager
 36    *
 37    * @return the one and only instance of the VariableManager
 38    */
 39  58 public static VariableManager getInstance() {
 40  58 if ( vm == null ) {
 41  1 vm = new VariableManager();
 42    }
 43  58 return vm;
 44    }
 45   
 46  23 public HashMap getVariables(String configName) {
 47  23 HashMap vars = (HashMap)varMapping.get(configName);
 48  23 if ( vars == null ) {
 49  0 vars = new HashMap();
 50    }
 51  23 Vector includes = (Vector)includedProperties.get(configName);
 52  23 if ( includes != null ) {
 53  14 for ( int i=0;i < includes.size();i++) {
 54  37 String name = (String)includes.get(i);
 55  37 if ( vars.containsKey(name)) {
 56  9 vars.remove(name);
 57    }
 58    }
 59    }
 60  23 return vars;
 61    }
 62   
 63  18 public String getVariable(String configName,String name) {
 64  18 HashMap vars = (HashMap)varMapping.get(configName);
 65  18 if ( vars != null ) {
 66  18 return (String)vars.get(name);
 67    }
 68  0 return null;
 69    }
 70    /**
 71    * This method adds a variable for a given configuration.
 72    *
 73    * @param varName name of the variable
 74    * @param varValue the value for this variable
 75    * @param configName the configuration to which this variable belongs
 76    */
 77  153 public void addVariable(String varName,String varValue,String configName) {
 78  153 HashMap vars = (HashMap)varMapping.get(configName);
 79  153 if ( vars == null ) {
 80  12 vars = new HashMap();
 81    }
 82  153 vars.put(varName,varValue);
 83  153 varMapping.put(configName,vars);
 84    }
 85   
 86    /**
 87    * This method removes a variable for a given configuration.
 88    *
 89    * @param varName name of the variable
 90    * @param configName the configuration to which this variable belongs
 91    */
 92  1 public void removeVariable(String varName,String configName) {
 93  1 HashMap vars = (HashMap)varMapping.get(configName);
 94  1 if ( vars != null ) {
 95  1 vars.remove(varName);
 96  1 varMapping.put(configName,vars);
 97    }
 98    }
 99   
 100  61 public void addIncludedVariable(String varName,String varValue,String configName) {
 101  61 addVariable(varName,varValue,configName);
 102  61 Vector includes = (Vector)includedProperties.get(configName);
 103  61 if ( includes == null ) {
 104  8 includes = new Vector();
 105    }
 106  61 includes.add(varName);
 107  61 includedProperties.put(configName,includes);
 108    }
 109   
 110    /**
 111    * This method will replace all variables inside one String that
 112    * are in this configuration. A variable is used as ${name}.
 113    * This occurance will be replaced with the specific value
 114    * or will be left as is if the variable name is not found
 115    * in the specific configuration.
 116    *
 117    * @param text the line of text which contains variables
 118    * @param configName the name of the configuration
 119    * @return a String where all variables are replaced with their values
 120    */
 121  201 public String replaceVariables(String text, String configName) {
 122  201 Map vars = (Map) varMapping.get(configName);
 123  201 if (vars != null && vars.size() > 0) {
 124  152 text = replaceVar(text, vars);
 125    }
 126  201 text = replaceEnvVar(text);
 127  201 return replaceSystemVar(text);
 128    }
 129  174 private String replaceVar(String text, Map vars) {
 130  174 if (text == null) {
 131  2 return text;
 132    }
 133    // Find the start of the variable
 134  172 int startPosition = text.indexOf("${");
 135  172 if (startPosition == -1) {
 136  149 return text;
 137    }
 138   
 139  23 StringBuffer buffer = new StringBuffer();
 140  23 int textLength = text.length();
 141  23 int lastIndex = 0;
 142    // Loop while there are instances of "${" and cursor position is < length -2
 143    // so there are no index out of bounds on the indexOf calls.
 144  23 while (startPosition > -1 && (startPosition+2) < textLength) {
 145  25 int endPosition = text.indexOf("}", startPosition + 2);
 146  25 if (endPosition == -1) {
 147    // No end tag
 148  0 break;
 149    }
 150   
 151    // Append previous text to this point
 152  25 buffer.append(text.substring(lastIndex, startPosition));
 153   
 154    // key is ${currentKey}
 155  25 String currentKey = text.substring(startPosition + 2, endPosition);
 156  25 String value = (String) vars.get(currentKey);
 157  25 if (value != null) {
 158    // Substitute any var's in this variable value
 159  22 value = replaceVar(value, vars);
 160   
 161    // Append the new String
 162  22 buffer.append(value);
 163  22 lastIndex = endPosition + 1;
 164    } else {
 165    // append the ${ and start at the character after { to check more possible
 166    // variables.
 167  3 buffer.append(text.charAt(startPosition));
 168  3 buffer.append(text.charAt(startPosition+1));
 169  3 lastIndex = startPosition + 2;
 170    }
 171    // Set the 'pointer' to the end of the oldString in 'text'
 172  25 startPosition = text.indexOf("${", lastIndex);
 173    }
 174    // Append the final part
 175  23 buffer.append(text.substring(lastIndex, textLength));
 176  23 return buffer.toString();
 177    }
 178   
 179  204 protected String replaceEnvVar(String text) {
 180  204 if ( text != null ) {
 181  201 if ( envVariables == null && text.indexOf("${env:") != -1 ) {
 182  1 envVariables = getEnvVars();
 183    }
 184  201 int idx = text.indexOf("${env:");
 185    // walk through the text and replace it
 186  201 while ( idx != -1 ) {
 187  6 String firstPart = text.substring(0, idx);
 188  6 String tmp = text.substring(idx+6);
 189  6 String envVar = tmp.substring(0,tmp.indexOf("}"));
 190  6 String secondPart = text.substring(idx + envVar.length()+7);
 191  6 String value = envVariables.getProperty(envVar);
 192  6 if ( value == null ) {
 193  1 value = "${env:"+envVar+"}";
 194    }
 195  6 text = firstPart + value + secondPart;
 196  6 idx = text.indexOf("${env:",idx+1);
 197    }
 198    }
 199  204 return text;
 200    }
 201   
 202  205 protected String replaceSystemVar(String text) {
 203  205 if ( text != null ) {
 204  202 int idx = text.indexOf("${system:");
 205    // walk through the text and replace it
 206  202 while ( idx != -1 ) {
 207  6 String firstPart = text.substring(0, idx);
 208  6 String tmp = text.substring(idx+9);
 209  6 String sysVar = tmp.substring(0,tmp.indexOf("}"));
 210  6 String secondPart = text.substring(idx + sysVar.length()+10);
 211  6 String value = System.getProperty(sysVar);
 212  6 if ( value == null ) {
 213  1 value = "${system:"+sysVar+"}";
 214    }
 215  6 text = firstPart + value + secondPart;
 216  6 idx = text.indexOf("${system:",idx+1);
 217    }
 218    }
 219  205 return text;
 220    }
 221   
 222    // Thanx to http://www.rgagnon.com/howto.html for
 223    // this implementation.
 224  1 private Properties getEnvVars() {
 225  1 Process p = null;
 226  1 Properties envVars = new Properties();
 227  1 BufferedReader br = null;
 228  1 try {
 229  1 Runtime r = Runtime.getRuntime();
 230  1 String OS = System.getProperty("os.name").toLowerCase();
 231  1 if (OS.indexOf("windows 9") > -1) {
 232  0 p = r.exec( "command.com /c set" );
 233    }
 234  1 else if ( (OS.indexOf("nt") > -1)
 235    || (OS.indexOf("windows 2000") > -1
 236    || (OS.indexOf("windows xp") > -1) ) ) {
 237    // thanks to JuanFran for the xp fix!
 238  1 p = r.exec( "cmd.exe /c set" );
 239    }
 240    else {
 241    // our last hope, we assume Unix (thanks to H. Ware for the fix)
 242  0 p = r.exec( "env" );
 243    }
 244  1 br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
 245  1 String line;
 246  ? while( (line = br.readLine()) != null ) {
 247  36 int idx = line.indexOf( '=' );
 248  36 String key = line.substring( 0, idx );
 249  36 String value = line.substring( idx+1 );
 250  36 envVars.setProperty( key, value );
 251    }
 252  1 br.close();
 253    }
 254    catch (Exception e) {
 255    // we do not care here. Just no env vars for the user. Sorry.
 256  0 try {
 257  0 if ( br != null ) {
 258  0 br.close();
 259    }
 260    } catch (IOException e1) {
 261    }
 262    }
 263  1 return envVars;
 264    }
 265    }