Skip to content

Commit 26dae55

Browse files
committed
Adds support for non-enumerable property sources in bootstrap.
Fixes gh-724
1 parent a10fbb9 commit 26dae55

File tree

5 files changed

+85
-10
lines changed

5 files changed

+85
-10
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
import static org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME;
2828

2929
/**
30+
* Enumerable wrapper for a property source.
31+
*
3032
* @author Ryan Baxter
3133
*/
3234
public class BootstrapPropertySource<T> extends EnumerablePropertySource<T> {
3335

34-
private PropertySource<T> delegate;
36+
private EnumerablePropertySource<T> delegate;
3537

36-
public BootstrapPropertySource(PropertySource<T> delegate) {
38+
public BootstrapPropertySource(EnumerablePropertySource<T> delegate) {
3739
super(BOOTSTRAP_PROPERTY_SOURCE_NAME + "-" + delegate.getName(),
3840
delegate.getSource());
3941
this.delegate = delegate;
@@ -47,13 +49,7 @@ public Object getProperty(String name) {
4749
@Override
4850
public String[] getPropertyNames() {
4951
Set<String> names = new LinkedHashSet<>();
50-
if (!(this.delegate instanceof EnumerablePropertySource)) {
51-
throw new IllegalStateException(
52-
"Failed to enumerate property names due to non-enumerable property source: "
53-
+ this.delegate);
54-
}
55-
names.addAll(Arrays.asList(
56-
((EnumerablePropertySource<?>) this.delegate).getPropertyNames()));
52+
names.addAll(Arrays.asList(this.delegate.getPropertyNames()));
5753

5854
return StringUtils.toStringArray(names);
5955
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
4646
import org.springframework.core.env.CompositePropertySource;
4747
import org.springframework.core.env.ConfigurableEnvironment;
48+
import org.springframework.core.env.EnumerablePropertySource;
4849
import org.springframework.core.env.Environment;
4950
import org.springframework.core.env.MutablePropertySources;
5051
import org.springframework.core.env.PropertySource;
@@ -100,7 +101,13 @@ public void initialize(ConfigurableApplicationContext applicationContext) {
100101
}
101102
List<PropertySource<?>> sourceList = new ArrayList<>();
102103
for (PropertySource<?> p : source) {
103-
sourceList.add(new BootstrapPropertySource<>(p));
104+
if (p instanceof EnumerablePropertySource) {
105+
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) p;
106+
sourceList.add(new BootstrapPropertySource<>(enumerable));
107+
}
108+
else {
109+
sourceList.add(new SimpleBootstrapPropertySource(p));
110+
}
104111
}
105112
logger.info("Located property source: " + sourceList);
106113
composite.addAll(sourceList);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.bootstrap.config;
18+
19+
import org.springframework.core.env.PropertySource;
20+
21+
import static org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME;
22+
23+
/**
24+
* Simple, non-enumerable PropertySource wrapper.
25+
* @author Ryan Baxter
26+
*/
27+
public class SimpleBootstrapPropertySource<T> extends PropertySource<T> {
28+
29+
private PropertySource<T> delegate;
30+
31+
public SimpleBootstrapPropertySource(PropertySource<T> delegate) {
32+
super(BOOTSTRAP_PROPERTY_SOURCE_NAME + "-" + delegate.getName(),
33+
delegate.getSource());
34+
this.delegate = delegate;
35+
}
36+
37+
@Override
38+
public Object getProperty(String name) {
39+
return this.delegate.getProperty(name);
40+
}
41+
42+
public PropertySource<T> getDelegate() {
43+
return delegate;
44+
}
45+
46+
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,37 @@ public void includeProfileFromBootstrapProperties() {
416416
.isEqualTo("Hello added!");
417417
}
418418

419+
@Test
420+
public void nonEnumerablePropertySourceWorks() {
421+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
422+
.sources(BareConfiguration.class)
423+
.properties("spring.cloud.bootstrap.name=nonenumerable").run();
424+
then(this.context.getEnvironment().getProperty("foo")).isEqualTo("bar");
425+
}
426+
419427
@Configuration(proxyBeanMethods = false)
420428
@EnableConfigurationProperties
421429
protected static class BareConfiguration {
422430

423431
}
424432

433+
@Configuration(proxyBeanMethods = false)
434+
// This is added to bootstrap context as a source in bootstrap.properties
435+
protected static class SimplePropertySourceConfiguration
436+
implements PropertySourceLocator {
437+
438+
@Override
439+
public PropertySource<?> locate(Environment environment) {
440+
return new PropertySource("testBootstrapSimple", this) {
441+
@Override
442+
public Object getProperty(String name) {
443+
return ("foo".equals(name)) ? "bar" : null;
444+
}
445+
};
446+
}
447+
448+
}
449+
425450
@Configuration(proxyBeanMethods = false)
426451
@ConfigurationProperties("expected")
427452
// This is added to bootstrap context as a source in bootstrap.properties
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.main.sources:org.springframework.cloud.bootstrap.config.BootstrapConfigurationTests.PropertySourceConfiguration,org.springframework.cloud.bootstrap.config.BootstrapConfigurationTests.SimplePropertySourceConfiguration

0 commit comments

Comments
 (0)