Package org.apache.shiro.hazelcast.cache
Class HazelcastCacheManager
- java.lang.Object
-
- org.apache.shiro.hazelcast.cache.HazelcastCacheManager
-
- All Implemented Interfaces:
org.apache.shiro.cache.CacheManager,org.apache.shiro.util.Destroyable,org.apache.shiro.util.Initializable
public class HazelcastCacheManager extends Object implements org.apache.shiro.cache.CacheManager, org.apache.shiro.util.Initializable, org.apache.shiro.util.Destroyable
ACacheManagerimplementation backed by Hazelcast, "an open source clustering and highly scalable data distribution platform for Java" This implementation interacts with aHazelcastInstancetoacquirenamedConcurrentMapinstances. Those clustered/distributed Map instances are then wrapped and made available toCacheManagercallers asMapCacheinstances viagetCache(String).Configuration
This implementation's backingHazelcastInstancecan be configured in one of three ways:- Doing nothing and leveraging default Hazelcast configuration mechanisms
- Supplying an already-existing
HazelcastInstance - Supplying a
Configinstance and using that to create a newHazelcastInstance
Default Configuration
If you simply instantiate aHazelcastCacheManagerand do nothing further, its backingHazelcastInstanceinstance will be created automatically by callingHazelcast.newHazelcastInstance(null). The null argument instructs Hazelcast to use whatever default configuration mechanism it has at its disposal, usually ahazelcast.xmlfile at the root of the classpath, or if that is not present, thehazelcast-default.xmlfile contained in the Hazelcast.jarfile itself.An existing
If you have created aHazelcastInstanceHazelcastInstanceoutside of Shiro's knowledge/control, you can simply configure it to be used by callingsetHazelcastInstance.A
If you do not want to use the above two options, you can have programmatic control over all of Hazelcast's configuration by creating and configuring a Config instance. Once constructed, you can set it viaConfiginstancesetConfig(config). This config instance will be used to acquire a new Hazelcast instance by callingHazelcast.newHazelcastInstance(config)- Since:
- 1.3
- See Also:
- Hazelcast Configuration Documentation
-
-
Field Summary
Fields Modifier and Type Field Description static org.slf4j.Loggerlog
-
Constructor Summary
Constructors Constructor Description HazelcastCacheManager()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected com.hazelcast.core.HazelcastInstancecreateHazelcastInstance()Implicitly creates and returns a newHazelcastInstancethat will be used to back this implementation.voiddestroy()Destroys anyimplicitly createdbackingHazelcastInstance.protected com.hazelcast.core.HazelcastInstanceensureHazelcastInstance()Ensures that this implementation has a backingHazelcastInstance, and if not, implicitly creates one viacreateHazelcastInstance().<K,V>
org.apache.shiro.cache.Cache<K,V>getCache(String name)Returns aMapCacheinstance representing the named Hazelcast-managedIMap.com.hazelcast.config.ConfiggetConfig()Returns the HazelcastConfigobject to use to create a backingHazelcastInstanceif one is notsupplied, ornullif the default Hazelcast configuration mechanisms will be used.com.hazelcast.core.HazelcastInstancegetHazelcastInstance()Returns theHazelcastInstancefrom which namedConcurrentMapinstances will be acquired to createMapCacheinstances.voidinit()Initializes this instance byensuringthere is a backingHazelcastInstance.protected booleanisImplicitlyCreated()NOT PART OF SHIRO'S ACCESSIBLE API.voidsetConfig(com.hazelcast.config.Config config)Sets the HazelcastConfigobject to use to create a backingHazelcastInstanceif one is notsupplied.voidsetHazelcastInstance(com.hazelcast.core.HazelcastInstance hazelcastInstance)Sets theHazelcastInstancefrom which namedConcurrentMapinstances will be acquired to createMapCacheinstances.
-
-
-
Method Detail
-
getCache
public <K,V> org.apache.shiro.cache.Cache<K,V> getCache(String name) throws org.apache.shiro.cache.CacheException
Returns aMapCacheinstance representing the named Hazelcast-managedIMap. The Hazelcast Map is obtained by callinghazelcastInstance.getMap(name).- Specified by:
getCachein interfaceorg.apache.shiro.cache.CacheManager- Type Parameters:
K- the type of map keyV- the type of map value- Parameters:
name- the name of the cache to acquire.- Returns:
- a
MapCacheinstance representing the named Hazelcast-managedIMap. - Throws:
org.apache.shiro.cache.CacheException- See Also:
HazelcastInstance.getMap(String),ensureHazelcastInstance()
-
ensureHazelcastInstance
protected com.hazelcast.core.HazelcastInstance ensureHazelcastInstance()
Ensures that this implementation has a backingHazelcastInstance, and if not, implicitly creates one viacreateHazelcastInstance().- Returns:
- the backing (potentially newly created)
HazelcastInstance. - See Also:
createHazelcastInstance(),HazelcastInstance
-
init
public void init() throws org.apache.shiro.ShiroExceptionInitializes this instance byensuringthere is a backingHazelcastInstance.- Specified by:
initin interfaceorg.apache.shiro.util.Initializable- Throws:
org.apache.shiro.ShiroException- See Also:
ensureHazelcastInstance(),HazelcastInstance
-
createHazelcastInstance
protected com.hazelcast.core.HazelcastInstance createHazelcastInstance()
Implicitly creates and returns a newHazelcastInstancethat will be used to back this implementation. This implementation calls:return Hazelcast.newHazelcastInstance(this.config);
using anyconfiguredConfigobject. If no config object has been specified,this.configwill benull, thereby using Hazelcast's default configuration mechanism. Can be overridden by subclasses for custom creation behavior.- Returns:
- a new
HazelcastInstancethat will be used to back this implementation - See Also:
Hazelcast.newHazelcastInstance(com.hazelcast.config.Config),Config
-
isImplicitlyCreated
protected final boolean isImplicitlyCreated()
NOT PART OF SHIRO'S ACCESSIBLE API. DO NOT DEPEND ON THIS. This method was added for testing purposes only. Returnstrueif thisHazelcastCacheManagerinstance implicitly created the backingHazelcastInstance, orfalseif one was externally provided viasetHazelcastInstance.- Returns:
trueif thisHazelcastCacheManagerinstance implicitly created the backingHazelcastInstance, orfalseif one was externally provided viasetHazelcastInstance.
-
destroy
public void destroy() throws ExceptionDestroys anyimplicitly createdbackingHazelcastInstance. If the backing Hazelcast was not implicitly created (i.e. because it was configured externally and supplied viasetHazelcastInstance), this method does nothing.- Specified by:
destroyin interfaceorg.apache.shiro.util.Destroyable- Throws:
Exception- if there is a problem shutting down
-
getHazelcastInstance
public com.hazelcast.core.HazelcastInstance getHazelcastInstance()
Returns theHazelcastInstancefrom which namedConcurrentMapinstances will be acquired to createMapCacheinstances.- Returns:
- the
HazelcastInstancefrom which namedConcurrentMapinstances will be acquired to createMapCacheinstances.
-
setHazelcastInstance
public void setHazelcastInstance(com.hazelcast.core.HazelcastInstance hazelcastInstance)
Sets theHazelcastInstancefrom which namedConcurrentMapinstances will be acquired to createMapCacheinstances.- Parameters:
hazelcastInstance- theHazelcastInstancefrom which namedConcurrentMapinstances will be acquired to createMapCacheinstances.
-
getConfig
public com.hazelcast.config.Config getConfig()
Returns the HazelcastConfigobject to use to create a backingHazelcastInstanceif one is notsupplied, ornullif the default Hazelcast configuration mechanisms will be used.- Returns:
- the Hazelcast
Configobject to use to create a backingHazelcastInstanceif one is notsupplied, ornullif the default Hazelcast configuration mechanisms will be used. - See Also:
Hazelcast.newHazelcastInstance(com.hazelcast.config.Config)
-
setConfig
public void setConfig(com.hazelcast.config.Config config)
Sets the HazelcastConfigobject to use to create a backingHazelcastInstanceif one is notsupplied.nullcan be set if the default Hazelcast configuration mechanisms will be used.- Parameters:
config- the HazelcastConfigobject to use to create a backingHazelcastInstanceif one is notsupplied, ornullif the default Hazelcast configuration mechanisms will be used.
-
-