org.infinispan.manager
Class DefaultCacheManager

java.lang.Object
  extended by org.infinispan.manager.DefaultCacheManager
All Implemented Interfaces:
Lifecycle, CacheManager, Listenable

public class DefaultCacheManager
extends Object
implements CacheManager

A CacheManager is the primary mechanism for retrieving a Cache instance, and is often used as a starting point to using the Cache.

CacheManagers are heavyweight objects, and we foresee no more than one CacheManager being used per JVM (unless specific configuration requirements require more than one; but either way, this would be a minimal and finite number of instances).

Constructing a CacheManager is done via one of its constructors, which optionally take in a Configuration or a path or URL to a configuration XML file.

Lifecycle - CacheManagers have a lifecycle (it implements Lifecycle) and the default constructors also call start(). Overloaded versions of the constructors are available, that do not start the CacheManager, although it must be kept in mind that CacheManagers need to be started before they can be used to create Cache instances.

Once constructed, CacheManagers should be made available to any component that requires a Cache, via JNDI or via some other mechanism such as an IoC container.

You obtain Cache instances from the CacheManager by using one of the overloaded getCache(), methods. Note that with getCache(), there is no guarantee that the instance you get is brand-new and empty, since caches are named and shared. Because of this, the CacheManager also acts as a repository of Caches, and is an effective mechanism of looking up or creating Caches on demand.

When the system shuts down, it should call stop() on the CacheManager. This will ensure all caches within its scope are properly stopped as well.

Sample usage: CacheManager manager = CacheManager.getInstance("my-config-file.xml"); Cache entityCache = manager.getCache("myEntityCache"); entityCache.put("aPerson", new Person());

Configuration myNewConfiguration = new Configuration(); myNewConfiguration.setCacheMode(Configuration.CacheMode.LOCAL); manager.defineCache("myLocalCache", myNewConfiguration); Cache localCache = manager.getCache("myLocalCache");

Since:
4.0
Author:
Manik Surtani (manik@jboss.org)

Field Summary
static String DEFAULT_CACHE_NAME
           
protected  GlobalConfiguration globalConfiguration
           
 
Constructor Summary
DefaultCacheManager()
          Constructs and starts a default instance of the CacheManager, using configuration defaults.
DefaultCacheManager(boolean start)
          Constructs a default instance of the CacheManager, using configuration defaults.
DefaultCacheManager(Configuration defaultConfiguration)
          Constructs and starts a new instance of the CacheManager, using the default configuration passed in.
DefaultCacheManager(Configuration defaultConfiguration, boolean start)
          Constructs a new instance of the CacheManager, using the default configuration passed in.
DefaultCacheManager(GlobalConfiguration globalConfiguration)
          Constructs and starts a new instance of the CacheManager, using the global configuration passed in, and system defaults for the default named cache configuration.
DefaultCacheManager(GlobalConfiguration globalConfiguration, boolean start)
          Constructs a new instance of the CacheManager, using the global configuration passed in, and system defaults for the default named cache configuration.
DefaultCacheManager(GlobalConfiguration globalConfiguration, Configuration defaultConfiguration)
          Constructs and starts a new instance of the CacheManager, using the global and default configurations passed in.
DefaultCacheManager(GlobalConfiguration globalConfiguration, Configuration defaultConfiguration, boolean start)
          Constructs a new instance of the CacheManager, using the global and default configurations passed in.
DefaultCacheManager(InputStream configurationStream)
          Constructs and starts a new instance of the CacheManager, using the input stream passed in to read configuration file contents.
DefaultCacheManager(InputStream configurationStream, boolean start)
          Constructs a new instance of the CacheManager, using the input stream passed in to read configuration file contents.
DefaultCacheManager(String configurationFile)
          Constructs and starts a new instance of the CacheManager, using the configuration file name passed in.
DefaultCacheManager(String configurationFile, boolean start)
          Constructs a new instance of the CacheManager, using the configuration file name passed in.
 
Method Summary
 void addListener(Object listener)
          Adds a listener to the component.
 void defineCache(String cacheName, Configuration configurationOverride)
          Defines a named cache.
 Address getAddress()
           
<K,V> Cache<K,V>
getCache()
          Retrieves the default cache associated with this cache manager.
<K,V> Cache<K,V>
getCache(String cacheName)
          Retrieves a named cache from the system.
 String getClusterName()
           
 String getCreatedCacheCount()
           
 String getDefinedCacheCount()
           
 String getDefinedCacheNames()
           
 Set<Object> getListeners()
           
 List<Address> getMembers()
           
 ComponentStatus getStatus()
           
 boolean isCoordinator()
           
 void removeListener(Object listener)
          Removes a listener from the component.
 void start()
           
 void stop()
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_CACHE_NAME

public static final String DEFAULT_CACHE_NAME
See Also:
Constant Field Values

globalConfiguration

protected GlobalConfiguration globalConfiguration
Constructor Detail

DefaultCacheManager

public DefaultCacheManager()
Constructs and starts a default instance of the CacheManager, using configuration defaults.


DefaultCacheManager

public DefaultCacheManager(boolean start)
Constructs a default instance of the CacheManager, using configuration defaults.

Parameters:
start - if true, the cache manager is started

DefaultCacheManager

public DefaultCacheManager(Configuration defaultConfiguration)
Constructs and starts a new instance of the CacheManager, using the default configuration passed in. Uses defaults for a GlobalConfiguration.

Parameters:
defaultConfiguration - configuration to use as a template for all caches created

DefaultCacheManager

public DefaultCacheManager(Configuration defaultConfiguration,
                           boolean start)
Constructs a new instance of the CacheManager, using the default configuration passed in. Uses defaults for a GlobalConfiguration.

Parameters:
defaultConfiguration - configuration file to use as a template for all caches created
start - if true, the cache manager is started

DefaultCacheManager

public DefaultCacheManager(GlobalConfiguration globalConfiguration)
Constructs and starts a new instance of the CacheManager, using the global configuration passed in, and system defaults for the default named cache configuration.

Parameters:
globalConfiguration - GlobalConfiguration to use for all caches created

DefaultCacheManager

public DefaultCacheManager(GlobalConfiguration globalConfiguration,
                           boolean start)
Constructs a new instance of the CacheManager, using the global configuration passed in, and system defaults for the default named cache configuration.

Parameters:
globalConfiguration - GlobalConfiguration to use for all caches created
start - if true, the cache manager is started.

DefaultCacheManager

public DefaultCacheManager(GlobalConfiguration globalConfiguration,
                           Configuration defaultConfiguration)
Constructs and starts a new instance of the CacheManager, using the global and default configurations passed in. If either of these are null, system defaults are used.

Parameters:
globalConfiguration - global configuration to use. If null, a default instance is created.
defaultConfiguration - default configuration to use. If null, a default instance is created.

DefaultCacheManager

public DefaultCacheManager(GlobalConfiguration globalConfiguration,
                           Configuration defaultConfiguration,
                           boolean start)
Constructs a new instance of the CacheManager, using the global and default configurations passed in. If either of these are null, system defaults are used.

Parameters:
globalConfiguration - global configuration to use. If null, a default instance is created.
defaultConfiguration - default configuration to use. If null, a default instance is created.
start - if true, the cache manager is started

DefaultCacheManager

public DefaultCacheManager(String configurationFile)
                    throws IOException
Constructs and starts a new instance of the CacheManager, using the configuration file name passed in. This constructor first searches for the named file on the classpath, and failing that, treats the file name as an absolute path.

Parameters:
configurationFile - name of configuration file to use as a template for all caches created
Throws:
IOException - if there is a problem with the configuration file.

DefaultCacheManager

public DefaultCacheManager(String configurationFile,
                           boolean start)
                    throws IOException
Constructs a new instance of the CacheManager, using the configuration file name passed in. This constructor first searches for the named file on the classpath, and failing that, treats the file name as an absolute path.

Parameters:
configurationFile - name of configuration file to use as a template for all caches created
start - if true, the cache manager is started
Throws:
IOException - if there is a problem with the configuration file.

DefaultCacheManager

public DefaultCacheManager(InputStream configurationStream)
                    throws IOException
Constructs and starts a new instance of the CacheManager, using the input stream passed in to read configuration file contents.

Parameters:
configurationStream - stream containing configuration file contents, to use as a template for all caches created
Throws:
IOException - if there is a problem with the configuration stream.

DefaultCacheManager

public DefaultCacheManager(InputStream configurationStream,
                           boolean start)
                    throws IOException
Constructs a new instance of the CacheManager, using the input stream passed in to read configuration file contents.

Parameters:
configurationStream - stream containing configuration file contents, to use as a template for all caches created
start - if true, the cache manager is started
Throws:
IOException - if there is a problem reading the configuration stream
Method Detail

defineCache

public void defineCache(String cacheName,
                        Configuration configurationOverride)
                 throws DuplicateCacheNameException
Defines a named cache. Named caches can be defined by using this method, in which case the configuration passed in is used to override the default configuration used when this cache manager instance was created.

The other way to define named caches is declaratively, in the XML file passed in to the cache manager.

A combination of approaches may also be used, provided the names do not conflict.

Specified by:
defineCache in interface CacheManager
Parameters:
cacheName - name of cache to define
configurationOverride - configuration overrides to use
Throws:
DuplicateCacheNameException - if the name is already in use.

getCache

public <K,V> Cache<K,V> getCache()
Retrieves the default cache associated with this cache manager. Note that the default cache does not need to be explicitly created with createCache(String) since it is automatically created lazily when first used.

As such, this method is always guaranteed to return the default cache.

Specified by:
getCache in interface CacheManager
Returns:
the default cache.

getCache

public <K,V> Cache<K,V> getCache(String cacheName)
Retrieves a named cache from the system. If the cache has been previously created with the same name, the running cache instance is returned. Otherwise, this method attempts to create the cache first.

When creating a new cache, this method will use the configuration passed in to the CacheManager on construction, as a template, and then optionally apply any overrides previously defined for the named cache using the defineCache(String, org.infinispan.config.Configuration) method, or declared in the configuration file.

Specified by:
getCache in interface CacheManager
Parameters:
cacheName - name of cache to retrieve
Returns:
a cache instance identified by cacheName

getClusterName

public String getClusterName()
Specified by:
getClusterName in interface CacheManager
Returns:
the name of the cluster. Null if running in local mode.

getMembers

public List<Address> getMembers()
Specified by:
getMembers in interface CacheManager

getAddress

public Address getAddress()
Specified by:
getAddress in interface CacheManager

isCoordinator

public boolean isCoordinator()
Specified by:
isCoordinator in interface CacheManager

start

public void start()
Specified by:
start in interface Lifecycle

stop

public void stop()
Specified by:
stop in interface Lifecycle

addListener

public void addListener(Object listener)
Description copied from interface: Listenable
Adds a listener to the component. Typically, listeners would need to be annotated with Listener and further to that, contain methods annotated appropriately, otherwise the listener will not be registered.

See the Listener annotation for more information.

Specified by:
addListener in interface Listenable
Parameters:
listener - must not be null.

removeListener

public void removeListener(Object listener)
Description copied from interface: Listenable
Removes a listener from the component.

Specified by:
removeListener in interface Listenable
Parameters:
listener - listener to remove. Must not be null.

getListeners

public Set<Object> getListeners()
Specified by:
getListeners in interface Listenable
Returns:
a set of all listeners registered on this component.

getStatus

public ComponentStatus getStatus()
Specified by:
getStatus in interface CacheManager

getDefinedCacheNames

public String getDefinedCacheNames()

getDefinedCacheCount

public String getDefinedCacheCount()

getCreatedCacheCount

public String getCreatedCacheCount()

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2009 JBoss, a division of Red Hat. All Rights Reserved.