~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/util/PropertiesHelper.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: PropertiesHelper.java 9712 2006-03-29 13:56:59Z steve.ebersole@jboss.com $
 
2
package org.hibernate.util;
 
3
 
 
4
import java.util.HashMap;
 
5
import java.util.Map;
 
6
import java.util.Properties;
 
7
import java.util.StringTokenizer;
 
8
import java.util.Iterator;
 
9
 
 
10
 
 
11
public final class PropertiesHelper {
 
12
 
 
13
        private static final String PLACEHOLDER_START = "${";
 
14
 
 
15
        public static boolean getBoolean(String property, Properties properties) {
 
16
                String setting = properties.getProperty(property);
 
17
                return setting != null && Boolean.valueOf( setting.trim() ).booleanValue();
 
18
        }
 
19
 
 
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();
 
23
        }
 
24
 
 
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() );
 
28
        }
 
29
 
 
30
        public static String getString(String property, Properties properties, String defaultValue) {
 
31
                String propValue = properties.getProperty(property);
 
32
                return propValue==null ? defaultValue : propValue;
 
33
        }
 
34
 
 
35
        public static Integer getInteger(String property, Properties properties) {
 
36
                String propValue = properties.getProperty(property);
 
37
                return propValue==null ? null : Integer.valueOf( propValue.trim() );
 
38
        }
 
39
 
 
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() ) {
 
46
                                map.put(
 
47
                                        tokens.nextToken(),
 
48
                                    tokens.hasMoreElements() ? tokens.nextToken() : ""
 
49
                                );
 
50
                        }
 
51
                }
 
52
                return map;
 
53
        }
 
54
 
 
55
        public static String[] toStringArray(String property, String delim, Properties properties) {
 
56
                return toStringArray( properties.getProperty(property), delim );
 
57
        }
 
58
 
 
59
        public static String[] toStringArray(String propValue, String delim) {
 
60
                if (propValue!=null) {
 
61
                        return StringHelper.split(delim, propValue);
 
62
                }
 
63
                else {
 
64
                        return ArrayHelper.EMPTY_STRING_ARRAY;
 
65
                }
 
66
        }
 
67
 
 
68
        /**
 
69
         * replace a property by a starred version
 
70
         *
 
71
         * @param props properties to check
 
72
         * @param key proeprty to mask
 
73
         * @return cloned and masked properties
 
74
         */
 
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, "****");
 
79
                }
 
80
                return clone;
 
81
        }
 
82
 
 
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 ) {
 
92
                                                itr.remove();
 
93
                                        }
 
94
                                        else {
 
95
                                                entry.setValue( resolved );
 
96
                                        }
 
97
                                }
 
98
                        }
 
99
                }
 
100
        }
 
101
 
 
102
        public static String resolvePlaceHolder(String property) {
 
103
                if ( property.indexOf( PLACEHOLDER_START ) < 0 ) {
 
104
                        return property;
 
105
                }
 
106
                StringBuffer buff = new StringBuffer();
 
107
                char[] chars = property.toCharArray();
 
108
                for ( int pos = 0; pos < chars.length; pos++ ) {
 
109
                        if ( chars[pos] == '$' ) {
 
110
                                // peek ahead
 
111
                                if ( chars[pos+1] == '{' ) {
 
112
                                        // we have a placeholder, spin forward till we find the end
 
113
                                        String systemPropertyName = "";
 
114
                                        int x = pos + 2;
 
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 + "]" );
 
121
                                                }
 
122
                                        }
 
123
                                        String systemProperty = extractFromSystem( systemPropertyName );
 
124
                                        buff.append( systemProperty == null ? "" : systemProperty );
 
125
                                        pos = x + 1;
 
126
                                        // make sure spinning forward did not put us past the end of the buffer...
 
127
                                        if ( pos >= chars.length ) {
 
128
                                                break;
 
129
                                        }
 
130
                                }
 
131
                        }
 
132
                        buff.append( chars[pos] );
 
133
                }
 
134
                String rtn = buff.toString();
 
135
                return StringHelper.isEmpty( rtn ) ? null : rtn;
 
136
        }
 
137
 
 
138
        private static String extractFromSystem(String systemPropertyName) {
 
139
                try {
 
140
                        return System.getProperty( systemPropertyName );
 
141
                }
 
142
                catch( Throwable t ) {
 
143
                        return null;
 
144
                }
 
145
        }
 
146
 
 
147
 
 
148
        private PropertiesHelper() {}
 
149
}