Open Source Repository

Home /hibernate/hibernate-3.2.4.ga | Repository Home



org/hibernate/util/PropertiesHelper.java
//$Id: PropertiesHelper.java 9712 2006-03-29 13:56:59Z [email protected] $
package org.hibernate.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Iterator;


public final class PropertiesHelper {

  private static final String PLACEHOLDER_START = "${";

  public static boolean getBoolean(String property, Properties properties) {
    String setting = properties.getProperty(property);
    return setting != null && Boolean.valueOfsetting.trim() ).booleanValue();
  }

  public static boolean getBoolean(String property, Properties properties, boolean defaultValue) {
    String setting = properties.getProperty(property);
    return setting==null ? defaultValue : Boolean.valueOfsetting.trim() ).booleanValue();
  }

  public static int getInt(String property, Properties properties, int defaultValue) {
    String propValue = properties.getProperty(property);
    return propValue==null ? defaultValue : Integer.parseIntpropValue.trim() );
  }

  public static String getString(String property, Properties properties, String defaultValue) {
    String propValue = properties.getProperty(property);
    return propValue==null ? defaultValue : propValue;
  }

  public static Integer getInteger(String property, Properties properties) {
    String propValue = properties.getProperty(property);
    return propValue==null null : Integer.valueOfpropValue.trim() );
  }

  public static Map toMap(String property, String delim, Properties properties) {
    Map map = new HashMap();
    String propValue = properties.getProperty(property);
    if (propValue!=null) {
      StringTokenizer tokens = new StringTokenizer(propValue, delim);
      while tokens.hasMoreTokens() ) {
        map.put(
          tokens.nextToken(),
            tokens.hasMoreElements() ? tokens.nextToken() ""
        );
      }
    }
    return map;
  }

  public static String[] toStringArray(String property, String delim, Properties properties) {
    return toStringArrayproperties.getProperty(property), delim );
  }

  public static String[] toStringArray(String propValue, String delim) {
    if (propValue!=null) {
      return StringHelper.split(delim, propValue);
    }
    else {
      return ArrayHelper.EMPTY_STRING_ARRAY;
    }
  }

  /**
   * replace a property by a starred version
   *
   @param props properties to check
   @param key proeprty to mask
   @return cloned and masked properties
   */
  public static Properties maskOut(Properties props, String key) {
    Properties clone = (Propertiesprops.clone();
    if (clone.get(key!= null) {
      clone.setProperty(key, "****");
    }
    return clone;
  }

  public static void resolvePlaceHolders(Properties properties) {
    Iterator itr = properties.entrySet().iterator();
    while itr.hasNext() ) {
      final Map.Entry entry = Map.Entry itr.next();
      final Object value = entry.getValue();
      if value != null && String.class.isInstancevalue ) ) {
        final String resolved = resolvePlaceHolder( ( String value );
        if !value.equalsresolved ) ) {
          if resolved == null ) {
            itr.remove();
          }
          else {
            entry.setValueresolved );
          }
        }
      }
    }
  }

  public static String resolvePlaceHolder(String property) {
    if property.indexOfPLACEHOLDER_START ) {
      return property;
    }
    StringBuffer buff = new StringBuffer();
    char[] chars = property.toCharArray();
    for int pos = 0; pos < chars.length; pos++ ) {
      if chars[pos== '$' ) {
        // peek ahead
        if chars[pos+1== '{' ) {
          // we have a placeholder, spin forward till we find the end
          String systemPropertyName = "";
          int x = pos + 2;
          for (  ; x < chars.length && chars[x!= '}'; x++ ) {
            systemPropertyName += chars[x];
            // if we reach the end of the string w/o finding the
            // matching end, that is an exception
            if x == chars.length - ) {
              throw new IllegalArgumentException"unmatched placeholder start [" + property + "]" );
            }
          }
          String systemProperty = extractFromSystemsystemPropertyName );
          buff.appendsystemProperty == null "" : systemProperty );
          pos = x + 1;
          // make sure spinning forward did not put us past the end of the buffer...
          if pos >= chars.length ) {
            break;
          }
        }
      }
      buff.appendchars[pos] );
    }
    String rtn = buff.toString();
    return StringHelper.isEmptyrtn null : rtn;
  }

  private static String extractFromSystem(String systemPropertyName) {
    try {
      return System.getPropertysystemPropertyName );
    }
    catchThrowable t ) {
      return null;
    }
  }


  private PropertiesHelper() {}
}