From 05bcc4028d63fe8896ca7959a47f0d17ae14ce51 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Mon, 13 Sep 2010 20:23:26 +0000 Subject: [PATCH] Fix assumption about file-based Resources in PropertiesLoaderSupport (SPR-7547) When using PropertiesLoaderSupport implementations (principally PropertyPlaceholderConfigurer), an assumption was made that any Resource representing a set of properties must be file-based. SPR-7547 exposed the fact that if a non-file-based Resource implementation such as ByteArrayResource were passed in, an IllegalStateException would be thrown from the AbstractResource base class' implementation of getFilename(). This is now patched, and PropertiesLoaderSupport implementations treat Resource implementations equally, regardless of file-orientation. See also SPR-7552. --- .../PropertyPlaceholderConfigurerTests.java | 44 +++++++++++++++++++ .../io/support/PropertiesLoaderSupport.java | 10 ++++- spring-framework-reference/src/images | 1 + 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 org.springframework.beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java create mode 120000 spring-framework-reference/src/images diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java new file mode 100644 index 00000000000..108a38b2a59 --- /dev/null +++ b/org.springframework.beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java @@ -0,0 +1,44 @@ +package org.springframework.beans.factory.config; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; + +import org.junit.Test; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.core.io.ByteArrayResource; + +import test.beans.TestBean; + +/** + * Tests cornering SPR-7547. + * + * @author Chris Beams + */ +public class PropertyPlaceholderConfigurerTests { + + /** + * Prior to the fix for SPR-7547, the following would throw + * IllegalStateException because the PropertiesLoaderSupport base class + * assumed ByteArrayResource implements Resource.getFilename(). It does + * not, and AbstractResource.getFilename() is called instead, raising the + * exception. The following now works, as getFilename() is called in a + * try/catch to check whether the resource is actually file-based or not. + * + * See SPR-7552, which suggests paths to address the root issue rather than + * just patching the problem. + */ + @Test + public void repro() { + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + bf.registerBeanDefinition("testBean", + rootBeanDefinition(TestBean.class) + .addPropertyValue("name", "${my.name}").getBeanDefinition()); + PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); + ppc.setLocation(new ByteArrayResource("my.name=Inigo Montoya".getBytes())); + ppc.postProcessBeanFactory(bf); + + TestBean testBean = bf.getBean(TestBean.class); + assertThat(testBean.getName(), equalTo("Inigo Montoya")); + } +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java b/org.springframework.core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java index 640780e4bce..65f80cec55c 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java @@ -179,7 +179,15 @@ public abstract class PropertiesLoaderSupport { InputStream is = null; try { is = location.getInputStream(); - if (location.getFilename().endsWith(XML_FILE_EXTENSION)) { + + String filename = null; + try { + filename = location.getFilename(); + } catch (IllegalStateException ex) { + // resource is not file-based. See SPR-7552. + } + + if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) { this.propertiesPersister.loadFromXml(props, is); } else { diff --git a/spring-framework-reference/src/images b/spring-framework-reference/src/images new file mode 120000 index 00000000000..5e67573196d --- /dev/null +++ b/spring-framework-reference/src/images @@ -0,0 +1 @@ +../images \ No newline at end of file