Package org.jzonic.jlo

Source Code of org.jzonic.jlo.VariableManager

package org.jzonic.jlo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
/**
* This class manages all variables for all log configurations.
* Every handler or formatter can use this class to replace
* any variables that are used inside of the parameters.
* Please refer to the specific handler or formatter to see
* which parameters support the use of variables.
*
* @author Andreas Mecky
* @author Terry Dye
*/
public class VariableManager {
   
    private static VariableManager vm = null;
    private HashMap varMapping;
    private Properties envVariables;
    
    private VariableManager() {
        varMapping = new HashMap();
        envVariables = getEnvVars();
    }
   
    /**
     * This method will return an instance of the VariableManager
     *
     * @return the one and only instance of the VariableManager
     */   
    public static VariableManager getInstance() {
        if ( vm == null ) {
            vm = new VariableManager();
        }
        return vm;
    }
   
    /**
     * This method adds a variable for a given configuration.
     *
     * @param varName name of the variable
     * @param varValue the value for this variable
     * @param configName the configuration to which this variable belongs
     */
    public void addVariable(String varName,String varValue,String configName) {
        HashMap vars = (HashMap)varMapping.get(configName);
        if ( vars == null ) {
            vars = new HashMap();
        }
        vars.put(varName,varValue);
        varMapping.put(configName,vars);
    }
   
    /**
     * This method will replace all variables inside one String that
     * are in this configuration. A variable is used as ${name}.
     * This occurance will be replaced with the specific value
     * or will be left as is if the variable name is not found
     * in the specific configuration. This method calls replaceEnvVars
     * at the end to replace environment variables.
     *
     * @param text the line of text which contains variables
     * @param configName the name of the configuration
     * @return a String where all variables are replaced with their values
     */   
    public String replaceVariables(String text,String configName) {
        HashMap vars = (HashMap)varMapping.get(configName);       
        if ( vars != null && vars.size() > 0 ) {
            Iterator it = vars.keySet().iterator();
            int pos = 0;
            // walk through all variables
            while ( it.hasNext() ) {
                String currentKey = (String) it.next();
                String value = (String) vars.get(currentKey);
                currentKey = "${" + currentKey + "}";
                pos = text.indexOf(currentKey);
                // check if we have found a variable
                while (pos != -1) {
                    // cut the line into 2 pieces and put in the
                    // value of the variable
                    String firstPart = text.substring(0, pos);
                    String secondPart = text.substring(pos + currentKey.length());
                    text = firstPart + value + secondPart;
                    pos = text.indexOf(currentKey);
                }
            }
        }
        text = replaceSystemVar(text);
        return replaceEnvVar(text);
    }
   
    /**
     * This method returns the number of variables
     * for one specific configuration. If the configuration
     * does not exist it will return 0;
     *
     * @param configName the name of the configuration
     * @return the number of variables
     */
    public int getVariableCount(String configName) {
        if ( varMapping.containsKey(configName) ) {
            return ((HashMap)varMapping.get(configName)).size();
        }
        else {
            return 0;
        }
    }
   
    public String replaceSystemVar(String text) {
        if ( text != null ) {
            int idx = text.indexOf("${system:");           
            // walk through the text and replace it
            while ( idx != -1 ) {
                String firstPart = text.substring(0, idx);
                String envVar = text.substring(idx+9,text.indexOf("}"));                  
                String secondPart = text.substring(idx + envVar.length()+10);
                String value = System.getProperty(envVar);               
                if ( value == null ) {
                    value = "${system:"+envVar+"}";
                }
                text = firstPart + value + secondPart;
                idx = text.indexOf("${system:",idx+1);
            }
        }
        return text;
    }
   
    /**
     * This method replace all occurences ${env:xxx} with
     * the value of an environment variable. If the env-variable
     * does not exist then the part is not converted. The
     * method gets called from replaceVariables
     *
     * @param text the line of text that will be processed
     * @return a String with the replaced env-variables.
     */
    public String replaceEnvVar(String text) {
        if ( text != null ) {
            int idx = text.indexOf("${env:");
            // walk through the text and replace it
            while ( idx != -1 ) {
                String firstPart = text.substring(0, idx);
                String envVar = text.substring(idx+6,text.indexOf("}"));             
                String secondPart = text.substring(idx + envVar.length()+7);
                String value = envVariables.getProperty(envVar);               
                if ( value == null ) {
                    value = "${env:"+envVar+"}";
                }
                text = firstPart + value + secondPart;
                idx = text.indexOf("${env:",idx+1);
            }
        }
        return text;
    }
   
    // Thanx to http://www.rgagnon.com/howto.html for
    // this implementation.
    private Properties getEnvVars() {
        Process p = null;
        Properties envVars = new Properties();
        try {
        Runtime r = Runtime.getRuntime();
        String OS = System.getProperty("os.name").toLowerCase();
        // System.out.println(OS);
        if (OS.indexOf("windows 9") > -1) {
            p = r.exec( "command.com /c set" );
        }
        else if ( (OS.indexOf("nt") > -1)
        || (OS.indexOf("windows 2000") > -1
        || (OS.indexOf("windows xp") > -1) ) ) {
            // thanks to JuanFran for the xp fix!
            p = r.exec( "cmd.exe /c set" );
        }
        else {
            // our last hope, we assume Unix (thanks to H. Ware for the fix)
            p = r.exec( "env" );
        }
        BufferedReader br = new BufferedReader
        ( new InputStreamReader( p.getInputStream() ) );
        String line;
        while( (line = br.readLine()) != null ) {
            int idx = line.indexOf( '=' );
            String key = line.substring( 0, idx );
            String value = line.substring( idx+1 );
            envVars.setProperty( key, value );
            //System.out.println( key + " = " + value );
        }
        }
        catch (Exception e) {
            // we do not care here. Just no env vars for the user. Sorry.
        }
        return envVars;
    }
   
}
TOP

Related Classes of org.jzonic.jlo.VariableManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.