Commit 049b94cb authored by Matija Obreza's avatar Matija Obreza
Browse files

Properties: Use PropertySourcesPlaceholderConfigurer

parent ef05ae0e
......@@ -20,12 +20,14 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
......@@ -36,30 +38,30 @@ import org.springframework.core.io.Resource;
@Import({ SchedulerConfig.class, DatabaseConfig.class, WebConfiguration.class, SwaggerConfig.class })
@EnableAspectJAutoProxy
public class ApplicationConfig {
final static Logger LOG = LoggerFactory.getLogger(ApplicationConfig.class);
@Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
propertyPlaceholderConfigurer.setIgnoreResourceNotFound(true);
propertyPlaceholderConfigurer.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
propertyPlaceholderConfigurer.setFileEncoding("utf-8");
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
pspc.setIgnoreResourceNotFound(true);
pspc.setFileEncoding("UTF-8");
pspc.setTrimValues(false); // this is also the default
final List<Resource> locations = new ArrayList<>();
locations.add(new ClassPathResource("application.properties"));
locations.add(new ClassPathResource("amphibian.properties"));
final String extraConfigFile = System.getenv("config.file");
final String extraConfigFile = StringUtils.defaultIfBlank(System.getenv("config.file"), "myconfig.properties");
if (StringUtils.isNotBlank(extraConfigFile)) {
final File f = new File(extraConfigFile);
if (f.exists() && f.canRead()) {
System.err.println("Including properties from config.file=" + extraConfigFile);
if (f.exists() && f.canRead() && f.isFile()) {
LOG.warn("Including extra properties from {}", extraConfigFile);
locations.add(new FileSystemResource(extraConfigFile));
} else {
System.err.println("Cannot read properties from config.file=" + extraConfigFile);
LOG.warn("Extra configuration file {} not found", extraConfigFile);
}
}
propertyPlaceholderConfigurer.setLocations(locations.toArray(new Resource[] {}));
return propertyPlaceholderConfigurer;
pspc.setLocations(locations.toArray(new Resource[] {}));
return pspc;
}
}
......@@ -19,6 +19,8 @@ package org.genesys.amphibian.config;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -38,6 +40,8 @@ import com.mongodb.ServerAddress;
@EnableMongoAuditing(modifyOnCreate = true)
public class DatabaseConfig extends AbstractMongoConfiguration {
final static Logger LOG = LoggerFactory.getLogger(ApplicationConfig.class);
@Value("${mongo.hosts}")
private String[] dbHosts;
......@@ -69,9 +73,10 @@ public class DatabaseConfig extends AbstractMongoConfiguration {
MongoClientOptions options = new MongoClientOptions.Builder().build();
List<ServerAddress> servers = new ArrayList<>();
for (String dbHost: dbHosts) {
LOG.warn("Adding mongo at {}:{}", dbHost, dbPort);
servers.add(new ServerAddress(dbHost, dbPort));
}
MongoClient mongoClient = new MongoClient(servers, credential, options);
return mongoClient;
}
}
\ No newline at end of file
}
Supports Markdown
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