org.apache.shiro.cache.ehcache
Class EhCacheManager

java.lang.Object
  extended by org.apache.shiro.cache.ehcache.EhCacheManager
All Implemented Interfaces:
CacheManager, Destroyable, Initializable

public class EhCacheManager
extends Object
implements CacheManager, Initializable, Destroyable

Shiro CacheManager implementation utilizing the Ehcache framework for all cache functionality.

This class can accept a manually configured net.sf.ehcache.CacheManager instance, or an ehcache.xml path location can be specified instead and one will be constructed. If neither are specified, Shiro's failsafe ehcache.xml} file will be used by default.

This implementation requires EhCache 1.2 and above. Make sure EhCache 1.1 or earlier is not in the classpath or it will not work.

Please see the Ehcache website for their documentation.

Since:
0.2
See Also:
The Ehcache website

Field Summary
protected  net.sf.ehcache.CacheManager manager
          The EhCache cache manager used by this implementation to create caches.
 
Constructor Summary
EhCacheManager()
          Default no argument constructor
 
Method Summary
 void destroy()
          Shuts-down the wrapped Ehcache CacheManager only if implicitly created.
<K,V> Cache<K,V>
getCache(String name)
          Loads an existing EhCache from the cache manager, or starts a new cache if one is not found.
 net.sf.ehcache.CacheManager getCacheManager()
          Returns the wrapped Ehcache CacheManager instance.
 String getCacheManagerConfigFile()
          Returns the resource location of the config file used to initialize a new EhCache CacheManager instance.
protected  InputStream getCacheManagerConfigFileInputStream()
          Acquires the InputStream for the ehcache configuration file using ResourceUtils.getInputStreamForPath with the path returned from getCacheManagerConfigFile().
 void init()
          Initializes this instance.
 void setCacheManager(net.sf.ehcache.CacheManager manager)
          Sets the wrapped Ehcache CacheManager instance.
 void setCacheManagerConfigFile(String classpathLocation)
          Sets the resource location of the config file used to initialize the wrapped EhCache CacheManager instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

manager

protected net.sf.ehcache.CacheManager manager
The EhCache cache manager used by this implementation to create caches.

Constructor Detail

EhCacheManager

public EhCacheManager()
Default no argument constructor

Method Detail

getCacheManager

public net.sf.ehcache.CacheManager getCacheManager()
Returns the wrapped Ehcache CacheManager instance.

Returns:
the wrapped Ehcache CacheManager instance.

setCacheManager

public void setCacheManager(net.sf.ehcache.CacheManager manager)
Sets the wrapped Ehcache CacheManager instance.

Parameters:
manager - the wrapped Ehcache CacheManager instance.

getCacheManagerConfigFile

public String getCacheManagerConfigFile()
Returns the resource location of the config file used to initialize a new EhCache CacheManager instance. The string can be any resource path supported by the ResourceUtils.getInputStreamForPath(String) call.

This property is ignored if the CacheManager instance is injected directly - that is, it is only used to lazily create a CacheManager if one is not already provided.

Returns:
the resource location of the config file used to initialize the wrapped EhCache CacheManager instance.

setCacheManagerConfigFile

public void setCacheManagerConfigFile(String classpathLocation)
Sets the resource location of the config file used to initialize the wrapped EhCache CacheManager instance. The string can be any resource path supported by the ResourceUtils.getInputStreamForPath(String) call.

This property is ignored if the CacheManager instance is injected directly - that is, it is only used to lazily create a CacheManager if one is not already provided.

Parameters:
classpathLocation - resource location of the config file used to create the wrapped EhCache CacheManager instance.

getCacheManagerConfigFileInputStream

protected InputStream getCacheManagerConfigFileInputStream()
Acquires the InputStream for the ehcache configuration file using ResourceUtils.getInputStreamForPath with the path returned from getCacheManagerConfigFile().

Returns:
the InputStream for the ehcache configuration file.

getCache

public final <K,V> Cache<K,V> getCache(String name)
                          throws CacheException
Loads an existing EhCache from the cache manager, or starts a new cache if one is not found.

Specified by:
getCache in interface CacheManager
Parameters:
name - the name of the cache to load/create.
Returns:
the Cache with the given name
Throws:
CacheException - if there is an error acquiring the Cache instance.

init

public final void init()
                throws CacheException
Initializes this instance.

If a CacheManager has been explicitly set (e.g. via Dependency Injection or programatically) prior to calling this method, this method does nothing.

However, if no CacheManager has been set, the default Ehcache singleton will be initialized, where Ehcache will look for an ehcache.xml file at the root of the classpath. If one is not found, Ehcache will use its own failsafe configuration file.

Because Shiro cannot use the failsafe defaults (fail-safe expunges cached objects after 2 minutes, something not desirable for Shiro sessions), this class manages an internal default configuration for this case.

Specified by:
init in interface Initializable
Throws:
CacheException - if there are any CacheExceptions thrown by EhCache.
See Also:
CacheManager.create()

destroy

public void destroy()
Shuts-down the wrapped Ehcache CacheManager only if implicitly created.

If another component injected a non-null CacheManager into this instace before calling init, this instance expects that same component to also destroy the CacheManager instance, and it will not attempt to do so.

Specified by:
destroy in interface Destroyable


Copyright © 2004-2014 The Apache Software Foundation. All Rights Reserved.