001: //$Id: PropertiesHelper.java 9712 2006-03-29 13:56:59Z steve.ebersole@jboss.com $
002: package org.hibernate.util;
003:
004: import java.util.HashMap;
005: import java.util.Map;
006: import java.util.Properties;
007: import java.util.StringTokenizer;
008: import java.util.Iterator;
009:
010: public final class PropertiesHelper {
011:
012: private static final String PLACEHOLDER_START = "${";
013:
014: public static boolean getBoolean(String property,
015: Properties properties) {
016: String setting = properties.getProperty(property);
017: return setting != null
018: && Boolean.valueOf(setting.trim()).booleanValue();
019: }
020:
021: public static boolean getBoolean(String property,
022: Properties properties, boolean defaultValue) {
023: String setting = properties.getProperty(property);
024: return setting == null ? defaultValue : Boolean.valueOf(
025: setting.trim()).booleanValue();
026: }
027:
028: public static int getInt(String property, Properties properties,
029: int defaultValue) {
030: String propValue = properties.getProperty(property);
031: return propValue == null ? defaultValue : Integer
032: .parseInt(propValue.trim());
033: }
034:
035: public static String getString(String property,
036: Properties properties, String defaultValue) {
037: String propValue = properties.getProperty(property);
038: return propValue == null ? defaultValue : propValue;
039: }
040:
041: public static Integer getInteger(String property,
042: Properties properties) {
043: String propValue = properties.getProperty(property);
044: return propValue == null ? null : Integer.valueOf(propValue
045: .trim());
046: }
047:
048: public static Map toMap(String property, String delim,
049: Properties properties) {
050: Map map = new HashMap();
051: String propValue = properties.getProperty(property);
052: if (propValue != null) {
053: StringTokenizer tokens = new StringTokenizer(propValue,
054: delim);
055: while (tokens.hasMoreTokens()) {
056: map.put(tokens.nextToken(),
057: tokens.hasMoreElements() ? tokens.nextToken()
058: : "");
059: }
060: }
061: return map;
062: }
063:
064: public static String[] toStringArray(String property, String delim,
065: Properties properties) {
066: return toStringArray(properties.getProperty(property), delim);
067: }
068:
069: public static String[] toStringArray(String propValue, String delim) {
070: if (propValue != null) {
071: return StringHelper.split(delim, propValue);
072: } else {
073: return ArrayHelper.EMPTY_STRING_ARRAY;
074: }
075: }
076:
077: /**
078: * replace a property by a starred version
079: *
080: * @param props properties to check
081: * @param key proeprty to mask
082: * @return cloned and masked properties
083: */
084: public static Properties maskOut(Properties props, String key) {
085: Properties clone = (Properties) props.clone();
086: if (clone.get(key) != null) {
087: clone.setProperty(key, "****");
088: }
089: return clone;
090: }
091:
092: public static void resolvePlaceHolders(Properties properties) {
093: Iterator itr = properties.entrySet().iterator();
094: while (itr.hasNext()) {
095: final Map.Entry entry = (Map.Entry) itr.next();
096: final Object value = entry.getValue();
097: if (value != null && String.class.isInstance(value)) {
098: final String resolved = resolvePlaceHolder((String) value);
099: if (!value.equals(resolved)) {
100: if (resolved == null) {
101: itr.remove();
102: } else {
103: entry.setValue(resolved);
104: }
105: }
106: }
107: }
108: }
109:
110: public static String resolvePlaceHolder(String property) {
111: if (property.indexOf(PLACEHOLDER_START) < 0) {
112: return property;
113: }
114: StringBuffer buff = new StringBuffer();
115: char[] chars = property.toCharArray();
116: for (int pos = 0; pos < chars.length; pos++) {
117: if (chars[pos] == '$') {
118: // peek ahead
119: if (chars[pos + 1] == '{') {
120: // we have a placeholder, spin forward till we find the end
121: String systemPropertyName = "";
122: int x = pos + 2;
123: for (; x < chars.length && chars[x] != '}'; x++) {
124: systemPropertyName += chars[x];
125: // if we reach the end of the string w/o finding the
126: // matching end, that is an exception
127: if (x == chars.length - 1) {
128: throw new IllegalArgumentException(
129: "unmatched placeholder start ["
130: + property + "]");
131: }
132: }
133: String systemProperty = extractFromSystem(systemPropertyName);
134: buff.append(systemProperty == null ? ""
135: : systemProperty);
136: pos = x + 1;
137: // make sure spinning forward did not put us past the end of the buffer...
138: if (pos >= chars.length) {
139: break;
140: }
141: }
142: }
143: buff.append(chars[pos]);
144: }
145: String rtn = buff.toString();
146: return StringHelper.isEmpty(rtn) ? null : rtn;
147: }
148:
149: private static String extractFromSystem(String systemPropertyName) {
150: try {
151: return System.getProperty(systemPropertyName);
152: } catch (Throwable t) {
153: return null;
154: }
155: }
156:
157: private PropertiesHelper() {
158: }
159: }
|