Commit 7b939b8d authored by LAVENIER's avatar LAVENIER
Browse files

[enh] ALlow to use Spring config env, to pass to nuiton applicationConfig

parent 079e1d42
......@@ -27,18 +27,19 @@ package net.sumaris.core.config;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import net.sumaris.core.dao.technical.Daos;
import net.sumaris.core.exception.SumarisTechnicalException;
import net.sumaris.core.util.env.ConfigurableEnvironments;
import org.apache.commons.lang3.StringUtils;
import org.nuiton.config.ApplicationConfig;
import org.nuiton.config.ApplicationConfigHelper;
import org.nuiton.config.ApplicationConfigProvider;
import org.nuiton.config.ArgumentsParserException;
import org.nuiton.config.*;
import org.nuiton.version.Version;
import org.nuiton.version.VersionBuilder;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.env.ConfigurableEnvironment;
import java.io.File;
import java.io.IOException;
......@@ -95,11 +96,13 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
private File configFile;
protected final Set<String> transientOptionKeys;
/**
* <p>initDefault.</p>
*/
public static void initDefault(String configFile) {
instance = new SumarisConfiguration(configFile, args);
public static void initDefault(@NonNull ConfigurableEnvironment env) {
instance = new SumarisConfiguration(env, args);
setInstance(instance);
}
......@@ -111,34 +114,57 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
public SumarisConfiguration(ApplicationConfig applicationConfig) {
super();
this.applicationConfig = applicationConfig;
this.transientOptionKeys = null;
// Override application version
initVersion(applicationConfig);
}
public SumarisConfiguration(ConfigurableEnvironment env,
String... args) {
this(env, "application.yml", args);
}
public SumarisConfiguration(String file,
String... args) {
this(null, file, args);
}
/**
* <p>Constructor for SumarisConfiguration.</p>
*
* @param env a {@link ConfigurableEnvironment} object.
* @param file a {@link String} object.
* @param args a {@link String} object.
*/
public SumarisConfiguration(String file, String... args) {
super();
protected SumarisConfiguration(ConfigurableEnvironment env,
String file,
String... args) {
this.applicationConfig = new ApplicationConfig();
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);
// Set options from env
if (env != null) {
Properties options = ConfigurableEnvironments.readProperties(env);
options.stringPropertyNames().forEach(key -> {
applicationConfig.setOption(key, options.getProperty(key));
});
}
System.setProperty("logging.level.Hibernate Types", "error");
// load all default options
Set<ApplicationConfigProvider> providers = getProviders();
// find all config providers
Set<ApplicationConfigProvider> providers =
ApplicationConfigHelper.getProviders(null,
null,
null,
true);
// Load transient options keys
this.transientOptionKeys = ImmutableSet.copyOf(ApplicationConfigHelper.getTransientOptionKeys(providers));
// load all default options
ApplicationConfigHelper.loadAllDefaultOption(applicationConfig,
providers);
System.setProperty("logging.level.Hibernate Types", "error");
// Load actions
for (ApplicationConfigProvider provider : providers) {
......@@ -151,6 +177,9 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
// Override some external module default config (sumaris)
overrideExternalModulesDefaultOptions(applicationConfig);
ApplicationConfigHelper.loadAllDefaultOption(applicationConfig, providers);
// parse config file and inline arguments
try {
applicationConfig.parse(args);
......@@ -159,35 +188,16 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
throw new SumarisTechnicalException(t("sumaris.config.parse.error"), e);
}
// Init the application version
initVersion(applicationConfig);
// Init time zone
initTimeZone();
initTimeZone(applicationConfig);
// TODO Review this, this is very dirty to do this...
File appBasedir = applicationConfig.getOptionAsFile(
SumarisConfigurationOption.BASEDIR.getKey());
if (appBasedir == null) {
appBasedir = new File("");
}
if (!appBasedir.isAbsolute()) {
appBasedir = new File(appBasedir.getAbsolutePath());
}
if (appBasedir.getName().equals("..")) {
appBasedir = appBasedir.getParentFile().getParentFile();
}
if (appBasedir.getName().equals(".")) {
appBasedir = appBasedir.getParentFile();
}
if (log.isInfoEnabled()) {
String appName = applicationConfig.getOption(SumarisConfigurationOption.APP_NAME.getKey());
log.info(String.format("Starting {%s} on basedir {%s}", appName, appBasedir));
}
applicationConfig.setOption(
SumarisConfigurationOption.BASEDIR.getKey(),
appBasedir.getAbsolutePath());
// Prepare basedir
fixBasedir(applicationConfig);
if (log.isDebugEnabled())
log.debug(applicationConfig.getPrintableConfig(null, 4));
......@@ -225,10 +235,18 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
}
protected static Set<ApplicationConfigProvider> getProviders() {
// get allOfToList config providers
return ApplicationConfigHelper.getProviders(null,
null,
null,
true);
}
/**
* Initialization default timezone, from configuration (mantis #24623)
*/
protected void initTimeZone() {
protected void initTimeZone(ApplicationConfig applicationConfig) {
String dbTimeZone = applicationConfig.getOption(SumarisConfigurationOption.DB_TIMEZONE.getKey());
if (StringUtils.isNotBlank(dbTimeZone)) {
......@@ -296,6 +314,32 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
}
protected void fixBasedir(ApplicationConfig applicationConfig) {
// TODO Review this, this is very dirty to do this...
File appBasedir = applicationConfig.getOptionAsFile(
SumarisConfigurationOption.BASEDIR.getKey());
if (appBasedir == null) {
appBasedir = new File("");
}
if (!appBasedir.isAbsolute()) {
appBasedir = new File(appBasedir.getAbsolutePath());
}
if (appBasedir.getName().equals("..")) {
appBasedir = appBasedir.getParentFile().getParentFile();
}
if (appBasedir.getName().equals(".")) {
appBasedir = appBasedir.getParentFile();
}
if (log.isInfoEnabled()) {
String appName = applicationConfig.getOption(SumarisConfigurationOption.APP_NAME.getKey());
log.info(String.format("Starting {%s} on basedir {%s}", appName, appBasedir));
}
applicationConfig.setOption(
SumarisConfigurationOption.BASEDIR.getKey(),
appBasedir.getAbsolutePath());
}
/**
......@@ -314,6 +358,10 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
return configFile;
}
public Set<String> getTransientOptionKeys() {
return transientOptionKeys;
}
/**
* <p>getBasedir.</p>
*
......
package net.sumaris.core.util.env;
import lombok.NonNull;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
public class ConfigurableEnvironments {
protected ConfigurableEnvironments() {
// Helper class
}
public static Properties readProperties(@NonNull ConfigurableEnvironment env) {
List<MapPropertySource> sources = env.getPropertySources().stream()
.filter(source -> source instanceof MapPropertySource)
.map(source -> (MapPropertySource)source).collect(Collectors.toList());
Properties target = null;
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) {
target.setProperty(key, value.toString());
}
}
}
return target;
}
}
......@@ -44,7 +44,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
......@@ -140,11 +140,11 @@ public class Application {
}
@Bean
public SumarisConfiguration configuration() {
public SumarisConfiguration configuration(ConfigurableEnvironment env) {
SumarisConfiguration config = SumarisConfiguration.getInstance();
if (config == null) {
SumarisConfiguration.initDefault(CONFIG_FILE);
SumarisConfiguration.initDefault(env);
config = SumarisConfiguration.getInstance();
}
......
......@@ -31,7 +31,6 @@ import net.sumaris.core.util.ApplicationUtils;
import net.sumaris.core.util.I18nUtil;
import net.sumaris.server.config.SumarisServerConfiguration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.nuiton.i18n.I18n;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......@@ -42,12 +41,10 @@ import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfigurati
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.File;
import java.io.IOException;
......@@ -77,18 +74,14 @@ import java.io.IOException;
@Slf4j
public class Application extends SpringBootServletInitializer {
public static final String CONFIG_FILE_NAME = "application.properties";
private static final String CONFIG_FILE_ENV_PROPERTY = "spring.config.location";
private static final String CONFIG_FILE_JNDI_NAME = "java:comp/env/" + CONFIG_FILE_NAME;
public static void main(String[] args) {
SumarisServerConfiguration.setArgs(ApplicationUtils.toApplicationConfigArgs(args));
SpringApplication.run(Application.class, args);
}
@Bean
public static SumarisServerConfiguration configuration() {
SumarisServerConfiguration.initDefault(getConfigFile());
public static SumarisServerConfiguration configuration(ConfigurableEnvironment env) {
SumarisServerConfiguration.initDefault(env);
SumarisServerConfiguration config = SumarisServerConfiguration.getInstance();
// Init I18n
......@@ -113,34 +106,6 @@ public class Application extends SpringBootServletInitializer {
/* -- Internal method -- */
/**
* <p>getWebConfigFile.</p>
*
* @return a {@link String} object.
*/
protected static String getConfigFile() {
// Could override config file id (useful for dev)
String configFile = CONFIG_FILE_NAME;
if (System.getProperty(CONFIG_FILE_ENV_PROPERTY) != null) {
configFile = System.getProperty(CONFIG_FILE_ENV_PROPERTY);
configFile = configFile.replaceAll("\\\\", "/");
}
else {
try {
InitialContext ic = new InitialContext();
String jndiPathToConfFile = (String) ic.lookup(CONFIG_FILE_JNDI_NAME);
if (StringUtils.isNotBlank(jndiPathToConfFile)) {
configFile = jndiPathToConfFile;
}
} catch (NamingException e) {
log.debug(String.format("Error while reading JNDI initial context. Skip configuration path override, from context [%s]", CONFIG_FILE_JNDI_NAME));
}
}
return configFile;
}
/**
* <p>initDirectories.</p>
*/
......
......@@ -32,6 +32,7 @@ import org.apache.commons.lang3.StringUtils;
import org.nuiton.config.ApplicationConfig;
import org.nuiton.version.VersionBuilder;
import org.nuiton.version.Version;
import org.springframework.core.env.ConfigurableEnvironment;
import java.io.File;
import java.util.TimeZone;
......@@ -48,8 +49,8 @@ public class SumarisServerConfiguration extends SumarisConfiguration {
/**
* <p>initDefault.</p>
*/
public static void initDefault(String configFileName) {
instance = new SumarisServerConfiguration(configFileName, args);
public static void initDefault(ConfigurableEnvironment env) {
instance = new SumarisServerConfiguration(env, args);
setInstance(instance);
}
......@@ -81,6 +82,16 @@ public class SumarisServerConfiguration extends SumarisConfiguration {
super(file, args);
}
/**
* <p>Constructor for SumarisServerConfiguration.</p>
*
* @param env a {@link ConfigurableEnvironment} object.
* @param args a {@link String} object.
*/
public SumarisServerConfiguration(ConfigurableEnvironment env, String... args) {
super(env, args);
}
/** {@inheritDoc} */
@Override
protected void overrideExternalModulesDefaultOptions(ApplicationConfig applicationConfig) {
......@@ -278,7 +289,7 @@ public class SumarisServerConfiguration extends SumarisConfiguration {
* Initialization default timezone, from configuration (mantis #34754)
*/
@Override
protected void initTimeZone() {
protected void initTimeZone(ApplicationConfig applicationConfig) {
String timeZone = applicationConfig.getOption(SumarisConfigurationOption.TIMEZONE.getKey());
if (StringUtils.isNotBlank(timeZone)) {
......
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