JSecurity

org.jsecurity.session.mgt.eis
Class CachingSessionDAO

java.lang.Object
  extended by org.jsecurity.session.mgt.eis.CachingSessionDAO
All Implemented Interfaces:
CacheManagerAware, SessionDAO
Direct Known Subclasses:
MemorySessionDAO

public abstract class CachingSessionDAO
extends Object
implements SessionDAO, 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) for enhanced performance.

This implementation caches all active sessions in a cache created by a CacheManager. 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
Author:
Les Hazlewood

Field Summary
static String ACTIVE_SESSION_CACHE_NAME
           
 
Constructor Summary
CachingSessionDAO()
          JavaBeans compatible constructor.
 
Method Summary
 Serializable create(Session session)
          Creates the session by delegating EIS creation to subclasses via the doCreate(org.jsecurity.session.Session) method, and then caches the session.
protected  Cache createActiveSessionsCache()
           
 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.jsecurity.session.Session).
protected abstract  Serializable doCreate(Session session)
          Subclass hook to actually persist the given Session instance to the underlying EIS.
protected abstract  void doDelete(Session session)
          Subclass implementation hook to permanently delete the given Session from the underlying EIS.
protected abstract  Session doReadSession(Serializable sessionId)
          Subclass implmentation hook to actually retrieve the Session object from the underlying EIS.
protected abstract  void doUpdate(Session session)
          Subclass implementation hook to actually persist the Session's state to the underlying EIS.
protected  void ensureUncached(Serializable sessionId)
          Ensures that there is no cache entry already in place for a session with id of sessionId.
 Collection<Session> getActiveSessions()
          Returns all active sessions in the system.
 Cache getActiveSessionsCache()
           
protected  Cache getActiveSessionsCacheLazy()
           
 String getActiveSessionsCacheName()
           
 CacheManager getCacheManager()
          Returns the CacheManager used by the implementation that creates the activeSessions Cache.
 Session readSession(Serializable sessionId)
          Retrieves the Session object from the underlying EIS identified by sessionId.
 void setActiveSessionsCache(Cache cache)
           
 void setActiveSessionsCacheName(String activeSessionsCacheName)
           
 void setCacheManager(CacheManager cacheManager)
          Sets the cacheManager to use for constructing the session cache.
 void update(Session session)
          Updates the state of the given session to the EIS.
protected  void verifySessionId(Serializable sessionId)
          Ensures the sessionId returned from the subclass implementation of doCreate(org.jsecurity.session.Session) is not null and not already in use.
 
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
See Also:
Constant Field Values
Constructor Detail

CachingSessionDAO

public CachingSessionDAO()
JavaBeans compatible constructor.

Method Detail

setCacheManager

public void setCacheManager(CacheManager cacheManager)
Sets the cacheManager to use for constructing the session cache.

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

getCacheManager

public CacheManager getCacheManager()
Returns the CacheManager used by the implementation that creates the activeSessions Cache.

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

getActiveSessionsCacheName

public String getActiveSessionsCacheName()

setActiveSessionsCacheName

public void setActiveSessionsCacheName(String activeSessionsCacheName)

getActiveSessionsCache

public Cache getActiveSessionsCache()

getActiveSessionsCacheLazy

protected Cache getActiveSessionsCacheLazy()

setActiveSessionsCache

public void setActiveSessionsCache(Cache cache)

createActiveSessionsCache

protected Cache createActiveSessionsCache()

create

public Serializable create(Session session)
Creates the session by delegating EIS creation to subclasses via the doCreate(org.jsecurity.session.Session) method, and then caches the session.

Specified by:
create in interface SessionDAO
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.

verifySessionId

protected void verifySessionId(Serializable sessionId)
Ensures the sessionId returned from the subclass implementation of doCreate(org.jsecurity.session.Session) is not null and not already in use.

Parameters:
sessionId - session id returned from the subclass implementation of doCreate(org.jsecurity.session.Session)

ensureUncached

protected void ensureUncached(Serializable sessionId)
Ensures that there is no cache entry already in place for a session with id of sessionId. Used by the verifySessionId(java.io.Serializable) implementation.

Parameters:
sessionId - the session id to check for non-existence in the cache.

doCreate

protected abstract Serializable doCreate(Session session)
Subclass hook to actually persist the given Session instance to the underlying EIS.

Parameters:
session - the Session instance to persist to the EIS.
Returns:
the id of the session created in the EIS (i.e. this is almost always a primary key and should be the value returned from Session.getId().

readSession

public Session readSession(Serializable sessionId)
                    throws UnknownSessionException
Retrieves the Session object from the underlying EIS identified by sessionId.

Upon receiving the Session object from the subclass's doReadSession(java.io.Serializable) implementation, it will be cached first and then returned to the caller.

Specified by:
readSession in interface SessionDAO
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.

doReadSession

protected abstract Session doReadSession(Serializable sessionId)
Subclass implmentation hook to actually retrieve the Session object from the underlying EIS.

Parameters:
sessionId - the id of the Session to retrieve.
Returns:
the Session in the EIS identified by sessionId

update

public void update(Session session)
            throws UnknownSessionException
Updates the state of the given session to the EIS.

If the specified session was previously cached, and the session is now invalid, it will be removed from the cache.

If the specified session is not stopped or expired, and was not yet in the cache, it will be added to the cache.

Finally, this method calls doUpdate(org.jsecurity.session.Session) for the subclass to actually push the object state to the EIS.

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.jsecurity.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.

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.

JSecurity

Copyright © 2004-2008 JSecurity.