unresolvable placeholders will be ignored by default for Resource array properties as well (SPR-6654)

This commit is contained in:
Juergen Hoeller
2010-01-12 19:50:18 +00:00
parent 7cbd9e1d93
commit e195c39d3c
5 changed files with 80 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2010 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.
@@ -30,11 +30,9 @@ import org.springframework.util.SystemPropertyUtils;
* <code>"classpath:myfile.txt"</code>) to <code>Resource</code>
* properties instead of using a <code>String</code> location property.
*
* <p>The path may contain <code>${...}</code> placeholders, to be resolved
* as system properties: e.g. <code>${user.dir}</code>. By default unresolvable
* placeholders are ignored, but if an exception is preferred set the
* {@link #setIgnoreUnresolvablePlaceholders(boolean) ignoreUnresolvablePlaceholders}
* flag to false.
* <p>The path may contain <code>${...}</code> placeholders,
* to be resolved as system properties: e.g. <code>${user.dir}</code>.
* Unresolvable placeholder are ignored by default.
*
* <p>Delegates to a {@link ResourceLoader} to do the heavy lifting,
* by default using a {@link DefaultResourceLoader}.
@@ -51,8 +49,9 @@ import org.springframework.util.SystemPropertyUtils;
public class ResourceEditor extends PropertyEditorSupport {
private final ResourceLoader resourceLoader;
private boolean ignoreUnresolvablePlaceholders = true;
private final boolean ignoreUnresolvablePlaceholders;
/**
* Create a new instance of the {@link ResourceEditor} class
@@ -68,18 +67,23 @@ public class ResourceEditor extends PropertyEditorSupport {
* @param resourceLoader the <code>ResourceLoader</code> to use
*/
public ResourceEditor(ResourceLoader resourceLoader) {
this(resourceLoader, true);
}
/**
* Create a new instance of the {@link ResourceEditor} class
* using the given {@link ResourceLoader}.
* @param resourceLoader the <code>ResourceLoader</code> to use
* @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
* if no corresponding system property could be found
*/
public ResourceEditor(ResourceLoader resourceLoader, boolean ignoreUnresolvablePlaceholders) {
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
this.resourceLoader = resourceLoader;
}
/**
* Flag to determine if unresolvable placeholders in System properties
* @param ignoreUnresolvablePlaceholders
*/
public void setIgnoreUnresolvablePlaceholders(boolean ignoreUnresolvablePlaceholders) {
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
}
@Override
public void setAsText(String text) {
if (StringUtils.hasText(text)) {
@@ -99,7 +103,7 @@ public class ResourceEditor extends PropertyEditorSupport {
* @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
*/
protected String resolvePath(String path) {
return SystemPropertyUtils.resolvePlaceholders(path, ignoreUnresolvablePlaceholders);
return SystemPropertyUtils.resolvePlaceholders(path, this.ignoreUnresolvablePlaceholders);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2010 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.
@@ -33,8 +33,9 @@ import org.springframework.util.SystemPropertyUtils;
* to <code>Resource</code> array properties. Can also translate a collection
* or array of location patterns into a merged Resource array.
*
* <p>The path may contain <code>${...}</code> placeholders, to be resolved
* as system properties: e.g. <code>${user.dir}</code>.
* <p>The path may contain <code>${...}</code> placeholders,
* to be resolved as system properties: e.g. <code>${user.dir}</code>.
* Unresolvable placeholder are ignored by default.
*
* <p>Delegates to a {@link ResourcePatternResolver},
* by default using a {@link PathMatchingResourcePatternResolver}.
@@ -51,6 +52,8 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
private final ResourcePatternResolver resourcePatternResolver;
private final boolean ignoreUnresolvablePlaceholders;
/**
* Create a new ResourceArrayPropertyEditor with a default
@@ -58,7 +61,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
* @see PathMatchingResourcePatternResolver
*/
public ResourceArrayPropertyEditor() {
this.resourcePatternResolver = new PathMatchingResourcePatternResolver();
this(new PathMatchingResourcePatternResolver());
}
/**
@@ -66,7 +69,18 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
* @param resourcePatternResolver the ResourcePatternResolver to use
*/
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver) {
this(resourcePatternResolver, true);
}
/**
* Create a new ResourceArrayPropertyEditor with the given ResourcePatternResolver.
* @param resourcePatternResolver the ResourcePatternResolver to use
* @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
* if no corresponding system property could be found
*/
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver, boolean ignoreUnresolvablePlaceholders) {
this.resourcePatternResolver = resourcePatternResolver;
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
}
@@ -81,7 +95,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
}
catch (IOException ex) {
throw new IllegalArgumentException(
"Could not resolve resource location pattern [" + pattern + "]: " + ex.getMessage());
"Could not resolve resource location pattern [" + pattern + "]: " + ex.getMessage());
}
}
@@ -142,7 +156,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
* @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
*/
protected String resolvePath(String path) {
return SystemPropertyUtils.resolvePlaceholders(path);
return SystemPropertyUtils.resolvePlaceholders(path, this.ignoreUnresolvablePlaceholders);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@@ -60,7 +60,7 @@ public abstract class SystemPropertyUtils {
* @see #PLACEHOLDER_SUFFIX
* @throws IllegalArgumentException if there is an unresolvable placeholder
*/
public static String resolvePlaceholders(final String text) {
public static String resolvePlaceholders(String text) {
return resolvePlaceholders(text, false);
}
@@ -75,7 +75,7 @@ public abstract class SystemPropertyUtils {
* @see #PLACEHOLDER_SUFFIX
* @throws IllegalArgumentException if there is an unresolvable placeholder and the flag is false
*/
public static String resolvePlaceholders(final String text, boolean ignoreUnresolvablePlaceholders) {
public static String resolvePlaceholders(String text, boolean ignoreUnresolvablePlaceholders) {
PropertyPlaceholderHelper helper = (ignoreUnresolvablePlaceholders ? nonStrictHelper : strictHelper);
return helper.replacePlaceholders(text, new SystemPropertyPlaceholderResolver(text));
}