[LANG-1141] StrLookup for system properties now sees updated values.
The lookup implementation now directly accesses system properties without caching the Properties object in any way.
This commit is contained in:
parent
0343b4fda8
commit
54e6300544
|
@ -16,9 +16,7 @@
|
|||
*/
|
||||
package org.apache.commons.lang3.text;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Lookup a String key to a String value.
|
||||
|
@ -42,6 +40,11 @@ public abstract class StrLookup<V> {
|
|||
*/
|
||||
private static final StrLookup<String> NONE_LOOKUP = new MapStrLookup<String>(null);
|
||||
|
||||
/**
|
||||
* Lookup based on system properties.
|
||||
*/
|
||||
private static final StrLookup<String> SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup();
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Returns a lookup which always returns null.
|
||||
|
@ -52,29 +55,6 @@ public abstract class StrLookup<V> {
|
|||
return NONE_LOOKUP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of the given properties instance.
|
||||
*
|
||||
* @param input the Properties instance to copy.
|
||||
* @return a copy of {@code input}.
|
||||
*/
|
||||
private static Properties copyProperties(Properties input) {
|
||||
if (input == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Properties output = new Properties();
|
||||
@SuppressWarnings("unchecked") // Property names are Strings.
|
||||
Enumeration<String> propertyNames = (Enumeration<String>) input.propertyNames();
|
||||
|
||||
while (propertyNames.hasMoreElements()) {
|
||||
String propertyName = propertyNames.nextElement();
|
||||
output.setProperty(propertyName, input.getProperty(propertyName));
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new lookup which uses a copy of the current
|
||||
* {@link System#getProperties() System properties}.
|
||||
|
@ -87,19 +67,7 @@ public abstract class StrLookup<V> {
|
|||
* @return a lookup using system properties, not null
|
||||
*/
|
||||
public static StrLookup<String> systemPropertiesLookup() {
|
||||
Properties systemProperties = null;
|
||||
|
||||
try {
|
||||
systemProperties = System.getProperties();
|
||||
} catch (final SecurityException ex) {
|
||||
// Squelched. All lookup(String) will return null.
|
||||
}
|
||||
|
||||
Properties properties = copyProperties(systemProperties);
|
||||
@SuppressWarnings("unchecked") // System property keys and values are always Strings
|
||||
final Map<String, String> propertiesMap = (Map) properties;
|
||||
|
||||
return new MapStrLookup<String>(propertiesMap);
|
||||
return SYSTEM_PROPERTIES_LOOKUP;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -188,4 +156,25 @@ public abstract class StrLookup<V> {
|
|||
return obj.toString();
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Lookup implementation based on system properties.
|
||||
*/
|
||||
private static class SystemPropertiesStrLookup extends StrLookup<String> {
|
||||
/**
|
||||
* {@inheritDoc} This implementation directly accesses system properties.
|
||||
*/
|
||||
@Override
|
||||
public String lookup(String key) {
|
||||
if (key.length() > 0) {
|
||||
try {
|
||||
return System.getProperty(key);
|
||||
} catch (SecurityException scex) {
|
||||
// Squelched. All lookup(String) will return null.
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
@ -22,6 +22,7 @@ import static org.junit.Assert.fail;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -51,20 +52,45 @@ public class StrLookupTest {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that a lookup object for system properties can deal with a full
|
||||
* replacement of the system properties object. This test is related to
|
||||
* LANG-1055.
|
||||
*/
|
||||
@Test
|
||||
public void testSystemPropertiesLookupNotSingleton() {
|
||||
public void testSystemPropertiesLookupReplacedProperties() {
|
||||
Properties oldProperties = System.getProperties();
|
||||
final String osName = "os.name";
|
||||
final String originalOsName = System.getProperty(osName);
|
||||
final String newOsName = oldProperties.getProperty(osName) + "_changed";
|
||||
|
||||
StrLookup<String> properties1 = StrLookup.systemPropertiesLookup();
|
||||
assertEquals(originalOsName, properties1.lookup(osName));
|
||||
StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
|
||||
Properties newProps = new Properties();
|
||||
newProps.setProperty(osName, newOsName);
|
||||
System.setProperties(newProps);
|
||||
try {
|
||||
assertEquals("Changed properties not detected", newOsName, sysLookup.lookup(osName));
|
||||
} finally {
|
||||
System.setProperties(oldProperties);
|
||||
}
|
||||
}
|
||||
|
||||
final String differentOsName = "HAL-9000";
|
||||
System.setProperty(osName, differentOsName);
|
||||
StrLookup<String> properties2 = StrLookup.systemPropertiesLookup();
|
||||
/**
|
||||
* Tests that a lookup object for system properties sees changes on system
|
||||
* properties. This test is related to LANG-1141.
|
||||
*/
|
||||
@Test
|
||||
public void testSystemPropertiesLookupUpdatedProperty() {
|
||||
final String osName = "os.name";
|
||||
String oldOs = System.getProperty(osName);
|
||||
final String newOsName = oldOs + "_changed";
|
||||
|
||||
assertEquals(originalOsName, properties1.lookup(osName));
|
||||
assertEquals(differentOsName, properties2.lookup(osName));
|
||||
StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
|
||||
System.setProperty(osName, newOsName);
|
||||
try {
|
||||
assertEquals("Changed properties not detected", newOsName, sysLookup.lookup(osName));
|
||||
} finally {
|
||||
System.setProperty(osName, oldOs);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue