Refine the behaviour of the defaults map

Fixes #734

- allow mutability
- use Environment directly for configuration

Signed-off-by: Marius Bogoevici <mbogoevici@pivotal.io>
This commit is contained in:
Marius Bogoevici
2016-12-13 20:19:45 -05:00
committed by Ilayaperumal Gopinathan
parent 5ea86e2f53
commit c5b9a9f3e7
2 changed files with 31 additions and 21 deletions

View File

@@ -33,7 +33,10 @@ import org.springframework.cloud.stream.binder.ProducerProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.integration.support.utils.IntegrationUtils;
import org.springframework.util.Assert;
@@ -45,8 +48,7 @@ import org.springframework.util.Assert;
*/
@ConfigurationProperties("spring.cloud.stream")
@JsonInclude(Include.NON_DEFAULT)
public class BindingServiceProperties
implements ApplicationContextAware, InitializingBean {
public class BindingServiceProperties implements ApplicationContextAware, EnvironmentAware, InitializingBean {
private ConversionService conversionService;
@@ -118,11 +120,17 @@ public class BindingServiceProperties
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = (ConfigurableApplicationContext) applicationContext;
// override the bindings store with the environment-initializing version if in a
// Spring context
this.bindings = new EnvironmentEntryInitializingTreeMap<>(this.applicationContext,
BindingProperties.class, "spring.cloud.stream.default",
new TreeMap<String, BindingProperties>(String.CASE_INSENSITIVE_ORDER));
}
@Override
public void setEnvironment(Environment environment) {
if (environment instanceof ConfigurableEnvironment) {
// override the bindings store with the environment-initializing version if in
// a Spring context
this.bindings = new EnvironmentEntryInitializingTreeMap<>((ConfigurableEnvironment) environment,
BindingProperties.class, "spring.cloud.stream.default",
new TreeMap<String, BindingProperties>(String.CASE_INSENSITIVE_ORDER));
}
}
public void setConversionService(ConversionService conversionService) {

View File

@@ -23,14 +23,14 @@ import java.util.Set;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.bind.PropertySourcesPropertyValues;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.Assert;
/**
* A {@link Map} implementation that initializes its entries by binding values from the
* supplied application context's environment. Any call to 'get()' will result in either
* returning the existing value or initializing a new entry by binding properties with the
* specified prefix from the environment.
* supplied environment. Any call to 'get()' will result in either returning the existing
* value or initializing a new entry by binding properties with the specified prefix from
* the environment.
*
* This is strictly intended to be used for configuration property values and not to be
* used as a general purpose map.
@@ -41,7 +41,7 @@ import org.springframework.util.Assert;
*/
public class EnvironmentEntryInitializingTreeMap<T> extends AbstractMap<String, T> {
private final ConfigurableApplicationContext applicationContext;
private final ConfigurableEnvironment environment;
private final Class<T> entryClass;
@@ -52,18 +52,18 @@ public class EnvironmentEntryInitializingTreeMap<T> extends AbstractMap<String,
/**
* Constructs the map.
*
* @param applicationContext the application context that supplies the property values
* @param environment the environment that supplies the default property values
* @param entryClass the entry class
* @param defaultsPrefix the prefix for initializing the properties
* @param delegate the actual map that stores the values
*/
public EnvironmentEntryInitializingTreeMap(ConfigurableApplicationContext applicationContext, Class<T> entryClass,
public EnvironmentEntryInitializingTreeMap(ConfigurableEnvironment environment, Class<T> entryClass,
String defaultsPrefix, Map<String, T> delegate) {
Assert.notNull(applicationContext, "The context cannot be null");
Assert.notNull(environment, "The environment cannot be null");
Assert.notNull(entryClass, "The entry class cannot be null");
Assert.notNull(defaultsPrefix, "The prefix for the property defaults cannot be null");
Assert.notNull(delegate, "The delegate cannot be null");
this.applicationContext = applicationContext;
this.environment = environment;
this.entryClass = entryClass;
this.defaultsPrefix = defaultsPrefix;
this.delegate = delegate;
@@ -73,16 +73,18 @@ public class EnvironmentEntryInitializingTreeMap<T> extends AbstractMap<String,
public T get(Object key) {
if (!this.delegate.containsKey(key) && key instanceof String) {
T entry = BeanUtils.instantiate(entryClass);
if (applicationContext != null) {
RelaxedDataBinder defaultsDataBinder = new RelaxedDataBinder(entry, defaultsPrefix);
defaultsDataBinder.bind(
new PropertySourcesPropertyValues(applicationContext.getEnvironment().getPropertySources()));
}
RelaxedDataBinder defaultsDataBinder = new RelaxedDataBinder(entry, defaultsPrefix);
defaultsDataBinder.bind(new PropertySourcesPropertyValues(environment.getPropertySources()));
this.delegate.put((String) key, entry);
}
return this.delegate.get(key);
}
@Override
public T put(String key, T value) {
return this.delegate.put(key, value);
}
@Override
public Set<Entry<String, T>> entrySet() {
return delegate.entrySet();