Class SessionManager

java.lang.Object
sunlabs.brazil.session.SessionManager
Direct Known Subclasses:
CacheManager, PJamaSessionManager

public class SessionManager extends Object
The SessionManager associates an object with a Session ID to give Handlers the ability to maintain state that lasts for the duration of a session instead of just for the duration of a request.

The SessionManager operates as a bag of globally accessible resources. Existing subclasses of the SessionManager also provide persistence, that is, a way to recover these resources even if the server process is terminated and later restarted, to get back to the state things were in.

A session manager is like a Dictionary only with fewer guarantees. Enumeration is not possible, and a "get" might return null even if there was a previous "put", so users should be prepared to handle that case.

Unlike a typical dictionary, a session manager uses two keys to identify to identify each resource. The first key, by convention, represents a client session id. The second (or resource) key is chosen to identify the resource within a session.

Care should be used when choosing resource keys, as they are global to a JVM, which may have several unrelated Brazil servers running at once. Sharing session manager resources between servers can cause unexpected behavior if done unintentionally.

Existing session manager implementations arrange for session resources that are Java Properties to be saved across restarts of the JVM, and should be used when practical.

Version:
2.2, 04/04/05
Author:
Stephen Uhler (stephen.uhler@sun.com), Colin Stevens (colin.stevens@sun.com)
  • Field Details

    • sessions

      protected Hashtable sessions
      NOTE: The previous implementation breaks for java > 1.1. Use this one instead. A Hashtable used when mapping Session IDs to objects.

      the key for the hashtable is derived from the "session" and "ident" objects.

  • Constructor Details

    • SessionManager

      public SessionManager()
  • Method Details

    • setSessionManager

      public static void setSessionManager(SessionManager mgr)
      Installs the given SessionManager object as the default session manager to be invoked when getSession is called.
      Parameters:
      mgr - The SessionManager object.
    • getSession

      public static Object getSession(Object session, Object ident, Class type)
      Returns the object associated with the given Session ID. Passing in the same (hash-key equivalent) Session ID will return the same object. This convenience method reflects common usage.
      Parameters:
      session - The Session ID for the persistent session information. If the session does not exist, a new one is created.
      ident - An arbitray identifier used to determine which object (associated with the given session) the caller wants.
      type - The Class of the object to create. If the given session and ident did not specify an existing object, a new one is created by calling newInstance based on the type. If null, then this method returns null if the object didn't exist, instead of allocating a new object.
      Returns:
      an object of type type, or null if the object doesn't exist and type is null.
    • get

      public static Object get(Object session, Object ident)
      get an object from the session manager. This static method will dispatch to the currently installed SessionManager instance.
    • put

      public static void put(Object session, Object ident, Object data)
      put an object into the session manager. This static method will dispatch to the currently installed SessionManager instance.
    • remove

      public static void remove(Object session, Object ident)
      Remove an object from the session manager. This static method will dispatch to the currently installed SessionManager instance.
    • getObj

      protected Object getObj(Object session, Object ident)
      Returns the object associated with the given Session ID and ident.
    • putObj

      protected void putObj(Object session, Object ident, Object value)
      Associates an object with a session id and ident. "value" may not be null.
    • removeObj

      protected void removeObj(Object session, Object ident)
      Removes the object associated with the given Session ID and ident.
    • makeKey

      protected String makeKey(Object session, Object ident)
      Invent a single key from the 2 separate ones