From 587a81617c79e5647f9d41cad2f5d43b6a0068f3 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 24 Sep 2014 17:56:38 +0200 Subject: [PATCH] SystemEnvironmentPropertySource uses actual SecurityManager check and direct keySet access Issue: SPR-12224 --- .../env/SystemEnvironmentPropertySource.java | 62 +++++++------------ .../SystemEnvironmentPropertySourceTests.java | 19 +++++- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/env/SystemEnvironmentPropertySource.java b/spring-core/src/main/java/org/springframework/core/env/SystemEnvironmentPropertySource.java index c7f0fd6f74..d60d3b50d1 100644 --- a/spring-core/src/main/java/org/springframework/core/env/SystemEnvironmentPropertySource.java +++ b/spring-core/src/main/java/org/springframework/core/env/SystemEnvironmentPropertySource.java @@ -19,7 +19,6 @@ package org.springframework.core.env; import java.util.Map; import org.springframework.util.Assert; -import org.springframework.util.ObjectUtils; /** * Specialization of {@link MapPropertySource} designed for use with @@ -56,6 +55,7 @@ import org.springframework.util.ObjectUtils; * and all its subclasses. * * @author Chris Beams + * @author Juergen Hoeller * @since 3.1 * @see StandardEnvironment * @see AbstractEnvironment#getSystemEnvironment() @@ -63,9 +63,6 @@ import org.springframework.util.ObjectUtils; */ public class SystemEnvironmentPropertySource extends MapPropertySource { - /** if SecurityManager scenarios mean that property access should be via getPropertyNames() */ - private boolean usePropertyNames; - /** * Create a new {@code SystemEnvironmentPropertySource} with the given name and * delegating to the given {@code MapPropertySource}. @@ -105,48 +102,37 @@ public class SystemEnvironmentPropertySource extends MapPropertySource { */ private String resolvePropertyName(String name) { Assert.notNull(name, "Property name must not be null"); - try { - String[] propertyNames = (this.usePropertyNames ? getPropertyNames() : null); - if (containsProperty(propertyNames, name)) { - return name; - } - - String usName = name.replace('.', '_'); - if (!name.equals(usName) && containsProperty(propertyNames, usName)) { - return usName; - } - - String ucName = name.toUpperCase(); - if (!name.equals(ucName)) { - if (containsProperty(propertyNames, ucName)) { - return ucName; - } - else { - String usUcName = ucName.replace('.', '_'); - if (!ucName.equals(usUcName) && containsProperty(propertyNames, usUcName)) { - return usUcName; - } - } - } - + if (containsKey(name)) { return name; } - catch (RuntimeException ex) { - if (this.usePropertyNames) { - throw ex; + + String usName = name.replace('.', '_'); + if (!name.equals(usName) && containsKey(usName)) { + return usName; + } + + String ucName = name.toUpperCase(); + if (!name.equals(ucName)) { + if (containsKey(ucName)) { + return ucName; } else { - this.usePropertyNames = true; - return resolvePropertyName(name); + String usUcName = ucName.replace('.', '_'); + if (!ucName.equals(usUcName) && containsKey(usUcName)) { + return usUcName; + } } } + + return name; } - private boolean containsProperty(String[] propertyNames, String name) { - if (propertyNames == null) { - return super.containsProperty(name); - } - return ObjectUtils.containsElement(propertyNames, name); + private boolean containsKey(String name) { + return (isSecurityManagerPresent() ? this.source.keySet().contains(name) : this.source.containsKey(name)); + } + + protected boolean isSecurityManagerPresent() { + return (System.getSecurityManager() != null); } } diff --git a/spring-core/src/test/java/org/springframework/core/env/SystemEnvironmentPropertySourceTests.java b/spring-core/src/test/java/org/springframework/core/env/SystemEnvironmentPropertySourceTests.java index 9c99c5579a..8926161a6d 100644 --- a/spring-core/src/test/java/org/springframework/core/env/SystemEnvironmentPropertySourceTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/SystemEnvironmentPropertySourceTests.java @@ -17,7 +17,9 @@ package org.springframework.core.env; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import org.junit.Before; import org.junit.Test; @@ -29,19 +31,23 @@ import static org.junit.Assert.*; * Unit tests for {@link SystemEnvironmentPropertySource}. * * @author Chris Beams + * @author Juergen Hoeller * @since 3.1 */ public class SystemEnvironmentPropertySourceTests { private Map envMap; + private PropertySource ps; + @Before public void setUp() { envMap = new HashMap(); ps = new SystemEnvironmentPropertySource("sysEnv", envMap); } + @Test public void none() { assertThat(ps.containsProperty("a.key"), equalTo(false)); @@ -107,9 +113,20 @@ public class SystemEnvironmentPropertySourceTests { public boolean containsKey(Object key) { throw new UnsupportedOperationException(); } + @Override + public Set keySet() { + return new HashSet(super.keySet()); + } }; - ps = new SystemEnvironmentPropertySource("sysEnv", envMap); envMap.put("A_KEY", "a_value"); + + ps = new SystemEnvironmentPropertySource("sysEnv", envMap) { + @Override + protected boolean isSecurityManagerPresent() { + return true; + } + }; + assertThat(ps.containsProperty("A_KEY"), equalTo(true)); assertThat(ps.getProperty("A_KEY"), equalTo((Object)"a_value")); }