Add constants for well-known PropertySource names

Closes gh-4776
This commit is contained in:
Stephane Nicoll 2015-12-15 11:54:10 +01:00
parent 543a746de7
commit 9be4b57182
5 changed files with 25 additions and 10 deletions

View File

@ -131,6 +131,11 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
*/ */
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10; public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
/**
* Name of the application configuration {@link PropertySource}.
*/
public static final String APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME = "applicationConfigurationProperties";
private final DeferredLog logger = new DeferredLog(); private final DeferredLog logger = new DeferredLog();
private String searchLocations; private String searchLocations;
@ -599,14 +604,14 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
static class ConfigurationPropertySources static class ConfigurationPropertySources
extends EnumerablePropertySource<Collection<PropertySource<?>>> { extends EnumerablePropertySource<Collection<PropertySource<?>>> {
private static final String NAME = "applicationConfigurationProperties";
private final Collection<PropertySource<?>> sources; private final Collection<PropertySource<?>> sources;
private final String[] names; private final String[] names;
ConfigurationPropertySources(Collection<PropertySource<?>> sources) { ConfigurationPropertySources(Collection<PropertySource<?>> sources) {
super(NAME, sources); super(APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME, sources);
this.sources = sources; this.sources = sources;
List<String> names = new ArrayList<String>(); List<String> names = new ArrayList<String>();
for (PropertySource<?> source : sources) { for (PropertySource<?> source : sources) {
@ -630,9 +635,9 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
} }
public static void finishAndRelocate(MutablePropertySources propertySources) { public static void finishAndRelocate(MutablePropertySources propertySources) {
String name = APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME;
ConfigurationPropertySources removed = (ConfigurationPropertySources) propertySources ConfigurationPropertySources removed = (ConfigurationPropertySources) propertySources
.get(ConfigurationPropertySources.NAME); .get(name);
String name = ConfigurationPropertySources.NAME;
if (removed != null) { if (removed != null) {
for (PropertySource<?> propertySource : removed.sources) { for (PropertySource<?> propertySource : removed.sources) {
if (propertySource instanceof EnumerableCompositePropertySource) { if (propertySource instanceof EnumerableCompositePropertySource) {
@ -646,7 +651,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
propertySources.addAfter(name, propertySource); propertySources.addAfter(name, propertySource);
} }
} }
propertySources.remove(ConfigurationPropertySources.NAME); propertySources.remove(APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME);
} }
} }

View File

@ -51,6 +51,11 @@ import org.springframework.util.StringUtils;
*/ */
public class RandomValuePropertySource extends PropertySource<Random> { public class RandomValuePropertySource extends PropertySource<Random> {
/**
* Name of the random {@link PropertySource}.
*/
public static final String RANDOM_PROPERTY_SOURCE_NAME = "random";
private static final String PREFIX = "random."; private static final String PREFIX = "random.";
private static Log logger = LogFactory.getLog(RandomValuePropertySource.class); private static Log logger = LogFactory.getLog(RandomValuePropertySource.class);
@ -59,6 +64,10 @@ public class RandomValuePropertySource extends PropertySource<Random> {
super(name, new Random()); super(name, new Random());
} }
public RandomValuePropertySource() {
this(RANDOM_PROPERTY_SOURCE_NAME);
}
@Override @Override
public Object getProperty(String name) { public Object getProperty(String name) {
if (!name.startsWith(PREFIX)) { if (!name.startsWith(PREFIX)) {
@ -126,7 +135,7 @@ public class RandomValuePropertySource extends PropertySource<Random> {
public static void addToEnvironment(ConfigurableEnvironment environment) { public static void addToEnvironment(ConfigurableEnvironment environment) {
environment.getPropertySources().addAfter( environment.getPropertySources().addAfter(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
new RandomValuePropertySource("random")); new RandomValuePropertySource(RANDOM_PROPERTY_SOURCE_NAME));
logger.trace("RandomValuePropertySource add to Environment"); logger.trace("RandomValuePropertySource add to Environment");
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2013 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -126,7 +126,7 @@ public class PropertiesConfigurationFactoryTests {
MutablePropertySources propertySources = new MutablePropertySources(); MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment",
Collections.<String, Object>singletonMap("FOO_BAR_NAME", "blah"))); Collections.<String, Object>singletonMap("FOO_BAR_NAME", "blah")));
propertySources.addLast(new RandomValuePropertySource("random")); propertySources.addLast(new RandomValuePropertySource());
setupFactory(); setupFactory();
this.factory.setPropertySources(propertySources); this.factory.setPropertySources(propertySources);
this.factory.afterPropertiesSet(); this.factory.afterPropertiesSet();

View File

@ -519,7 +519,8 @@ public class ConfigFileApplicationListenerTests {
assertThat(Arrays.asList(this.environment.getActiveProfiles()), contains("dev")); assertThat(Arrays.asList(this.environment.getActiveProfiles()), contains("dev"));
assertThat(property, equalTo("fromdevprofile")); assertThat(property, equalTo("fromdevprofile"));
ConfigurationPropertySources propertySource = (ConfigurationPropertySources) this.environment ConfigurationPropertySources propertySource = (ConfigurationPropertySources) this.environment
.getPropertySources().get("applicationConfigurationProperties"); .getPropertySources()
.get(ConfigFileApplicationListener.APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME);
Collection<org.springframework.core.env.PropertySource<?>> sources = propertySource Collection<org.springframework.core.env.PropertySource<?>> sources = propertySource
.getSource(); .getSource();
assertEquals(2, sources.size()); assertEquals(2, sources.size());

View File

@ -33,7 +33,7 @@ import static org.junit.Assert.assertTrue;
*/ */
public class RandomValuePropertySourceTests { public class RandomValuePropertySourceTests {
private RandomValuePropertySource source = new RandomValuePropertySource("random"); private RandomValuePropertySource source = new RandomValuePropertySource();
@Test @Test
public void notRandom() { public void notRandom() {