All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.scalecube.config.source.SystemEnvironmentSingleVariableConfigSource Maven / Gradle / Ivy

There is a newer version: 0.4.21.RC1
Show newest version
package io.scalecube.config.source;

import io.scalecube.config.ConfigProperty;
import java.util.Map;
import java.util.TreeMap;

public final class SystemEnvironmentSingleVariableConfigSource implements ConfigSource {

  private static final String VAR_NAME = "SETTINGS";

  private static final String DELIMITER = ";";
  private static final String SEPARATOR = "=";

  private final String varName;
  private final String delimiter;
  private final String separator;

  private Map loadedConfig;

  public SystemEnvironmentSingleVariableConfigSource() {
    this(VAR_NAME, DELIMITER, SEPARATOR);
  }

  /**
   * Constructor.
   *
   * @param varName varName
   * @param delimiter delimiter
   * @param separator separator
   */
  public SystemEnvironmentSingleVariableConfigSource(
      String varName, String delimiter, String separator) {
    this.varName = varName;
    this.delimiter = delimiter;
    this.separator = separator;
  }

  @Override
  public Map loadConfig() {
    if (loadedConfig != null) {
      return loadedConfig;
    }

    Map result = new TreeMap<>();

    String settings = System.getenv(varName);

    if (settings != null && !settings.isEmpty()) {
      for (String str : settings.split(delimiter)) {
        String[] split = str.split(separator);
        if (split.length != 2) {
          break;
        }

        String propName = split[0];
        String propValue = split[1];

        // store value as system property under mapped name
        System.setProperty(propName, propValue);
        result.put(propName, LoadedConfigProperty.forNameAndValue(propName, propValue));
      }
    }

    return loadedConfig = result;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy