Refactor Environment and PropertySource
* Environment now extends PropertyResolver * Environment no longer exposes resolver and sources * PropertySource is String,Object instead of String,String * PropertySource no longer assumes enumerability of property names * Introduced EnumerablePropertySource for those that do have enumerable property names
This commit is contained in:
parent
7c4582b4b3
commit
2b99cf6d29
|
|
@ -223,7 +223,7 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve system properties: e.g. "${user.dir}"
|
// Resolve system properties: e.g. "${user.dir}"
|
||||||
location = environment.getPropertyResolver().resolveRequiredPlaceholders(location);
|
location = environment.resolveRequiredPlaceholders(location);
|
||||||
|
|
||||||
Set<Resource> actualResources = new LinkedHashSet<Resource>(4);
|
Set<Resource> actualResources = new LinkedHashSet<Resource>(4);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2010 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -36,6 +36,7 @@ import org.springframework.beans.propertyeditors.URIEditor;
|
||||||
import org.springframework.beans.propertyeditors.URLEditor;
|
import org.springframework.beans.propertyeditors.URLEditor;
|
||||||
import org.springframework.core.env.DefaultEnvironment;
|
import org.springframework.core.env.DefaultEnvironment;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.core.env.PropertyResolver;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.ResourceEditor;
|
import org.springframework.core.io.ResourceEditor;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
|
|
@ -51,11 +52,12 @@ import org.springframework.core.io.support.ResourcePatternResolver;
|
||||||
* {@link org.springframework.context.support.AbstractApplicationContext}.
|
* {@link org.springframework.context.support.AbstractApplicationContext}.
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Chris Beams
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
|
public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
|
||||||
|
|
||||||
private final Environment environment;
|
private final PropertyResolver propertyResolver;
|
||||||
|
|
||||||
private final ResourceLoader resourceLoader;
|
private final ResourceLoader resourceLoader;
|
||||||
|
|
||||||
|
|
@ -82,9 +84,9 @@ public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
|
||||||
* @see org.springframework.core.io.support.ResourcePatternResolver
|
* @see org.springframework.core.io.support.ResourcePatternResolver
|
||||||
* @see org.springframework.context.ApplicationContext
|
* @see org.springframework.context.ApplicationContext
|
||||||
*/
|
*/
|
||||||
public ResourceEditorRegistrar(ResourceLoader resourceLoader, Environment environment) {
|
public ResourceEditorRegistrar(ResourceLoader resourceLoader, PropertyResolver propertyResolver) {
|
||||||
this.resourceLoader = resourceLoader;
|
this.resourceLoader = resourceLoader;
|
||||||
this.environment = environment;
|
this.propertyResolver = propertyResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -102,7 +104,7 @@ public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
|
||||||
* @see org.springframework.core.io.support.ResourceArrayPropertyEditor
|
* @see org.springframework.core.io.support.ResourceArrayPropertyEditor
|
||||||
*/
|
*/
|
||||||
public void registerCustomEditors(PropertyEditorRegistry registry) {
|
public void registerCustomEditors(PropertyEditorRegistry registry) {
|
||||||
ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.environment);
|
ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);
|
||||||
doRegisterEditor(registry, Resource.class, baseEditor);
|
doRegisterEditor(registry, Resource.class, baseEditor);
|
||||||
doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
|
doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
|
||||||
doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
|
doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
|
||||||
|
|
@ -116,7 +118,7 @@ public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
|
||||||
|
|
||||||
if (this.resourceLoader instanceof ResourcePatternResolver) {
|
if (this.resourceLoader instanceof ResourcePatternResolver) {
|
||||||
doRegisterEditor(registry, Resource[].class,
|
doRegisterEditor(registry, Resource[].class,
|
||||||
new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.environment));
|
new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -114,8 +114,8 @@ public class AnnotatedBeanDefinitionReader {
|
||||||
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
|
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
|
||||||
AnnotationMetadata metadata = abd.getMetadata();
|
AnnotationMetadata metadata = abd.getMetadata();
|
||||||
|
|
||||||
if (Profile.Helper.isProfileAnnotationPresent(metadata)) {
|
if (ProfileHelper.isProfileAnnotationPresent(metadata)) {
|
||||||
if (!this.environment.acceptsProfiles(Profile.Helper.getCandidateProfiles(metadata))) {
|
if (!this.environment.acceptsProfiles(ProfileHelper.getCandidateProfiles(metadata))) {
|
||||||
// TODO SPR-7508: log that this bean is being rejected on profile mismatch
|
// TODO SPR-7508: log that this bean is being rejected on profile mismatch
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -280,7 +280,7 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
|
||||||
* @return the pattern specification to be used for package searching
|
* @return the pattern specification to be used for package searching
|
||||||
*/
|
*/
|
||||||
protected String resolveBasePackage(String basePackage) {
|
protected String resolveBasePackage(String basePackage) {
|
||||||
return ClassUtils.convertClassNameToResourcePath(environment.getPropertyResolver().resolveRequiredPlaceholders(basePackage));
|
return ClassUtils.convertClassNameToResourcePath(environment.resolveRequiredPlaceholders(basePackage));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -298,11 +298,11 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
|
||||||
for (TypeFilter tf : this.includeFilters) {
|
for (TypeFilter tf : this.includeFilters) {
|
||||||
if (tf.match(metadataReader, this.metadataReaderFactory)) {
|
if (tf.match(metadataReader, this.metadataReaderFactory)) {
|
||||||
AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
|
AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
|
||||||
if (!Profile.Helper.isProfileAnnotationPresent(metadata)) {
|
if (!ProfileHelper.isProfileAnnotationPresent(metadata)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// TODO SPR-7508: log that this bean is being rejected on profile mismatch
|
// TODO SPR-7508: log that this bean is being rejected on profile mismatch
|
||||||
return this.environment.acceptsProfiles(Profile.Helper.getCandidateProfiles(metadata));
|
return this.environment.acceptsProfiles(ProfileHelper.getCandidateProfiles(metadata));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -102,8 +102,8 @@ class ConfigurationClassParser {
|
||||||
|
|
||||||
protected void processConfigurationClass(ConfigurationClass configClass) throws IOException {
|
protected void processConfigurationClass(ConfigurationClass configClass) throws IOException {
|
||||||
AnnotationMetadata metadata = configClass.getMetadata();
|
AnnotationMetadata metadata = configClass.getMetadata();
|
||||||
if (this.environment != null && Profile.Helper.isProfileAnnotationPresent(metadata)) {
|
if (this.environment != null && ProfileHelper.isProfileAnnotationPresent(metadata)) {
|
||||||
if (!this.environment.acceptsProfiles(Profile.Helper.getCandidateProfiles(metadata))) {
|
if (!this.environment.acceptsProfiles(ProfileHelper.getCandidateProfiles(metadata))) {
|
||||||
// TODO SPR-7508: log that this bean is being rejected on profile mismatch
|
// TODO SPR-7508: log that this bean is being rejected on profile mismatch
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ import java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.springframework.core.env.AbstractEnvironment;
|
import org.springframework.core.env.AbstractEnvironment;
|
||||||
import org.springframework.core.env.ConfigurableEnvironment;
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates that a component is eligible for registration when one or more {@linkplain #value
|
* Indicates that a component is eligible for registration when one or more {@linkplain #value
|
||||||
|
|
@ -73,21 +72,4 @@ public @interface Profile {
|
||||||
*/
|
*/
|
||||||
String[] value();
|
String[] value();
|
||||||
|
|
||||||
|
|
||||||
static class Helper {
|
|
||||||
/**
|
|
||||||
* Return whether the given metadata includes Profile information, whether directly or
|
|
||||||
* through meta-annotation.
|
|
||||||
*/
|
|
||||||
static boolean isProfileAnnotationPresent(AnnotationMetadata metadata) {
|
|
||||||
return metadata.isAnnotated(Profile.class.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the String[] of candidate profiles from {@link Profile#value()}.
|
|
||||||
*/
|
|
||||||
static String[] getCandidateProfiles(AnnotationMetadata metadata) {
|
|
||||||
return (String[])metadata.getAnnotationAttributes(Profile.class.getName()).get("value");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2011 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
|
||||||
|
*
|
||||||
|
* http://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.context.annotation;
|
||||||
|
|
||||||
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
|
|
||||||
|
class ProfileHelper {
|
||||||
|
/**
|
||||||
|
* Return whether the given metadata includes Profile information, whether directly or
|
||||||
|
* through meta-annotation.
|
||||||
|
*/
|
||||||
|
static boolean isProfileAnnotationPresent(AnnotationMetadata metadata) {
|
||||||
|
return metadata.isAnnotated(Profile.class.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the String[] of candidate profiles from {@link Profile#value()}.
|
||||||
|
*/
|
||||||
|
static String[] getCandidateProfiles(AnnotationMetadata metadata) {
|
||||||
|
return (String[])metadata.getAnnotationAttributes(Profile.class.getName()).get("value");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2010 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -48,7 +48,7 @@ public class EnvironmentAccessor implements PropertyAccessor {
|
||||||
* environment.
|
* environment.
|
||||||
*/
|
*/
|
||||||
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
|
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
|
||||||
return new TypedValue(((Environment)target).getPropertyResolver().getProperty(name));
|
return new TypedValue(((Environment)target).getProperty(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -119,7 +119,7 @@ public abstract class AbstractRefreshableConfigApplicationContext extends Abstra
|
||||||
* @return the resolved file path
|
* @return the resolved file path
|
||||||
*/
|
*/
|
||||||
protected String resolvePath(String path) {
|
protected String resolvePath(String path) {
|
||||||
return this.getEnvironment().getPropertyResolver().resolveRequiredPlaceholders(path);
|
return this.getEnvironment().resolveRequiredPlaceholders(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,8 @@ import org.springframework.beans.factory.config.AbstractPropertyPlaceholderConfi
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||||
import org.springframework.context.EnvironmentAware;
|
import org.springframework.context.EnvironmentAware;
|
||||||
|
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.PropertiesPropertySource;
|
import org.springframework.core.env.PropertiesPropertySource;
|
||||||
|
|
@ -111,7 +113,7 @@ public class PropertySourcesPlaceholderConfigurer extends AbstractPropertyPlaceh
|
||||||
if (this.propertySources == null) {
|
if (this.propertySources == null) {
|
||||||
this.propertySources = new MutablePropertySources();
|
this.propertySources = new MutablePropertySources();
|
||||||
if (this.environment != null) {
|
if (this.environment != null) {
|
||||||
this.propertySources.addAll(this.environment.getPropertySources());
|
this.propertySources.addAll(((ConfigurableEnvironment)this.environment).getPropertySources());
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
PropertySource<?> localPropertySource =
|
PropertySource<?> localPropertySource =
|
||||||
|
|
@ -128,7 +130,18 @@ public class PropertySourcesPlaceholderConfigurer extends AbstractPropertyPlaceh
|
||||||
}
|
}
|
||||||
|
|
||||||
this.propertyResolver = new PropertySourcesPropertyResolver(this.propertySources);
|
this.propertyResolver = new PropertySourcesPropertyResolver(this.propertySources);
|
||||||
this.processProperties(beanFactory, this.propertyResolver.asProperties());
|
this.processProperties(beanFactory, asProperties(this.propertySources));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Properties asProperties(PropertySources propertySources) {
|
||||||
|
Properties mergedProps = new Properties();
|
||||||
|
java.util.List<PropertySource<?>> propertySourcesList = propertySources.asList();
|
||||||
|
for (int i = propertySourcesList.size() -1; i >= 0; i--) {
|
||||||
|
PropertySource<?> source = propertySourcesList.get(i);
|
||||||
|
for (String key : ((EnumerablePropertySource<?>)source).getPropertyNames()) {
|
||||||
|
mergedProps.put(key, source.getProperty(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mergedProps;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,7 @@ public class MockPropertySource extends PropertiesPropertySource {
|
||||||
/**
|
/**
|
||||||
* Set the given property on the underlying {@link Properties} object.
|
* Set the given property on the underlying {@link Properties} object.
|
||||||
*/
|
*/
|
||||||
public void setProperty(String key, String value) {
|
public void setProperty(String key, Object value) {
|
||||||
this.source.put(key, value);
|
this.source.put(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -96,7 +96,7 @@ public class MockPropertySource extends PropertiesPropertySource {
|
||||||
* Useful for method chaining and fluent-style use.
|
* Useful for method chaining and fluent-style use.
|
||||||
* @return this {@link MockPropertySource} instance
|
* @return this {@link MockPropertySource} instance
|
||||||
*/
|
*/
|
||||||
public MockPropertySource withProperty(String key, String value) {
|
public MockPropertySource withProperty(String key, Object value) {
|
||||||
this.setProperty(key, value);
|
this.setProperty(key, value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import java.util.Set;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.springframework.core.convert.ConversionService;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
|
@ -61,6 +62,10 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||||
private ConfigurablePropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
|
private ConfigurablePropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
// Implementation of ConfigurableEnvironment interface
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
public String[] getActiveProfiles() {
|
public String[] getActiveProfiles() {
|
||||||
return this.doGetActiveProfiles().toArray(new String[]{});
|
return this.doGetActiveProfiles().toArray(new String[]{});
|
||||||
}
|
}
|
||||||
|
|
@ -123,8 +128,9 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||||
return this.propertyResolver;
|
return this.propertyResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getSystemEnvironment() {
|
@SuppressWarnings("unchecked")
|
||||||
Map<String,String> systemEnvironment;
|
public Map<String, Object> getSystemEnvironment() {
|
||||||
|
Map<String, ?> systemEnvironment;
|
||||||
try {
|
try {
|
||||||
systemEnvironment = System.getenv();
|
systemEnvironment = System.getenv();
|
||||||
}
|
}
|
||||||
|
|
@ -145,11 +151,11 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return systemEnvironment;
|
return (Map<String, Object>) systemEnvironment;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
public Map<String, String> getSystemProperties() {
|
public Map<String, Object> getSystemProperties() {
|
||||||
Map systemProperties;
|
Map systemProperties;
|
||||||
try {
|
try {
|
||||||
systemProperties = System.getProperties();
|
systemProperties = System.getProperties();
|
||||||
|
|
@ -174,6 +180,62 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||||
return systemProperties;
|
return systemProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
// Implementation of ConfigurablePropertyResolver interface
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
public boolean containsProperty(String key) {
|
||||||
|
return this.propertyResolver.containsProperty(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getProperty(String key) {
|
||||||
|
return this.propertyResolver.getProperty(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T getProperty(String key, Class<T> targetType) {
|
||||||
|
return this.propertyResolver.getProperty(key, targetType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRequiredProperty(String key) throws IllegalStateException {
|
||||||
|
return this.propertyResolver.getRequiredProperty(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException {
|
||||||
|
return this.propertyResolver.getRequiredProperty(key, targetType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String resolvePlaceholders(String text) {
|
||||||
|
return this.propertyResolver.resolvePlaceholders(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
|
||||||
|
return this.propertyResolver.resolveRequiredPlaceholders(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConversionService(ConversionService conversionService) {
|
||||||
|
this.propertyResolver.setConversionService(conversionService);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConversionService getConversionService() {
|
||||||
|
return this.propertyResolver.getConversionService();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPlaceholderPrefix(String placeholderPrefix) {
|
||||||
|
this.propertyResolver.setPlaceholderPrefix(placeholderPrefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setPlaceholderSuffix(String placeholderSuffix) {
|
||||||
|
this.propertyResolver.setPlaceholderSuffix(placeholderSuffix);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setValueSeparator(String valueSeparator) {
|
||||||
|
this.propertyResolver.setValueSeparator(valueSeparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return format("%s [activeProfiles=%s, defaultProfiles=%s, propertySources=%s]",
|
return format("%s [activeProfiles=%s, defaultProfiles=%s, propertySources=%s]",
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@
|
||||||
package org.springframework.core.env;
|
package org.springframework.core.env;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
import static java.lang.String.format;
|
||||||
|
|
||||||
import static org.springframework.util.SystemPropertyUtils.PLACEHOLDER_PREFIX;
|
import static org.springframework.util.SystemPropertyUtils.PLACEHOLDER_PREFIX;
|
||||||
import static org.springframework.util.SystemPropertyUtils.PLACEHOLDER_SUFFIX;
|
import static org.springframework.util.SystemPropertyUtils.PLACEHOLDER_SUFFIX;
|
||||||
import static org.springframework.util.SystemPropertyUtils.VALUE_SEPARATOR;
|
import static org.springframework.util.SystemPropertyUtils.VALUE_SEPARATOR;
|
||||||
|
|
@ -42,12 +41,12 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
|
||||||
|
|
||||||
protected ConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
|
protected ConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
|
||||||
|
|
||||||
private final PropertyPlaceholderHelper nonStrictHelper =
|
private PropertyPlaceholderHelper nonStrictHelper;
|
||||||
new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true);
|
private PropertyPlaceholderHelper strictHelper;
|
||||||
|
|
||||||
private final PropertyPlaceholderHelper strictHelper =
|
|
||||||
new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, false);
|
|
||||||
|
|
||||||
|
private String placeholderPrefix = PLACEHOLDER_PREFIX;
|
||||||
|
private String placeholderSuffix = PLACEHOLDER_SUFFIX;
|
||||||
|
private String valueSeparator = VALUE_SEPARATOR;
|
||||||
|
|
||||||
public ConversionService getConversionService() {
|
public ConversionService getConversionService() {
|
||||||
return this.conversionService;
|
return this.conversionService;
|
||||||
|
|
@ -73,16 +72,35 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPropertyCount() {
|
public void setPlaceholderPrefix(String placeholderPrefix) {
|
||||||
return asProperties().size();
|
this.placeholderPrefix = placeholderPrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPlaceholderSuffix(String placeholderSuffix) {
|
||||||
|
this.placeholderSuffix = placeholderSuffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValueSeparator(String valueSeparator) {
|
||||||
|
this.valueSeparator = valueSeparator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String resolvePlaceholders(String text) {
|
public String resolvePlaceholders(String text) {
|
||||||
return doResolvePlaceholders(text, this.nonStrictHelper);
|
if (nonStrictHelper == null) {
|
||||||
|
nonStrictHelper = createPlaceholderHelper(true);
|
||||||
|
}
|
||||||
|
return doResolvePlaceholders(text, nonStrictHelper);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String resolveRequiredPlaceholders(String text) {
|
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
|
||||||
return doResolvePlaceholders(text, this.strictHelper);
|
if (strictHelper == null) {
|
||||||
|
strictHelper = createPlaceholderHelper(false);
|
||||||
|
}
|
||||||
|
return doResolvePlaceholders(text, strictHelper);
|
||||||
|
}
|
||||||
|
|
||||||
|
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {
|
||||||
|
return new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix,
|
||||||
|
this.valueSeparator, ignoreUnresolvablePlaceholders);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
|
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2010 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -16,18 +16,19 @@
|
||||||
|
|
||||||
package org.springframework.core.env;
|
package org.springframework.core.env;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration interface to be implemented by most if not all {@link Environment
|
* Configuration interface to be implemented by most if not all {@link Environment
|
||||||
* Environments}. Provides facilities for setting active and default profiles as well
|
* Environments}. Provides facilities for setting active and default profiles as well
|
||||||
* as specializing the return types for {@link #getPropertySources()} and
|
* as accessing the {@linkplain #getPropertySources() property sources}.
|
||||||
* {@link #getPropertyResolver()} such that they return types that may be manipulated.
|
|
||||||
*
|
*
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
* @see DefaultEnvironment
|
* @see DefaultEnvironment
|
||||||
* @see org.springframework.context.ConfigurableApplicationContext#getEnvironment
|
* @see org.springframework.context.ConfigurableApplicationContext#getEnvironment
|
||||||
*/
|
*/
|
||||||
public interface ConfigurableEnvironment extends Environment {
|
public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify the set of profiles active for this Environment. Profiles are
|
* Specify the set of profiles active for this Environment. Profiles are
|
||||||
|
|
@ -53,8 +54,33 @@ public interface ConfigurableEnvironment extends Environment {
|
||||||
MutablePropertySources getPropertySources();
|
MutablePropertySources getPropertySources();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the {@link PropertyResolver} for this environment in configurable form
|
* Return the value of {@link System#getenv()} if allowed by the current {@link SecurityManager},
|
||||||
|
* otherwise return a map implementation that will attempt to access individual keys using calls to
|
||||||
|
* {@link System#getenv(String)}.
|
||||||
|
*
|
||||||
|
* <p>Note that most {@link Environment} implementations will include this system environment map as
|
||||||
|
* a default {@link PropertySource} to be searched. Therefore, it is recommended that this method not be
|
||||||
|
* used directly unless bypassing other property sources is expressly intended.
|
||||||
|
*
|
||||||
|
* <p>Calls to {@link Map#get(Object)} on the Map returned will never throw {@link IllegalAccessException};
|
||||||
|
* in cases where the SecurityManager forbids access to a property, {@code null} will be returned and an
|
||||||
|
* INFO-level log message will be issued noting the exception.
|
||||||
*/
|
*/
|
||||||
ConfigurablePropertyResolver getPropertyResolver();
|
Map<String, Object> getSystemEnvironment();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the value of {@link System#getProperties()} if allowed by the current {@link SecurityManager},
|
||||||
|
* otherwise return a map implementation that will attempt to access individual keys using calls to
|
||||||
|
* {@link System#getProperty(String)}.
|
||||||
|
*
|
||||||
|
* <p>Note that most {@code Environment} implementations will include this system properties map as a
|
||||||
|
* default {@link PropertySource} to be searched. Therefore, it is recommended that this method not be
|
||||||
|
* used directly unless bypassing other property sources is expressly intended.
|
||||||
|
*
|
||||||
|
* <p>Calls to {@link Map#get(Object)} on the Map returned will never throw {@link IllegalAccessException};
|
||||||
|
* in cases where the SecurityManager forbids access to a property, {@code null} will be returned and an
|
||||||
|
* INFO-level log message will be issued noting the exception.
|
||||||
|
*/
|
||||||
|
Map<String, Object> getSystemProperties();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,4 +44,7 @@ public interface ConfigurablePropertyResolver extends PropertyResolver {
|
||||||
*/
|
*/
|
||||||
void setConversionService(ConversionService conversionService);
|
void setConversionService(ConversionService conversionService);
|
||||||
|
|
||||||
|
void setPlaceholderPrefix(String placeholderPrefix);
|
||||||
|
void setPlaceholderSuffix(String placeholderSuffix);
|
||||||
|
void setValueSeparator(String valueSeparator);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
package org.springframework.core.env;
|
package org.springframework.core.env;
|
||||||
|
|
||||||
|
import org.springframework.core.convert.ConversionService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default implementation of the {@link Environment} interface. Used throughout all non-Web*
|
* Default implementation of the {@link Environment} interface. Used throughout all non-Web*
|
||||||
* ApplicationContext implementations.
|
* ApplicationContext implementations.
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2011 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
|
||||||
|
*
|
||||||
|
* http://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.core.env;
|
||||||
|
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO SPR-7508: document
|
||||||
|
*
|
||||||
|
* @author Chris Beams
|
||||||
|
* @since 3.1
|
||||||
|
*/
|
||||||
|
public abstract class EnumerablePropertySource<T> extends PropertySource<T> {
|
||||||
|
|
||||||
|
protected static final String[] EMPTY_NAMES_ARRAY = new String[0];
|
||||||
|
|
||||||
|
|
||||||
|
public EnumerablePropertySource(String name, T source) {
|
||||||
|
super(name, source);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the names of all properties contained by the {@linkplain #getSource()
|
||||||
|
* source} object (never {@code null}).
|
||||||
|
*/
|
||||||
|
public abstract String[] getPropertyNames();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether this {@code PropertySource} contains the given key.
|
||||||
|
* <p>This implementation checks for the presence of the given key within
|
||||||
|
* the {@link #getPropertyNames()} array.
|
||||||
|
* @param key the property key to find
|
||||||
|
*/
|
||||||
|
public boolean containsProperty(String name) {
|
||||||
|
Assert.notNull(name, "property name must not be null");
|
||||||
|
for (String candidate : this.getPropertyNames()) {
|
||||||
|
if (candidate.equals(name)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -16,8 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.core.env;
|
package org.springframework.core.env;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface representing the environment in which the current application is running.
|
* Interface representing the environment in which the current application is running.
|
||||||
* Models two key aspects of the application environment:
|
* Models two key aspects of the application environment:
|
||||||
|
|
@ -65,7 +63,7 @@ import java.util.Map;
|
||||||
* @see org.springframework.context.ConfigurableApplicationContext#setEnvironment
|
* @see org.springframework.context.ConfigurableApplicationContext#setEnvironment
|
||||||
* @see org.springframework.context.support.AbstractApplicationContext#createEnvironment
|
* @see org.springframework.context.support.AbstractApplicationContext#createEnvironment
|
||||||
*/
|
*/
|
||||||
public interface Environment {
|
public interface Environment extends PropertyResolver {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the set of profiles explicitly made active for this environment. Profiles are used for
|
* Return the set of profiles explicitly made active for this environment. Profiles are used for
|
||||||
|
|
@ -101,47 +99,4 @@ public interface Environment {
|
||||||
*/
|
*/
|
||||||
boolean acceptsProfiles(String... profiles);
|
boolean acceptsProfiles(String... profiles);
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the {@link PropertyResolver} used for accessing properties.
|
|
||||||
* @see PropertyResolver
|
|
||||||
* @see #getPropertySources
|
|
||||||
*/
|
|
||||||
PropertyResolver getPropertyResolver();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the set of {@link PropertySource} objects used by by this Environment's PropertyResolver
|
|
||||||
* @see #getPropertyResolver
|
|
||||||
*/
|
|
||||||
PropertySources getPropertySources();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the value of {@link System#getenv()} if allowed by the current {@link SecurityManager},
|
|
||||||
* otherwise return a map implementation that will attempt to access individual keys using calls to
|
|
||||||
* {@link System#getenv(String)}.
|
|
||||||
*
|
|
||||||
* <p>Note that most {@link Environment} implementations will include this system environment map as
|
|
||||||
* a default {@link PropertySource} to be searched. Therefore, it is recommended that this method not be
|
|
||||||
* used directly unless bypassing other property sources is expressly intended.
|
|
||||||
*
|
|
||||||
* <p>Calls to {@link Map#get(Object)} on the Map returned will never throw {@link IllegalAccessException};
|
|
||||||
* in cases where the SecurityManager forbids access to a property, {@code null} will be returned and an
|
|
||||||
* INFO-level log message will be issued noting the exception.
|
|
||||||
*/
|
|
||||||
Map<String, String> getSystemEnvironment();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the value of {@link System#getProperties()} if allowed by the current {@link SecurityManager},
|
|
||||||
* otherwise return a map implementation that will attempt to access individual keys using calls to
|
|
||||||
* {@link System#getProperty(String)}.
|
|
||||||
*
|
|
||||||
* <p>Note that most {@code Environment} implementations will include this system properties map as a
|
|
||||||
* default {@link PropertySource} to be searched. Therefore, it is recommended that this method not be
|
|
||||||
* used directly unless bypassing other property sources is expressly intended.
|
|
||||||
*
|
|
||||||
* <p>Calls to {@link Map#get(Object)} on the Map returned will never throw {@link IllegalAccessException};
|
|
||||||
* in cases where the SecurityManager forbids access to a property, {@code null} will be returned and an
|
|
||||||
* INFO-level log message will be issued noting the exception.
|
|
||||||
*/
|
|
||||||
Map<String, String> getSystemProperties();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,20 +25,20 @@ import java.util.Map;
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
* @see PropertiesPropertySource
|
* @see PropertiesPropertySource
|
||||||
*/
|
*/
|
||||||
public class MapPropertySource extends PropertySource<Map<String, String>> {
|
public class MapPropertySource extends EnumerablePropertySource<Map<String, ? super Object>> {
|
||||||
|
|
||||||
protected MapPropertySource(String name, Map<String, String> source) {
|
protected MapPropertySource(String name, Map<String, ? super Object> source) {
|
||||||
super(name, source);
|
super(name, source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getProperty(String key) {
|
||||||
|
return this.source.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] getPropertyNames() {
|
public String[] getPropertyNames() {
|
||||||
return this.source.keySet().toArray(EMPTY_NAMES_ARRAY);
|
return this.source.keySet().toArray(EMPTY_NAMES_ARRAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getProperty(String key) {
|
|
||||||
return this.source.get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.core.env;
|
package org.springframework.core.env;
|
||||||
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -24,52 +23,41 @@ import java.util.Properties;
|
||||||
*
|
*
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
* @see Environment#getPropertyResolver()
|
|
||||||
*/
|
*/
|
||||||
public interface PropertyResolver {
|
public interface PropertyResolver {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return whether the given property key is available for resolution
|
* Return whether the given property key is available for resolution.
|
||||||
*/
|
*/
|
||||||
boolean containsProperty(String key);
|
boolean containsProperty(String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the property value associated with the given key
|
* Return the property value associated with the given key.
|
||||||
* @see #getProperty(String, Class)
|
* @see #getProperty(String, Class)
|
||||||
*/
|
*/
|
||||||
String getProperty(String key);
|
String getProperty(String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the property value associated with the given key, or {@code null}
|
* Return the property value associated with the given key, or {@code null}
|
||||||
* if the key cannot be resolved
|
* if the key cannot be resolved.
|
||||||
*/
|
*/
|
||||||
<T> T getProperty(String key, Class<T> targetType);
|
<T> T getProperty(String key, Class<T> targetType);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the property value associated with the given key, converted to the given
|
* Return the property value associated with the given key, converted to the given
|
||||||
* targetType (never {@code null})
|
* targetType (never {@code null}).
|
||||||
* @throws IllegalStateException if the key cannot be resolved
|
* @throws IllegalStateException if the key cannot be resolved
|
||||||
* @see #getRequiredProperty(String, Class)
|
* @see #getRequiredProperty(String, Class)
|
||||||
*/
|
*/
|
||||||
String getRequiredProperty(String key) throws IllegalStateException;
|
String getRequiredProperty(String key) throws IllegalStateException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the property value associated with the given key, converted to the given
|
* Return the property value associated with the given key, converted to the given
|
||||||
* targetType (never {@code null})
|
* targetType (never {@code null}).
|
||||||
* @throws IllegalStateException if the given key cannot be resolved
|
* @throws IllegalStateException if the given key cannot be resolved
|
||||||
*/
|
*/
|
||||||
<T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException;
|
<T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the number of unique properties keys resolvable
|
|
||||||
*/
|
|
||||||
int getPropertyCount();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return all property key/value pairs as a {@link java.util.Properties} instance
|
|
||||||
*/
|
|
||||||
Properties asProperties();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve ${...} placeholders in the given text, replacing them with corresponding
|
* Resolve ${...} placeholders in the given text, replacing them with corresponding
|
||||||
* property values as resolved by {@link #getProperty}. Unresolvable placeholders with
|
* property values as resolved by {@link #getProperty}. Unresolvable placeholders with
|
||||||
|
|
@ -91,6 +79,5 @@ public interface PropertyResolver {
|
||||||
* @throws IllegalArgumentException if any placeholders are unresolvable
|
* @throws IllegalArgumentException if any placeholders are unresolvable
|
||||||
* @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders(String, boolean)
|
* @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders(String, boolean)
|
||||||
*/
|
*/
|
||||||
String resolveRequiredPlaceholders(String path) throws IllegalArgumentException;
|
String resolveRequiredPlaceholders(String text) throws IllegalArgumentException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,8 +47,6 @@ import org.springframework.util.Assert;
|
||||||
*/
|
*/
|
||||||
public abstract class PropertySource<T> {
|
public abstract class PropertySource<T> {
|
||||||
|
|
||||||
protected static final String[] EMPTY_NAMES_ARRAY = new String[0];
|
|
||||||
|
|
||||||
protected final Log logger = LogFactory.getLog(this.getClass());
|
protected final Log logger = LogFactory.getLog(this.getClass());
|
||||||
|
|
||||||
protected final String name;
|
protected final String name;
|
||||||
|
|
@ -80,38 +78,22 @@ public abstract class PropertySource<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the names of all properties contained by the {@linkplain #getSource() source}
|
* Return whether this {@code PropertySource} contains the given key.
|
||||||
* object (never {@code null}).
|
* <p>This implementation simply checks for a null return value
|
||||||
|
* from {@link #getProperty(String)}. Subclasses may wish to
|
||||||
|
* implement a more efficient algorithm if possible.
|
||||||
|
* @param key the property key to find
|
||||||
*/
|
*/
|
||||||
public abstract String[] getPropertyNames();
|
public boolean containsProperty(String key) {
|
||||||
|
return this.getProperty(key) != null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the value associated with the given key, {@code null} if not found.
|
* Return the value associated with the given key, {@code null} if not found.
|
||||||
* @param key the property key to find
|
* @param key the property key to find
|
||||||
* @see PropertyResolver#getRequiredProperty(String)
|
* @see PropertyResolver#getRequiredProperty(String)
|
||||||
*/
|
*/
|
||||||
public abstract String getProperty(String key);
|
public abstract Object getProperty(String key);
|
||||||
|
|
||||||
/**
|
|
||||||
* Return whether this {@code PropertySource} contains a property with the given key.
|
|
||||||
* @param key the property key to find
|
|
||||||
*/
|
|
||||||
public boolean containsProperty(String name) {
|
|
||||||
Assert.notNull(name, "property name must not be null");
|
|
||||||
for (String candidate : this.getPropertyNames()) {
|
|
||||||
if (candidate.equals(name)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the number of unique property keys available to this {@code PropertySource}.
|
|
||||||
*/
|
|
||||||
public int size() {
|
|
||||||
return this.getPropertyNames().length;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a hashcode derived from the {@code name} property of this {@code PropertySource}
|
* Return a hashcode derived from the {@code name} property of this {@code PropertySource}
|
||||||
|
|
@ -152,9 +134,9 @@ public abstract class PropertySource<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Produce concise output (type, name, and number of properties) if the current log level does
|
* Produce concise output (type and name) if the current log level does not include debug.
|
||||||
* not include debug. If debug is enabled, produce verbose output including hashcode of the
|
* If debug is enabled, produce verbose output including hashcode of the PropertySource instance
|
||||||
* PropertySource instance and every key/value property pair.
|
* and every key/value property pair.
|
||||||
*
|
*
|
||||||
* This variable verbosity is useful as a property source such as system properties
|
* This variable verbosity is useful as a property source such as system properties
|
||||||
* or environment variables may contain an arbitrary number of property pairs, potentially
|
* or environment variables may contain an arbitrary number of property pairs, potentially
|
||||||
|
|
@ -169,8 +151,8 @@ public abstract class PropertySource<T> {
|
||||||
this.getClass().getSimpleName(), System.identityHashCode(this), this.name, this.source);
|
this.getClass().getSimpleName(), System.identityHashCode(this), this.name, this.source);
|
||||||
}
|
}
|
||||||
|
|
||||||
return String.format("%s [name='%s', propertyCount=%d]",
|
return String.format("%s [name='%s']",
|
||||||
this.getClass().getSimpleName(), this.name, this.size());
|
this.getClass().getSimpleName(), this.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -225,11 +207,6 @@ public abstract class PropertySource<T> {
|
||||||
// TODO SPR-7408: logging
|
// TODO SPR-7408: logging
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String[] getPropertyNames() {
|
|
||||||
return EMPTY_NAMES_ARRAY;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -252,7 +229,7 @@ public abstract class PropertySource<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] getPropertyNames() {
|
public boolean containsProperty(String key) {
|
||||||
throw new UnsupportedOperationException(USAGE_ERROR);
|
throw new UnsupportedOperationException(USAGE_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2010 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -18,11 +18,17 @@ package org.springframework.core.env;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO SPR-7508: document
|
||||||
|
*
|
||||||
|
* @author Chris Beams
|
||||||
|
* @since 3.1
|
||||||
|
*/
|
||||||
public interface PropertySources {
|
public interface PropertySources {
|
||||||
|
|
||||||
PropertySource<?> get(String propertySourceName);
|
PropertySource<?> get(String propertySourceName);
|
||||||
|
|
||||||
|
// TODO make iterable
|
||||||
List<PropertySource<?>> asList();
|
List<PropertySource<?>> asList();
|
||||||
|
|
||||||
int size();
|
int size();
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,6 @@ package org.springframework.core.env;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
import static java.lang.String.format;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link PropertyResolver} implementation that resolves property values against
|
* {@link PropertyResolver} implementation that resolves property values against
|
||||||
* an underlying set of {@link PropertySources}.
|
* an underlying set of {@link PropertySources}.
|
||||||
|
|
@ -40,10 +37,9 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||||
this.propertySources = propertySources;
|
this.propertySources = propertySources;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean containsProperty(String key) {
|
public boolean containsProperty(String key) {
|
||||||
for (PropertySource<?> propertySource : this.propertySources.asList()) {
|
for (PropertySource<?> propertySource : this.propertySources.asList()) {
|
||||||
if (propertySource.containsProperty(key)) {
|
if (propertySource.getProperty(key) != null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -67,17 +63,13 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||||
if (debugEnabled) {
|
if (debugEnabled) {
|
||||||
logger.debug(format("Searching for key '%s' in [%s]", key, propertySource.getName()));
|
logger.debug(format("Searching for key '%s' in [%s]", key, propertySource.getName()));
|
||||||
}
|
}
|
||||||
if (propertySource.containsProperty(key)) {
|
Object value;
|
||||||
Object value = propertySource.getProperty(key);
|
if ((value = propertySource.getProperty(key)) != null) {
|
||||||
Class<?> valueType = value == null ? null : value.getClass();
|
Class<?> valueType = value.getClass();
|
||||||
if (debugEnabled) {
|
if (debugEnabled) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
format("Found key '%s' in [%s] with type [%s] and value '%s'",
|
format("Found key '%s' in [%s] with type [%s] and value '%s'",
|
||||||
key, propertySource.getName(),
|
key, propertySource.getName(), valueType.getSimpleName(), value));
|
||||||
valueType == null ? "" : valueType.getSimpleName(), value));
|
|
||||||
}
|
|
||||||
if (value == null) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
if (!this.conversionService.canConvert(valueType, targetValueType)) {
|
if (!this.conversionService.canConvert(valueType, targetValueType)) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
|
|
@ -94,16 +86,4 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Properties asProperties() {
|
|
||||||
Properties mergedProps = new Properties();
|
|
||||||
List<PropertySource<?>> propertySourcesList = this.propertySources.asList();
|
|
||||||
for (int i = propertySourcesList.size() -1; i >= 0; i--) {
|
|
||||||
PropertySource<?> source = propertySourcesList.get(i);
|
|
||||||
for (String key : source.getPropertyNames()) {
|
|
||||||
mergedProps.put(key, source.getProperty(key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return mergedProps;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2010 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -20,7 +20,6 @@ import java.beans.PropertyEditorSupport;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.springframework.core.env.DefaultEnvironment;
|
import org.springframework.core.env.DefaultEnvironment;
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
import org.springframework.core.env.PropertyResolver;
|
import org.springframework.core.env.PropertyResolver;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
@ -33,8 +32,8 @@ import org.springframework.util.StringUtils;
|
||||||
* {@code Resource} properties instead of using a {@code String} location property.
|
* {@code Resource} properties instead of using a {@code String} location property.
|
||||||
*
|
*
|
||||||
* <p>The path may contain <code>${...}</code> placeholders, to be
|
* <p>The path may contain <code>${...}</code> placeholders, to be
|
||||||
* resolved as {@link Environment} properties: e.g. <code>${user.dir}</code>.
|
* resolved as {@link org.springframework.core.env.Environment} properties:
|
||||||
* Unresolvable placeholders are ignored by default.
|
* e.g. <code>${user.dir}</code>. Unresolvable placeholders are ignored by default.
|
||||||
*
|
*
|
||||||
* <p>Delegates to a {@link ResourceLoader} to do the heavy lifting,
|
* <p>Delegates to a {@link ResourceLoader} to do the heavy lifting,
|
||||||
* by default using a {@link DefaultResourceLoader}.
|
* by default using a {@link DefaultResourceLoader}.
|
||||||
|
|
@ -46,14 +45,14 @@ import org.springframework.util.StringUtils;
|
||||||
* @see Resource
|
* @see Resource
|
||||||
* @see ResourceLoader
|
* @see ResourceLoader
|
||||||
* @see DefaultResourceLoader
|
* @see DefaultResourceLoader
|
||||||
* @see Environment#resolvePlaceholders
|
* @see PropertyResolver#resolvePlaceholders
|
||||||
*/
|
*/
|
||||||
public class ResourceEditor extends PropertyEditorSupport {
|
public class ResourceEditor extends PropertyEditorSupport {
|
||||||
|
|
||||||
private final Environment environment;
|
|
||||||
|
|
||||||
private final ResourceLoader resourceLoader;
|
private final ResourceLoader resourceLoader;
|
||||||
|
|
||||||
|
private final PropertyResolver propertyResolver;
|
||||||
|
|
||||||
private final boolean ignoreUnresolvablePlaceholders;
|
private final boolean ignoreUnresolvablePlaceholders;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -70,7 +69,7 @@ public class ResourceEditor extends PropertyEditorSupport {
|
||||||
* using the given {@link ResourceLoader} and a {@link DefaultEnvironment}.
|
* using the given {@link ResourceLoader} and a {@link DefaultEnvironment}.
|
||||||
* @param resourceLoader the <code>ResourceLoader</code> to use
|
* @param resourceLoader the <code>ResourceLoader</code> to use
|
||||||
* @deprecated as of Spring 3.1 in favor of
|
* @deprecated as of Spring 3.1 in favor of
|
||||||
* {@link #ResourceEditor(ResourceLoader, Environment)}
|
* {@link #ResourceEditor(ResourceLoader, PropertyResolver)}
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public ResourceEditor(ResourceLoader resourceLoader) {
|
public ResourceEditor(ResourceLoader resourceLoader) {
|
||||||
|
|
@ -79,21 +78,22 @@ public class ResourceEditor extends PropertyEditorSupport {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance of the {@link ResourceEditor} class
|
* Create a new instance of the {@link ResourceEditor} class
|
||||||
* using the given {@link ResourceLoader} and {@link Environment}.
|
* using the given {@link ResourceLoader} and {@link PropertyResolver}.
|
||||||
* @param resourceLoader the <code>ResourceLoader</code> to use
|
* @param resourceLoader the <code>ResourceLoader</code> to use
|
||||||
|
* @param propertyResolver the <code>PropertyResolver</code> to use
|
||||||
*/
|
*/
|
||||||
public ResourceEditor(ResourceLoader resourceLoader, Environment environment) {
|
public ResourceEditor(ResourceLoader resourceLoader, PropertyResolver propertyResolver) {
|
||||||
this(resourceLoader, environment, true);
|
this(resourceLoader, propertyResolver, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance of the {@link ResourceEditor} class
|
* Create a new instance of the {@link ResourceEditor} class
|
||||||
* using the given {@link ResourceLoader} and a {@link DefaultEnvironment}.
|
* using the given {@link ResourceLoader}.
|
||||||
* @param resourceLoader the <code>ResourceLoader</code> to use
|
* @param resourceLoader the <code>ResourceLoader</code> to use
|
||||||
* @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
|
* @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
|
||||||
* if no corresponding property could be found in the DefaultEnvironment
|
* if no corresponding property could be found
|
||||||
* @deprecated as of Spring 3.1 in favor of
|
* @deprecated as of Spring 3.1 in favor of
|
||||||
* {@link #ResourceEditor(ResourceLoader, Environment, boolean)}
|
* {@link #ResourceEditor(ResourceLoader, PropertyResolver, boolean)}
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public ResourceEditor(ResourceLoader resourceLoader, boolean ignoreUnresolvablePlaceholders) {
|
public ResourceEditor(ResourceLoader resourceLoader, boolean ignoreUnresolvablePlaceholders) {
|
||||||
|
|
@ -104,15 +104,15 @@ public class ResourceEditor extends PropertyEditorSupport {
|
||||||
* Create a new instance of the {@link ResourceEditor} class
|
* Create a new instance of the {@link ResourceEditor} class
|
||||||
* using the given {@link ResourceLoader}.
|
* using the given {@link ResourceLoader}.
|
||||||
* @param resourceLoader the <code>ResourceLoader</code> to use
|
* @param resourceLoader the <code>ResourceLoader</code> to use
|
||||||
* @param environment the <code>Environment</code> to use
|
* @param propertyResolver the <code>PropertyResolver</code> to use
|
||||||
* @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
|
* @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
|
||||||
* if no corresponding property could be found in the given <code>environment</code>
|
* if no corresponding property could be found in the given <code>propertyResolver</code>
|
||||||
*/
|
*/
|
||||||
public ResourceEditor(ResourceLoader resourceLoader, Environment environment, boolean ignoreUnresolvablePlaceholders) {
|
public ResourceEditor(ResourceLoader resourceLoader, PropertyResolver propertyResolver, boolean ignoreUnresolvablePlaceholders) {
|
||||||
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
|
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
|
||||||
Assert.notNull(environment, "Environment must not be null");
|
Assert.notNull(propertyResolver, "PropertyResolver must not be null");
|
||||||
this.resourceLoader = resourceLoader;
|
this.resourceLoader = resourceLoader;
|
||||||
this.environment = environment;
|
this.propertyResolver = propertyResolver;
|
||||||
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
|
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -133,14 +133,13 @@ public class ResourceEditor extends PropertyEditorSupport {
|
||||||
* property values from the <code>environment</code> if necessary.
|
* property values from the <code>environment</code> if necessary.
|
||||||
* @param path the original file path
|
* @param path the original file path
|
||||||
* @return the resolved file path
|
* @return the resolved file path
|
||||||
* @see Environment#resolvePlaceholders
|
* @see PropertyResolver#resolvePlaceholders
|
||||||
* @see Environment#resolveRequiredPlaceholders
|
* @see PropertyResolver#resolveRequiredPlaceholders
|
||||||
*/
|
*/
|
||||||
protected String resolvePath(String path) {
|
protected String resolvePath(String path) {
|
||||||
PropertyResolver resolver = environment.getPropertyResolver();
|
|
||||||
return this.ignoreUnresolvablePlaceholders ?
|
return this.ignoreUnresolvablePlaceholders ?
|
||||||
resolver.resolvePlaceholders(path) :
|
this.propertyResolver.resolvePlaceholders(path) :
|
||||||
resolver.resolveRequiredPlaceholders(path);
|
this.propertyResolver.resolveRequiredPlaceholders(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2010 the original author or authors.
|
* Copyright 2002-2011 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.
|
||||||
|
|
@ -50,13 +50,12 @@ import org.springframework.core.io.Resource;
|
||||||
* @see org.springframework.core.io.Resource
|
* @see org.springframework.core.io.Resource
|
||||||
* @see ResourcePatternResolver
|
* @see ResourcePatternResolver
|
||||||
* @see PathMatchingResourcePatternResolver
|
* @see PathMatchingResourcePatternResolver
|
||||||
* @see org.springframework.env.Environment#resolvePlaceholders
|
|
||||||
*/
|
*/
|
||||||
public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
|
public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
|
||||||
|
|
||||||
private static final Log logger = LogFactory.getLog(ResourceArrayPropertyEditor.class);
|
private static final Log logger = LogFactory.getLog(ResourceArrayPropertyEditor.class);
|
||||||
|
|
||||||
private final Environment environment;
|
private final PropertyResolver propertyResolver;
|
||||||
|
|
||||||
private final ResourcePatternResolver resourcePatternResolver;
|
private final ResourcePatternResolver resourcePatternResolver;
|
||||||
|
|
||||||
|
|
@ -88,10 +87,10 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
|
||||||
* Create a new ResourceArrayPropertyEditor with the given {@link ResourcePatternResolver}
|
* Create a new ResourceArrayPropertyEditor with the given {@link ResourcePatternResolver}
|
||||||
* and {@link Environment}.
|
* and {@link Environment}.
|
||||||
* @param resourcePatternResolver the ResourcePatternResolver to use
|
* @param resourcePatternResolver the ResourcePatternResolver to use
|
||||||
* @param environment the Environment to use
|
* @param propertyResolver the PropertyResolver to use
|
||||||
*/
|
*/
|
||||||
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver, Environment environment) {
|
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver, PropertyResolver propertyResolver) {
|
||||||
this(resourcePatternResolver, environment, true);
|
this(resourcePatternResolver, propertyResolver, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -111,13 +110,14 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
|
||||||
* Create a new ResourceArrayPropertyEditor with the given {@link ResourcePatternResolver}
|
* Create a new ResourceArrayPropertyEditor with the given {@link ResourcePatternResolver}
|
||||||
* and {@link Environment}.
|
* and {@link Environment}.
|
||||||
* @param resourcePatternResolver the ResourcePatternResolver to use
|
* @param resourcePatternResolver the ResourcePatternResolver to use
|
||||||
* @param environment the Environment to use
|
* @param propertyResolver the PropertyResolver to use
|
||||||
* @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
|
* @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
|
||||||
* if no corresponding system property could be found
|
* if no corresponding system property could be found
|
||||||
*/
|
*/
|
||||||
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver, Environment environment, boolean ignoreUnresolvablePlaceholders) {
|
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver,
|
||||||
|
PropertyResolver propertyResolver, boolean ignoreUnresolvablePlaceholders) {
|
||||||
this.resourcePatternResolver = resourcePatternResolver;
|
this.resourcePatternResolver = resourcePatternResolver;
|
||||||
this.environment = environment;
|
this.propertyResolver = propertyResolver;
|
||||||
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
|
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -193,14 +193,13 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
|
||||||
* corresponding system property values if necessary.
|
* corresponding system property values if necessary.
|
||||||
* @param path the original file path
|
* @param path the original file path
|
||||||
* @return the resolved file path
|
* @return the resolved file path
|
||||||
* @see Environment#resolvePlaceholders
|
* @see PropertyResolver#resolvePlaceholders
|
||||||
* @see Environment#resolveRequiredPlaceholders
|
* @see PropertyResolver#resolveRequiredPlaceholders(String)
|
||||||
*/
|
*/
|
||||||
protected String resolvePath(String path) {
|
protected String resolvePath(String path) {
|
||||||
PropertyResolver resolver = environment.getPropertyResolver();
|
|
||||||
return this.ignoreUnresolvablePlaceholders ?
|
return this.ignoreUnresolvablePlaceholders ?
|
||||||
resolver.resolvePlaceholders(path) :
|
this.propertyResolver.resolvePlaceholders(path) :
|
||||||
resolver.resolveRequiredPlaceholders(path);
|
this.propertyResolver.resolveRequiredPlaceholders(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -235,7 +235,7 @@ public class EnvironmentTests {
|
||||||
getModifiableSystemEnvironment().put(DISALLOWED_PROPERTY_NAME, DISALLOWED_PROPERTY_VALUE);
|
getModifiableSystemEnvironment().put(DISALLOWED_PROPERTY_NAME, DISALLOWED_PROPERTY_VALUE);
|
||||||
|
|
||||||
{
|
{
|
||||||
Map<String, String> systemEnvironment = environment.getSystemEnvironment();
|
Map<String, Object> systemEnvironment = environment.getSystemEnvironment();
|
||||||
assertThat(systemEnvironment, notNullValue());
|
assertThat(systemEnvironment, notNullValue());
|
||||||
assertSame(systemEnvironment, System.getenv());
|
assertSame(systemEnvironment, System.getenv());
|
||||||
}
|
}
|
||||||
|
|
@ -258,10 +258,10 @@ public class EnvironmentTests {
|
||||||
System.setSecurityManager(securityManager);
|
System.setSecurityManager(securityManager);
|
||||||
|
|
||||||
{
|
{
|
||||||
Map<String, String> systemEnvironment = environment.getSystemEnvironment();
|
Map<String, Object> systemEnvironment = environment.getSystemEnvironment();
|
||||||
assertThat(systemEnvironment, notNullValue());
|
assertThat(systemEnvironment, notNullValue());
|
||||||
assertThat(systemEnvironment, instanceOf(ReadOnlySystemAttributesMap.class));
|
assertThat(systemEnvironment, instanceOf(ReadOnlySystemAttributesMap.class));
|
||||||
assertThat(systemEnvironment.get(ALLOWED_PROPERTY_NAME), equalTo(ALLOWED_PROPERTY_VALUE));
|
assertThat(systemEnvironment.get(ALLOWED_PROPERTY_NAME), equalTo((Object)ALLOWED_PROPERTY_VALUE));
|
||||||
assertThat(systemEnvironment.get(DISALLOWED_PROPERTY_NAME), nullValue());
|
assertThat(systemEnvironment.get(DISALLOWED_PROPERTY_NAME), nullValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ package org.springframework.core.env;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
|
||||||
import static org.hamcrest.CoreMatchers.nullValue;
|
import static org.hamcrest.CoreMatchers.nullValue;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
@ -81,7 +80,7 @@ public class PropertyResolverTests {
|
||||||
@Test
|
@Test
|
||||||
public void getProperty_withExplicitNullValue() {
|
public void getProperty_withExplicitNullValue() {
|
||||||
// java.util.Properties does not allow null values (because Hashtable does not)
|
// java.util.Properties does not allow null values (because Hashtable does not)
|
||||||
Map<String, String> nullableProperties = new HashMap<String, String>();
|
Map<String, Object> nullableProperties = new HashMap<String, Object>();
|
||||||
propertySources.addLast(new MapPropertySource("nullableProperties", nullableProperties));
|
propertySources.addLast(new MapPropertySource("nullableProperties", nullableProperties));
|
||||||
nullableProperties.put("foo", null);
|
nullableProperties.put("foo", null);
|
||||||
assertThat(propertyResolver.getProperty("foo"), nullValue());
|
assertThat(propertyResolver.getProperty("foo"), nullValue());
|
||||||
|
|
@ -114,7 +113,7 @@ public class PropertyResolverTests {
|
||||||
String value1 = "bar";
|
String value1 = "bar";
|
||||||
String value2 = "biz";
|
String value2 = "biz";
|
||||||
|
|
||||||
HashMap<String, String> map = new HashMap<String, String>();
|
HashMap<String, Object> map = new HashMap<String, Object>();
|
||||||
map.put(key, value1); // before construction
|
map.put(key, value1); // before construction
|
||||||
MutablePropertySources propertySources = new MutablePropertySources();
|
MutablePropertySources propertySources = new MutablePropertySources();
|
||||||
propertySources.addFirst(new MapPropertySource("testProperties", map));
|
propertySources.addFirst(new MapPropertySource("testProperties", map));
|
||||||
|
|
@ -126,7 +125,7 @@ public class PropertyResolverTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getProperty_doesNotCache_addNewKeyPostConstruction() {
|
public void getProperty_doesNotCache_addNewKeyPostConstruction() {
|
||||||
HashMap<String, String> map = new HashMap<String, String>();
|
HashMap<String, Object> map = new HashMap<String, Object>();
|
||||||
MutablePropertySources propertySources = new MutablePropertySources();
|
MutablePropertySources propertySources = new MutablePropertySources();
|
||||||
propertySources.addFirst(new MapPropertySource("testProperties", map));
|
propertySources.addFirst(new MapPropertySource("testProperties", map));
|
||||||
PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
|
PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
|
||||||
|
|
@ -180,45 +179,9 @@ public class PropertyResolverTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void asProperties() {
|
|
||||||
propertySources = new MutablePropertySources();
|
|
||||||
propertyResolver = new PropertySourcesPropertyResolver(propertySources);
|
|
||||||
assertThat(propertyResolver.asProperties(), notNullValue());
|
|
||||||
|
|
||||||
propertySources.addLast(new MockPropertySource("highestPrecedence").withProperty("common", "highCommon").withProperty("highKey", "highVal"));
|
|
||||||
propertySources.addLast(new MockPropertySource("middlePrecedence").withProperty("common", "midCommon").withProperty("midKey", "midVal"));
|
|
||||||
propertySources.addLast(new MockPropertySource("lowestPrecedence").withProperty("common", "lowCommon").withProperty("lowKey", "lowVal"));
|
|
||||||
|
|
||||||
Properties props = propertyResolver.asProperties();
|
|
||||||
assertThat(props.getProperty("common"), is("highCommon"));
|
|
||||||
assertThat(props.getProperty("lowKey"), is("lowVal"));
|
|
||||||
assertThat(props.getProperty("midKey"), is("midVal"));
|
|
||||||
assertThat(props.getProperty("highKey"), is("highVal"));
|
|
||||||
assertThat(props.size(), is(4));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void asProperties_withMixedPropertySourceTypes() {
|
|
||||||
class Foo { }
|
|
||||||
class FooPropertySource extends PropertySource<Foo> {
|
|
||||||
public FooPropertySource() { super("fooProperties", new Foo()); }
|
|
||||||
public String[] getPropertyNames() { return new String[] {"pName"}; }
|
|
||||||
public String getProperty(String key) { return "fooValue"; }
|
|
||||||
}
|
|
||||||
propertySources = new MutablePropertySources();
|
|
||||||
propertyResolver = new PropertySourcesPropertyResolver(propertySources);
|
|
||||||
assertThat(propertyResolver.asProperties(), notNullValue());
|
|
||||||
|
|
||||||
propertySources.addLast(new MockPropertySource());
|
|
||||||
propertySources.addLast(new FooPropertySource());
|
|
||||||
|
|
||||||
Properties props = propertyResolver.asProperties();
|
|
||||||
assertThat(props.getProperty("pName"), is("fooValue"));
|
|
||||||
assertThat(props.size(), is(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
@Test
|
@Test
|
||||||
public void resolvePlaceholders() {
|
public void resolvePlaceholders() {
|
||||||
MutablePropertySources propertySources = new MutablePropertySources();
|
MutablePropertySources propertySources = new MutablePropertySources();
|
||||||
|
|
@ -279,4 +242,5 @@ public class PropertyResolverTests {
|
||||||
public void resolveRequiredPlaceholders_withNullInput() {
|
public void resolveRequiredPlaceholders_withNullInput() {
|
||||||
new PropertySourcesPropertyResolver(new MutablePropertySources()).resolveRequiredPlaceholders(null);
|
new PropertySourcesPropertyResolver(new MutablePropertySources()).resolveRequiredPlaceholders(null);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,8 @@ import org.junit.Test;
|
||||||
public class PropertySourceTests {
|
public class PropertySourceTests {
|
||||||
@Test @SuppressWarnings("serial")
|
@Test @SuppressWarnings("serial")
|
||||||
public void equals() {
|
public void equals() {
|
||||||
Map<String, String> map1 = new HashMap<String, String>() {{ put("a", "b"); }};
|
Map<String, Object> map1 = new HashMap<String, Object>() {{ put("a", "b"); }};
|
||||||
Map<String, String> map2 = new HashMap<String, String>() {{ put("c", "d"); }};
|
Map<String, Object> map2 = new HashMap<String, Object>() {{ put("c", "d"); }};
|
||||||
Properties props1 = new Properties() {{ setProperty("a", "b"); }};
|
Properties props1 = new Properties() {{ setProperty("a", "b"); }};
|
||||||
Properties props2 = new Properties() {{ setProperty("c", "d"); }};
|
Properties props2 = new Properties() {{ setProperty("c", "d"); }};
|
||||||
|
|
||||||
|
|
@ -62,8 +62,8 @@ public class PropertySourceTests {
|
||||||
|
|
||||||
@Test @SuppressWarnings("serial")
|
@Test @SuppressWarnings("serial")
|
||||||
public void collectionsOperations() {
|
public void collectionsOperations() {
|
||||||
Map<String, String> map1 = new HashMap<String, String>() {{ put("a", "b"); }};
|
Map<String, Object> map1 = new HashMap<String, Object>() {{ put("a", "b"); }};
|
||||||
Map<String, String> map2 = new HashMap<String, String>() {{ put("c", "d"); }};
|
Map<String, Object> map2 = new HashMap<String, Object>() {{ put("c", "d"); }};
|
||||||
|
|
||||||
PropertySource<?> ps1 = new MapPropertySource("ps1", map1);
|
PropertySource<?> ps1 = new MapPropertySource("ps1", map1);
|
||||||
ps1.getSource();
|
ps1.getSource();
|
||||||
|
|
@ -92,7 +92,7 @@ public class PropertySourceTests {
|
||||||
@Test @SuppressWarnings("serial")
|
@Test @SuppressWarnings("serial")
|
||||||
public void toString_verbosityVariesOnLogLevel() {
|
public void toString_verbosityVariesOnLogLevel() {
|
||||||
String name = "props";
|
String name = "props";
|
||||||
Map<String, String> map = new HashMap<String, String>() {{ put("k1", "v1"); }};
|
Map<String, Object> map = new HashMap<String, Object>() {{ put("k1", "v1"); }};
|
||||||
MapPropertySource ps = new MapPropertySource(name, map);
|
MapPropertySource ps = new MapPropertySource(name, map);
|
||||||
Logger logger = Logger.getLogger(ps.getClass());
|
Logger logger = Logger.getLogger(ps.getClass());
|
||||||
Level original = logger.getLevel();
|
Level original = logger.getLevel();
|
||||||
|
|
@ -110,7 +110,7 @@ public class PropertySourceTests {
|
||||||
logger.setLevel(Level.INFO);
|
logger.setLevel(Level.INFO);
|
||||||
assertThat("PropertySource.toString() should render concise output for log levels >= INFO",
|
assertThat("PropertySource.toString() should render concise output for log levels >= INFO",
|
||||||
ps.toString(),
|
ps.toString(),
|
||||||
equalTo(String.format("%s [name='%s', propertyCount=%d]",
|
equalTo(String.format("%s [name='%s']",
|
||||||
ps.getClass().getSimpleName(),
|
ps.getClass().getSimpleName(),
|
||||||
name,
|
name,
|
||||||
map.size())));
|
map.size())));
|
||||||
|
|
|
||||||
|
|
@ -45,9 +45,9 @@ public class PropertySourcesTests {
|
||||||
assertThat(sources.contains("g"), is(false));
|
assertThat(sources.contains("g"), is(false));
|
||||||
|
|
||||||
assertThat(sources.get("b"), not(nullValue()));
|
assertThat(sources.get("b"), not(nullValue()));
|
||||||
assertThat(sources.get("b").getProperty("p1"), equalTo("bValue"));
|
assertThat(sources.get("b").getProperty("p1"), equalTo((Object)"bValue"));
|
||||||
assertThat(sources.get("d"), not(nullValue()));
|
assertThat(sources.get("d"), not(nullValue()));
|
||||||
assertThat(sources.get("d").getProperty("p1"), equalTo("dValue"));
|
assertThat(sources.get("d").getProperty("p1"), equalTo((Object)"dValue"));
|
||||||
|
|
||||||
sources.addBefore("b", new MockPropertySource("a"));
|
sources.addBefore("b", new MockPropertySource("a"));
|
||||||
sources.addAfter("b", new MockPropertySource("c"));
|
sources.addAfter("b", new MockPropertySource("c"));
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,6 @@ public class PropertyPlaceholderHelperTests {
|
||||||
|
|
||||||
assertEquals("foo=bar",
|
assertEquals("foo=bar",
|
||||||
this.helper.replacePlaceholders(text, new PropertyPlaceholderHelper.PlaceholderResolver() {
|
this.helper.replacePlaceholders(text, new PropertyPlaceholderHelper.PlaceholderResolver() {
|
||||||
|
|
||||||
public String resolvePlaceholder(String placeholderName) {
|
public String resolvePlaceholder(String placeholderName) {
|
||||||
if ("foo".equals(placeholderName)) {
|
if ("foo".equals(placeholderName)) {
|
||||||
return "bar";
|
return "bar";
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,6 @@ import org.springframework.core.env.PropertySource;
|
||||||
*
|
*
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
* @see MockEnvironment
|
|
||||||
*/
|
*/
|
||||||
public class MockPropertySource extends PropertiesPropertySource {
|
public class MockPropertySource extends PropertiesPropertySource {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -433,18 +433,17 @@ public class EnvironmentIntegrationTests {
|
||||||
ConfigurableEnvironment environment = ctx.getEnvironment();
|
ConfigurableEnvironment environment = ctx.getEnvironment();
|
||||||
assertThat(environment, instanceOf(DefaultWebEnvironment.class));
|
assertThat(environment, instanceOf(DefaultWebEnvironment.class));
|
||||||
MutablePropertySources propertySources = environment.getPropertySources();
|
MutablePropertySources propertySources = environment.getPropertySources();
|
||||||
PropertyResolver propertyResolver = environment.getPropertyResolver();
|
|
||||||
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
|
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
|
||||||
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true));
|
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true));
|
||||||
|
|
||||||
// ServletConfig gets precedence
|
// ServletConfig gets precedence
|
||||||
assertThat(propertyResolver.getProperty("pCommon"), is("pCommonConfigValue"));
|
assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
|
||||||
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
||||||
lessThan(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
|
lessThan(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
|
||||||
|
|
||||||
// but all params are available
|
// but all params are available
|
||||||
assertThat(propertyResolver.getProperty("pContext1"), is("pContext1Value"));
|
assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
|
||||||
assertThat(propertyResolver.getProperty("pConfig1"), is("pConfig1Value"));
|
assertThat(environment.getProperty("pConfig1"), is("pConfig1Value"));
|
||||||
|
|
||||||
// Servlet* PropertySources have precedence over System* PropertySources
|
// Servlet* PropertySources have precedence over System* PropertySources
|
||||||
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
||||||
|
|
@ -457,8 +456,8 @@ public class EnvironmentIntegrationTests {
|
||||||
propertySources.replace(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);
|
propertySources.replace(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);
|
||||||
|
|
||||||
// assert that servletconfig params resolve with higher precedence than sysprops
|
// assert that servletconfig params resolve with higher precedence than sysprops
|
||||||
assertThat(propertyResolver.getProperty("pCommon"), is("pCommonConfigValue"));
|
assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
|
||||||
assertThat(propertyResolver.getProperty("pSysProps1"), is("pSysProps1Value"));
|
assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -474,12 +473,11 @@ public class EnvironmentIntegrationTests {
|
||||||
ConfigurableEnvironment environment = ctx.getEnvironment();
|
ConfigurableEnvironment environment = ctx.getEnvironment();
|
||||||
assertThat(environment, instanceOf(DefaultWebEnvironment.class));
|
assertThat(environment, instanceOf(DefaultWebEnvironment.class));
|
||||||
MutablePropertySources propertySources = environment.getPropertySources();
|
MutablePropertySources propertySources = environment.getPropertySources();
|
||||||
PropertyResolver propertyResolver = environment.getPropertyResolver();
|
|
||||||
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
|
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
|
||||||
|
|
||||||
// ServletContext params are available
|
// ServletContext params are available
|
||||||
assertThat(propertyResolver.getProperty("pCommon"), is("pCommonContextValue"));
|
assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
|
||||||
assertThat(propertyResolver.getProperty("pContext1"), is("pContext1Value"));
|
assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
|
||||||
|
|
||||||
// Servlet* PropertySources have precedence over System* PropertySources
|
// Servlet* PropertySources have precedence over System* PropertySources
|
||||||
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)),
|
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)),
|
||||||
|
|
@ -492,8 +490,8 @@ public class EnvironmentIntegrationTests {
|
||||||
propertySources.replace(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);
|
propertySources.replace(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);
|
||||||
|
|
||||||
// assert that servletcontext init params resolve with higher precedence than sysprops
|
// assert that servletcontext init params resolve with higher precedence than sysprops
|
||||||
assertThat(propertyResolver.getProperty("pCommon"), is("pCommonContextValue"));
|
assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
|
||||||
assertThat(propertyResolver.getProperty("pSysProps1"), is("pSysProps1Value"));
|
assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -512,18 +510,17 @@ public class EnvironmentIntegrationTests {
|
||||||
|
|
||||||
ConfigurableEnvironment environment = ctx.getEnvironment();
|
ConfigurableEnvironment environment = ctx.getEnvironment();
|
||||||
MutablePropertySources propertySources = environment.getPropertySources();
|
MutablePropertySources propertySources = environment.getPropertySources();
|
||||||
PropertyResolver propertyResolver = environment.getPropertyResolver();
|
|
||||||
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
|
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
|
||||||
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true));
|
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true));
|
||||||
|
|
||||||
// ServletConfig gets precedence
|
// ServletConfig gets precedence
|
||||||
assertThat(propertyResolver.getProperty("pCommon"), is("pCommonConfigValue"));
|
assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
|
||||||
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
||||||
lessThan(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
|
lessThan(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
|
||||||
|
|
||||||
// but all params are available
|
// but all params are available
|
||||||
assertThat(propertyResolver.getProperty("pContext1"), is("pContext1Value"));
|
assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
|
||||||
assertThat(propertyResolver.getProperty("pConfig1"), is("pConfig1Value"));
|
assertThat(environment.getProperty("pConfig1"), is("pConfig1Value"));
|
||||||
|
|
||||||
// Servlet* PropertySources have precedence over System* PropertySources
|
// Servlet* PropertySources have precedence over System* PropertySources
|
||||||
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
||||||
|
|
@ -536,8 +533,8 @@ public class EnvironmentIntegrationTests {
|
||||||
propertySources.replace(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);
|
propertySources.replace(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);
|
||||||
|
|
||||||
// assert that servletconfig params resolve with higher precedence than sysprops
|
// assert that servletconfig params resolve with higher precedence than sysprops
|
||||||
assertThat(propertyResolver.getProperty("pCommon"), is("pCommonConfigValue"));
|
assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
|
||||||
assertThat(propertyResolver.getProperty("pSysProps1"), is("pSysProps1Value"));
|
assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,8 @@ import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
|
import org.springframework.core.env.DefaultEnvironment;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -141,6 +143,13 @@ public class SpringContextResourceAdapter implements ResourceAdapter {
|
||||||
return this.contextConfigLocation;
|
return this.contextConfigLocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return
|
||||||
|
*/
|
||||||
|
protected ConfigurableEnvironment createEnvironment() {
|
||||||
|
// TODO: should actually return
|
||||||
|
return new DefaultEnvironment();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This implementation loads a Spring ApplicationContext through the
|
* This implementation loads a Spring ApplicationContext through the
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package org.springframework.web.portlet.context;
|
||||||
|
|
||||||
import javax.portlet.PortletConfig;
|
import javax.portlet.PortletConfig;
|
||||||
|
|
||||||
|
import org.springframework.core.env.EnumerablePropertySource;
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
|
@ -28,7 +29,7 @@ import org.springframework.util.CollectionUtils;
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
* @see PortletContextPropertySource
|
* @see PortletContextPropertySource
|
||||||
*/
|
*/
|
||||||
public class PortletConfigPropertySource extends PropertySource<PortletConfig> {
|
public class PortletConfigPropertySource extends EnumerablePropertySource<PortletConfig> {
|
||||||
|
|
||||||
public PortletConfigPropertySource(String name, PortletConfig portletConfig) {
|
public PortletConfigPropertySource(String name, PortletConfig portletConfig) {
|
||||||
super(name, portletConfig);
|
super(name, portletConfig);
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package org.springframework.web.portlet.context;
|
||||||
|
|
||||||
import javax.portlet.PortletContext;
|
import javax.portlet.PortletContext;
|
||||||
|
|
||||||
|
import org.springframework.core.env.EnumerablePropertySource;
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
|
@ -28,7 +29,7 @@ import org.springframework.util.CollectionUtils;
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
* @see PortletConfigPropertySource
|
* @see PortletConfigPropertySource
|
||||||
*/
|
*/
|
||||||
public class PortletContextPropertySource extends PropertySource<PortletContext> {
|
public class PortletContextPropertySource extends EnumerablePropertySource<PortletContext> {
|
||||||
|
|
||||||
public PortletContextPropertySource(String name, PortletContext portletContext) {
|
public PortletContextPropertySource(String name, PortletContext portletContext) {
|
||||||
super(name, portletContext);
|
super(name, portletContext);
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.util.Enumeration;
|
||||||
|
|
||||||
import javax.servlet.ServletConfig;
|
import javax.servlet.ServletConfig;
|
||||||
|
|
||||||
|
import org.springframework.core.env.EnumerablePropertySource;
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
|
@ -30,7 +31,7 @@ import org.springframework.util.CollectionUtils;
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
* @see ServletContextPropertySource
|
* @see ServletContextPropertySource
|
||||||
*/
|
*/
|
||||||
public class ServletConfigPropertySource extends PropertySource<ServletConfig> {
|
public class ServletConfigPropertySource extends EnumerablePropertySource<ServletConfig> {
|
||||||
|
|
||||||
public ServletConfigPropertySource(String name, ServletConfig servletConfig) {
|
public ServletConfigPropertySource(String name, ServletConfig servletConfig) {
|
||||||
super(name, servletConfig);
|
super(name, servletConfig);
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.util.Enumeration;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
|
import org.springframework.core.env.EnumerablePropertySource;
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
|
@ -30,7 +31,7 @@ import org.springframework.util.CollectionUtils;
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
* @see ServletConfigPropertySource
|
* @see ServletConfigPropertySource
|
||||||
*/
|
*/
|
||||||
public class ServletContextPropertySource extends PropertySource<ServletContext> {
|
public class ServletContextPropertySource extends EnumerablePropertySource<ServletContext> {
|
||||||
|
|
||||||
public ServletContextPropertySource(String name, ServletContext servletContext) {
|
public ServletContextPropertySource(String name, ServletContext servletContext) {
|
||||||
super(name, servletContext);
|
super(name, servletContext);
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@ import javax.servlet.ServletException;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.beans.BeanWrapper;
|
import org.springframework.beans.BeanWrapper;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.MutablePropertyValues;
|
import org.springframework.beans.MutablePropertyValues;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue