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 java.util.Map;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ObjectUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specialization of {@link MapPropertySource} designed for use with
|
* Specialization of {@link MapPropertySource} designed for use with
|
||||||
|
@ -56,6 +55,7 @@ import org.springframework.util.ObjectUtils;
|
||||||
* and all its subclasses.
|
* and all its subclasses.
|
||||||
*
|
*
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
|
* @author Juergen Hoeller
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
* @see StandardEnvironment
|
* @see StandardEnvironment
|
||||||
* @see AbstractEnvironment#getSystemEnvironment()
|
* @see AbstractEnvironment#getSystemEnvironment()
|
||||||
|
@ -63,9 +63,6 @@ import org.springframework.util.ObjectUtils;
|
||||||
*/
|
*/
|
||||||
public class SystemEnvironmentPropertySource extends MapPropertySource {
|
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
|
* Create a new {@code SystemEnvironmentPropertySource} with the given name and
|
||||||
* delegating to the given {@code MapPropertySource}.
|
* delegating to the given {@code MapPropertySource}.
|
||||||
|
@ -105,48 +102,37 @@ public class SystemEnvironmentPropertySource extends MapPropertySource {
|
||||||
*/
|
*/
|
||||||
private String resolvePropertyName(String name) {
|
private String resolvePropertyName(String name) {
|
||||||
Assert.notNull(name, "Property name must not be null");
|
Assert.notNull(name, "Property name must not be null");
|
||||||
try {
|
if (containsKey(name)) {
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
catch (RuntimeException ex) {
|
|
||||||
if (this.usePropertyNames) {
|
String usName = name.replace('.', '_');
|
||||||
throw ex;
|
if (!name.equals(usName) && containsKey(usName)) {
|
||||||
|
return usName;
|
||||||
|
}
|
||||||
|
|
||||||
|
String ucName = name.toUpperCase();
|
||||||
|
if (!name.equals(ucName)) {
|
||||||
|
if (containsKey(ucName)) {
|
||||||
|
return ucName;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.usePropertyNames = true;
|
String usUcName = ucName.replace('.', '_');
|
||||||
return resolvePropertyName(name);
|
if (!ucName.equals(usUcName) && containsKey(usUcName)) {
|
||||||
|
return usUcName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean containsProperty(String[] propertyNames, String name) {
|
private boolean containsKey(String name) {
|
||||||
if (propertyNames == null) {
|
return (isSecurityManagerPresent() ? this.source.keySet().contains(name) : this.source.containsKey(name));
|
||||||
return super.containsProperty(name);
|
}
|
||||||
}
|
|
||||||
return ObjectUtils.containsElement(propertyNames, name);
|
protected boolean isSecurityManagerPresent() {
|
||||||
|
return (System.getSecurityManager() != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,9 @@
|
||||||
package org.springframework.core.env;
|
package org.springframework.core.env;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -29,19 +31,23 @@ import static org.junit.Assert.*;
|
||||||
* Unit tests for {@link SystemEnvironmentPropertySource}.
|
* Unit tests for {@link SystemEnvironmentPropertySource}.
|
||||||
*
|
*
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
|
* @author Juergen Hoeller
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
*/
|
*/
|
||||||
public class SystemEnvironmentPropertySourceTests {
|
public class SystemEnvironmentPropertySourceTests {
|
||||||
|
|
||||||
private Map<String, Object> envMap;
|
private Map<String, Object> envMap;
|
||||||
|
|
||||||
private PropertySource<?> ps;
|
private PropertySource<?> ps;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
envMap = new HashMap<String, Object>();
|
envMap = new HashMap<String, Object>();
|
||||||
ps = new SystemEnvironmentPropertySource("sysEnv", envMap);
|
ps = new SystemEnvironmentPropertySource("sysEnv", envMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void none() {
|
public void none() {
|
||||||
assertThat(ps.containsProperty("a.key"), equalTo(false));
|
assertThat(ps.containsProperty("a.key"), equalTo(false));
|
||||||
|
@ -107,9 +113,20 @@ public class SystemEnvironmentPropertySourceTests {
|
||||||
public boolean containsKey(Object key) {
|
public boolean containsKey(Object key) {
|
||||||
throw new UnsupportedOperationException();
|
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");
|
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.containsProperty("A_KEY"), equalTo(true));
|
||||||
assertThat(ps.getProperty("A_KEY"), equalTo((Object)"a_value"));
|
assertThat(ps.getProperty("A_KEY"), equalTo((Object)"a_value"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue