Package org.apache.shiro.session.mgt.eis
Class AbstractSessionDAO
- java.lang.Object
-
- org.apache.shiro.session.mgt.eis.AbstractSessionDAO
-
- All Implemented Interfaces:
SessionDAO
- Direct Known Subclasses:
CachingSessionDAO,MemorySessionDAO
public abstract class AbstractSessionDAO extends Object implements SessionDAO
An abstractSessionDAOimplementation that performs some sanity checks on session creation and reading and allows for pluggable Session ID generation strategies if desired. TheSessionDAOupdateanddeletemethods are left to subclasses.Session ID Generation
This class also allows for plugging in aSessionIdGeneratorfor custom ID generation strategies. This is optional, as the default generator is probably sufficient for most cases. Subclass implementations that do use a generator (default or custom) will want to call thegenerateSessionId(org.apache.shiro.session.Session)method from within theirdoCreate(org.apache.shiro.session.Session)implementations. Subclass implementations that rely on the EIS data store to generate the ID automatically (e.g. when the session ID is also an auto-generated primary key), they can simply ignore theSessionIdGeneratorconcept entirely and just return the data store's ID from thedoCreate(org.apache.shiro.session.Session)implementation.- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description AbstractSessionDAO()Default no-arg constructor that defaults thesessionIdGeneratorto be aJavaUuidSessionIdGenerator.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidassignSessionId(Session session, Serializable sessionId)Utility method available to subclasses that wish to assign a generated session ID to the session instance directly.Serializablecreate(Session session)Creates the session by delegating EIS creation to subclasses via thedoCreate(org.apache.shiro.session.Session)method, and then asserting that the returned sessionId is not null.protected abstract SerializabledoCreate(Session session)Subclass hook to actually persist the given Session instance to the underlying EIS.protected abstract SessiondoReadSession(Serializable sessionId)Subclass implementation hook that retrieves the Session object from the underlying EIS ornullif a session with that ID could not be found.protected SerializablegenerateSessionId(Session session)Generates a new ID to be applied to the specifiedsessioninstance.SessionIdGeneratorgetSessionIdGenerator()Returns theSessionIdGeneratorused by thegenerateSessionId(org.apache.shiro.session.Session)method.SessionreadSession(Serializable sessionId)Retrieves the Session object from the underlying EIS identified by sessionId by delegating to thedoReadSession(java.io.Serializable)method.voidsetSessionIdGenerator(SessionIdGenerator sessionIdGenerator)Sets theSessionIdGeneratorused by thegenerateSessionId(org.apache.shiro.session.Session)method.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.shiro.session.mgt.eis.SessionDAO
delete, getActiveSessions, update
-
-
-
-
Constructor Detail
-
AbstractSessionDAO
public AbstractSessionDAO()
Default no-arg constructor that defaults thesessionIdGeneratorto be aJavaUuidSessionIdGenerator.
-
-
Method Detail
-
getSessionIdGenerator
public SessionIdGenerator getSessionIdGenerator()
Returns theSessionIdGeneratorused by thegenerateSessionId(org.apache.shiro.session.Session)method. Unless overridden by thesetSessionIdGenerator(SessionIdGenerator)method, the default instance is aJavaUuidSessionIdGenerator.- Returns:
- the
SessionIdGeneratorused by thegenerateSessionId(org.apache.shiro.session.Session)method.
-
setSessionIdGenerator
public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator)
Sets theSessionIdGeneratorused by thegenerateSessionId(org.apache.shiro.session.Session)method. Unless overridden by this method, the default instance ss aJavaUuidSessionIdGenerator.- Parameters:
sessionIdGenerator- theSessionIdGeneratorto use in thegenerateSessionId(org.apache.shiro.session.Session)method.
-
generateSessionId
protected Serializable generateSessionId(Session session)
Generates a new ID to be applied to the specifiedsessioninstance. This method is usually called from within a subclass'sdoCreate(org.apache.shiro.session.Session)implementation where they assign the returned id to the session instance and then create a record with this ID in the EIS data store. Subclass implementations backed by EIS data stores that auto-generate IDs during record creation, such as relational databases, don't need to use this method or thesessionIdGeneratorattribute - they can simply return the data store's generated ID from thedoCreate(org.apache.shiro.session.Session)implementation if desired. This implementation uses theconfiguredSessionIdGeneratorto create the ID.- Parameters:
session- the new session instance for which an ID will be generated and then assigned- Returns:
- the generated ID to assign
-
create
public Serializable create(Session session)
Creates the session by delegating EIS creation to subclasses via thedoCreate(org.apache.shiro.session.Session)method, and then asserting that the returned sessionId is not null.- Specified by:
createin interfaceSessionDAO- Parameters:
session- Session object to create in the EIS and associate with an ID.- Returns:
- the EIS id (e.g. primary key) of the created
Sessionobject.
-
assignSessionId
protected void assignSessionId(Session session, Serializable sessionId)
Utility method available to subclasses that wish to assign a generated session ID to the session instance directly. This method is not used by theAbstractSessionDAOimplementation directly, but it is provided so subclasses don't need to know theSessionimplementation if they don't need to. This default implementation casts the argument to aSimpleSession, Shiro's default EIS implementation.- Parameters:
session- the session instance to which the sessionId will be appliedsessionId- the id to assign to the specified session instance.
-
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 by delegating to thedoReadSession(java.io.Serializable)method. Ifnullis returned from that method, anUnknownSessionExceptionwill be thrown.- Specified by:
readSessionin interfaceSessionDAO- 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 EIS.
-
doReadSession
protected abstract Session doReadSession(Serializable sessionId)
Subclass implementation hook that retrieves the Session object from the underlying EIS ornullif a session with that ID could not be found.- Parameters:
sessionId- the id of the Session to retrieve.- Returns:
- the Session in the EIS identified by sessionId or
nullif a session with that ID could not be found.
-
-