org.sape.carbon.core.config.cache
Class DoubleCheckConfigurationCache

java.lang.Object
  |
  +--org.sape.carbon.core.config.cache.DoubleCheckConfigurationCache
All Implemented Interfaces:
ConfigurationCache, EventListener, NodeEventListener

public class DoubleCheckConfigurationCache
extends Object
implements ConfigurationCache, NodeEventListener

This implementation uses the infamous double-check lock to minimize the synchronization required. Multi-threaded read access to the cache will not cause contention. Adding new configurations to the cache is synchronized.

Caution: Double-check locking is not bulletproof. There is a small possibility when using optimizing compilers or multi-processor machines that stale configurations or invalid configurations are returned when multiple threads concurrently access the same configuration when that configuration is being loaded, changed, or removed.

This implementation uses 2 maps to store configuration data, an active map and a background map. When the cache is modified, the background map is updated first. Then the maps are swapped making the background map the active one. Then the background (used to be active) is updated. This is done to make use of the fact that reference assignment is atomic and prevent map from being accessed while they are being updated.

Configuration objects are cached lazily, i.e. they are cached after they are requested the first time. Once a configuration object is cached, the cache will listen for Node events on the configuration. If a node event occurs, the entire cache is cleared. This is done to prevent inconsistencies between this cache and caches internal to the configuration objects themselves in the case of nested configurations.

Copyright 2002 Sapient

Since:
carbon 1.1
Version:
$Revision: 1.10 $($Author: dvoet $ / $Date: 2003/05/05 21:21:16 $)
Author:
Douglas Voet, Oct 24, 2002

Field Summary
private  Map activeCache
          Active cache used to return value.
private  Map backgroundCache
          Background cache used for loading.
private  ConfigurationService configService
          Holds the config service being cached.
private  int entranceCount
          Holds the entrance count for re-entrant calls.
private  Map loadingConfigurations
          Map of loaded configuration.
private  org.apache.commons.logging.Log log
          Provides a handle to Apache-commons logger
 
Constructor Summary
DoubleCheckConfigurationCache(ConfigurationService configService)
          Constructs a new DoubleCheckConfigurationCache around the given service.
 
Method Summary
private  void activateBackgroundCache()
          Makes the background cache active and the active cache the background.
 Configuration getConfiguration(String name)
          Gets back a configuration with the given name.
 void nodeChanged(Node changedNode)
          Updates the configuration cache with the new instance of the node.
 void nodeRemoved(String removedNodeName)
          Removes a given node from the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private org.apache.commons.logging.Log log
Provides a handle to Apache-commons logger


activeCache

private Map activeCache
Active cache used to return value.


backgroundCache

private Map backgroundCache
Background cache used for loading.


configService

private ConfigurationService configService
Holds the config service being cached.


entranceCount

private int entranceCount
Holds the entrance count for re-entrant calls.


loadingConfigurations

private Map loadingConfigurations
Map of loaded configuration.

Constructor Detail

DoubleCheckConfigurationCache

public DoubleCheckConfigurationCache(ConfigurationService configService)
Constructs a new DoubleCheckConfigurationCache around the given service.

Parameters:
configService - service to provide a double-check cache for.
Method Detail

getConfiguration

public Configuration getConfiguration(String name)
Gets back a configuration with the given name. This will either return the configuration from the cache or load it if needed.

Specified by:
getConfiguration in interface ConfigurationCache
Parameters:
name - the name of the configuration to load
Returns:
configuration at the given name
Throws:
ConfigurationNotFoundException - indicates there is no configuration at the given name

nodeChanged

public void nodeChanged(Node changedNode)
Updates the configuration cache with the new instance of the node.

Specified by:
nodeChanged in interface NodeEventListener
Parameters:
changedNode - the node that has been changed

nodeRemoved

public void nodeRemoved(String removedNodeName)
Removes a given node from the cache.

Specified by:
nodeRemoved in interface NodeEventListener
Parameters:
removedNodeName - node to remove from the cache

activateBackgroundCache

private void activateBackgroundCache()
Makes the background cache active and the active cache the background.



Copyright 1999-2003 Sapient Corporation. All Rights Reserved.