com.netflix.config
Class ConcurrentMapConfiguration

java.lang.Object
  extended by org.apache.commons.configuration.event.EventSource
      extended by org.apache.commons.configuration.AbstractConfiguration
          extended by com.netflix.config.ConcurrentMapConfiguration
All Implemented Interfaces:
org.apache.commons.configuration.Configuration
Direct Known Subclasses:
ClasspathPropertiesConfiguration, ConcurrentCompositeConfiguration, DynamicConfiguration

public class ConcurrentMapConfiguration
extends org.apache.commons.configuration.AbstractConfiguration

This class uses a ConcurrentHashMap for reading/writing a property to achieve high throughput and thread safety. The implementation is lock free for getProperty(String) and setProperty(String, Object), but has some synchronization cost for addProperty(String, Object) if the object to add is not a String or the key already exists.

The methods from AbstractConfiguration related to listeners and event generation are overridden so that adding/deleting listeners and firing events are no longer synchronized. Also, it catches Throwable when it invokes the listeners, making it more robust.

This configuration does not allow null as key or value and will throw NullPointerException when trying to add or set properties with empty key or value.


Field Summary
 
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
ConcurrentMapConfiguration()
          Create an instance with an empty map.
ConcurrentMapConfiguration(org.apache.commons.configuration.Configuration config)
          Create an instance by copying the properties from an existing Configuration.
ConcurrentMapConfiguration(java.util.Map<java.lang.String,java.lang.Object> mapToCopy)
           
 
Method Summary
 void addConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener l)
           
 void addErrorListener(org.apache.commons.configuration.event.ConfigurationErrorListener l)
           
 void addProperty(java.lang.String key, java.lang.Object value)
           
protected  void addPropertyDirect(java.lang.String key, java.lang.Object value)
           
 void clear()
          Clear the map and fire corresonding events.
 void clearConfigurationListeners()
           
 void clearErrorListeners()
           
protected  void clearPropertyDirect(java.lang.String key)
           
 boolean containsKey(java.lang.String key)
           
 void copy(org.apache.commons.configuration.Configuration c)
          Copy properties of a configuration into this configuration.
protected  void fireError(int type, java.lang.String propName, java.lang.Object propValue, java.lang.Throwable ex)
          Creates an error event and calls ConfigurationErrorListener.configurationError(ConfigurationErrorEvent) for all listeners while catching Throwable.
protected  void fireEvent(int type, java.lang.String propName, java.lang.Object propValue, boolean beforeUpdate)
          Creates an event and calls ConfigurationListener.configurationChanged(ConfigurationEvent) for all listeners while catching Throwable.
 java.util.Collection<org.apache.commons.configuration.event.ConfigurationListener> getConfigurationListeners()
           
 java.util.Collection<org.apache.commons.configuration.event.ConfigurationErrorListener> getErrorListeners()
           
 java.util.Iterator getKeys()
           
 java.util.Properties getProperties()
          Utility method to get a Properties object from this Configuration
 java.lang.Object getProperty(java.lang.String key)
           
 boolean isEmpty()
           
 void loadProperties(java.util.Properties props)
          Load properties into the configuration.
 boolean removeConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener l)
           
 boolean removeErrorListener(org.apache.commons.configuration.event.ConfigurationErrorListener l)
           
 void setProperty(java.lang.String key, java.lang.Object value)
          Override the same method in AbstractConfiguration to simplify the logic to avoid multiple events being generated.
 
Methods inherited from class org.apache.commons.configuration.AbstractConfiguration
addErrorLogListener, append, clearProperty, createInterpolator, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDefaultListDelimiter, getDelimiter, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getInterpolator, getKeys, getList, getList, getListDelimiter, getLogger, getLong, getLong, getLong, getProperties, getProperties, getShort, getShort, getShort, getString, getString, getStringArray, getSubstitutor, interpolate, interpolate, interpolatedConfiguration, interpolateHelper, isDelimiterParsingDisabled, isScalarValue, isThrowExceptionOnMissing, resolveContainerStore, setDefaultListDelimiter, setDelimiter, setDelimiterParsingDisabled, setListDelimiter, setLogger, setThrowExceptionOnMissing, subset
 
Methods inherited from class org.apache.commons.configuration.event.EventSource
clone, createErrorEvent, createEvent, isDetailEvents, setDetailEvents
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConcurrentMapConfiguration

public ConcurrentMapConfiguration()
Create an instance with an empty map.


ConcurrentMapConfiguration

public ConcurrentMapConfiguration(java.util.Map<java.lang.String,java.lang.Object> mapToCopy)

ConcurrentMapConfiguration

public ConcurrentMapConfiguration(org.apache.commons.configuration.Configuration config)
Create an instance by copying the properties from an existing Configuration. Future changes to the Configuration passed in will not be reflected in this object.

Parameters:
config - Configuration to be copied
Method Detail

getProperty

public java.lang.Object getProperty(java.lang.String key)

addPropertyDirect

protected void addPropertyDirect(java.lang.String key,
                                 java.lang.Object value)
Specified by:
addPropertyDirect in class org.apache.commons.configuration.AbstractConfiguration

isEmpty

public boolean isEmpty()

containsKey

public boolean containsKey(java.lang.String key)

clearPropertyDirect

protected void clearPropertyDirect(java.lang.String key)
Overrides:
clearPropertyDirect in class org.apache.commons.configuration.AbstractConfiguration

getKeys

public java.util.Iterator getKeys()

addProperty

public void addProperty(java.lang.String key,
                        java.lang.Object value)
Specified by:
addProperty in interface org.apache.commons.configuration.Configuration
Overrides:
addProperty in class org.apache.commons.configuration.AbstractConfiguration

setProperty

public void setProperty(java.lang.String key,
                        java.lang.Object value)
Override the same method in AbstractConfiguration to simplify the logic to avoid multiple events being generated. It calls clearPropertyDirect(String) followed by logic to add the property including calling addPropertyDirect(String, Object).

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

loadProperties

public void loadProperties(java.util.Properties props)
Load properties into the configuration. This method iterates through the entries of the properties and call setProperty(String, Object) for non-null key/value.


copy

public void copy(org.apache.commons.configuration.Configuration c)
Copy properties of a configuration into this configuration. This method simply iterates the keys of the configuration to be copied and call setProperty(String, Object) for non-null key/value.

Overrides:
copy in class org.apache.commons.configuration.AbstractConfiguration

clear

public void clear()
Clear the map and fire corresonding events.

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

getProperties

public java.util.Properties getProperties()
Utility method to get a Properties object from this Configuration


fireEvent

protected void fireEvent(int type,
                         java.lang.String propName,
                         java.lang.Object propValue,
                         boolean beforeUpdate)
Creates an event and calls ConfigurationListener.configurationChanged(ConfigurationEvent) for all listeners while catching Throwable.

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

addConfigurationListener

public void addConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener l)
Overrides:
addConfigurationListener in class org.apache.commons.configuration.event.EventSource

addErrorListener

public void addErrorListener(org.apache.commons.configuration.event.ConfigurationErrorListener l)
Overrides:
addErrorListener in class org.apache.commons.configuration.event.EventSource

clearConfigurationListeners

public void clearConfigurationListeners()
Overrides:
clearConfigurationListeners in class org.apache.commons.configuration.event.EventSource

clearErrorListeners

public void clearErrorListeners()
Overrides:
clearErrorListeners in class org.apache.commons.configuration.event.EventSource

getConfigurationListeners

public java.util.Collection<org.apache.commons.configuration.event.ConfigurationListener> getConfigurationListeners()
Overrides:
getConfigurationListeners in class org.apache.commons.configuration.event.EventSource

getErrorListeners

public java.util.Collection<org.apache.commons.configuration.event.ConfigurationErrorListener> getErrorListeners()
Overrides:
getErrorListeners in class org.apache.commons.configuration.event.EventSource

removeConfigurationListener

public boolean removeConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener l)
Overrides:
removeConfigurationListener in class org.apache.commons.configuration.event.EventSource

removeErrorListener

public boolean removeErrorListener(org.apache.commons.configuration.event.ConfigurationErrorListener l)
Overrides:
removeErrorListener in class org.apache.commons.configuration.event.EventSource

fireError

protected void fireError(int type,
                         java.lang.String propName,
                         java.lang.Object propValue,
                         java.lang.Throwable ex)
Creates an error event and calls ConfigurationErrorListener.configurationError(ConfigurationErrorEvent) for all listeners while catching Throwable.

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