Package org.apache.shiro.cache.ehcache
Class EhCacheManager
- java.lang.Object
-
- org.apache.shiro.cache.ehcache.EhCacheManager
-
- All Implemented Interfaces:
org.apache.shiro.cache.CacheManager,org.apache.shiro.util.Destroyable,org.apache.shiro.util.Initializable
public class EhCacheManager extends Object implements org.apache.shiro.cache.CacheManager, org.apache.shiro.util.Initializable, org.apache.shiro.util.Destroyable
ShiroCacheManagerimplementation utilizing the Ehcache framework for all cache functionality. This class canaccepta manually configurednet.sf.ehcache.CacheManagerinstance, or anehcache.xmlpath location can be specified instead and one will be constructed. If neither are specified, Shiro's failsafeehcache.xmlfile 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
Fields Modifier and Type Field Description protected net.sf.ehcache.CacheManagermanagerThe EhCache cache manager used by this implementation to create caches.
-
Constructor Summary
Constructors Constructor Description EhCacheManager()Default no argument constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddestroy()Shuts-down the wrapped Ehcache CacheManager only if implicitly created.<K,V>
org.apache.shiro.cache.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.CacheManagergetCacheManager()Returns the wrapped EhcacheCacheManagerinstance.StringgetCacheManagerConfigFile()Returns the resource location of the config file used to initialize a new EhCache CacheManager instance.protected InputStreamgetCacheManagerConfigFileInputStream()Acquires the InputStream for the ehcache configuration file usingResourceUtils.getInputStreamForPathwith the path returned fromgetCacheManagerConfigFile().voidinit()Initializes this instance.voidsetCacheManager(net.sf.ehcache.CacheManager manager)Sets the wrapped EhcacheCacheManagerinstance.voidsetCacheManagerConfigFile(String classpathLocation)Sets the resource location of the config file used to initialize the wrapped EhCache CacheManager instance.
-
-
-
Method Detail
-
getCacheManager
public net.sf.ehcache.CacheManager getCacheManager()
Returns the wrapped EhcacheCacheManagerinstance.- Returns:
- the wrapped Ehcache
CacheManagerinstance.
-
setCacheManager
public void setCacheManager(net.sf.ehcache.CacheManager manager)
Sets the wrapped EhcacheCacheManagerinstance.- Parameters:
manager- the wrapped EhcacheCacheManagerinstance.
-
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 theResourceUtils.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 theResourceUtils.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 usingResourceUtils.getInputStreamForPathwith the path returned fromgetCacheManagerConfigFile().- Returns:
- the InputStream for the ehcache configuration file.
-
getCache
public final <K,V> org.apache.shiro.cache.Cache<K,V> getCache(String name) throws org.apache.shiro.cache.CacheException
Loads an existing EhCache from the cache manager, or starts a new cache if one is not found.- Specified by:
getCachein interfaceorg.apache.shiro.cache.CacheManager- Parameters:
name- the name of the cache to load/create.- Throws:
org.apache.shiro.cache.CacheException
-
init
public final void init() throws org.apache.shiro.cache.CacheExceptionInitializes this instance. If aCacheManagerhas been explicitly set (e.g. via Dependency Injection or programmatically) prior to calling this method, this method does nothing. However, if noCacheManagerhas been set, the default Ehcache singleton will be initialized, where Ehcache will look for anehcache.xmlfile 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:
initin interfaceorg.apache.shiro.util.Initializable- Throws:
org.apache.shiro.cache.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 instance before callinginit, this instance expects that same component to also destroy the CacheManager instance, and it will not attempt to do so.- Specified by:
destroyin interfaceorg.apache.shiro.util.Destroyable
-
-