SystemEnvironmentPropertySource uses actual SecurityManager check and direct keySet access
Issue: SPR-12224
This commit is contained in:
parent
0934751d7a
commit
587a81617c
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<String, Object> envMap;
|
||||
|
||||
private PropertySource<?> ps;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
envMap = new HashMap<String, Object>();
|
||||
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<String> keySet() {
|
||||
return new HashSet<String>(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"));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue