Browse Source

Adds support for non-enumerable property sources in bootstrap.

Fixes gh-724
pull/766/head
spencergibb 5 years ago
parent
commit
26dae55042
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 14
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/BootstrapPropertySource.java
  2. 9
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java
  3. 46
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/SimpleBootstrapPropertySource.java
  4. 25
      spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java
  5. 1
      spring-cloud-context/src/test/resources/nonenumerable.properties

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

@ -27,13 +27,15 @@ import org.springframework.util.StringUtils;
import static org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME; import static org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME;
/** /**
* Enumerable wrapper for a property source.
*
* @author Ryan Baxter * @author Ryan Baxter
*/ */
public class BootstrapPropertySource<T> extends EnumerablePropertySource<T> { public class BootstrapPropertySource<T> extends EnumerablePropertySource<T> {
private PropertySource<T> delegate; private EnumerablePropertySource<T> delegate;
public BootstrapPropertySource(PropertySource<T> delegate) { public BootstrapPropertySource(EnumerablePropertySource<T> delegate) {
super(BOOTSTRAP_PROPERTY_SOURCE_NAME + "-" + delegate.getName(), super(BOOTSTRAP_PROPERTY_SOURCE_NAME + "-" + delegate.getName(),
delegate.getSource()); delegate.getSource());
this.delegate = delegate; this.delegate = delegate;
@ -47,13 +49,7 @@ public class BootstrapPropertySource<T> extends EnumerablePropertySource<T> {
@Override @Override
public String[] getPropertyNames() { public String[] getPropertyNames() {
Set<String> names = new LinkedHashSet<>(); Set<String> names = new LinkedHashSet<>();
if (!(this.delegate instanceof EnumerablePropertySource)) { names.addAll(Arrays.asList(this.delegate.getPropertyNames()));
throw new IllegalStateException(
"Failed to enumerate property names due to non-enumerable property source: "
+ this.delegate);
}
names.addAll(Arrays.asList(
((EnumerablePropertySource<?>) this.delegate).getPropertyNames()));
return StringUtils.toStringArray(names); return StringUtils.toStringArray(names);
} }

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

@ -45,6 +45,7 @@ import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
@ -100,7 +101,13 @@ public class PropertySourceBootstrapConfiguration implements
} }
List<PropertySource<?>> sourceList = new ArrayList<>(); List<PropertySource<?>> sourceList = new ArrayList<>();
for (PropertySource<?> p : source) { for (PropertySource<?> p : source) {
sourceList.add(new BootstrapPropertySource<>(p)); if (p instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) p;
sourceList.add(new BootstrapPropertySource<>(enumerable));
}
else {
sourceList.add(new SimpleBootstrapPropertySource(p));
}
} }
logger.info("Located property source: " + sourceList); logger.info("Located property source: " + sourceList);
composite.addAll(sourceList); composite.addAll(sourceList);

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

@ -0,0 +1,46 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.bootstrap.config;
import org.springframework.core.env.PropertySource;
import static org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME;
/**
* Simple, non-enumerable PropertySource wrapper.
* @author Ryan Baxter
*/
public class SimpleBootstrapPropertySource<T> extends PropertySource<T> {
private PropertySource<T> delegate;
public SimpleBootstrapPropertySource(PropertySource<T> delegate) {
super(BOOTSTRAP_PROPERTY_SOURCE_NAME + "-" + delegate.getName(),
delegate.getSource());
this.delegate = delegate;
}
@Override
public Object getProperty(String name) {
return this.delegate.getProperty(name);
}
public PropertySource<T> getDelegate() {
return delegate;
}
}

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

@ -416,12 +416,37 @@ public class BootstrapConfigurationTests {
.isEqualTo("Hello added!"); .isEqualTo("Hello added!");
} }
@Test
public void nonEnumerablePropertySourceWorks() {
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.sources(BareConfiguration.class)
.properties("spring.cloud.bootstrap.name=nonenumerable").run();
then(this.context.getEnvironment().getProperty("foo")).isEqualTo("bar");
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties @EnableConfigurationProperties
protected static class BareConfiguration { protected static class BareConfiguration {
} }
@Configuration(proxyBeanMethods = false)
// This is added to bootstrap context as a source in bootstrap.properties
protected static class SimplePropertySourceConfiguration
implements PropertySourceLocator {
@Override
public PropertySource<?> locate(Environment environment) {
return new PropertySource("testBootstrapSimple", this) {
@Override
public Object getProperty(String name) {
return ("foo".equals(name)) ? "bar" : null;
}
};
}
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConfigurationProperties("expected") @ConfigurationProperties("expected")
// This is added to bootstrap context as a source in bootstrap.properties // This is added to bootstrap context as a source in bootstrap.properties

1
spring-cloud-context/src/test/resources/nonenumerable.properties

@ -0,0 +1 @@
spring.main.sources:org.springframework.cloud.bootstrap.config.BootstrapConfigurationTests.PropertySourceConfiguration,org.springframework.cloud.bootstrap.config.BootstrapConfigurationTests.SimplePropertySourceConfiguration
Loading…
Cancel
Save