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

cucumber.runtime.Env Maven / Gradle / Ivy

There is a newer version: 1.2.6
Show newest version
package cucumber.runtime;

import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * Looks up values in the following order:
 * 
    *
  1. Environment variable
  2. *
  3. System property
  4. *
  5. Resource bundle
  6. *
*/ public class Env { private final String bundleName; private final Properties properties; public Env() { this(null, System.getProperties()); } public Env(String bundleName) { this(bundleName, System.getProperties()); } public Env(Properties properties) { this(null, properties); } public Env(String bundleName, Properties properties) { this.bundleName = bundleName; this.properties = properties; } public String get(String key) { String result = getFromEnvironment(key); if (result == null) { result = getFromProperty(key); if (result == null && bundleName != null) { result = getFromBundle(key); } } return result; } private String getFromEnvironment(String key) { String value = System.getenv(asEnvKey(key)); if (value == null) { value = System.getenv(asPropertyKey(key)); } return value; } private String getFromProperty(String key) { String value = properties.getProperty(asEnvKey(key)); if (value == null) { value = properties.getProperty(asPropertyKey(key)); } return value; } private String getFromBundle(String key) { try { String value = ResourceBundle.getBundle(bundleName).getString(asEnvKey(key)); if (value == null) { value = ResourceBundle.getBundle(bundleName).getString(asPropertyKey(key)); } return value; } catch (MissingResourceException ignore) { } return null; } public String get(String key, String defaultValue) { String result = get(key); return result != null ? result : defaultValue; } private static String asEnvKey(String key) { return key.replace('.', '_').toUpperCase(); } private static String asPropertyKey(String key) { return key.replace('_', '.').toLowerCase(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy