diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/org/traccar/Config.java | 67 |
1 files changed, 35 insertions, 32 deletions
diff --git a/src/org/traccar/Config.java b/src/org/traccar/Config.java index 1be86fce0..c52c1bd81 100644 --- a/src/org/traccar/Config.java +++ b/src/org/traccar/Config.java @@ -23,40 +23,51 @@ import java.util.Properties; public class Config { private final Properties properties = new Properties(); - private Properties defaultProperties; - public void load(String file) throws IOException { - try (InputStream inputStream = new FileInputStream(file)) { - properties.loadFromXML(inputStream); - } + private boolean useEnvVars = false; + void load(String file) throws IOException { + // First we load default config (if any) String defaultConfigFile = properties.getProperty("config.default"); if (defaultConfigFile != null) { try (InputStream inputStream = new FileInputStream(defaultConfigFile)) { - defaultProperties = new Properties(); - defaultProperties.loadFromXML(inputStream); + properties.loadFromXML(inputStream); } } + // Then we override by loading <code>file</code> + try (InputStream inputStream = new FileInputStream(file)) { + Properties props = new Properties(); + props.loadFromXML(inputStream); + properties.putAll(props); + } + // Environment variables interpolation support + if ("true".equals(System.getenv("CONFIG_USE_ENV"))) { + useEnvVars = true; + } else { + useEnvVars = properties.getProperty("config.useEnv", "false").equalsIgnoreCase("true"); + } } + public boolean hasKey(String key) { - return properties.containsKey(key) || defaultProperties != null && defaultProperties.containsKey(key); + if (useEnvVars && System.getenv().containsKey(getEnvVarName(key))) { + return true; + } + return properties.containsKey(key); } public String getString(String key) { - if (properties.containsKey(key) || defaultProperties == null) { - return properties.getProperty(key); - } else { - return defaultProperties.getProperty(key); + if (useEnvVars) { + String envValue = System.getenv(getEnvVarName(key)); + if (envValue != null && !envValue.isEmpty()) { + return envValue; + } } + return properties.getProperty(key); } public String getString(String key, String defaultValue) { - if (hasKey(key)) { - return getString(key); - } else { - return defaultValue; - } + return hasKey(key) ? getString(key) : defaultValue; } public boolean getBoolean(String key) { @@ -68,11 +79,7 @@ public class Config { } public int getInteger(String key, int defaultValue) { - if (hasKey(key)) { - return Integer.parseInt(getString(key)); - } else { - return defaultValue; - } + return hasKey(key) ? Integer.parseInt(getString(key)) : defaultValue; } public long getLong(String key) { @@ -80,11 +87,7 @@ public class Config { } public long getLong(String key, long defaultValue) { - if (hasKey(key)) { - return Long.parseLong(getString(key)); - } else { - return defaultValue; - } + return hasKey(key) ? Long.parseLong(getString(key)) : defaultValue; } public double getDouble(String key) { @@ -92,11 +95,11 @@ public class Config { } public double getDouble(String key, double defaultValue) { - if (hasKey(key)) { - return Double.parseDouble(getString(key)); - } else { - return defaultValue; - } + return hasKey(key) ? Double.parseDouble(getString(key)) : defaultValue; + } + + public static String getEnvVarName(String key) { + return key.replaceAll("\\.", "_").replaceAll("(.)(\\p{Lu})", "$1_$2").toUpperCase(); } } |