aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/Config.java
diff options
context:
space:
mode:
authorgahissy <titus@sillaps.com>2017-05-03 00:10:13 +0100
committergahissy <titus@sillaps.com>2017-05-03 00:10:13 +0100
commitfa6548a7b613b5bd3919cbf0269d58b2d8ebb999 (patch)
treefa5b6a248ece0abdf2774902f7c183ef35a67edd /src/org/traccar/Config.java
parentaa6b11f88085a149b70b3f55fbae7928280c2b74 (diff)
downloadtrackermap-server-fa6548a7b613b5bd3919cbf0269d58b2d8ebb999.tar.gz
trackermap-server-fa6548a7b613b5bd3919cbf0269d58b2d8ebb999.tar.bz2
trackermap-server-fa6548a7b613b5bd3919cbf0269d58b2d8ebb999.zip
Unit Test for ENV_VAR interpolation. config.useEnv introduced
Diffstat (limited to 'src/org/traccar/Config.java')
-rw-r--r--src/org/traccar/Config.java48
1 files changed, 29 insertions, 19 deletions
diff --git a/src/org/traccar/Config.java b/src/org/traccar/Config.java
index e9b17a33b..c52c1bd81 100644
--- a/src/org/traccar/Config.java
+++ b/src/org/traccar/Config.java
@@ -18,44 +18,50 @@ package org.traccar;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.util.Enumeration;
import java.util.Properties;
public class Config {
private final Properties properties = new Properties();
- 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)) {
- Properties defaultProperties = new Properties();
- defaultProperties.loadFromXML(inputStream);
-
- Enumeration props = defaultProperties.propertyNames();
- while (props.hasMoreElements()) {
- String key = (String) props.nextElement();
- if (!properties.containsKey(key)) {
- properties.setProperty(key, defaultProperties.getProperty(key));
- }
- }
+ 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) {
+ if (useEnvVars && System.getenv().containsKey(getEnvVarName(key))) {
+ return true;
+ }
return properties.containsKey(key);
}
public String getString(String key) {
- String envName = key.toUpperCase().replaceAll("\\.", "_");
- String envValue = System.getenv(envName);
- if (envValue != null && !envValue.isEmpty()) {
- return envValue;
+ if (useEnvVars) {
+ String envValue = System.getenv(getEnvVarName(key));
+ if (envValue != null && !envValue.isEmpty()) {
+ return envValue;
+ }
}
return properties.getProperty(key);
}
@@ -92,4 +98,8 @@ public class Config {
return hasKey(key) ? Double.parseDouble(getString(key)) : defaultValue;
}
+ public static String getEnvVarName(String key) {
+ return key.replaceAll("\\.", "_").replaceAll("(.)(\\p{Lu})", "$1_$2").toUpperCase();
+ }
+
}