Browse Source

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

pull/23217/head
Juergen Hoeller 15 years ago
parent
commit
e195c39d3c
  1. 34
      org.springframework.core/src/main/java/org/springframework/core/io/ResourceEditor.java
  2. 26
      org.springframework.core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java
  3. 6
      org.springframework.core/src/main/java/org/springframework/util/SystemPropertyUtils.java
  4. 10
      org.springframework.core/src/test/java/org/springframework/core/io/ResourceEditorTests.java
  5. 37
      org.springframework.core/src/test/java/org/springframework/core/io/support/ResourceArrayPropertyEditorTests.java

34
org.springframework.core/src/main/java/org/springframework/core/io/ResourceEditor.java

@ -1,5 +1,5 @@ @@ -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; @@ -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; @@ -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 { @@ -68,18 +67,23 @@ public class ResourceEditor extends PropertyEditorSupport {
* @param resourceLoader the <code>ResourceLoader</code> to use
*/
public ResourceEditor(ResourceLoader resourceLoader) {
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
this.resourceLoader = resourceLoader;
this(resourceLoader, true);
}
/**
* Flag to determine if unresolvable placeholders in System properties
* @param ignoreUnresolvablePlaceholders
* 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 void setIgnoreUnresolvablePlaceholders(boolean ignoreUnresolvablePlaceholders) {
public ResourceEditor(ResourceLoader resourceLoader, boolean ignoreUnresolvablePlaceholders) {
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
this.resourceLoader = resourceLoader;
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
}
@Override
public void setAsText(String text) {
if (StringUtils.hasText(text)) {
@ -99,7 +103,7 @@ public class ResourceEditor extends PropertyEditorSupport { @@ -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);
}

26
org.springframework.core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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);
}
}

6
org.springframework.core/src/main/java/org/springframework/util/SystemPropertyUtils.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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 { @@ -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));
}

10
org.springframework.core/src/test/java/org/springframework/core/io/ResourceEditorTests.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -16,12 +16,9 @@ @@ -16,12 +16,9 @@
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 static org.junit.Assert.*;
import org.junit.Test;
/**
@ -77,8 +74,7 @@ public final class ResourceEditorTests { @@ -77,8 +74,7 @@ public final class ResourceEditorTests {
@Test(expected=IllegalArgumentException.class)
public void testStrictSystemPropertyReplacement() {
ResourceEditor editor = new ResourceEditor();
editor.setIgnoreUnresolvablePlaceholders(false);
PropertyEditor editor = new ResourceEditor(new DefaultResourceLoader(), false);
System.setProperty("test.prop", "foo");
try {
editor.setAsText("${test.prop}-${bar}");

37
org.springframework.core/src/test/java/org/springframework/core/io/support/ResourceArrayPropertyEditorTests.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@ @@ -16,6 +16,8 @@
package org.springframework.core.io.support;
import java.beans.PropertyEditor;
import static org.junit.Assert.*;
import org.junit.Test;
@ -23,13 +25,13 @@ import org.springframework.core.io.Resource; @@ -23,13 +25,13 @@ import org.springframework.core.io.Resource;
/**
* @author Dave Syer
* @author Juergen Hoeller
*/
public class ResourceArrayPropertyEditorTests {
private ResourceArrayPropertyEditor editor = new ResourceArrayPropertyEditor();
@Test
public void testVanillaResource() throws Exception {
PropertyEditor editor = new ResourceArrayPropertyEditor();
editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class");
Resource[] resources = (Resource[]) editor.getValue();
assertNotNull(resources);
@ -42,10 +44,39 @@ public class ResourceArrayPropertyEditorTests { @@ -42,10 +44,39 @@ public class ResourceArrayPropertyEditorTests {
// 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
// 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");
Resource[] resources = (Resource[]) editor.getValue();
assertNotNull(resources);
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…
Cancel
Save