|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
* Copyright 2002-2013 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,25 +16,22 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.core.env; |
|
|
|
package org.springframework.core.env; |
|
|
|
|
|
|
|
|
|
|
|
import static java.lang.String.format; |
|
|
|
|
|
|
|
import static org.springframework.util.SystemPropertyUtils.PLACEHOLDER_PREFIX; |
|
|
|
|
|
|
|
import static org.springframework.util.SystemPropertyUtils.PLACEHOLDER_SUFFIX; |
|
|
|
|
|
|
|
import static org.springframework.util.SystemPropertyUtils.VALUE_SEPARATOR; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.LinkedHashSet; |
|
|
|
import java.util.LinkedHashSet; |
|
|
|
import java.util.Set; |
|
|
|
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.support.ConfigurableConversionService; |
|
|
|
import org.springframework.core.convert.support.ConfigurableConversionService; |
|
|
|
import org.springframework.core.convert.support.DefaultConversionService; |
|
|
|
import org.springframework.core.convert.support.DefaultConversionService; |
|
|
|
import org.springframework.util.PropertyPlaceholderHelper; |
|
|
|
import org.springframework.util.PropertyPlaceholderHelper; |
|
|
|
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; |
|
|
|
import org.springframework.util.SystemPropertyUtils; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Abstract base class for resolving properties against any underlying source. |
|
|
|
* Abstract base class for resolving properties against any underlying source. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Chris Beams |
|
|
|
* @author Chris Beams |
|
|
|
|
|
|
|
* @author Juergen Hoeller |
|
|
|
* @since 3.1 |
|
|
|
* @since 3.1 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public abstract class AbstractPropertyResolver implements ConfigurablePropertyResolver { |
|
|
|
public abstract class AbstractPropertyResolver implements ConfigurablePropertyResolver { |
|
|
|
@ -44,15 +41,20 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe |
|
|
|
protected ConfigurableConversionService conversionService = new DefaultConversionService(); |
|
|
|
protected ConfigurableConversionService conversionService = new DefaultConversionService(); |
|
|
|
|
|
|
|
|
|
|
|
private PropertyPlaceholderHelper nonStrictHelper; |
|
|
|
private PropertyPlaceholderHelper nonStrictHelper; |
|
|
|
|
|
|
|
|
|
|
|
private PropertyPlaceholderHelper strictHelper; |
|
|
|
private PropertyPlaceholderHelper strictHelper; |
|
|
|
|
|
|
|
|
|
|
|
private boolean ignoreUnresolvableNestedPlaceholders = false; |
|
|
|
private boolean ignoreUnresolvableNestedPlaceholders = false; |
|
|
|
|
|
|
|
|
|
|
|
private String placeholderPrefix = PLACEHOLDER_PREFIX; |
|
|
|
private String placeholderPrefix = SystemPropertyUtils.PLACEHOLDER_PREFIX; |
|
|
|
private String placeholderSuffix = PLACEHOLDER_SUFFIX; |
|
|
|
|
|
|
|
private String valueSeparator = VALUE_SEPARATOR; |
|
|
|
private String placeholderSuffix = SystemPropertyUtils.PLACEHOLDER_SUFFIX; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String valueSeparator = SystemPropertyUtils.VALUE_SEPARATOR; |
|
|
|
|
|
|
|
|
|
|
|
private final Set<String> requiredProperties = new LinkedHashSet<String>(); |
|
|
|
private final Set<String> requiredProperties = new LinkedHashSet<String>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public ConfigurableConversionService getConversionService() { |
|
|
|
public ConfigurableConversionService getConversionService() { |
|
|
|
return this.conversionService; |
|
|
|
return this.conversionService; |
|
|
|
@ -66,13 +68,13 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String getProperty(String key, String defaultValue) { |
|
|
|
public String getProperty(String key, String defaultValue) { |
|
|
|
String value = getProperty(key); |
|
|
|
String value = getProperty(key); |
|
|
|
return value == null ? defaultValue : value; |
|
|
|
return (value != null ? value : defaultValue); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) { |
|
|
|
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) { |
|
|
|
T value = getProperty(key, targetType); |
|
|
|
T value = getProperty(key, targetType); |
|
|
|
return value == null ? defaultValue : value; |
|
|
|
return (value != null ? value : defaultValue); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -99,7 +101,7 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe |
|
|
|
public String getRequiredProperty(String key) throws IllegalStateException { |
|
|
|
public String getRequiredProperty(String key) throws IllegalStateException { |
|
|
|
String value = getProperty(key); |
|
|
|
String value = getProperty(key); |
|
|
|
if (value == null) { |
|
|
|
if (value == null) { |
|
|
|
throw new IllegalStateException(format("required key [%s] not found", key)); |
|
|
|
throw new IllegalStateException(String.format("required key [%s] not found", key)); |
|
|
|
} |
|
|
|
} |
|
|
|
return value; |
|
|
|
return value; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -108,7 +110,7 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe |
|
|
|
public <T> T getRequiredProperty(String key, Class<T> valueType) throws IllegalStateException { |
|
|
|
public <T> T getRequiredProperty(String key, Class<T> valueType) throws IllegalStateException { |
|
|
|
T value = getProperty(key, valueType); |
|
|
|
T value = getProperty(key, valueType); |
|
|
|
if (value == null) { |
|
|
|
if (value == null) { |
|
|
|
throw new IllegalStateException(format("required key [%s] not found", key)); |
|
|
|
throw new IllegalStateException(String.format("required key [%s] not found", key)); |
|
|
|
} |
|
|
|
} |
|
|
|
return value; |
|
|
|
return value; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -142,18 +144,18 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String resolvePlaceholders(String text) { |
|
|
|
public String resolvePlaceholders(String text) { |
|
|
|
if (nonStrictHelper == null) { |
|
|
|
if (this.nonStrictHelper == null) { |
|
|
|
nonStrictHelper = createPlaceholderHelper(true); |
|
|
|
this.nonStrictHelper = createPlaceholderHelper(true); |
|
|
|
} |
|
|
|
} |
|
|
|
return doResolvePlaceholders(text, nonStrictHelper); |
|
|
|
return doResolvePlaceholders(text, this.nonStrictHelper); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException { |
|
|
|
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException { |
|
|
|
if (strictHelper == null) { |
|
|
|
if (this.strictHelper == null) { |
|
|
|
strictHelper = createPlaceholderHelper(false); |
|
|
|
this.strictHelper = createPlaceholderHelper(false); |
|
|
|
} |
|
|
|
} |
|
|
|
return doResolvePlaceholders(text, strictHelper); |
|
|
|
return doResolvePlaceholders(text, this.strictHelper); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -168,15 +170,19 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Resolve placeholders within the given string, deferring to the value of |
|
|
|
* Resolve placeholders within the given string, deferring to the value of |
|
|
|
* {@link #setIgnoreUnresolvableNestedPlaceholders(boolean)} to determine whether any |
|
|
|
* {@link #setIgnoreUnresolvableNestedPlaceholders} to determine whether any |
|
|
|
* unresolvable placeholders should raise an exception or be ignored. |
|
|
|
* unresolvable placeholders should raise an exception or be ignored. |
|
|
|
|
|
|
|
* <p>Invoked from {@link #getProperty} and its variants, implicitly resolving |
|
|
|
|
|
|
|
* nested placeholders. In contrast, {@link #resolvePlaceholders} and |
|
|
|
|
|
|
|
* {@link #resolveRequiredPlaceholders} do <emphasis>not</emphasis> delegate |
|
|
|
|
|
|
|
* to this method but rather perform their own handling of unresolvable |
|
|
|
|
|
|
|
* placeholders, as specified by each of those methods. |
|
|
|
* @since 3.2 |
|
|
|
* @since 3.2 |
|
|
|
* @see #setIgnoreUnresolvableNestedPlaceholders(boolean) |
|
|
|
* @see #setIgnoreUnresolvableNestedPlaceholders |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected String resolveNestedPlaceholders(String value) { |
|
|
|
protected String resolveNestedPlaceholders(String value) { |
|
|
|
return this.ignoreUnresolvableNestedPlaceholders ? |
|
|
|
return this.ignoreUnresolvableNestedPlaceholders ? |
|
|
|
this.resolvePlaceholders(value) : |
|
|
|
resolvePlaceholders(value) : resolveRequiredPlaceholders(value); |
|
|
|
this.resolveRequiredPlaceholders(value); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) { |
|
|
|
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) { |
|
|
|
@ -185,12 +191,20 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) { |
|
|
|
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) { |
|
|
|
return helper.replacePlaceholders(text, new PlaceholderResolver() { |
|
|
|
return helper.replacePlaceholders(text, new PropertyPlaceholderHelper.PlaceholderResolver() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String resolvePlaceholder(String placeholderName) { |
|
|
|
public String resolvePlaceholder(String placeholderName) { |
|
|
|
return getProperty(placeholderName); |
|
|
|
return getPropertyAsRawString(placeholderName); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Retrieve the specified property as a raw String, |
|
|
|
|
|
|
|
* i.e. without resolution of nested placeholders. |
|
|
|
|
|
|
|
* @param key the property name to resolve |
|
|
|
|
|
|
|
* @return the property value or {@code null} if none found |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected abstract String getPropertyAsRawString(String key); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|