aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xsetup/docker/build.sh7
-rw-r--r--src/org/traccar/Config.java67
-rw-r--r--test/org/traccar/config/EnvVarTest.java18
3 files changed, 57 insertions, 35 deletions
diff --git a/setup/docker/build.sh b/setup/docker/build.sh
index eedc633bc..0b292fc5f 100755
--- a/setup/docker/build.sh
+++ b/setup/docker/build.sh
@@ -5,9 +5,10 @@ which awk &> /dev/null || { echo >&2 "Awk package cant be found on path. Aborti
which docker &> /dev/null || { echo >&2 "Docker package cant be found on path. Aborting."; exit 1; }
mvn package || { echo >&2 "Maven package has failed. Aborting."; exit 1; }
-export company="tananaev"
-export software="traccar"
-export version=$(head -n 10 ./pom.xml |grep version|cut -d ">" -f2|cut -d"<" -f1)
+export company=${1:-"tananaev"}
+export software=${2:-"traccar"}
+export _version=$(head -n 10 ./pom.xml |grep version|cut -d ">" -f2|cut -d"<" -f1)
+export version=${3:-_version}
tmp="./setup/docker/tmp"
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();
}
}
diff --git a/test/org/traccar/config/EnvVarTest.java b/test/org/traccar/config/EnvVarTest.java
new file mode 100644
index 000000000..b90fd485c
--- /dev/null
+++ b/test/org/traccar/config/EnvVarTest.java
@@ -0,0 +1,18 @@
+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"));
+ }
+}