unresolvable placeholders will be ignored by default for Resource array properties as well (SPR-6654)
This commit is contained in:
parent
7cbd9e1d93
commit
e195c39d3c
|
|
@ -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");
|
* 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.
|
||||||
|
|
@ -30,11 +30,9 @@ import org.springframework.util.SystemPropertyUtils;
|
||||||
* <code>"classpath:myfile.txt"</code>) to <code>Resource</code>
|
* <code>"classpath:myfile.txt"</code>) to <code>Resource</code>
|
||||||
* properties instead of using a <code>String</code> location property.
|
* properties instead of using a <code>String</code> location property.
|
||||||
*
|
*
|
||||||
* <p>The path may contain <code>${...}</code> placeholders, to be resolved
|
* <p>The path may contain <code>${...}</code> placeholders,
|
||||||
* as system properties: e.g. <code>${user.dir}</code>. By default unresolvable
|
* to be resolved as system properties: e.g. <code>${user.dir}</code>.
|
||||||
* placeholders are ignored, but if an exception is preferred set the
|
* Unresolvable placeholder are ignored by default.
|
||||||
* {@link #setIgnoreUnresolvablePlaceholders(boolean) ignoreUnresolvablePlaceholders}
|
|
||||||
* flag to false.
|
|
||||||
*
|
*
|
||||||
* <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}.
|
||||||
|
|
@ -52,7 +50,8 @@ public class ResourceEditor extends PropertyEditorSupport {
|
||||||
|
|
||||||
private final ResourceLoader resourceLoader;
|
private final ResourceLoader resourceLoader;
|
||||||
|
|
||||||
private boolean ignoreUnresolvablePlaceholders = true;
|
private final boolean ignoreUnresolvablePlaceholders;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance of the {@link ResourceEditor} class
|
* 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
|
* @param resourceLoader the <code>ResourceLoader</code> to use
|
||||||
*/
|
*/
|
||||||
public ResourceEditor(ResourceLoader resourceLoader) {
|
public ResourceEditor(ResourceLoader resourceLoader) {
|
||||||
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
|
this(resourceLoader, true);
|
||||||
this.resourceLoader = resourceLoader;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flag to determine if unresolvable placeholders in System properties
|
* Create a new instance of the {@link ResourceEditor} class
|
||||||
* @param ignoreUnresolvablePlaceholders
|
* 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 void setIgnoreUnresolvablePlaceholders(boolean ignoreUnresolvablePlaceholders) {
|
public ResourceEditor(ResourceLoader resourceLoader, boolean ignoreUnresolvablePlaceholders) {
|
||||||
|
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
|
||||||
|
this.resourceLoader = resourceLoader;
|
||||||
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
|
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAsText(String text) {
|
public void setAsText(String text) {
|
||||||
if (StringUtils.hasText(text)) {
|
if (StringUtils.hasText(text)) {
|
||||||
|
|
@ -99,7 +103,7 @@ public class ResourceEditor extends PropertyEditorSupport {
|
||||||
* @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
|
* @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
|
||||||
*/
|
*/
|
||||||
protected String resolvePath(String path) {
|
protected String resolvePath(String path) {
|
||||||
return SystemPropertyUtils.resolvePlaceholders(path, ignoreUnresolvablePlaceholders);
|
return SystemPropertyUtils.resolvePlaceholders(path, this.ignoreUnresolvablePlaceholders);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* 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.
|
||||||
|
|
@ -33,8 +33,9 @@ import org.springframework.util.SystemPropertyUtils;
|
||||||
* to <code>Resource</code> array properties. Can also translate a collection
|
* to <code>Resource</code> array properties. Can also translate a collection
|
||||||
* or array of location patterns into a merged Resource array.
|
* or array of location patterns into a merged Resource array.
|
||||||
*
|
*
|
||||||
* <p>The path may contain <code>${...}</code> placeholders, to be resolved
|
* <p>The path may contain <code>${...}</code> placeholders,
|
||||||
* as system properties: e.g. <code>${user.dir}</code>.
|
* to be resolved as system properties: e.g. <code>${user.dir}</code>.
|
||||||
|
* Unresolvable placeholder are ignored by default.
|
||||||
*
|
*
|
||||||
* <p>Delegates to a {@link ResourcePatternResolver},
|
* <p>Delegates to a {@link ResourcePatternResolver},
|
||||||
* by default using a {@link PathMatchingResourcePatternResolver}.
|
* by default using a {@link PathMatchingResourcePatternResolver}.
|
||||||
|
|
@ -51,6 +52,8 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
|
||||||
|
|
||||||
private final ResourcePatternResolver resourcePatternResolver;
|
private final ResourcePatternResolver resourcePatternResolver;
|
||||||
|
|
||||||
|
private final boolean ignoreUnresolvablePlaceholders;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new ResourceArrayPropertyEditor with a default
|
* Create a new ResourceArrayPropertyEditor with a default
|
||||||
|
|
@ -58,7 +61,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
|
||||||
* @see PathMatchingResourcePatternResolver
|
* @see PathMatchingResourcePatternResolver
|
||||||
*/
|
*/
|
||||||
public ResourceArrayPropertyEditor() {
|
public ResourceArrayPropertyEditor() {
|
||||||
this.resourcePatternResolver = new PathMatchingResourcePatternResolver();
|
this(new PathMatchingResourcePatternResolver());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -66,7 +69,18 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
|
||||||
* @param resourcePatternResolver the ResourcePatternResolver to use
|
* @param resourcePatternResolver the ResourcePatternResolver to use
|
||||||
*/
|
*/
|
||||||
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver) {
|
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.resourcePatternResolver = resourcePatternResolver;
|
||||||
|
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -81,7 +95,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
throw new IllegalArgumentException(
|
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
|
* @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
|
||||||
*/
|
*/
|
||||||
protected String resolvePath(String path) {
|
protected String resolvePath(String path) {
|
||||||
return SystemPropertyUtils.resolvePlaceholders(path);
|
return SystemPropertyUtils.resolvePlaceholders(path, this.ignoreUnresolvablePlaceholders);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* 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.
|
||||||
|
|
@ -60,7 +60,7 @@ public abstract class SystemPropertyUtils {
|
||||||
* @see #PLACEHOLDER_SUFFIX
|
* @see #PLACEHOLDER_SUFFIX
|
||||||
* @throws IllegalArgumentException if there is an unresolvable placeholder
|
* @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);
|
return resolvePlaceholders(text, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,7 +75,7 @@ public abstract class SystemPropertyUtils {
|
||||||
* @see #PLACEHOLDER_SUFFIX
|
* @see #PLACEHOLDER_SUFFIX
|
||||||
* @throws IllegalArgumentException if there is an unresolvable placeholder and the flag is false
|
* @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);
|
PropertyPlaceholderHelper helper = (ignoreUnresolvablePlaceholders ? nonStrictHelper : strictHelper);
|
||||||
return helper.replacePlaceholders(text, new SystemPropertyPlaceholderResolver(text));
|
return helper.replacePlaceholders(text, new SystemPropertyPlaceholderResolver(text));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2006 the original author or authors.
|
* Copyright 2002-2010 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,12 +16,9 @@
|
||||||
|
|
||||||
package org.springframework.core.io;
|
package org.springframework.core.io;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.beans.PropertyEditor;
|
import java.beans.PropertyEditor;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -77,8 +74,7 @@ public final class ResourceEditorTests {
|
||||||
|
|
||||||
@Test(expected=IllegalArgumentException.class)
|
@Test(expected=IllegalArgumentException.class)
|
||||||
public void testStrictSystemPropertyReplacement() {
|
public void testStrictSystemPropertyReplacement() {
|
||||||
ResourceEditor editor = new ResourceEditor();
|
PropertyEditor editor = new ResourceEditor(new DefaultResourceLoader(), false);
|
||||||
editor.setIgnoreUnresolvablePlaceholders(false);
|
|
||||||
System.setProperty("test.prop", "foo");
|
System.setProperty("test.prop", "foo");
|
||||||
try {
|
try {
|
||||||
editor.setAsText("${test.prop}-${bar}");
|
editor.setAsText("${test.prop}-${bar}");
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2006 the original author or authors.
|
* Copyright 2002-2010 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,6 +16,8 @@
|
||||||
|
|
||||||
package org.springframework.core.io.support;
|
package org.springframework.core.io.support;
|
||||||
|
|
||||||
|
import java.beans.PropertyEditor;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
@ -23,13 +25,13 @@ import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
|
* @author Juergen Hoeller
|
||||||
*/
|
*/
|
||||||
public class ResourceArrayPropertyEditorTests {
|
public class ResourceArrayPropertyEditorTests {
|
||||||
|
|
||||||
private ResourceArrayPropertyEditor editor = new ResourceArrayPropertyEditor();
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testVanillaResource() throws Exception {
|
public void testVanillaResource() throws Exception {
|
||||||
|
PropertyEditor editor = new ResourceArrayPropertyEditor();
|
||||||
editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class");
|
editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class");
|
||||||
Resource[] resources = (Resource[]) editor.getValue();
|
Resource[] resources = (Resource[]) editor.getValue();
|
||||||
assertNotNull(resources);
|
assertNotNull(resources);
|
||||||
|
|
@ -42,10 +44,39 @@ public class ResourceArrayPropertyEditorTests {
|
||||||
// The result depends on the classpath - if test-classes are segregated from classes
|
// The result depends on the classpath - if test-classes are segregated from classes
|
||||||
// and they come first on the classpath (like in Maven) then it breaks, if classes
|
// and they come first on the classpath (like in Maven) then it breaks, if classes
|
||||||
// comes first (like in Spring Build) then it is OK.
|
// comes first (like in Spring Build) then it is OK.
|
||||||
|
PropertyEditor editor = new ResourceArrayPropertyEditor();
|
||||||
editor.setAsText("classpath*:org/springframework/core/io/support/Resource*Editor.class");
|
editor.setAsText("classpath*:org/springframework/core/io/support/Resource*Editor.class");
|
||||||
Resource[] resources = (Resource[]) editor.getValue();
|
Resource[] resources = (Resource[]) editor.getValue();
|
||||||
assertNotNull(resources);
|
assertNotNull(resources);
|
||||||
assertTrue(resources[0].exists());
|
assertTrue(resources[0].exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSystemPropertyReplacement() {
|
||||||
|
PropertyEditor editor = new ResourceArrayPropertyEditor();
|
||||||
|
System.setProperty("test.prop", "foo");
|
||||||
|
try {
|
||||||
|
editor.setAsText("${test.prop}-${bar}");
|
||||||
|
Resource[] resources = (Resource[]) editor.getValue();
|
||||||
|
assertEquals("foo-${bar}", resources[0].getFilename());
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
System.getProperties().remove("test.prop");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void testStrictSystemPropertyReplacement() {
|
||||||
|
PropertyEditor editor = new ResourceArrayPropertyEditor(new PathMatchingResourcePatternResolver(), false);
|
||||||
|
System.setProperty("test.prop", "foo");
|
||||||
|
try {
|
||||||
|
editor.setAsText("${test.prop}-${bar}");
|
||||||
|
Resource[] resources = (Resource[]) editor.getValue();
|
||||||
|
assertEquals("foo-${bar}", resources[0].getFilename());
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
System.getProperties().remove("test.prop");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue