77import java .io .FileInputStream ;
88import java .io .FileOutputStream ;
99import java .io .IOException ;
10- import java .util .Arrays ;
11- import java .util .List ;
1210import java .util .Properties ;
1311import java .util .concurrent .Executor ;
1412
15- import org .apache .commons .collections4 .ListUtils ;
16- import org .apache .commons .collections4 .Predicate ;
1713import org .apache .commons .lang3 .StringUtils ;
1814import org .slf4j .LoggerFactory ;
1915import org .springframework .aop .interceptor .AsyncUncaughtExceptionHandler ;
2016import org .springframework .aop .interceptor .SimpleAsyncUncaughtExceptionHandler ;
21- import org .springframework .context .EnvironmentAware ;
2217import org .springframework .context .annotation .Bean ;
2318import org .springframework .context .annotation .Configuration ;
2419import org .springframework .context .support .PropertySourcesPlaceholderConfigurer ;
25- import org .springframework .core .env .Environment ;
2620import org .springframework .core .io .FileSystemResource ;
2721import org .springframework .jmx .export .annotation .AnnotationMBeanExporter ;
2822import org .springframework .scheduling .annotation .AsyncConfigurer ;
3125
3226@ Configuration
3327@ EnableAsync
34- public class AppConfig implements AsyncConfigurer , EnvironmentAware {
28+ public class AppConfig implements AsyncConfigurer {
3529
3630 private static final org .slf4j .Logger LOG = LoggerFactory .getLogger (AppConfig .class );
3731
3832 public static final String CONFIG_FILE = "application.properties" ;
3933
40- private Environment env ;
41-
42- @ Override
43- public void setEnvironment (Environment e ) {
44- this .env = e ;
45- }
46-
47-
4834 /**
49- * Return the configured environment name
35+ * Return the configured environment name (via spring.profiles.active system prop)
5036 *
51- * Search order:
52- * - ETH_ENV environment variable
53- * - eth.environment system property (-Deth.environment param)
54- * - Default to 'local' if none found
55- *
56- * @return Environment name
37+ * @return String Environment name
5738 */
58- public String getEnv () {
59- //String env = getProp("ETH_ENV", "eth.environment");
60- if (null == env ) {
61- // FIXME only default to local based on a flag passed down from maven build?
62- LOG .warn ("spring.profiles.active is not set" );
63- }
64- List <String > profiles = ListUtils .selectRejected (Arrays .asList (env .getActiveProfiles ()), new Predicate <String >() {
65- @ Override
66- public boolean evaluate (String profile ) {
67- return profile .equalsIgnoreCase ("container" ) || profile .equalsIgnoreCase ("spring-boot" );
68- }
69- });
70- return profiles .get (0 );
39+ public static String getEnv () {
40+ return System .getProperty ("spring.profiles.active" );
7141 }
7242
7343 /**
@@ -80,7 +50,7 @@ public boolean evaluate(String profile) {
8050 *
8151 * @return
8252 */
83- public String getConfigPath () {
53+ public static String getConfigPath () {
8454 String configPath = getProp ("ETH_CONFIG" , "eth.config.dir" );
8555 if (!StringUtils .isBlank (configPath )) {
8656 return FileUtils .expandPath (configPath , getEnv ());
@@ -108,20 +78,19 @@ private static String getProp(String env, String java) {
10878 }
10979
11080 @ Bean
111- // @Profile("container")
112- public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer () throws IOException {
113- return createPropConfigurer (env , getConfigPath ());
81+ public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer () throws IOException {
82+ return createPropConfigurer (getConfigPath ());
11483 }
11584
116- public String getVendorConfigFile () {
85+ public static String getVendorConfigFile () {
11786 return "config" .concat (File .separator ).concat ("application.properties" );
11887 }
11988
120- public String getVendorEnvConfigFile () {
89+ public static String getVendorEnvConfigFile () {
12190 return "config" .concat (File .separator ).concat ("application-" ).concat (getEnv ()).concat (".properties" );
12291 }
12392
124- public void initVendorConfig (File configFile ) throws IOException {
93+ public static void initVendorConfig (File configFile ) throws IOException {
12594 // copy default file
12695 String path = FileUtils .getClasspathPath (getVendorEnvConfigFile ()).toString ();
12796 LOG .info ("Initializing new config from " + path );
@@ -134,14 +103,13 @@ public void initVendorConfig(File configFile) throws IOException {
134103 SortedProperties .store (mergedProps , new FileOutputStream (configFile ));
135104 }
136105
137- public PropertySourcesPlaceholderConfigurer createPropConfigurer (Environment env ,
138- String configDir ) throws IOException {
106+ public static PropertySourcesPlaceholderConfigurer createPropConfigurer (String configDir )
107+ throws IOException {
139108
140- if (null == env ) {
141- throw new IOException ("ENV var 'spring.profiles.active' not set; unable to load config" );
109+ if (StringUtils . isBlank ( getEnv ()) ) {
110+ throw new IOException ("System property 'spring.profiles.active' not set; unable to load config" );
142111 }
143112
144- LOG .info ("eth.environment=" + env );
145113 LOG .info ("eth.config.dir=" + configDir );
146114
147115 File configPath = new File (configDir );
@@ -171,6 +139,9 @@ public PropertySourcesPlaceholderConfigurer createPropConfigurer(Environment en
171139 PropertySourcesPlaceholderConfigurer propConfig = new PropertySourcesPlaceholderConfigurer ();
172140 propConfig .setLocation (new FileSystemResource (configFile ));
173141 propConfig .setProperties (localProps );
142+ propConfig .setLocalOverride (true );
143+
144+ LOG .info ("Loading config from " + configFile .toString ());
174145
175146 return propConfig ;
176147 }
0 commit comments