001package io.ebean.config;
002
003import java.util.Properties;
004
005public class PropertiesWrapper {
006
007  protected final Properties properties;
008
009  protected final String prefix;
010
011  protected final String serverName;
012
013  private final ClassLoadConfig classLoadConfig;
014
015  /**
016   * Construct with a prefix, serverName and properties.
017   */
018  public PropertiesWrapper(String prefix, String serverName, Properties properties, ClassLoadConfig classLoadConfig) {
019    this.serverName = serverName;
020    this.prefix = prefix;
021    this.properties = properties;
022    this.classLoadConfig = classLoadConfig;
023  }
024
025  /**
026   * Construct without prefix of serverName.
027   */
028  public PropertiesWrapper(Properties properties, ClassLoadConfig classLoadConfig) {
029    this(null, null, properties, classLoadConfig);
030  }
031
032  /**
033   * Return the serverName (optional).
034   */
035  public String getServerName() {
036    return serverName;
037  }
038
039  /**
040   * Get a property with no default value.
041   */
042  public String get(String key) {
043    return get(key, null);
044  }
045
046  /**
047   * Get a property with a default value.
048   * <p>
049   * This performs a search using the prefix and server name (if supplied) to search for the property
050   * value in order based on:
051   * <pre>{@code
052   *   prefix.serverName.key
053   *   prefix.key
054   *   key
055   * }</pre>
056   * </p>
057   */
058  public String get(String key, String defaultValue) {
059
060    String value = null;
061    if (serverName != null && prefix != null) {
062      value = internalGet(prefix + "." + serverName + "." + key);
063    }
064    if (value == null && prefix != null) {
065      value = internalGet(prefix + "." + key);
066    }
067    if (value == null) {
068      value = internalGet(key);
069    }
070    return value == null ? defaultValue : value;
071  }
072
073  private String internalGet(String key) {
074    return properties.getProperty(key);
075  }
076
077  /**
078   * Return a double property value.
079   */
080  public double getDouble(String key, double defaultValue) {
081
082    String value = get(key, String.valueOf(defaultValue));
083    return Double.parseDouble(value);
084  }
085
086  /**
087   * Return an int property value.
088   */
089  public int getInt(String key, int defaultValue) {
090
091    String value = get(key, String.valueOf(defaultValue));
092    return Integer.parseInt(value);
093  }
094
095  /**
096   * Return a long property value.
097   */
098  public long getLong(String key, long defaultValue) {
099
100    String value = get(key, String.valueOf(defaultValue));
101    return Long.parseLong(value);
102  }
103
104  /**
105   * Return a boolean property value.
106   */
107  public boolean getBoolean(String key, boolean defaultValue) {
108
109    String value = get(key, String.valueOf(defaultValue));
110    return Boolean.parseBoolean(value);
111  }
112
113  /**
114   * Return a Enum property value.
115   */
116  public <T extends Enum<T>> T getEnum(Class<T> enumType, String key, T defaultValue) {
117    String level = get(key, null);
118    return (level == null) ? defaultValue : Enum.valueOf(enumType, level.toUpperCase());
119  }
120
121  /**
122   * Return the instance to use (can be null) for the given plugin.
123   *
124   * @param pluginType the type of plugin
125   * @param key        properties key
126   * @param instance   existing instance
127   */
128  public <T> T createInstance(Class<T> pluginType, String key, T instance) {
129
130    if (instance != null) {
131      return instance;
132    }
133    String classname = get(key, null);
134    return createInstance(pluginType, classname);
135  }
136
137  /**
138   * Return the instance to use (can be null) for the given plugin.
139   *
140   * @param pluginType the type of plugin
141   * @param classname  the implementation class as per properties
142   */
143  @SuppressWarnings("unchecked")
144  public <T> T createInstance(Class<T> pluginType, String classname) {
145    return classname == null ? null : (T) classLoadConfig.newInstance(classname);
146  }
147}