Commit 4b6e61cd authored by LAVENIER's avatar LAVENIER
Browse files

[fix] Configuration: load spring option as nuiton defaults

parent 7b939b8d
...@@ -122,7 +122,7 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer { ...@@ -122,7 +122,7 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
public SumarisConfiguration(ConfigurableEnvironment env, public SumarisConfiguration(ConfigurableEnvironment env,
String... args) { String... args) {
this(env, "application.yml", args); this(env, "application.properties", args);
} }
public SumarisConfiguration(String file, public SumarisConfiguration(String file,
...@@ -141,23 +141,23 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer { ...@@ -141,23 +141,23 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
String file, String file,
String... args) { String... args) {
this.applicationConfig = new ApplicationConfig( ApplicationConfigInit configInit = ApplicationConfigInit.forScopes(ApplicationConfigScope.DEFAULTS,
ApplicationConfigInit.forScopes(ApplicationConfigScope.DEFAULTS, ApplicationConfigScope.CLASS_PATH,
ApplicationConfigScope.CLASS_PATH, ApplicationConfigScope.ENV,
ApplicationConfigScope.ENV, ApplicationConfigScope.JVM,
ApplicationConfigScope.JVM, ApplicationConfigScope.OPTIONS);
ApplicationConfigScope.OPTIONS));
this.applicationConfig.setEncoding(Charsets.UTF_8.name());
this.applicationConfig.setConfigFileName(file);
// Set options from env // Set options from env
if (env != null) { if (env != null) {
Properties options = ConfigurableEnvironments.readProperties(env); Properties defaults = ConfigurableEnvironments.readProperties(env, null);
options.stringPropertyNames().forEach(key -> { configInit.setDefaults(defaults);
applicationConfig.setOption(key, options.getProperty(key));
});
} }
this.applicationConfig = new ApplicationConfig(configInit);
this.applicationConfig.setEncoding(Charsets.UTF_8.name());
this.applicationConfig.setConfigFileName(file);
if (log.isDebugEnabled()) log.debug("Application options: {}", applicationConfig.getFlatOptions());
// load all default options // load all default options
Set<ApplicationConfigProvider> providers = getProviders(); Set<ApplicationConfigProvider> providers = getProviders();
...@@ -188,8 +188,6 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer { ...@@ -188,8 +188,6 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
throw new SumarisTechnicalException(t("sumaris.config.parse.error"), e); throw new SumarisTechnicalException(t("sumaris.config.parse.error"), e);
} }
// Init the application version // Init the application version
initVersion(applicationConfig); initVersion(applicationConfig);
......
package net.sumaris.core.util.env; package net.sumaris.core.util.env;
import lombok.NonNull; import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
...@@ -8,23 +9,26 @@ import java.util.List; ...@@ -8,23 +9,26 @@ import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Slf4j
public class ConfigurableEnvironments { public class ConfigurableEnvironments {
protected ConfigurableEnvironments() { protected ConfigurableEnvironments() {
// Helper class // Helper class
} }
public static Properties readProperties(@NonNull ConfigurableEnvironment env) { public static Properties readProperties(@NonNull ConfigurableEnvironment env, Properties defaultOptions) {
List<MapPropertySource> sources = env.getPropertySources().stream() List<MapPropertySource> sources = env.getPropertySources().stream()
.filter(source -> source instanceof MapPropertySource) .filter(source -> source instanceof MapPropertySource)
.map(source -> (MapPropertySource)source).collect(Collectors.toList()); .map(source -> (MapPropertySource)source).collect(Collectors.toList());
Properties target = null; Properties target = defaultOptions;
for (MapPropertySource source: sources) { for (MapPropertySource source: sources) {
// Cascade properties (keep original order) // Cascade properties (keep original order)
target = new Properties(target); target = new Properties(target);
for (String key: source.getPropertyNames()) { for (String key: source.getPropertyNames()) {
Object value = source.getProperty(key); Object value = source.getProperty(key);
if (value != null) { if (value != null) {
//log.info(" {}={}", key, value.toString());
target.setProperty(key, value.toString()); target.setProperty(key, value.toString());
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment