Class AbstractSessionDAO

  • All Implemented Interfaces:
    SessionDAO
    Direct Known Subclasses:
    CachingSessionDAO, MemorySessionDAO

    public abstract class AbstractSessionDAO
    extends 
    implements SessionDAO
    An abstract SessionDAO implementation that performs some sanity checks on session creation and reading and allows for pluggable Session ID generation strategies if desired. The SessionDAO update and delete methods are left to subclasses.

    Session ID Generation

    This class also allows for plugging in a SessionIdGenerator for 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 the generateSessionId(org.apache.shiro.session.Session) method from within their doCreate(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 the SessionIdGenerator concept entirely and just return the data store's ID from the doCreate(org.apache.shiro.session.Session) implementation.

    Since:
    1.0
    • Method Detail

      • generateSessionId

        protected  generateSessionId​(Session session)
        Generates a new ID to be applied to the specified session instance. This method is usually called from within a subclass's doCreate(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 the sessionIdGenerator attribute - they can simply return the data store's generated ID from the doCreate(org.apache.shiro.session.Session) implementation if desired.

        This implementation uses the configured SessionIdGenerator to 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  create​(Session session)
        Creates the session by delegating EIS creation to subclasses via the doCreate(org.apache.shiro.session.Session) method, and then asserting that the returned sessionId is not null.
        Specified by:
        create in interface SessionDAO
        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 Session object.
      • assignSessionId

        protected void assignSessionId​(Session session,
                                        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 the AbstractSessionDAO implementation directly, but it is provided so subclasses don't need to know the Session implementation if they don't need to.

        This default implementation casts the argument to a SimpleSession, Shiro's default EIS implementation.

        Parameters:
        session - the session instance to which the sessionId will be applied
        sessionId - the id to assign to the specified session instance.
      • doCreate

        protected abstract  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().
      • doReadSession

        protected abstract  sessionId)
        Subclass implementation hook that retrieves the Session object from the underlying EIS or null if 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 null if a session with that ID could not be found.