Class SessionRegistry

java.lang.Object
org.dlese.dpc.schemedit.SessionRegistry
All Implemented Interfaces:
EventListener, HttpSessionListener, ServletContextListener

public class SessionRegistry extends Object implements ServletContextListener, HttpSessionListener
Maintains a registry of sessions and also manages record locking.

As ServletContextListener, receives "contextInitialized" event, and sets servletContext attribute.

As HttpSessionListener, receives "sessionCreated" and "sessionDestroyed" events. SessionBeans are created when the session is created, and destroyed when the session is destroyed.

Author:
ostwald
  • Constructor Details

    • SessionRegistry

      public SessionRegistry()
      Constructor for the SessionRegistry object.

      activeSessionsMap map maintains the set of ALL active sessions, and is only modified by sessionCreated(HttpSessionEvent) and sessionDestroyed(HttpSessionEvent) listeners.

      sessionBeanMap maintains the set of sessionBean instances, which are created only for interactive sessions - which excludes sessions created by webservice requests.

  • Method Details

    • contextDestroyed

      public void contextDestroyed(ServletContextEvent event)
      Method called when a context is destroyed. Unregisters all sessions and empties lockedRecords.
      Specified by:
      contextDestroyed in interface ServletContextListener
      Parameters:
      event - Description of the Parameter
      See Also:
    • contextInitialized

      public void contextInitialized(ServletContextEvent event)
      Called when context is initialized, sets ServletContext as class attribute.
      Specified by:
      contextInitialized in interface ServletContextListener
      Parameters:
      event - Description of the Parameter
    • getLockedRecords

      public Map getLockedRecords()
      Gets the lockedRecords attribute of the SessionRegistry object, which stores all locked records and the sessions that owns the lock.
      Returns:
      The lockedRecords value
    • sessionCreated

      public void sessionCreated(HttpSessionEvent se)
      Called when a session is created with sole effect of putting the created session into the activeSessions map. Items are removed from the activeSessionsMap wehn sessions are destroyed.
      Specified by:
      sessionCreated in interface HttpSessionListener
      Parameters:
      se - notification event
    • sessionDestroyed

      public void sessionDestroyed(HttpSessionEvent se)
      Notified when a session is about to be destroyed - removes the session from the activeSessions map and unregisteres sessionBean.
      Specified by:
      sessionDestroyed in interface HttpSessionListener
      Parameters:
      se - notification event
      See Also:
    • isLocked

      public boolean isLocked(String recId)
      Returns true if there is a lock for the given record.

      Parameters:
      recId - Description of the Parameter
      Returns:
      The locked value
    • ownsLock

      public boolean ownsLock(String recId, String sessionId)
      Is a record locked by the given session?
      Parameters:
      recId - Description of the Parameter
      sessionId - Id of given session
      Returns:
      true if session owns lock to record
    • myLockedRecords

      public List myLockedRecords(String sessionId)
      Find records locked by a given session. Should never be more than one!
      Parameters:
      sessionId - Description of the Parameter
      Returns:
      list of record Ids
    • releaseAllLocks

      public void releaseAllLocks()
      Release all locked records.
    • releaseAllLocks

      public void releaseAllLocks(String sessionId)
      Release all the locks owned by a particluar session.
      Parameters:
      sessionId - Id of session that is releasing records.
    • getLock

      public boolean getLock(String recId, String sessionId)
      Gets the lock attribute of the SessionRegistry object.
      Parameters:
      recId - Description of the Parameter
      sessionId - Description of the Parameter
      Returns:
      true if lock is obtained by requesting session
    • getUniqueLock

      public boolean getUniqueLock(String recId, String sessionId)
      A version of getLock that only allows a single record to be locked by a single session at a time
      Parameters:
      recId - Description of the Parameter
      sessionId - Description of the Parameter
      Returns:
      The uniqueLock value
    • releaseLock

      public boolean releaseLock(String recId)
      Releases the lock for given record, regardless of what session owns the lock.
      Parameters:
      recId - Description of the Parameter
      Returns:
      true if there was a lock for given record ID
    • releaseLock

      public boolean releaseLock(String recId, String sessionId)
      Release lock held by specified sesssion
      Parameters:
      recId - Description of the Parameter
      sessionId - Description of the Parameter
      Returns:
      true if specified session owned lock on record
    • registerSessionBean

      public void registerSessionBean(SessionBean sessionBean)
      Register a sessionBean by putting it into the sessionBeanMap.
      Parameters:
      sessionBean - Description of the Parameter
    • unregisterSessionBean

      public void unregisterSessionBean(String sessionId)
      Unregister a sessionBean by calling its destroy method (releasing all held locks), and removing its entry from the sessionBeanMap.
      Parameters:
      sessionId - Description of the Parameter
    • getSession

      public HttpSession getSession(String id)
      Returns active session corresponding to given Id.
      Parameters:
      id - session id
      Returns:
      a HttpSession instance, or null if none was found for given id.
    • getSessionBean

      public SessionBean getSessionBean(HttpServletRequest request)
      Return the SessionBean associated with the HttpSession for the provided request.

      If a sessionBean does not yet exist for a valid session, then it is created and bound to the session context. This method is the standard way of obtaining of obtaining a sessionBean from within a Controller or FormBean.

      Parameters:
      request - A request instance that is associated with a session
      Returns:
      The sessionBean value for the request's session.
    • getUserSessionBeans

      public List getUserSessionBeans(User user)
    • getSessionBeans

      public List getSessionBeans()
      Returns a list of all active SessionBean instances.

      Unregisters sessionBeans corresponding to sessions that have been invalidated.

      Returns:
      list of SessionBean
    • destroy

      public void destroy()
      Release locked records