Package org.apache.shiro.hazelcast.cache
Class HazelcastCacheManager
- java.lang.Object
-
- org.apache.shiro.hazelcast.cache.HazelcastCacheManager
-
- All Implemented Interfaces:
CacheManager
,Destroyable
,Initializable
public class HazelcastCacheManager extends Object implements CacheManager, Initializable, Destroyable
ACacheManager
implementation backed by Hazelcast, "an open source clustering and highly scalable data distribution platform for Java" This implementation interacts with aHazelcastInstance
toacquire
namedConcurrentMap
instances. Those clustered/distributed Map instances are then wrapped and made available toCacheManager
callers asMapCache
instances viagetCache(String)
.Configuration
This implementation's backingHazelcastInstance
can be configured in one of three ways:- Doing nothing and leveraging default Hazelcast configuration mechanisms
- Supplying an already-existing
HazelcastInstance
- Supplying a
Config
instance and using that to create a newHazelcastInstance
Default Configuration
If you simply instantiate aHazelcastCacheManager
and do nothing further, its backingHazelcastInstance
instance 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.xml
file at the root of the classpath, or if that is not present, thehazelcast-default.xml
file contained in the Hazelcast.jar
file itself.An existing
If you have created aHazelcastInstance
HazelcastInstance
outside 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 viaConfig
instancesetConfig(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
-
-
Constructor Summary
Constructors Constructor Description HazelcastCacheManager()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected com.hazelcast.core.HazelcastInstance
createHazelcastInstance()
Implicitly creates and returns a newHazelcastInstance
that will be used to back this implementation.void
destroy()
Destroys anyimplicitly created
backingHazelcastInstance
.protected com.hazelcast.core.HazelcastInstance
ensureHazelcastInstance()
Ensures that this implementation has a backingHazelcastInstance
, and if not, implicitly creates one viacreateHazelcastInstance()
.<K,V>
Cache<K,V>getCache(String name)
Returns aMapCache
instance representing the named Hazelcast-managedIMap
.com.hazelcast.config.Config
getConfig()
Returns the HazelcastConfig
object to use to create a backingHazelcastInstance
if one is notsupplied
, ornull
if the default Hazelcast configuration mechanisms will be used.com.hazelcast.core.HazelcastInstance
getHazelcastInstance()
Returns theHazelcastInstance
from which namedConcurrentMap
instances will be acquired to createMapCache
instances.void
init()
Initializes this instance byensuring
there is a backingHazelcastInstance
.protected boolean
isImplicitlyCreated()
NOT PART OF SHIRO'S ACCESSIBLE API.void
setConfig(com.hazelcast.config.Config config)
Sets the HazelcastConfig
object to use to create a backingHazelcastInstance
if one is notsupplied
.void
setHazelcastInstance(com.hazelcast.core.HazelcastInstance hazelcastInstance)
Sets theHazelcastInstance
from which namedConcurrentMap
instances will be acquired to createMapCache
instances.
-
-
-
Constructor Detail
-
HazelcastCacheManager
public HazelcastCacheManager()
-
-
Method Detail
-
getCache
public <K,V> Cache<K,V> getCache(String name) throws CacheException
Returns aMapCache
instance representing the named Hazelcast-managedIMap
. The Hazelcast Map is obtained by callinghazelcastInstance.getMap(name)
.- Specified by:
getCache
in interfaceCacheManager
- Type Parameters:
K
- the type of map keyV
- the type of map value- Parameters:
name
- the name of the cache to acquire.- Returns:
- a
MapCache
instance representing the named Hazelcast-managedIMap
. - Throws:
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 ShiroException
Initializes this instance byensuring
there is a backingHazelcastInstance
.- Specified by:
init
in interfaceInitializable
- Throws:
ShiroException
- See Also:
ensureHazelcastInstance()
,HazelcastInstance
-
createHazelcastInstance
protected com.hazelcast.core.HazelcastInstance createHazelcastInstance()
Implicitly creates and returns a newHazelcastInstance
that will be used to back this implementation. This implementation calls:return Hazelcast.newHazelcastInstance(this.config);
using anyconfigured
Config
object. If no config object has been specified,this.config
will benull
, thereby using Hazelcast's default configuration mechanism. Can be overridden by subclasses for custom creation behavior.- Returns:
- a new
HazelcastInstance
that 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. Returnstrue
if thisHazelcastCacheManager
instance implicitly created the backingHazelcastInstance
, orfalse
if one was externally provided viasetHazelcastInstance
.- Returns:
true
if thisHazelcastCacheManager
instance implicitly created the backingHazelcastInstance
, orfalse
if one was externally provided viasetHazelcastInstance
.
-
destroy
public void destroy() throws Exception
Destroys anyimplicitly created
backingHazelcastInstance
. If the backing Hazelcast was not implicitly created (i.e. because it was configured externally and supplied viasetHazelcastInstance
), this method does nothing.- Specified by:
destroy
in interfaceDestroyable
- Throws:
Exception
- if there is a problem shutting down
-
getHazelcastInstance
public com.hazelcast.core.HazelcastInstance getHazelcastInstance()
Returns theHazelcastInstance
from which namedConcurrentMap
instances will be acquired to createMapCache
instances.- Returns:
- the
HazelcastInstance
from which namedConcurrentMap
instances will be acquired to createMapCache
instances.
-
setHazelcastInstance
public void setHazelcastInstance(com.hazelcast.core.HazelcastInstance hazelcastInstance)
Sets theHazelcastInstance
from which namedConcurrentMap
instances will be acquired to createMapCache
instances.- Parameters:
hazelcastInstance
- theHazelcastInstance
from which namedConcurrentMap
instances will be acquired to createMapCache
instances.
-
getConfig
public com.hazelcast.config.Config getConfig()
Returns the HazelcastConfig
object to use to create a backingHazelcastInstance
if one is notsupplied
, ornull
if the default Hazelcast configuration mechanisms will be used.- Returns:
- the Hazelcast
Config
object to use to create a backingHazelcastInstance
if one is notsupplied
, ornull
if 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 HazelcastConfig
object to use to create a backingHazelcastInstance
if one is notsupplied
.null
can be set if the default Hazelcast configuration mechanisms will be used.- Parameters:
config
- the HazelcastConfig
object to use to create a backingHazelcastInstance
if one is notsupplied
, ornull
if the default Hazelcast configuration mechanisms will be used.
-
-