FileDocCategorySizeDatePackage
PropertiesHelper.javaAPI DocHibernate 3.2.54609Wed Mar 29 07:57:00 BST 2006org.hibernate.util

PropertiesHelper

public final class PropertiesHelper extends Object

Fields Summary
private static final String
PLACEHOLDER_START
Constructors Summary
private PropertiesHelper()

Methods Summary
private static java.lang.StringextractFromSystem(java.lang.String systemPropertyName)

		try {
			return System.getProperty( systemPropertyName );
		}
		catch( Throwable t ) {
			return null;
		}
	
public static booleangetBoolean(java.lang.String property, java.util.Properties properties)


	       
		String setting = properties.getProperty(property);
		return setting != null && Boolean.valueOf( setting.trim() ).booleanValue();
	
public static booleangetBoolean(java.lang.String property, java.util.Properties properties, boolean defaultValue)

		String setting = properties.getProperty(property);
		return setting==null ? defaultValue : Boolean.valueOf( setting.trim() ).booleanValue();
	
public static intgetInt(java.lang.String property, java.util.Properties properties, int defaultValue)

		String propValue = properties.getProperty(property);
		return propValue==null ? defaultValue : Integer.parseInt( propValue.trim() );
	
public static java.lang.IntegergetInteger(java.lang.String property, java.util.Properties properties)

		String propValue = properties.getProperty(property);
		return propValue==null ? null : Integer.valueOf( propValue.trim() );
	
public static java.lang.StringgetString(java.lang.String property, java.util.Properties properties, java.lang.String defaultValue)

		String propValue = properties.getProperty(property);
		return propValue==null ? defaultValue : propValue;
	
public static java.util.PropertiesmaskOut(java.util.Properties props, java.lang.String key)
replace a property by a starred version

param
props properties to check
param
key proeprty to mask
return
cloned and masked properties

		Properties clone = (Properties) props.clone();
		if (clone.get(key) != null) {
			clone.setProperty(key, "****");
		}
		return clone;
	
public static java.lang.StringresolvePlaceHolder(java.lang.String property)

		if ( property.indexOf( PLACEHOLDER_START ) < 0 ) {
			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 - 1 ) {
							throw new IllegalArgumentException( "unmatched placeholder start [" + property + "]" );
						}
					}
					String systemProperty = extractFromSystem( systemPropertyName );
					buff.append( systemProperty == 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.append( chars[pos] );
		}
		String rtn = buff.toString();
		return StringHelper.isEmpty( rtn ) ? null : rtn;
	
public static voidresolvePlaceHolders(java.util.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.isInstance( value ) ) {
				final String resolved = resolvePlaceHolder( ( String ) value );
				if ( !value.equals( resolved ) ) {
					if ( resolved == null ) {
						itr.remove();
					}
					else {
						entry.setValue( resolved );
					}
				}
			}
		}
	
public static java.util.MaptoMap(java.lang.String property, java.lang.String delim, java.util.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 java.lang.String[]toStringArray(java.lang.String property, java.lang.String delim, java.util.Properties properties)

		return toStringArray( properties.getProperty(property), delim );
	
public static java.lang.String[]toStringArray(java.lang.String propValue, java.lang.String delim)

		if (propValue!=null) {
			return StringHelper.split(delim, propValue);
		}
		else {
			return ArrayHelper.EMPTY_STRING_ARRAY;
		}