aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/traccar/Config.java48
-rw-r--r--test/org/traccar/ConfigTest.java19
-rw-r--r--test/org/traccar/config/EnvVarTest.java18
3 files changed, 40 insertions, 45 deletions
diff --git a/src/org/traccar/Config.java b/src/org/traccar/Config.java
index c52c1bd81..0bc3cafaa 100644
--- a/src/org/traccar/Config.java
+++ b/src/org/traccar/Config.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org)
+ * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,43 +24,37 @@ public class Config {
private final Properties properties = new Properties();
- private boolean useEnvVars = false;
+ private boolean useEnvironmentVariables;
void load(String file) throws IOException {
- // First we load default config (if any)
- String defaultConfigFile = properties.getProperty("config.default");
+ Properties mainProperties = new Properties();
+ try (InputStream inputStream = new FileInputStream(file)) {
+ mainProperties.loadFromXML(inputStream);
+ }
+
+ String defaultConfigFile = mainProperties.getProperty("config.default");
if (defaultConfigFile != null) {
try (InputStream inputStream = new FileInputStream(defaultConfigFile)) {
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");
- }
- }
+ properties.putAll(mainProperties); // override defaults
+
+ useEnvironmentVariables = Boolean.parseBoolean(System.getenv("CONFIG_USE_ENVIRONMENT_VARIABLES"))
+ || Boolean.parseBoolean(properties.getProperty("config.useEnvironmentVariables"));
+ }
public boolean hasKey(String key) {
- if (useEnvVars && System.getenv().containsKey(getEnvVarName(key))) {
- return true;
- }
- return properties.containsKey(key);
+ return useEnvironmentVariables && System.getenv().containsKey(getEnvironmentVariableName(key))
+ || properties.containsKey(key);
}
public String getString(String key) {
- if (useEnvVars) {
- String envValue = System.getenv(getEnvVarName(key));
- if (envValue != null && !envValue.isEmpty()) {
- return envValue;
+ if (useEnvironmentVariables) {
+ String value = System.getenv(getEnvironmentVariableName(key));
+ if (value != null && !value.isEmpty()) {
+ return value;
}
}
return properties.getProperty(key);
@@ -98,8 +92,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();
+ public static String getEnvironmentVariableName(String key) {
+ return key.replaceAll("\\.", "_").replaceAll("(\\p{Lu})", "_$1").toUpperCase();
}
}
diff --git a/test/org/traccar/ConfigTest.java b/test/org/traccar/ConfigTest.java
new file mode 100644
index 000000000..fa404b827
--- /dev/null
+++ b/test/org/traccar/ConfigTest.java
@@ -0,0 +1,19 @@
+package org.traccar;
+
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import org.traccar.Config;
+
+public class ConfigTest {
+
+ @Test
+ public void testFormat() {
+ assertEquals("DATABASE_URL", Config.getEnvironmentVariableName("database.url"));
+ assertEquals("DATABASE_CHECK_CONNECTION", Config.getEnvironmentVariableName("database.checkConnection"));
+ assertEquals("DATABASE_MAX_POOL_SIZE", Config.getEnvironmentVariableName("database.maxPoolSize"));
+ assertEquals("DEVICE_MANAGER_LOOKUP_GROUPS_ATTRIBUTE", Config.getEnvironmentVariableName("deviceManager.lookupGroupsAttribute"));
+ assertEquals("COMMAND_FALLBACK_TO_SMS", Config.getEnvironmentVariableName("command.fallbackToSms"));
+ assertEquals("STATUS_TIMEOUT", Config.getEnvironmentVariableName("status.timeout"));
+ }
+
+}
diff --git a/test/org/traccar/config/EnvVarTest.java b/test/org/traccar/config/EnvVarTest.java
deleted file mode 100644
index b90fd485c..000000000
--- a/test/org/traccar/config/EnvVarTest.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package org.traccar.config;
-
-import org.junit.Test;
-import static org.junit.Assert.assertEquals;
-import org.traccar.Config;
-
-public class EnvVarTest {
-
- @Test
- public void testFormat() {
- assertEquals("DATABASE_URL", Config.getEnvVarName("database.url"));
- assertEquals("DATABASE_CHECK_CONNECTION", Config.getEnvVarName("database.checkConnection"));
- assertEquals("DATABASE_MAX_POOL_SIZE", Config.getEnvVarName("database.maxPoolSize"));
- assertEquals("DEVICE_MANAGER_LOOKUP_GROUPS_ATTRIBUTE", Config.getEnvVarName("deviceManager.lookupGroupsAttribute"));
- assertEquals("COMMAND_FALLBACK_TO_SMS", Config.getEnvVarName("command.fallbackToSms"));
- assertEquals("STATUS_TIMEOUT", Config.getEnvVarName("status.timeout"));
- }
-}