|
1 | 1 | /*
|
2 |
| - * Copyright 2013-2014 the original author or authors. |
| 2 | + * Copyright 2013-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | import java.util.Collections;
|
22 | 22 | import java.util.List;
|
23 | 23 | import java.util.Map;
|
| 24 | +import java.util.Set; |
| 25 | +import java.util.TreeSet; |
24 | 26 |
|
25 | 27 | import org.apache.commons.logging.Log;
|
26 | 28 | import org.apache.commons.logging.LogFactory;
|
27 | 29 | import org.springframework.beans.factory.annotation.Autowired;
|
28 | 30 | import org.springframework.boot.bind.PropertySourcesPropertyValues;
|
29 | 31 | import org.springframework.boot.bind.RelaxedDataBinder;
|
30 | 32 | import org.springframework.boot.bind.RelaxedPropertyResolver;
|
| 33 | +import org.springframework.boot.context.config.ConfigFileApplicationListener; |
31 | 34 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
32 | 35 | import org.springframework.boot.logging.LogFile;
|
33 | 36 | import org.springframework.boot.logging.LoggingInitializationContext;
|
|
46 | 49 | import org.springframework.core.env.PropertySource;
|
47 | 50 | import org.springframework.core.env.StandardEnvironment;
|
48 | 51 | import org.springframework.util.ResourceUtils;
|
| 52 | +import org.springframework.util.StringUtils; |
49 | 53 |
|
50 | 54 | /**
|
51 | 55 | * @author Dave Syer
|
@@ -104,6 +108,7 @@ public void initialize(ConfigurableApplicationContext applicationContext) {
|
104 | 108 | insertPropertySources(propertySources, composite);
|
105 | 109 | reinitializeLoggingSystem(environment, logConfig, logFile);
|
106 | 110 | setLogLevels(environment);
|
| 111 | + handleIncludedProfiles(environment); |
107 | 112 | }
|
108 | 113 | }
|
109 | 114 |
|
@@ -177,4 +182,45 @@ private void insertPropertySources(MutablePropertySources propertySources,
|
177 | 182 | }
|
178 | 183 | }
|
179 | 184 |
|
| 185 | + private void handleIncludedProfiles(ConfigurableEnvironment environment) { |
| 186 | + Set<String> includeProfiles = new TreeSet<>(); |
| 187 | + for (PropertySource<?> propertySource : environment.getPropertySources()) { |
| 188 | + addIncludedProfilesTo(includeProfiles, propertySource); |
| 189 | + } |
| 190 | + List<String> activeProfiles = new ArrayList<>(); |
| 191 | + Collections.addAll(activeProfiles, environment.getActiveProfiles()); |
| 192 | + |
| 193 | + // If it's already accepted we assume the order was set intentionally |
| 194 | + includeProfiles.removeAll(activeProfiles); |
| 195 | + if (includeProfiles.isEmpty()) { |
| 196 | + return; |
| 197 | + } |
| 198 | + // Prepend each added profile (last wins in a property key clash) |
| 199 | + for (String profile : includeProfiles) { |
| 200 | + activeProfiles.add(0, profile); |
| 201 | + } |
| 202 | + environment.setActiveProfiles( |
| 203 | + activeProfiles.toArray(new String[activeProfiles.size()])); |
| 204 | + } |
| 205 | + |
| 206 | + private Set<String> addIncludedProfilesTo(Set<String> profiles, |
| 207 | + PropertySource<?> propertySource) { |
| 208 | + if (propertySource instanceof CompositePropertySource) { |
| 209 | + for (PropertySource<?> nestedPropertySource : ((CompositePropertySource) propertySource) |
| 210 | + .getPropertySources()) { |
| 211 | + addIncludedProfilesTo(profiles, nestedPropertySource); |
| 212 | + } |
| 213 | + } |
| 214 | + else { |
| 215 | + Collections.addAll(profiles, getProfilesForValue(propertySource.getProperty( |
| 216 | + ConfigFileApplicationListener.INCLUDE_PROFILES_PROPERTY))); |
| 217 | + } |
| 218 | + return profiles; |
| 219 | + } |
| 220 | + |
| 221 | + private String[] getProfilesForValue(Object property) { |
| 222 | + final String value = (property == null ? null : property.toString()); |
| 223 | + return StringUtils.commaDelimitedListToStringArray(value); |
| 224 | + } |
| 225 | + |
180 | 226 | }
|
0 commit comments