001package io.ebean.config.properties;
002
003import java.util.Enumeration;
004import java.util.Properties;
005
006/**
007 * Loads and evaluates properties and yml configuration.
008 */
009public class PropertiesLoader {
010
011  private static Properties properties;
012
013  /**
014   * Provides properties by reading known locations.
015   * <p>
016   * <h3>Main configuration</h3>
017   * <p>
018   * <p>Firstly loads from main resources</p>
019   * <pre>
020   *   - application.properties
021   *   - application.yaml
022   *   - ebean.properties
023   * </pre>
024   * <p>
025   * <p>Then loads from local files</p>
026   * <pre>
027   *   - application.properties
028   *   - application.yaml
029   *   - ebean.properties
030   * </pre>
031   * <p>
032   * <p>Then loads from environment variable <em>EBEAN_PROPS_FILE</em></p>
033   * <p>Then loads from system property <em>ebean.props.file</em></p>
034   * <p>Then loads from <em>load.properties</em></p>
035   * <p>
036   * <h3>Test configuration</h3>
037   * <p>
038   * Once the main configuration is read it will try to read common test configuration.
039   * This will only be successful if the test resources are available (i.e. running tests).
040   * </p>
041   * <p>Loads from test resources</p>
042   * <pre>
043   *   - application-test.properties
044   *   - application-test.yaml
045   *   - test-ebean.properties
046   * </pre>
047   */
048  public static synchronized Properties load() {
049
050    if (properties == null) {
051      Loader loader = new Loader();
052      loader.load();
053      properties = loader.eval();
054    }
055
056    return properties;
057  }
058
059  /**
060   * Return a copy of the properties with 'eval' run on all the values.
061   * This resolves expressions like ${HOME} etc.
062   */
063  public static Properties eval(Properties properties) {
064    Properties evalCopy = new Properties();
065
066    Enumeration<?> names = properties.propertyNames();
067    while (names.hasMoreElements()) {
068      String name = (String)names.nextElement();
069      String value = PropertyEval.eval(properties.getProperty(name));
070      evalCopy.setProperty(name, value);
071    }
072    return evalCopy;
073  }
074}