From e195c39d3c95c27aaeaff61c20f97648a0b363d1 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 12 Jan 2010 19:50:18 +0000 Subject: [PATCH] unresolvable placeholders will be ignored by default for Resource array properties as well (SPR-6654) --- .../core/io/ResourceEditor.java | 36 ++++++++++-------- .../support/ResourceArrayPropertyEditor.java | 26 ++++++++++--- .../util/SystemPropertyUtils.java | 6 +-- .../core/io/ResourceEditorTests.java | 10 ++--- .../ResourceArrayPropertyEditorTests.java | 37 +++++++++++++++++-- 5 files changed, 80 insertions(+), 35 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/ResourceEditor.java b/org.springframework.core/src/main/java/org/springframework/core/io/ResourceEditor.java index cfbb20b1d57..710a4fb275e 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/ResourceEditor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/ResourceEditor.java @@ -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; * "classpath:myfile.txt") to Resource * properties instead of using a String location property. * - *

The path may contain ${...} placeholders, to be resolved - * as system properties: e.g. ${user.dir}. By default unresolvable - * placeholders are ignored, but if an exception is preferred set the - * {@link #setIgnoreUnresolvablePlaceholders(boolean) ignoreUnresolvablePlaceholders} - * flag to false. + *

The path may contain ${...} placeholders, + * to be resolved as system properties: e.g. ${user.dir}. + * Unresolvable placeholder are ignored by default. * *

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 ResourceLoader 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 ResourceLoader 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); } diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java b/org.springframework.core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java index 358941ff346..a087d4a5f9e 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java @@ -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 Resource array properties. Can also translate a collection * or array of location patterns into a merged Resource array. * - *

The path may contain ${...} placeholders, to be resolved - * as system properties: e.g. ${user.dir}. + *

The path may contain ${...} placeholders, + * to be resolved as system properties: e.g. ${user.dir}. + * Unresolvable placeholder are ignored by default. * *

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); } } diff --git a/org.springframework.core/src/main/java/org/springframework/util/SystemPropertyUtils.java b/org.springframework.core/src/main/java/org/springframework/util/SystemPropertyUtils.java index a99b31e7897..6e8eaff2f13 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/SystemPropertyUtils.java +++ b/org.springframework.core/src/main/java/org/springframework/util/SystemPropertyUtils.java @@ -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)); } diff --git a/org.springframework.core/src/test/java/org/springframework/core/io/ResourceEditorTests.java b/org.springframework.core/src/test/java/org/springframework/core/io/ResourceEditorTests.java index 7a06ab7c1ed..792115e643e 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/io/ResourceEditorTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/io/ResourceEditorTests.java @@ -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 @@ 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 { @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}"); diff --git a/org.springframework.core/src/test/java/org/springframework/core/io/support/ResourceArrayPropertyEditorTests.java b/org.springframework.core/src/test/java/org/springframework/core/io/support/ResourceArrayPropertyEditorTests.java index d861ac89de2..c12bd5b8e7e 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/io/support/ResourceArrayPropertyEditorTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/io/support/ResourceArrayPropertyEditorTests.java @@ -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 @@ 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; /** * @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 { // 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"); + } + } + }