Skip to content

Commit 941275d

Browse files
mbensonspencergibb
authored andcommitted
consider spring.profiles.include values from bootstrap PropertySources (spring-projects#123)
1 parent 625b906 commit 941275d

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,13 +21,16 @@
2121
import java.util.Collections;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.Set;
25+
import java.util.TreeSet;
2426

2527
import org.apache.commons.logging.Log;
2628
import org.apache.commons.logging.LogFactory;
2729
import org.springframework.beans.factory.annotation.Autowired;
2830
import org.springframework.boot.bind.PropertySourcesPropertyValues;
2931
import org.springframework.boot.bind.RelaxedDataBinder;
3032
import org.springframework.boot.bind.RelaxedPropertyResolver;
33+
import org.springframework.boot.context.config.ConfigFileApplicationListener;
3134
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3235
import org.springframework.boot.logging.LogFile;
3336
import org.springframework.boot.logging.LoggingInitializationContext;
@@ -46,6 +49,7 @@
4649
import org.springframework.core.env.PropertySource;
4750
import org.springframework.core.env.StandardEnvironment;
4851
import org.springframework.util.ResourceUtils;
52+
import org.springframework.util.StringUtils;
4953

5054
/**
5155
* @author Dave Syer
@@ -104,6 +108,7 @@ public void initialize(ConfigurableApplicationContext applicationContext) {
104108
insertPropertySources(propertySources, composite);
105109
reinitializeLoggingSystem(environment, logConfig, logFile);
106110
setLogLevels(environment);
111+
handleIncludedProfiles(environment);
107112
}
108113
}
109114

@@ -177,4 +182,45 @@ private void insertPropertySources(MutablePropertySources propertySources,
177182
}
178183
}
179184

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+
180226
}

spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -315,6 +315,15 @@ public void differentProfileInChild() {
315315
assertEquals("parent", this.context.getEnvironment().getProperty("info.name"));
316316
}
317317

318+
@Test
319+
public void includeProfileFromBootstrapPropertySource() {
320+
PropertySourceConfiguration.MAP.put("spring.profiles.include", "bar,baz");
321+
this.context = new SpringApplicationBuilder().web(false).profiles("foo")
322+
.sources(BareConfiguration.class).run();
323+
assertTrue(this.context.getEnvironment().acceptsProfiles("baz"));
324+
assertTrue(this.context.getEnvironment().acceptsProfiles("bar"));
325+
}
326+
318327
@Configuration
319328
@EnableConfigurationProperties
320329
protected static class BareConfiguration {

0 commit comments

Comments
 (0)