[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;
|
package org.apache.commons.lang3.text;
|
||||||
|
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lookup a String key to a String value.
|
* 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);
|
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.
|
* Returns a lookup which always returns null.
|
||||||
|
@ -52,29 +55,6 @@ public abstract class StrLookup<V> {
|
||||||
return NONE_LOOKUP;
|
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
|
* Returns a new lookup which uses a copy of the current
|
||||||
* {@link System#getProperties() System properties}.
|
* {@link System#getProperties() System properties}.
|
||||||
|
@ -87,19 +67,7 @@ public abstract class StrLookup<V> {
|
||||||
* @return a lookup using system properties, not null
|
* @return a lookup using system properties, not null
|
||||||
*/
|
*/
|
||||||
public static StrLookup<String> systemPropertiesLookup() {
|
public static StrLookup<String> systemPropertiesLookup() {
|
||||||
Properties systemProperties = null;
|
return SYSTEM_PROPERTIES_LOOKUP;
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -188,4 +156,25 @@ public abstract class StrLookup<V> {
|
||||||
return obj.toString();
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.junit.Test;
|
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
|
@Test
|
||||||
public void testSystemPropertiesLookupNotSingleton() {
|
public void testSystemPropertiesLookupReplacedProperties() {
|
||||||
|
Properties oldProperties = System.getProperties();
|
||||||
final String osName = "os.name";
|
final String osName = "os.name";
|
||||||
final String originalOsName = System.getProperty(osName);
|
final String newOsName = oldProperties.getProperty(osName) + "_changed";
|
||||||
|
|
||||||
StrLookup<String> properties1 = StrLookup.systemPropertiesLookup();
|
StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
|
||||||
assertEquals(originalOsName, properties1.lookup(osName));
|
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);
|
* Tests that a lookup object for system properties sees changes on system
|
||||||
StrLookup<String> properties2 = StrLookup.systemPropertiesLookup();
|
* 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));
|
StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
|
||||||
assertEquals(differentOsName, properties2.lookup(osName));
|
System.setProperty(osName, newOsName);
|
||||||
|
try {
|
||||||
|
assertEquals("Changed properties not detected", newOsName, sysLookup.lookup(osName));
|
||||||
|
} finally {
|
||||||
|
System.setProperty(osName, oldOs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue