aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2017-05-03 19:52:20 +1200
committerGitHub <noreply@github.com>2017-05-03 19:52:20 +1200
commitc22fd3e9e786575a912684f95b41e413f9d96292 (patch)
treefa5b6a248ece0abdf2774902f7c183ef35a67edd /src
parent1bf2353bafe3481f1deeb9e73e9bcaa1a40f0103 (diff)
parentfa6548a7b613b5bd3919cbf0269d58b2d8ebb999 (diff)
downloadtrackermap-server-c22fd3e9e786575a912684f95b41e413f9d96292.tar.gz
trackermap-server-c22fd3e9e786575a912684f95b41e413f9d96292.tar.bz2
trackermap-server-c22fd3e9e786575a912684f95b41e413f9d96292.zip
Merge pull request #3136 from sillaps/master
Environment variables
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/Config.java67
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();
}
}