Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Hibernate, Relational Persistence for Idiomatic Java
   *
   * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as
   * indicated by the @author tags or express copyright attribution
   * statements applied by the authors.  All third-party contributions are
   * distributed under license by Red Hat Inc.
   *
   * This copyrighted material is made available to anyone wishing to use, modify,
  * copy, or redistribute it subject to the terms and conditions of the GNU
  * Lesser General Public License, as published by the Free Software Foundation.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
  * for more details.
  *
  * You should have received a copy of the GNU Lesser General Public License
  * along with this distribution; if not, write to:
  * Free Software Foundation, Inc.
  * 51 Franklin Street, Fifth Floor
  * Boston, MA  02110-1301  USA
  */
 package org.hibernate.internal.util.config;
 
 import java.util.Map;
Collection of helper methods for dealing with configuration settings.

Author(s):
Gavin King
Steve Ebersole
 
 public final class ConfigurationHelper {
 
 	private static final String PLACEHOLDER_START = "${";

Disallow instantiation
 
 	private ConfigurationHelper() {
 	}

Get the config value as a java.lang.String

Parameters:
name The config setting name.
values The map of config values
Returns:
The value, or null if not found
 
 	public static String getString(String nameMap values) {
 		Object value = values.getname );
 		if ( value == null ) {
 			return null;
 		}
 		if ( String.class.isInstancevalue ) ) {
 			return (Stringvalue;
 		}
 		return value.toString();
 	}

Get the config value as a java.lang.String

Parameters:
name The config setting name.
values The map of config values
defaultValue The default value to use if not found
Returns:
The value.
 
 	public static String getString(String nameMap valuesString defaultValue) {
 		final String value = getStringnamevalues );
 		return value == null ? defaultValue : value;
 	}

Get the config value as a boolean (default of false)

Parameters:
name The config setting name.
values The map of config values
Returns:
The value.
 
 	public static boolean getBoolean(String nameMap values) {
 		return getBooleannamevaluesfalse );
 	}

Get the config value as a boolean.

Parameters:
name The config setting name.
values The map of config values
defaultValue The default value to use if not found
Returns:
The value.
	public static boolean getBoolean(String nameMap valuesboolean defaultValue) {
		Object value = values.getname );
		if ( value == null ) {
			return defaultValue;
		}
		if ( Boolean.class.isInstancevalue ) ) {
			return ( (Booleanvalue ).booleanValue();
		}
		if ( String.class.isInstancevalue ) ) {
			return Boolean.parseBoolean( (Stringvalue );
		}
				"Could not determine how to handle configuration value [name=" + name + ", value=" + value + "] as boolean"
		);
	}

Get the config value as an int

Parameters:
name The config setting name.
values The map of config values
defaultValue The default value to use if not found
Returns:
The value.
	public static int getInt(String nameMap valuesint defaultValue) {
		Object value = values.getname );
		if ( value == null ) {
			return defaultValue;
		}
		if ( Integer.class.isInstancevalue ) ) {
			return ( (Integervalue ).intValue();
		}
		if ( String.class.isInstancevalue ) ) {
			return Integer.parseInt( (Stringvalue );
		}
				"Could not determine how to handle configuration value [name=" + name +
						", value=" + value + "(" + value.getClass().getName() + ")] as int"
		);
	}

Get the config value as an java.lang.Integer

Parameters:
name The config setting name.
values The map of config values
Returns:
The value, or null if not found
	public static Integer getInteger(String nameMap values) {
		Object value = values.getname );
		if ( value == null ) {
			return null;
		}
		if ( Integer.class.isInstancevalue ) ) {
			return (Integervalue;
		}
		if ( String.class.isInstancevalue ) ) {
			//empty values are ignored
			final String trimmed = value.toString().trim();
			if ( trimmed.isEmpty() ) {
				return null;
			}
			return Integer.valueOftrimmed );
		}
				"Could not determine how to handle configuration value [name=" + name +
						", value=" + value + "(" + value.getClass().getName() + ")] as Integer"
		);
	}

Make a clone of the configuration values.

Parameters:
configurationValues The config values to clone
Returns:
The clone
	@SuppressWarnings({ "unchecked" })
	public static Map clone(Map<?,?> configurationValues) {
		if ( configurationValues == null ) {
			return null;
		}
		// If a Properties object, leverage its clone() impl
		if ( Properties.class.isInstanceconfigurationValues ) ) {
			return (Properties) ( (PropertiesconfigurationValues ).clone();
		}
		// Otherwise make a manual copy
		HashMap clone = new HashMap();
		for ( Map.Entry entry : configurationValues.entrySet() ) {
			clone.putentry.getKey(), entry.getValue() );
		}
		return clone;
	}



replace a property by a starred version

Parameters:
props properties to check
key proeprty to mask
Returns:
cloned and masked properties
	public static Properties maskOut(Properties propsString key) {
		Properties clone = ( Properties ) props.clone();
		if ( clone.getkey ) != null ) {
			clone.setPropertykey"****" );
		}
		return clone;
	}





Extract a property value by name from the given properties object.

Both null and empty string are viewed as the same, and return null.

Parameters:
propertyName The name of the property for which to extract value
properties The properties object
Returns:
The property value; may be null.
	public static String extractPropertyValue(String propertyNameProperties properties) {
		String value = properties.getPropertypropertyName );
		if ( value == null ) {
			return null;
		}
		value = value.trim();
		if ( StringHelper.isEmptyvalue ) ) {
			return null;
		}
		return value;
	}

Constructs a map from a property value.

The exact behavior here is largely dependant upon what is passed in as the delimiter.

Parameters:
propertyName The name of the property for which to retrieve value
delim The string defining tokens used as both entry and key/value delimiters.
properties The properties object
Returns:
The resulting map; never null, though perhaps empty.
See also:
extractPropertyValue(java.lang.String,java.util.Properties)
	public static Map toMap(String propertyNameString delimProperties properties) {
		Map map = new HashMap();
		String value = extractPropertyValuepropertyNameproperties );
		if ( value != null ) {
			StringTokenizer tokens = new StringTokenizervaluedelim );
			while ( tokens.hasMoreTokens() ) {
				map.puttokens.nextToken(), tokens.hasMoreElements() ? tokens.nextToken() : "" );
			}
		}
		return map;
	}

Get a property value as a string array.

Parameters:
propertyName The name of the property for which to retrieve value
delim The delimiter used to separate individual array elements.
properties The properties object
Returns:
The array; never null, though may be empty.
See also:
extractPropertyValue(java.lang.String,java.util.Properties)
toStringArray(java.lang.String,java.lang.String)
	public static String[] toStringArray(String propertyNameString delimProperties properties) {
		return toStringArrayextractPropertyValuepropertyNameproperties ), delim );
	}

Convert a string to an array of strings. The assumption is that the individual array elements are delimited in the source stringForm param by the delim param.

Parameters:
stringForm The string form of the string array.
delim The delimiter used to separate individual array elements.
Returns:
The array; never null, though may be empty.
	public static String[] toStringArray(String stringFormString delim) {
		// todo : move to StringHelper?
		if ( stringForm != null ) {
			return StringHelper.splitdelimstringForm );
		}
		else {
		}
	}

Handles interpolation processing for all entries in a properties object.

Parameters:
configurationValues The configuration map.
	public static void resolvePlaceHolders(Map<?,?> configurationValues) {
		Iterator itr = configurationValues.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 );
					}
				}
			}
		}
	}

Handles interpolation processing for a single property.

Parameters:
property The property value to be processed for interpolation.
Returns:
The (possibly) interpolated property value.
	public static String resolvePlaceHolder(String property) {
		if ( property.indexOf ) < 0 ) {
			return property;
		}
		StringBuffer buff = new StringBuffer();
		char[] chars = property.toCharArray();
		for ( int pos = 0; pos < chars.lengthpos++ ) {
			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 = 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;
		}
	}
New to GrepCode? Check out our FAQ X