org.apache.shiro.session.mgt.eis
Class CachingSessionDAO

java.lang.Object
  extended by org.apache.shiro.session.mgt.eis.AbstractSessionDAO
      extended by org.apache.shiro.session.mgt.eis.CachingSessionDAO
All Implemented Interfaces:
CacheManagerAware, SessionDAO
Direct Known Subclasses:
EnterpriseCacheSessionDAO

public abstract class CachingSessionDAO
extends AbstractSessionDAO
implements CacheManagerAware

An CachingSessionDAO is a SessionDAO that provides a transparent caching layer between the components that use it and the underlying EIS (Enterprise Information System) session backing store (for example, filesystem, database, enterprise grid/cloud, etc).

This implementation caches all active sessions in a configured activeSessionsCache. This property is null by default and if one is not explicitly set, a cacheManager is expected to be configured which will in turn be used to acquire the Cache instance to use for the activeSessionsCache.

All SessionDAO methods are implemented by this class to employ caching behavior and delegates the actual EIS operations to respective do* methods to be implemented by subclasses (doCreate, doRead, etc).

Since:
0.2

Field Summary
static String ACTIVE_SESSION_CACHE_NAME
          The default active sessions cache name, equal to shiro-activeSessionCache.
 
Constructor Summary
CachingSessionDAO()
          Default no-arg constructor.
 
Method Summary
protected  void cache(Session session, Serializable sessionId)
          Caches the specified session under the cache entry key of sessionId.
protected  void cache(Session session, Serializable sessionId, Cache<Serializable,Session> cache)
          Caches the specified session in the given cache under the key of sessionId.
 Serializable create(Session session)
          Calls super.create(session), then caches the session keyed by the returned sessionId, and then returns this sessionId.
protected  Cache<Serializable,Session> createActiveSessionsCache()
          Creates a cache instance used to store active sessions.
 void delete(Session session)
          Removes the specified session from any cache and then permanently deletes the session from the EIS by delegating to doDelete(org.apache.shiro.session.Session).
protected abstract  void doDelete(Session session)
          Subclass implementation hook to permanently delete the given Session from the underlying EIS.
protected abstract  void doUpdate(Session session)
          Subclass implementation hook to actually persist the Session's state to the underlying EIS.
 Collection<Session> getActiveSessions()
          Returns all active sessions in the system.
 Cache<Serializable,Session> getActiveSessionsCache()
          Returns the cache instance to use for storing active sessions.
 String getActiveSessionsCacheName()
          Returns the name of the actives sessions cache to be returned by the CacheManager.
protected  Session getCachedSession(Serializable sessionId)
          Returns the cached session with the corresponding sessionId or null if there is no session cached under that id (or if there is no Cache).
protected  Session getCachedSession(Serializable sessionId, Cache<Serializable,Session> cache)
          Returns the Session with the specified id from the specified cache.
 CacheManager getCacheManager()
          Returns the CacheManager to use for acquiring the activeSessionsCache if one is not configured.
 Session readSession(Serializable sessionId)
          Attempts to acquire the Session from the cache first using the session ID as the cache key.
 void setActiveSessionsCache(Cache<Serializable,Session> cache)
          Sets the cache instance to use for storing active sessions.
 void setActiveSessionsCacheName(String activeSessionsCacheName)
          Sets the name of the active sessions cache to be returned by the CacheManager.
 void setCacheManager(CacheManager cacheManager)
          Sets the cacheManager to use for acquiring the activeSessionsCache if one is not configured.
protected  void uncache(Session session)
          Removes the specified Session from the cache.
 void update(Session session)
          Updates the state of the given session to the EIS by first delegating to doUpdate(org.apache.shiro.session.Session).
 
Methods inherited from class org.apache.shiro.session.mgt.eis.AbstractSessionDAO
assignSessionId, doCreate, doReadSession, generateSessionId, getSessionIdGenerator, setSessionIdGenerator
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ACTIVE_SESSION_CACHE_NAME

public static final String ACTIVE_SESSION_CACHE_NAME
The default active sessions cache name, equal to shiro-activeSessionCache.

See Also:
Constant Field Values
Constructor Detail

CachingSessionDAO

public CachingSessionDAO()
Default no-arg constructor.

Method Detail

setCacheManager

public void setCacheManager(CacheManager cacheManager)
Sets the cacheManager to use for acquiring the activeSessionsCache if one is not configured.

Specified by:
setCacheManager in interface CacheManagerAware
Parameters:
cacheManager - the manager to use for constructing the session cache.

getCacheManager

public CacheManager getCacheManager()
Returns the CacheManager to use for acquiring the activeSessionsCache if one is not configured. That is, the CacheManager will only be used if the activeSessionsCache property is null.

Returns:
the CacheManager used by the implementation that creates the activeSessions Cache.

getActiveSessionsCacheName

public String getActiveSessionsCacheName()
Returns the name of the actives sessions cache to be returned by the CacheManager. Unless overridden by setActiveSessionsCacheName(String), defaults to ACTIVE_SESSION_CACHE_NAME.

Returns:
the name of the active sessions cache.

setActiveSessionsCacheName

public void setActiveSessionsCacheName(String activeSessionsCacheName)
Sets the name of the active sessions cache to be returned by the CacheManager. Defaults to ACTIVE_SESSION_CACHE_NAME.

Parameters:
activeSessionsCacheName - the name of the active sessions cache to be returned by the CacheManager.

getActiveSessionsCache

public Cache<Serializable,Session> getActiveSessionsCache()
Returns the cache instance to use for storing active sessions. If one is not available (it is null), it will be acquired from the configured CacheManager using the activeSessionsCacheName.

Returns:
the cache instance to use for storing active sessions or null if the Cache instance should be retrieved from the

setActiveSessionsCache

public void setActiveSessionsCache(Cache<Serializable,Session> cache)
Sets the cache instance to use for storing active sessions. If one is not set (it remains null), it will be acquired from the configured CacheManager using the activeSessionsCacheName.

Parameters:
cache - the cache instance to use for storing active sessions or null if the cache is to be acquired from the configured CacheManager.

createActiveSessionsCache

protected Cache<Serializable,Session> createActiveSessionsCache()
Creates a cache instance used to store active sessions. Creation is done by first acquiring the CacheManager. If the cache manager is not null, the cache returned is that resulting from the following call:
       String name = getActiveSessionsCacheName();
 cacheManager.getCache(name);

Returns:
a cache instance used to store active sessions, or null if the CacheManager has not been set.

create

public Serializable create(Session session)
Calls super.create(session), then caches the session keyed by the returned sessionId, and then returns this sessionId.

Specified by:
create in interface SessionDAO
Overrides:
create in class AbstractSessionDAO
Parameters:
session - Session object to create in the EIS and then cache.
Returns:
the EIS id (e.g. primary key) of the created Session object.

getCachedSession

protected Session getCachedSession(Serializable sessionId)
Returns the cached session with the corresponding sessionId or null if there is no session cached under that id (or if there is no Cache).

Parameters:
sessionId - the id of the cached session to acquire.
Returns:
the cached session with the corresponding sessionId, or null if the session does not exist or is not cached.

getCachedSession

protected Session getCachedSession(Serializable sessionId,
                                   Cache<Serializable,Session> cache)
Returns the Session with the specified id from the specified cache. This method simply calls cache.get(sessionId) and can be overridden by subclasses for custom acquisition behavior.

Parameters:
sessionId - the id of the session to acquire.
cache - the cache to acquire the session from
Returns:
the cached session, or null if the session wasn't in the cache.

cache

protected void cache(Session session,
                     Serializable sessionId)
Caches the specified session under the cache entry key of sessionId.

Parameters:
session - the session to cache
sessionId - the session id, to be used as the cache entry key.
Since:
1.0

cache

protected void cache(Session session,
                     Serializable sessionId,
                     Cache<Serializable,Session> cache)
Caches the specified session in the given cache under the key of sessionId. This implementation simply calls cache.put(sessionId,session) and can be overridden for custom behavior.

Parameters:
session - the session to cache
sessionId - the id of the session, expected to be the cache key.
cache - the cache to store the session

readSession

public Session readSession(Serializable sessionId)
                    throws UnknownSessionException
Attempts to acquire the Session from the cache first using the session ID as the cache key. If no session is found, super.readSession(sessionId) is called to perform the actual retrieval.

Specified by:
readSession in interface SessionDAO
Overrides:
readSession in class AbstractSessionDAO
Parameters:
sessionId - the id of the session to retrieve from the EIS.
Returns:
the session identified by sessionId in the EIS.
Throws:
UnknownSessionException - if the id specified does not correspond to any session in the cache or EIS.

update

public void update(Session session)
            throws UnknownSessionException
Updates the state of the given session to the EIS by first delegating to doUpdate(org.apache.shiro.session.Session). If the session is a ValidatingSession, it will be added to the cache only if it is ValidatingSession.isValid() and if invalid, will be removed from the cache. If it is not a ValidatingSession instance, it will be added to the cache in any event.

Specified by:
update in interface SessionDAO
Parameters:
session - the session object to update in the EIS.
Throws:
UnknownSessionException - if no existing EIS session record exists with the identifier of session.getId()

doUpdate

protected abstract void doUpdate(Session session)
Subclass implementation hook to actually persist the Session's state to the underlying EIS.

Parameters:
session - the session object whose state will be propagated to the EIS.

delete

public void delete(Session session)
Removes the specified session from any cache and then permanently deletes the session from the EIS by delegating to doDelete(org.apache.shiro.session.Session).

Specified by:
delete in interface SessionDAO
Parameters:
session - the session to remove from caches and permanently delete from the EIS.

doDelete

protected abstract void doDelete(Session session)
Subclass implementation hook to permanently delete the given Session from the underlying EIS.

Parameters:
session - the session instance to permanently delete from the EIS.

uncache

protected void uncache(Session session)
Removes the specified Session from the cache.

Parameters:
session - the session to remove from the cache.

getActiveSessions

public Collection<Session> getActiveSessions()
Returns all active sessions in the system.

This implementation merely returns the sessions found in the activeSessions cache. Subclass implementations may wish to override this method to retrieve them in a different way, perhaps by an RDBMS query or by other means.

Specified by:
getActiveSessions in interface SessionDAO
Returns:
the sessions found in the activeSessions cache.


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