cucumber.runtime.Env Maven / Gradle / Ivy
package cucumber.runtime;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
/**
* Looks up values in the following order:
*
* - Environment variable
* - System property
* - Resource bundle
*
*/
public class Env {
public static final Env INSTANCE = new Env("cucumber");
private final Map map = new HashMap();
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) {
if (bundleName != null) {
try {
ResourceBundle bundle = ResourceBundle.getBundle(bundleName);
for (String key : bundle.keySet()) {
put(key, bundle.getString(key));
}
} catch (MissingResourceException ignore) {
}
}
if (properties != null) {
for (String key : properties.stringPropertyNames()) {
put(key, properties.getProperty(key));
}
}
Map env = System.getenv();
for (String key : env.keySet()) {
put(key, env.get(key));
}
}
private void put(String key, String string) {
map.put(key, string);
// Support old skool
map.put(key.replace('.', '_').toUpperCase(), string);
map.put(key.replace('_', '.').toLowerCase(), string);
}
public String get(String key) {
return map.get(key);
}
public String get(String key, String defaultValue) {
String result = get(key);
return result != null ? result : defaultValue;
}
}