Skip to content

Instantly share code, notes, and snippets.

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.lang.reflect.Array;
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 {
public OverridablePropertiesConfiguration(File file) throws ConfigurationException {
public OverridablePropertiesConfiguration(String fileName) throws ConfigurationException {
public OverridablePropertiesConfiguration() {
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;
// Leave with *last* value or null
value =;
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