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

com.aventstack.chaintest.conf.Configuration Maven / Gradle / Ivy

The newest version!
package com.aventstack.chaintest.conf;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * ChainTest allows you to load externalized configuration from properties files,
 * environment variables and system properties using the following order:
 *
 * 
    *
  1. application.properties
  2. *
  3. application-test.properties
  4. *
  5. test.properties
  6. *
  7. config.properties
  8. *
  9. chaintest.properties
  10. *
  11. OS environment variables
  12. *
  13. System.getProperties()
  14. *
*/ public class Configuration { private static final Logger log = LoggerFactory.getLogger(Configuration.class); private static final String APP_NAME = "chaintest"; private static final String[] RESOURCES = new String[] { "application.properties", "application-test.properties", "test.properties", "config.properties", "chaintest.properties" }; private final Map _config = new HashMap<>(); public String get(final String prop) { return _config.get(prop); } public Map getConfig() { return _config; } public void load() throws IOException { log.trace("Starting load externalized configuration"); for (final String resource : RESOURCES) { loadFromClasspathResource(resource); } System.getenv().forEach((k, v) -> { if (k.startsWith(APP_NAME)) { log.trace("Adding environment property {} to configuration", k); _config.put(k, v); } }); loadFromProperties(System.getProperties()); } public void loadFromClasspathResource(final String resource) throws IOException { log.trace("Loading configuration from resource {}", resource); final Properties properties = new Properties(); try (final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource)) { if (is != null) { properties.load(is); loadFromProperties(properties); } } log.trace("Configuration entries after loading from resource {}: {}", resource, _config); } public void loadFromProperties(final Properties properties) { properties.forEach((key, value) -> { if (key.toString().startsWith(APP_NAME)) { _config.put(key.toString(), value.toString()); } }); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy