1
//$Id: PropertiesHelper.java 9712 2006-03-29 13:56:59Z steve.ebersole@jboss.com $
2
package org.hibernate.util;
4
import java.util.HashMap;
6
import java.util.Properties;
7
import java.util.StringTokenizer;
8
import java.util.Iterator;
11
public final class PropertiesHelper {
13
private static final String PLACEHOLDER_START = "${";
15
public static boolean getBoolean(String property, Properties properties) {
16
String setting = properties.getProperty(property);
17
return setting != null && Boolean.valueOf( setting.trim() ).booleanValue();
20
public static boolean getBoolean(String property, Properties properties, boolean defaultValue) {
21
String setting = properties.getProperty(property);
22
return setting==null ? defaultValue : Boolean.valueOf( setting.trim() ).booleanValue();
25
public static int getInt(String property, Properties properties, int defaultValue) {
26
String propValue = properties.getProperty(property);
27
return propValue==null ? defaultValue : Integer.parseInt( propValue.trim() );
30
public static String getString(String property, Properties properties, String defaultValue) {
31
String propValue = properties.getProperty(property);
32
return propValue==null ? defaultValue : propValue;
35
public static Integer getInteger(String property, Properties properties) {
36
String propValue = properties.getProperty(property);
37
return propValue==null ? null : Integer.valueOf( propValue.trim() );
40
public static Map toMap(String property, String delim, Properties properties) {
41
Map map = new HashMap();
42
String propValue = properties.getProperty(property);
43
if (propValue!=null) {
44
StringTokenizer tokens = new StringTokenizer(propValue, delim);
45
while ( tokens.hasMoreTokens() ) {
48
tokens.hasMoreElements() ? tokens.nextToken() : ""
55
public static String[] toStringArray(String property, String delim, Properties properties) {
56
return toStringArray( properties.getProperty(property), delim );
59
public static String[] toStringArray(String propValue, String delim) {
60
if (propValue!=null) {
61
return StringHelper.split(delim, propValue);
64
return ArrayHelper.EMPTY_STRING_ARRAY;
69
* replace a property by a starred version
71
* @param props properties to check
72
* @param key proeprty to mask
73
* @return cloned and masked properties
75
public static Properties maskOut(Properties props, String key) {
76
Properties clone = (Properties) props.clone();
77
if (clone.get(key) != null) {
78
clone.setProperty(key, "****");
83
public static void resolvePlaceHolders(Properties properties) {
84
Iterator itr = properties.entrySet().iterator();
85
while ( itr.hasNext() ) {
86
final Map.Entry entry = ( Map.Entry ) itr.next();
87
final Object value = entry.getValue();
88
if ( value != null && String.class.isInstance( value ) ) {
89
final String resolved = resolvePlaceHolder( ( String ) value );
90
if ( !value.equals( resolved ) ) {
91
if ( resolved == null ) {
95
entry.setValue( resolved );
102
public static String resolvePlaceHolder(String property) {
103
if ( property.indexOf( PLACEHOLDER_START ) < 0 ) {
106
StringBuffer buff = new StringBuffer();
107
char[] chars = property.toCharArray();
108
for ( int pos = 0; pos < chars.length; pos++ ) {
109
if ( chars[pos] == '$' ) {
111
if ( chars[pos+1] == '{' ) {
112
// we have a placeholder, spin forward till we find the end
113
String systemPropertyName = "";
115
for ( ; x < chars.length && chars[x] != '}'; x++ ) {
116
systemPropertyName += chars[x];
117
// if we reach the end of the string w/o finding the
118
// matching end, that is an exception
119
if ( x == chars.length - 1 ) {
120
throw new IllegalArgumentException( "unmatched placeholder start [" + property + "]" );
123
String systemProperty = extractFromSystem( systemPropertyName );
124
buff.append( systemProperty == null ? "" : systemProperty );
126
// make sure spinning forward did not put us past the end of the buffer...
127
if ( pos >= chars.length ) {
132
buff.append( chars[pos] );
134
String rtn = buff.toString();
135
return StringHelper.isEmpty( rtn ) ? null : rtn;
138
private static String extractFromSystem(String systemPropertyName) {
140
return System.getProperty( systemPropertyName );
142
catch( Throwable t ) {
148
private PropertiesHelper() {}