From e1ed9217f32fd26493043bfd038072f1cb79e53e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 3 May 2017 20:06:00 +1200 Subject: Fix config issue and clean up (fix #3136) --- src/org/traccar/Config.java | 48 ++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) (limited to 'src/org') 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 file - 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(); } } -- cgit v1.2.3