Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, QOS.ch. All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
 
 package ch.qos.logback.core.util;
 
 

Author(s):
Ceki Gulcu
 
 public class OptionHelper {
 
   public static Object instantiateByClassName(String className,
                                               Class superClassContext contextthrows IncompatibleClassException,
           DynamicClassLoadingException {
     ClassLoader classLoader = Loader.getClassLoaderOfObject(context);
     return instantiateByClassName(classNamesuperClassclassLoader);
   }
 
   public static Object instantiateByClassNameAndParameter(String className,
                                                           Class superClassContext contextClass typeObject paramthrows IncompatibleClassException,
           DynamicClassLoadingException {
     ClassLoader classLoader = Loader.getClassLoaderOfObject(context);
     return instantiateByClassNameAndParameter(classNamesuperClassclassLoadertypeparam);
   }
 
   @SuppressWarnings("unchecked")
   public static Object instantiateByClassName(String className,
                                               Class superClassClassLoader classLoader)
     return instantiateByClassNameAndParameter(classNamesuperClassclassLoadernullnull);
   }
 
   public static Object instantiateByClassNameAndParameter(String className,
                                                           Class superClassClassLoader classLoaderClass typeObject parameter)
 
     if (className == null) {
       throw new NullPointerException();
     }
     try {
       Class classObj = null;
       classObj = classLoader.loadClass(className);
       if (!superClass.isAssignableFrom(classObj)) {
         throw new IncompatibleClassException(superClassclassObj);
       }
       if (type == null) {
         return classObj.newInstance();
       } else {
         Constructor constructor = classObj.getConstructor(type);
         return constructor.newInstance(parameter);
       }
     } catch (IncompatibleClassException ice) {
       throw ice;
     } catch (Throwable t) {
       throw new DynamicClassLoadingException("Failed to instantiate type "
               + classNamet);
     }
   }

  
Find the value corresponding to key in props. Then perform variable substitution on the found value.
 
   // public static String findAndSubst(String key, Properties props) {
   // String value = props.getProperty(key);
   //
   // if (value == null) {
   // return null;
   // }
   //
   // try {
   // return substVars(value, props);
   // } catch (IllegalArgumentException e) {
   // return value;
   // }
   // }
   final static String DELIM_START = "${";
   final static char DELIM_STOP = '}';
   final static String DELIM_DEFAULT = ":-";
 
  final static int DELIM_START_LEN = 2;
  final static int DELIM_STOP_LEN = 1;
  final static int DELIM_DEFAULT_LEN = 2;
  final static String _IS_UNDEFINED = "_IS_UNDEFINED";

  
  public static String substVars(String valPropertyContainer pc1) {
    return substVars(valpc1null);
  }

  
See http://logback.qos.ch/manual/configuration.html#variableSubstitution
  public static String substVars(String inputPropertyContainer pc0PropertyContainer pc1) {
    try {
      String replacement = NodeToStringTransformer.substituteVariable(inputpc0pc1);
      // for backward compatibility sake, perform one level of recursion
      if(replacement.contains()) {
        replacement =  NodeToStringTransformer.substituteVariable(replacementpc0pc1);
      }
      return replacement;
    } catch (ScanException e) {
      throw new IllegalArgumentException("Failed to parse input [" + input + "]"e);
    }
  }
  public static String propertyLookup(String keyPropertyContainer pc1,
                                      PropertyContainer pc2) {
    String value = null;
    // first try the props passed as parameter
    value = pc1.getProperty(key);
    // then try  the pc2
    if (value == null && pc2 != null) {
      value = pc2.getProperty(key);
    }
    // then try in System properties
    if (value == null) {
      value = getSystemProperty(keynull);
    }
    if (value == null) {
      value = getEnv(key);
    }
    return value;
  }

  
Very similar to System.getProperty except that the java.lang.SecurityException is absorbed.

Parameters:
key The key to search for.
def The default value to return.
Returns:
the string value of the system property, or the default value if there is no property with that key.
  public static String getSystemProperty(String keyString def) {
    try {
      return System.getProperty(keydef);
    } catch (SecurityException e) {
      return def;
    }
  }

  
Lookup a key from the environment.

Parameters:
key
Returns:
value corresponding to key from the OS environment
  public static String getEnv(String key) {
    try {
      return System.getenv(key);
    } catch (SecurityException e) {
      return null;
    }
  }


  
Very similar to System.getProperty except that the java.lang.SecurityException is absorbed.

Parameters:
key The key to search for.
Returns:
the string value of the system property.
  public static String getSystemProperty(String key) {
    try {
      return System.getProperty(key);
    } catch (SecurityException e) {
      return null;
    }
  }
  public static void setSystemProperties(ContextAware contextAwareProperties props) {
    for (Object o : props.keySet()) {
      String key = (Stringo;
      String value = props.getProperty(key);
      setSystemProperty(contextAwarekeyvalue);
    }
  }
  public static void setSystemProperty(ContextAware contextAwareString keyString value) {
    try {
      System.setProperty(keyvalue);
    } catch (SecurityException e) {
      contextAware.addError("Failed to set system property [" + key + "]"e);
    }
  }

  
Very similar to java.lang.System.getProperties() except that the java.lang.SecurityException is absorbed.

Returns:
the system properties
  public static Properties getSystemProperties() {
    try {
      return System.getProperties();
    } catch (SecurityException e) {
      return new Properties();
    }
  }
  static public String[] extractDefaultReplacement(String key) {
    String[] result = new String[2];
    result[0] = key;
    int d = key.indexOf();
    if (d != -1) {
      result[0] = key.substring(0, d);
      result[1] = key.substring(d + );
    }
    return result;
  }

  
If value is "true", then true is returned. If value is "false", then true is returned. Otherwise, default is returned.

Case of value is unimportant.

  public static boolean toBoolean(String valueboolean dEfault) {
    if (value == null) {
      return dEfault;
    }
    String trimmedVal = value.trim();
    if ("true".equalsIgnoreCase(trimmedVal)) {
      return true;
    }
    if ("false".equalsIgnoreCase(trimmedVal)) {
      return false;
    }
    return dEfault;
  }
  public static boolean isEmpty(String str) {
    return ((str == null) || ..equals(str));
  }
New to GrepCode? Check out our FAQ X