001package com.avaje.ebean.config;
002
003import java.io.Serializable;
004import java.util.LinkedHashMap;
005import java.util.Map;
006import java.util.Map.Entry;
007import java.util.Properties;
008import java.util.Set;
009
010/**
011 * A map like structure of properties.
012 * <p/>
013 * Handles evaluation of expressions like ${home} and provides convenience methods for int, long and boolean.
014 */
015public final class PropertyMap implements Serializable {
016
017  private static final long serialVersionUID = 1L;
018
019  private final LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
020
021  /**
022   * Return properties loaded from test-ebean.properties.
023   */
024  public static Properties testProperties() {
025    PropertyMap propertyMap = PropertyMapLoader.loadTestProperties();
026    return (propertyMap == null) ? new Properties() : propertyMap.asProperties();
027  }
028
029  public static Properties defaultProperties() {
030    PropertyMap propertyMap = PropertyMapLoader.loadGlobalProperties();
031    return (propertyMap == null) ? new Properties() : propertyMap.asProperties();
032  }
033
034  public static boolean loadTestProperties() {
035    String disable = System.getProperty("disableTestProperties");
036    return (disable == null || !disable.equalsIgnoreCase("true"));
037  }
038
039  public String toString() {
040    return map.toString();
041  }
042
043  /**
044   * Return as standard Properties.
045   */
046  public Properties asProperties() {
047    Properties properties = new Properties();
048    for (Entry<String, String> e : entrySet()) {
049      properties.put(e.getKey(), e.getValue());
050    }
051    return properties;
052  }
053
054  /**
055   * Go through all the properties and evaluate any expressions that have not
056   * been resolved.
057   */
058  public void evaluateProperties() {
059
060    for (Entry<String, String> e : entrySet()) {
061      String key = e.getKey();
062      String val = e.getValue();
063      String eval = eval(val);
064      if (eval != null && !eval.equals(val)) {
065        put(key, eval);
066      }
067    }
068  }
069
070  /**
071   * Returns the value with expressions like ${home} evaluated using system properties and environment variables.
072   */
073  public synchronized String eval(String val) {
074    return PropertyExpression.eval(val, this);
075  }
076
077  /**
078   * Return the boolean property value with a given default.
079   */
080  public synchronized boolean getBoolean(String key, boolean defaultValue) {
081    String value = get(key);
082    if (value == null) {
083      return defaultValue;
084    } else {
085      return Boolean.parseBoolean(value);
086    }
087  }
088
089  /**
090   * Return the int property value with a given default.
091   */
092  public synchronized int getInt(String key, int defaultValue) {
093    String value = get(key);
094    if (value == null) {
095      return defaultValue;
096    } else {
097      return Integer.parseInt(value);
098    }
099  }
100
101  /**
102   * Return the long property value with a given default.
103   */
104  public synchronized long getLong(String key, long defaultValue) {
105    String value = get(key);
106    if (value == null) {
107      return defaultValue;
108    } else {
109      return Long.parseLong(value);
110    }
111  }
112
113  /**
114   * Return the string property value with a given default.
115   */
116  public synchronized String get(String key, String defaultValue) {
117    String value = map.get(key.toLowerCase());
118    return value == null ? defaultValue : value;
119  }
120
121  /**
122   * Return the property value returning null if there is no value defined.
123   */
124  public synchronized String get(String key) {
125    return map.get(key.toLowerCase());
126  }
127
128  /**
129   * Put all evaluating any expressions in the values.
130   */
131  public synchronized void putEvalAll(Map<String, String> keyValueMap) {
132
133    for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
134      putEval(entry.getKey(), entry.getValue());
135    }
136  }
137
138  /**
139   * Put a single key value evaluating any expressions in the value.
140   */
141  public synchronized String putEval(String key, String value) {
142    value = PropertyExpression.eval(value, this);
143    return map.put(key.toLowerCase(), value);
144  }
145
146  /**
147   * Put a single key value with no expression evaluation.
148   */
149  public synchronized String put(String key, String value) {
150    return map.put(key.toLowerCase(), value);
151  }
152
153  /**
154   * Remove an entry.
155   */
156  public synchronized String remove(String key) {
157    return map.remove(key.toLowerCase());
158  }
159
160  /**
161   * Return the entries.
162   */
163  public synchronized Set<Entry<String, String>> entrySet() {
164    return map.entrySet();
165  }
166
167}