From 546d9968d96359f02cadae9482a35fc099c5cc12 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 21 Apr 2010 09:22:20 +0000 Subject: [PATCH] property placeholders can deal with nested expressions which happen to use the same suffix (SPR-7098) git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3282 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../util/PropertyPlaceholderHelper.java | 26 ++++++++-- .../util/SystemPropertyUtilsTests.java | 50 ++++++++++++++++++- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java b/org.springframework.core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java index d0fffbe2c71..12d2768ad60 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java +++ b/org.springframework.core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.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. @@ -16,7 +16,9 @@ package org.springframework.util; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Properties; import java.util.Set; @@ -37,10 +39,21 @@ public class PropertyPlaceholderHelper { private static final Log logger = LogFactory.getLog(PropertyPlaceholderHelper.class); + private static final Map wellKnownSimplePrefixes = new HashMap(4); + + static { + wellKnownSimplePrefixes.put("}", "{"); + wellKnownSimplePrefixes.put("]", "["); + wellKnownSimplePrefixes.put(")", "("); + } + + private final String placeholderPrefix; private final String placeholderSuffix; + private final String simplePrefix; + private final String valueSeparator; private final boolean ignoreUnresolvablePlaceholders; @@ -70,6 +83,13 @@ public class PropertyPlaceholderHelper { Assert.notNull(placeholderSuffix, "placeholderSuffix must not be null"); this.placeholderPrefix = placeholderPrefix; this.placeholderSuffix = placeholderSuffix; + String simplePrefixForSuffix = wellKnownSimplePrefixes.get(this.placeholderSuffix); + if (simplePrefixForSuffix != null && this.placeholderPrefix.endsWith(simplePrefixForSuffix)) { + this.simplePrefix = simplePrefixForSuffix; + } + else { + this.simplePrefix = this.placeholderPrefix; + } this.valueSeparator = valueSeparator; this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders; } @@ -176,9 +196,9 @@ public class PropertyPlaceholderHelper { return index; } } - else if (StringUtils.substringMatch(buf, index, this.placeholderPrefix)) { + else if (StringUtils.substringMatch(buf, index, this.simplePrefix)) { withinNestedPlaceholder++; - index = index + this.placeholderPrefix.length(); + index = index + this.simplePrefix.length(); } else { index++; diff --git a/org.springframework.core/src/test/java/org/springframework/util/SystemPropertyUtilsTests.java b/org.springframework.core/src/test/java/org/springframework/util/SystemPropertyUtilsTests.java index 7f29c2601d7..d1fb6f8ffa0 100644 --- a/org.springframework.core/src/test/java/org/springframework/util/SystemPropertyUtilsTests.java +++ b/org.springframework.core/src/test/java/org/springframework/util/SystemPropertyUtilsTests.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. @@ -39,12 +39,60 @@ public class SystemPropertyUtilsTests { } } + @Test + public void testReplaceFromSystemPropertyWithDefault() { + System.setProperty("test.prop", "bar"); + try { + String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:foo}"); + assertEquals("bar", resolved); + } + finally { + System.getProperties().remove("test.prop"); + } + } + + @Test + public void testReplaceFromSystemPropertyWithExpressionDefault() { + System.setProperty("test.prop", "bar"); + try { + String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:#{foo.bar}}"); + assertEquals("bar", resolved); + } + finally { + System.getProperties().remove("test.prop"); + } + } + + @Test + public void testReplaceFromSystemPropertyWithExpressionContainingDefault() { + System.setProperty("test.prop", "bar"); + try { + String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:Y#{foo.bar}X}"); + assertEquals("bar", resolved); + } + finally { + System.getProperties().remove("test.prop"); + } + } + @Test public void testReplaceWithDefault() { String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:foo}"); assertEquals("foo", resolved); } + @Test + public void testReplaceWithExpressionDefault() { + String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:#{foo.bar}}"); + assertEquals("#{foo.bar}", resolved); + } + + @Test + public void testReplaceWithExpressionContainingDefault() { + String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:Y#{foo.bar}X}"); + assertEquals("Y#{foo.bar}X", resolved); + } + @Test(expected=IllegalArgumentException.class) public void testReplaceWithNoDefault() { String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop}");