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 {
public SumarisConfiguration(ConfigurableEnvironment env,
String... args) {
this(env, "application.yml", args);
this(env, "application.properties", args);
}
public SumarisConfiguration(String file,
......@@ -141,23 +141,23 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
String file,
String... args) {
this.applicationConfig = new ApplicationConfig(
ApplicationConfigInit.forScopes(ApplicationConfigScope.DEFAULTS,
ApplicationConfigScope.CLASS_PATH,
ApplicationConfigScope.ENV,
ApplicationConfigScope.JVM,
ApplicationConfigScope.OPTIONS));
this.applicationConfig.setEncoding(Charsets.UTF_8.name());
this.applicationConfig.setConfigFileName(file);
ApplicationConfigInit configInit = ApplicationConfigInit.forScopes(ApplicationConfigScope.DEFAULTS,
ApplicationConfigScope.CLASS_PATH,
ApplicationConfigScope.ENV,
ApplicationConfigScope.JVM,
ApplicationConfigScope.OPTIONS);
// Set options from env
if (env != null) {
Properties options = ConfigurableEnvironments.readProperties(env);
options.stringPropertyNames().forEach(key -> {
applicationConfig.setOption(key, options.getProperty(key));
});
Properties defaults = ConfigurableEnvironments.readProperties(env, null);
configInit.setDefaults(defaults);
}
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
Set<ApplicationConfigProvider> providers = getProviders();
......@@ -188,8 +188,6 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
throw new SumarisTechnicalException(t("sumaris.config.parse.error"), e);
}
// Init the application version
initVersion(applicationConfig);
......
package net.sumaris.core.util.env;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
......@@ -8,23 +9,26 @@ import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
@Slf4j
public class ConfigurableEnvironments {
protected ConfigurableEnvironments() {
// Helper class
}
public static Properties readProperties(@NonNull ConfigurableEnvironment env) {
public static Properties readProperties(@NonNull ConfigurableEnvironment env, Properties defaultOptions) {
List<MapPropertySource> sources = env.getPropertySources().stream()
.filter(source -> source instanceof MapPropertySource)
.map(source -> (MapPropertySource)source).collect(Collectors.toList());
Properties target = null;
Properties target = defaultOptions;
for (MapPropertySource source: sources) {
// Cascade properties (keep original order)
target = new Properties(target);
for (String key: source.getPropertyNames()) {
Object value = source.getProperty(key);
if (value != null) {
//log.info(" {}={}", 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