Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Overrides behavior on Apache commons-configuration so that the property-file include mechanism "overwrites" older values
package com.technofovea.springtest;
import java.io.File;
import java.lang.reflect.Array;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
/**
* Functions just like PropertiesConfiguration, except that when trying
* to retrieve a scalar value from an array or collection, the *last* element is
* produced instead of the first.
*
* This means that later attempts to set a property will override earlier uses,
* instead of being hidden.
*
* @author Darien Hager
*/
public class OverridablePropertiesConfiguration extends PropertiesConfiguration implements Configuration{
public OverridablePropertiesConfiguration(URL url) throws ConfigurationException {
super(url);
}
public OverridablePropertiesConfiguration(File file) throws ConfigurationException {
super(file);
}
public OverridablePropertiesConfiguration(String fileName) throws ConfigurationException {
super(fileName);
}
public OverridablePropertiesConfiguration() {
super();
}
@Override
protected Object resolveContainerStore(String key)
{
Object value = getProperty(key);
if (value != null)
{
if (value instanceof Collection)
{
Collection collection = (Collection) value;
Iterator i = collection.iterator();
value = null;
while(i.hasNext()){
// Leave with *last* value or null
value = i.next();
}
}
else if (value.getClass().isArray() && Array.getLength(value) > 0)
{
// Pull back *last* value
value = Array.get(value, Array.getLength(value)-1);
}
}
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment