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 String toString() {
035    return map.toString();
036  }
037
038  /**
039   * Return as standard Properties.
040   */
041  public Properties asProperties() {
042    Properties properties = new Properties();
043    for (Entry<String, String> e : entrySet()) {
044      properties.put(e.getKey(), e.getValue());
045    }
046    return properties;
047  }
048
049  /**
050   * Go through all the properties and evaluate any expressions that have not
051   * been resolved.
052   */
053  public void evaluateProperties() {
054
055    for (Entry<String, String> e : entrySet()) {
056      String key = e.getKey();
057      String val = e.getValue();
058      String eval = eval(val);
059      if (eval != null && !eval.equals(val)) {
060        put(key, eval);
061      }
062    }
063  }
064
065  /**
066   * Returns the value with expressions like ${home} evaluated using system properties and environment variables.
067   */
068  public synchronized String eval(String val) {
069    return PropertyExpression.eval(val, this);
070  }
071
072  /**
073   * Return the boolean property value with a given default.
074   */
075  public synchronized boolean getBoolean(String key, boolean defaultValue) {
076    String value = get(key);
077    if (value == null) {
078      return defaultValue;
079    } else {
080      return Boolean.parseBoolean(value);
081    }
082  }
083
084  /**
085   * Return the int property value with a given default.
086   */
087  public synchronized int getInt(String key, int defaultValue) {
088    String value = get(key);
089    if (value == null) {
090      return defaultValue;
091    } else {
092      return Integer.parseInt(value);
093    }
094  }
095
096  /**
097   * Return the long property value with a given default.
098   */
099  public synchronized long getLong(String key, long defaultValue) {
100    String value = get(key);
101    if (value == null) {
102      return defaultValue;
103    } else {
104      return Long.parseLong(value);
105    }
106  }
107
108  /**
109   * Return the string property value with a given default.
110   */
111  public synchronized String get(String key, String defaultValue) {
112    String value = map.get(key.toLowerCase());
113    return value == null ? defaultValue : value;
114  }
115
116  /**
117   * Return the property value returning null if there is no value defined.
118   */
119  public synchronized String get(String key) {
120    return map.get(key.toLowerCase());
121  }
122
123  /**
124   * Put all evaluating any expressions in the values.
125   */
126  public synchronized void putEvalAll(Map<String, String> keyValueMap) {
127
128    for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
129      putEval(entry.getKey(), entry.getValue());
130    }
131  }
132
133  /**
134   * Put a single key value evaluating any expressions in the value.
135   */
136  public synchronized String putEval(String key, String value) {
137    value = PropertyExpression.eval(value, this);
138    return map.put(key.toLowerCase(), value);
139  }
140
141  /**
142   * Put a single key value with no expression evaluation.
143   */
144  public synchronized String put(String key, String value) {
145    return map.put(key.toLowerCase(), value);
146  }
147
148  /**
149   * Remove an entry.
150   */
151  public synchronized String remove(String key) {
152    return map.remove(key.toLowerCase());
153  }
154
155  /**
156   * Return the entries.
157   */
158  public synchronized Set<Entry<String, String>> entrySet() {
159    return map.entrySet();
160  }
161
162}