com.netflix.config
Class ConcurrentCompositeConfiguration

java.lang.Object
  extended by org.apache.commons.configuration.event.EventSource
      extended by org.apache.commons.configuration.AbstractConfiguration
          extended by com.netflix.config.ConcurrentMapConfiguration
              extended by com.netflix.config.ConcurrentCompositeConfiguration
All Implemented Interfaces:
AggregatedConfiguration, java.lang.Cloneable, org.apache.commons.configuration.Configuration, org.apache.commons.configuration.event.ConfigurationListener

public class ConcurrentCompositeConfiguration
extends ConcurrentMapConfiguration
implements AggregatedConfiguration, org.apache.commons.configuration.event.ConfigurationListener, java.lang.Cloneable

This class maintains a hierarchy of configurations in a list structure. The order of the list stands for the descending priority of the configurations when a property value is to be determined. For example, if you add Configuration1, and then Configuration2, getProperty(String) will return any properties defined by Configuration1. Only if Configuration1 doesn't have the property, then Configuration2 will be checked.

There are two internal configurations for properties that are programmatically set: When adding configuration to this class, it is recommended to convert it into ConcurrentMapConfiguration or ConcurrentCompositeConfiguration using ConfigurationUtils to achieve maximal performance and thread safety.

Example:

   // configuration from local properties file
  String fileName = "...";
  ConcurrentMapConfiguration configFromPropertiesFile =
      new ConcurrentMapConfiguration(new PropertiesConfiguration(fileName));
  // configuration from system properties
  ConcurrentMapConfiguration configFromSystemProperties = 
      new ConcurrentMapConfiguration(new SystemConfiguration());
  // configuration from a dynamic source
  PolledConfigurationSource source = createMyOwnSource();
  AbstractPollingScheduler scheduler = createMyOwnScheduler();
  DynamicConfiguration dynamicConfiguration =
      new DynamicConfiguration(source, scheduler);

  // create a hierarchy of configuration that makes
  // 1) dynamic configuration source override system properties and,
  // 2) system properties override properties file
  ConcurrentCompositeConfiguration finalConfig = new ConcurrentCompositeConfiguration();
  finalConfig.add(dynamicConfiguration, "dynamicConfig");
  finalConfig.add(configFromSystemProperties, "systemConfig");
  finalConfig.add(configFromPropertiesFile, "fileConfig");

  // register with DynamicPropertyFactory so that finalConfig
  // becomes the source of dynamic properties
  DynamicPropertyFactory.initWithConfigurationSource(finalConfig);    
 


Field Summary
static int EVENT_CONFIGURATION_SOURCE_CHANGED
           
 
Fields inherited from class com.netflix.config.ConcurrentMapConfiguration
map
 
Fields inherited from class org.apache.commons.configuration.AbstractConfiguration
END_TOKEN, EVENT_ADD_PROPERTY, EVENT_CLEAR, EVENT_CLEAR_PROPERTY, EVENT_READ_PROPERTY, EVENT_SET_PROPERTY, START_TOKEN
 
Constructor Summary
ConcurrentCompositeConfiguration()
          Creates an empty CompositeConfiguration object which can then be added some other Configuration files
ConcurrentCompositeConfiguration(org.apache.commons.configuration.AbstractConfiguration containerConfiguration)
          Creates a ConcurrentCompositeConfiguration object with a specified container configuration.
ConcurrentCompositeConfiguration(org.apache.commons.configuration.AbstractConfiguration containerConfiguration, java.util.Collection<? extends org.apache.commons.configuration.AbstractConfiguration> configurations)
          Creates a ConcurrentCompositeConfiguration with a specified container configuration, and then adds the given collection of configurations.
 
Method Summary
 void addConfiguration(org.apache.commons.configuration.AbstractConfiguration config)
          Add a child configuration without a name.
 void addConfiguration(org.apache.commons.configuration.AbstractConfiguration config, java.lang.String name)
          Adds a new child configuration to this configuration with an optional name.
 void addConfigurationAtFront(org.apache.commons.configuration.AbstractConfiguration config, java.lang.String name)
           
 void addConfigurationAtIndex(org.apache.commons.configuration.AbstractConfiguration config, java.lang.String name, int index)
          Add a configuration with a name at a particular index.
 void addProperty(java.lang.String key, java.lang.Object value)
          Add the property with the container configuration.
 void clear()
          Removes all child configurations and reinitializes the container configuration.
 void clearOverrideProperty(java.lang.String key)
          Remove the overriding property set by setOverrideProperty(String, Object)
 void clearProperty(java.lang.String key)
          Clear the property with the container configuration.
 java.lang.Object clone()
          Returns a copy of this object.
 void configurationChanged(org.apache.commons.configuration.event.ConfigurationEvent event)
          Event listener call back for configuration update events.
 boolean containsKey(java.lang.String key)
          Check if the any of the sub configurations contains the specified key.
 org.apache.commons.configuration.Configuration getConfiguration(int index)
          Return the configuration at the specified index.
 org.apache.commons.configuration.Configuration getConfiguration(java.lang.String name)
          Returns the configuration with the given name.
 java.util.List<java.lang.String> getConfigurationNameList()
           
 java.util.Set<java.lang.String> getConfigurationNames()
          Returns a set with the names of all configurations contained in this configuration.
 java.util.List<org.apache.commons.configuration.AbstractConfiguration> getConfigurations()
          Get the configurations added.
 org.apache.commons.configuration.Configuration getContainerConfiguration()
          Returns the container configuration In this configuration changes are stored.
 int getIndexOfConfiguration(org.apache.commons.configuration.AbstractConfiguration config)
           
 int getIndexOfContainerConfiguration()
           
 java.util.Iterator<java.lang.String> getKeys()
          Get all the keys contained by sub configurations.
 java.util.Iterator<java.lang.String> getKeys(java.lang.String prefix)
          Get the list of the keys contained in the sub configurations that match the specified prefix.
 java.util.List getList(java.lang.String key, java.util.List defaultValue)
          Get a List of objects associated with the given configuration key.
 int getNumberOfConfigurations()
          Return the number of configurations.
 java.lang.Object getProperty(java.lang.String key)
          Read property from underlying composite.
 org.apache.commons.configuration.Configuration getSource(java.lang.String key)
          Returns the configuration source, in which the specified key is defined.
 java.lang.String[] getStringArray(java.lang.String key)
          Get an array of strings associated with the given configuration key.
 void invalidate()
           
 boolean isEmpty()
           
 boolean isPropagateEventFromSubConfigurations()
          Return whether sub configurations should propagate events to listeners to this configuration.
 boolean removeConfiguration(org.apache.commons.configuration.Configuration config)
          Remove a configuration.
 org.apache.commons.configuration.Configuration removeConfiguration(java.lang.String name)
          Removes the configuration with the specified name.
 org.apache.commons.configuration.AbstractConfiguration removeConfigurationAt(int index)
           
 void setContainerConfiguration(org.apache.commons.configuration.AbstractConfiguration config, java.lang.String name, int index)
          Adds a child configuration and makes it the container configuration.
 void setContainerConfigurationIndex(int newIndex)
          Change the position of the container configuration to a new index.
 void setDelimiterParsingDisabled(boolean delimiterParsingDisabled)
          Sets a flag whether added values for string properties should be checked for the list delimiter.
 void setListDelimiter(char listDelimiter)
          Sets the character that is used as list delimiter.
 void setOverrideProperty(java.lang.String key, java.lang.Object finalValue)
          Override the same property in any other configurations in the list.
 void setPropagateEventFromSubConfigurations(boolean propagateEventToParent)
          Set whether sub configurations should propagate events to listeners to this configuration.
 void setProperty(java.lang.String key, java.lang.Object value)
          Set the property with the container configuration.
 
Methods inherited from class com.netflix.config.ConcurrentMapConfiguration
addConfigurationListener, addErrorListener, addPropertyDirect, addPropertyImpl, clearConfigurationListeners, clearErrorListeners, clearPropertyDirect, copy, fireError, fireEvent, getConfigurationListeners, getErrorListeners, getProperties, loadProperties, removeConfigurationListener, removeErrorListener, setPropertyImpl
 
Methods inherited from class org.apache.commons.configuration.AbstractConfiguration
addErrorLogListener, append, createInterpolator, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDefaultListDelimiter, getDelimiter, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getInterpolator, getList, getListDelimiter, getLogger, getLong, getLong, getLong, getProperties, getProperties, getShort, getShort, getShort, getString, getString, getSubstitutor, interpolate, interpolate, interpolatedConfiguration, interpolateHelper, isDelimiterParsingDisabled, isScalarValue, isThrowExceptionOnMissing, resolveContainerStore, setDefaultListDelimiter, setDelimiter, setLogger, setThrowExceptionOnMissing, subset
 
Methods inherited from class org.apache.commons.configuration.event.EventSource
createErrorEvent, createEvent, isDetailEvents, setDetailEvents
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.commons.configuration.Configuration
getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getList, getLong, getLong, getLong, getProperties, getShort, getShort, getShort, getString, getString, subset
 

Field Detail

EVENT_CONFIGURATION_SOURCE_CHANGED

public static final int EVENT_CONFIGURATION_SOURCE_CHANGED
See Also:
Constant Field Values
Constructor Detail

ConcurrentCompositeConfiguration

public ConcurrentCompositeConfiguration()
Creates an empty CompositeConfiguration object which can then be added some other Configuration files


ConcurrentCompositeConfiguration

public ConcurrentCompositeConfiguration(org.apache.commons.configuration.AbstractConfiguration containerConfiguration)
Creates a ConcurrentCompositeConfiguration object with a specified container configuration. This configuration will store any changes made by setProperty(String, Object) and addProperty(String, Object).

Parameters:
containerConfiguration - the configuration to use as container configuration

ConcurrentCompositeConfiguration

public ConcurrentCompositeConfiguration(org.apache.commons.configuration.AbstractConfiguration containerConfiguration,
                                        java.util.Collection<? extends org.apache.commons.configuration.AbstractConfiguration> configurations)
Creates a ConcurrentCompositeConfiguration with a specified container configuration, and then adds the given collection of configurations.

Parameters:
containerConfiguration - container configuration to use
configurations - the collection of configurations to add
Method Detail

configurationChanged

public void configurationChanged(org.apache.commons.configuration.event.ConfigurationEvent event)
Event listener call back for configuration update events. This method is called whenever one of the contained configurations was modified. This method does nothing.

Specified by:
configurationChanged in interface org.apache.commons.configuration.event.ConfigurationListener
Parameters:
event - the update event

invalidate

public void invalidate()

addConfiguration

public final void addConfiguration(org.apache.commons.configuration.AbstractConfiguration config)
Add a child configuration without a name. Make a call to addConfiguration(AbstractConfiguration, String) with the name being null.

Specified by:
addConfiguration in interface AggregatedConfiguration
Parameters:
config - the configuration to add

addConfiguration

public void addConfiguration(org.apache.commons.configuration.AbstractConfiguration config,
                             java.lang.String name)
Adds a new child configuration to this configuration with an optional name. The configuration will be added to the end of the list if container configuration has been changed to new one or no longer at the end of the list. Otherwise it will be added in front of the container configuration.

Specified by:
addConfiguration in interface AggregatedConfiguration
Parameters:
config - the configuration to add (must not be null)
name - the name of this configuration (can be null)

getConfigurations

public java.util.List<org.apache.commons.configuration.AbstractConfiguration> getConfigurations()
Get the configurations added.

Specified by:
getConfigurations in interface AggregatedConfiguration

getConfigurationNameList

public java.util.List<java.lang.String> getConfigurationNameList()
Specified by:
getConfigurationNameList in interface AggregatedConfiguration

getIndexOfConfiguration

public int getIndexOfConfiguration(org.apache.commons.configuration.AbstractConfiguration config)

getIndexOfContainerConfiguration

public int getIndexOfContainerConfiguration()

setContainerConfiguration

public void setContainerConfiguration(org.apache.commons.configuration.AbstractConfiguration config,
                                      java.lang.String name,
                                      int index)
                               throws java.lang.IndexOutOfBoundsException
Adds a child configuration and makes it the container configuration. This means that all future property write operations are executed on this configuration. Note that the current container configuration stays in the list of child configurations at its current position, but it passes its role as container configuration to the new one.

Parameters:
config - the configuration to be added
name - the name of the configuration to be added
index - index to add this configuration
Throws:
java.lang.IndexOutOfBoundsException

setContainerConfigurationIndex

public void setContainerConfigurationIndex(int newIndex)
                                    throws java.lang.IndexOutOfBoundsException
Change the position of the container configuration to a new index.

Throws:
java.lang.IndexOutOfBoundsException

addConfigurationAtIndex

public void addConfigurationAtIndex(org.apache.commons.configuration.AbstractConfiguration config,
                                    java.lang.String name,
                                    int index)
                             throws java.lang.IndexOutOfBoundsException
Add a configuration with a name at a particular index.

Throws:
java.lang.IndexOutOfBoundsException

addConfigurationAtFront

public void addConfigurationAtFront(org.apache.commons.configuration.AbstractConfiguration config,
                                    java.lang.String name)

removeConfiguration

public boolean removeConfiguration(org.apache.commons.configuration.Configuration config)
Remove a configuration. The container configuration cannot be removed.

Specified by:
removeConfiguration in interface AggregatedConfiguration
Parameters:
config - The configuration to remove

removeConfigurationAt

public org.apache.commons.configuration.AbstractConfiguration removeConfigurationAt(int index)
Specified by:
removeConfigurationAt in interface AggregatedConfiguration

removeConfiguration

public org.apache.commons.configuration.Configuration removeConfiguration(java.lang.String name)
Removes the configuration with the specified name.

Specified by:
removeConfiguration in interface AggregatedConfiguration
Parameters:
name - the name of the configuration to be removed
Returns:
the removed configuration (null if this configuration was not found)

getNumberOfConfigurations

public int getNumberOfConfigurations()
Return the number of configurations.

Specified by:
getNumberOfConfigurations in interface AggregatedConfiguration
Returns:
the number of configuration

clear

public final void clear()
Removes all child configurations and reinitializes the container configuration. Attention: A new container configuration is created; the old one is lost.

Specified by:
clear in interface org.apache.commons.configuration.Configuration
Overrides:
clear in class ConcurrentMapConfiguration

setOverrideProperty

public void setOverrideProperty(java.lang.String key,
                                java.lang.Object finalValue)
Override the same property in any other configurations in the list.


clearOverrideProperty

public void clearOverrideProperty(java.lang.String key)
Remove the overriding property set by setOverrideProperty(String, Object)


setProperty

public void setProperty(java.lang.String key,
                        java.lang.Object value)
Set the property with the container configuration. Warning: getProperty(String) on this key may not return the same value set by this method if there is any other configuration that contain the same property and is in front of the container configuration in the configurations list.

Specified by:
setProperty in interface org.apache.commons.configuration.Configuration
Overrides:
setProperty in class ConcurrentMapConfiguration

addProperty

public void addProperty(java.lang.String key,
                        java.lang.Object value)
Add the property with the container configuration. Warning: getProperty(String) on this key may not return the same value set by this method if there is any other configuration that contain the same property and is in front of the container configuration in the configurations list.

Specified by:
addProperty in interface org.apache.commons.configuration.Configuration
Overrides:
addProperty in class ConcurrentMapConfiguration

clearProperty

public void clearProperty(java.lang.String key)
Clear the property with the container configuration. Warning: getProperty(String) on this key may still return some value if there is any other configuration that contain the same property and is in front of the container configuration in the configurations list.

Specified by:
clearProperty in interface org.apache.commons.configuration.Configuration
Overrides:
clearProperty in class org.apache.commons.configuration.AbstractConfiguration

getProperty

public java.lang.Object getProperty(java.lang.String key)
Read property from underlying composite. It first checks if the property has been overridden by setOverrideProperty(String, Object) and if so return the overriding value. Otherwise, it iterates through the list of sub configurations until it finds one that contains the property and return the value from that sub configuration. It returns null of the property does not exist.

Specified by:
getProperty in interface org.apache.commons.configuration.Configuration
Overrides:
getProperty in class ConcurrentMapConfiguration
Parameters:
key - key to use for mapping
Returns:
object associated with the given configuration key. null if it does not exist.

getKeys

public java.util.Iterator<java.lang.String> getKeys()
                                             throws java.util.ConcurrentModificationException
Get all the keys contained by sub configurations.

Specified by:
getKeys in interface org.apache.commons.configuration.Configuration
Overrides:
getKeys in class ConcurrentMapConfiguration
Throws:
java.util.ConcurrentModificationException - if concurrent modification happens on any sub configuration when it is iterated to get all the keys

getKeys

public java.util.Iterator<java.lang.String> getKeys(java.lang.String prefix)
Get the list of the keys contained in the sub configurations that match the specified prefix.

Specified by:
getKeys in interface org.apache.commons.configuration.Configuration
Overrides:
getKeys in class org.apache.commons.configuration.AbstractConfiguration

getConfigurationNames

public java.util.Set<java.lang.String> getConfigurationNames()
Returns a set with the names of all configurations contained in this configuration. Of course here are only these configurations listed, for which a name was specified when they were added.

Specified by:
getConfigurationNames in interface AggregatedConfiguration
Returns:
a set with the names of the contained configurations (never null)

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface org.apache.commons.configuration.Configuration
Overrides:
isEmpty in class ConcurrentMapConfiguration

containsKey

public boolean containsKey(java.lang.String key)
Check if the any of the sub configurations contains the specified key.

Specified by:
containsKey in interface org.apache.commons.configuration.Configuration
Overrides:
containsKey in class ConcurrentMapConfiguration
Parameters:
key - the key whose presence in this configuration is to be tested
Returns:
true if the configuration contains a value for this key, false otherwise

getList

public java.util.List getList(java.lang.String key,
                              java.util.List defaultValue)
Get a List of objects associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.

Specified by:
getList in interface org.apache.commons.configuration.Configuration
Overrides:
getList in class org.apache.commons.configuration.AbstractConfiguration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated List of value.

getStringArray

public java.lang.String[] getStringArray(java.lang.String key)
Get an array of strings associated with the given configuration key. If the key doesn't map to an existing object an empty array is returned

Specified by:
getStringArray in interface org.apache.commons.configuration.Configuration
Overrides:
getStringArray in class org.apache.commons.configuration.AbstractConfiguration
Parameters:
key - The configuration key.
Returns:
The associated string array if key is found.

getConfiguration

public org.apache.commons.configuration.Configuration getConfiguration(int index)
Return the configuration at the specified index.

Specified by:
getConfiguration in interface AggregatedConfiguration
Parameters:
index - The index of the configuration to retrieve
Returns:
the configuration at this index

getConfiguration

public org.apache.commons.configuration.Configuration getConfiguration(java.lang.String name)
Returns the configuration with the given name. This can be null if no such configuration exists.

Specified by:
getConfiguration in interface AggregatedConfiguration
Parameters:
name - the name of the configuration
Returns:
the configuration with this name

getContainerConfiguration

public org.apache.commons.configuration.Configuration getContainerConfiguration()
Returns the container configuration In this configuration changes are stored.

Returns:
the container configuration

clone

public java.lang.Object clone()
Returns a copy of this object. This implementation will create a deep clone, i.e. all configurations contained in this composite will also be cloned. This only works if all contained configurations support cloning; otherwise a runtime exception will be thrown. Registered event handlers won't get cloned.

Overrides:
clone in class org.apache.commons.configuration.event.EventSource

setDelimiterParsingDisabled

public void setDelimiterParsingDisabled(boolean delimiterParsingDisabled)
Sets a flag whether added values for string properties should be checked for the list delimiter. This implementation ensures that the container configuration is correctly initialized.

Overrides:
setDelimiterParsingDisabled in class org.apache.commons.configuration.AbstractConfiguration
Parameters:
delimiterParsingDisabled - the new value of the flag

setListDelimiter

public void setListDelimiter(char listDelimiter)
Sets the character that is used as list delimiter. This implementation ensures that the container configuration is correctly initialized.

Overrides:
setListDelimiter in class org.apache.commons.configuration.AbstractConfiguration
Parameters:
listDelimiter - the new list delimiter character

getSource

public org.apache.commons.configuration.Configuration getSource(java.lang.String key)
Returns the configuration source, in which the specified key is defined. This method will iterate over all existing child configurations and check whether they contain the specified key. The following constellations are possible:

Parameters:
key - the key to be checked
Returns:
the source configuration of this key

isPropagateEventFromSubConfigurations

public final boolean isPropagateEventFromSubConfigurations()
Return whether sub configurations should propagate events to listeners to this configuration.


setPropagateEventFromSubConfigurations

public final void setPropagateEventFromSubConfigurations(boolean propagateEventToParent)
Set whether sub configurations should propagate events to listeners to this configuration. This is needed if this configuration is used as the configuration source of DynamicPropertyFactory.

Parameters:
propagateEventToParent - value to set