FileDocCategorySizeDatePackage
SessionManager.javaAPI DocGlassfish v2 API11023Tue May 22 16:54:54 BST 2007oracle.toplink.essentials.tools.sessionmanagement

SessionManager

public class SessionManager extends Object

Purpose: Global session location.

Description: This allows for a global session local which can be accessed globally from other classes. This is needed for EJB data stores as they must have a globally accessible place to access the session. This can be by EJB session beans, BMP beans and CMP beans as well as Servlets and other three-tier services.

Responsibilities:

  • Store a global session.
  • Allow the storage of alternative sessions as well.

author
James Sutherland
since
TOPLink/Java 3.0

Fields Summary
protected static SessionManager
manager
protected AbstractSession
defaultSession
protected Hashtable
sessions
Constructors Summary
public SessionManager()
PUBLIC: The default constructor to create a new session manager.

    
                   
      
        sessions = new Hashtable(5);
    
Methods Summary
public voidaddSession(oracle.toplink.essentials.sessions.Session session)
INTERNAL: add an named session to the hashtable. session must have a name prior to setting into session Manager

        getSessions().put(session.getName(), session);
    
public voidaddSession(java.lang.String sessionName, oracle.toplink.essentials.sessions.Session session)
ADVANCED: add an named session to the hashtable.

        session.setName(sessionName);
        getSessions().put(sessionName, session);
    
private voiddestroy(oracle.toplink.essentials.sessions.DatabaseSession session)

        if (session.isConnected()) {
            session.logout();
        }

        sessions.remove(session.getName());
        session = null;
    
public voiddestroyAllSessions()
INTERNAL: Destroy all sessions held onto by this manager.

        Enumeration toRemoveSessions = getSessions().elements();

        while (toRemoveSessions.hasMoreElements()) {
            destroy((DatabaseSession)toRemoveSessions.nextElement());
        }
    
public voiddestroySession(java.lang.String sessionName)
INTERNAL: Destroy the session defined by sessionName on this manager.

        DatabaseSession session = (DatabaseSession)getSessions().get(sessionName);

        if (session != null) {
            destroy(session);
        } else {
            logAndThrowException(SessionLog.WARNING, ValidationException.noSessionRegisteredForName(sessionName));
        }
    
public oracle.toplink.essentials.sessions.SessiongetDefaultSession()
PUBLIC: Return the default session.

        if (defaultSession == null) {
            defaultSession = getSession("default");
        }
        return defaultSession;
    
public static synchronized oracle.toplink.essentials.tools.sessionmanagement.SessionManagergetManager()
PUBLIC: Return the singleton session manager. This allow global access to a set of named sessions.

        if (manager == null) {
            manager = new SessionManager();
        }

        return manager;
    
private java.lang.ClassLoadergetMyClassLoader()

        ClassLoader classLoader = null;
        if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
            try{
                return (ClassLoader)AccessController.doPrivileged(new PrivilegedGetClassLoaderForClass(this.getClass()));
            } catch (PrivilegedActionException exc){
                return null;
            }
        } else {
            return PrivilegedAccessHelper.getClassLoaderForClass(this.getClass());
        }
    
public oracle.toplink.essentials.internal.sessions.AbstractSessiongetSession(java.lang.String sessionName)
PUBLIC: Return the session by name. Use the classLoader that loaded the SessionManager.

        return getSession(sessionName, getMyClassLoader(), true, false, false);
    
public oracle.toplink.essentials.internal.sessions.AbstractSessiongetSession(java.lang.String sessionName, boolean shouldLoginSession)
PUBLIC: Return the session by name. Use the classLoader that loaded the SessionManager. Log the session in only if the user specifies to.

        return getSession(sessionName, getMyClassLoader(), shouldLoginSession, false, false);
    
public oracle.toplink.essentials.internal.sessions.AbstractSessiongetSession(java.lang.String sessionName, boolean shouldLoginSession, boolean shouldRefreshSession)
PUBLIC: Return the session by name. Use the classLoader that loaded the SessionManager. Log the session in only if the user specifies to. Refresh the session only if the user specifies to.

        return getSession(sessionName, getMyClassLoader(), shouldLoginSession, shouldRefreshSession, false);
    
public oracle.toplink.essentials.internal.sessions.AbstractSessiongetSession(java.lang.String sessionName, java.lang.ClassLoader objectClassLoader)
PUBLIC: Return the session by name. Provide the class loader for loading the project, the configuration file and the deployed classes. E.g. SessionManager.getManager().getSession("mySession", MySessionBean.getClassLoader()); This method will cause the class loader to be compared with the classloader used to load the original session of this name, with this classloader. If they are not the same then the session will be refreshed.

        return getSession(sessionName, objectClassLoader, true, false, false);
    
public synchronized oracle.toplink.essentials.internal.sessions.AbstractSessiongetSession(java.lang.String sessionName, java.lang.ClassLoader objectClassLoader, boolean shouldLoginSession, boolean shouldRefreshSession, boolean shouldCheckClassLoader)
PUBLIC: Return the session by name, loading the configuration from the file specified in the xmlLoader. Provide the class loader for loading the project, the configuration file and the deployed classes. Pass in true for shouldLoginSession if the session returned should be logged in before returned otherwise false. Pass in true for shouldRefreshSession if the XMLSessionConfigLoader should reparse the configuration file for new sessions. False, will cause the XMLSessionConfigLoader not to parse the file again. This method will cause the class loader to be compared with the classloader used to load the original session of this name, with this classloader. If they are not the same then the session will be refreshed.

        AbstractSession session = (AbstractSession)getSessions().get(sessionName);
        if (shouldCheckClassLoader && (session != null) && !session.getDatasourcePlatform().getConversionManager().getLoader().equals(objectClassLoader)) {
            //bug 3766808  if a different classloader is being used then a reload of the session should
            //be completed otherwise failures may occur
            shouldRefreshSession = true;
        }
        if ((session == null) || shouldRefreshSession) {
            if (session != null) {
                if (session.isDatabaseSession() && session.isConnected()) {
                    ((DatabaseSession)session).logout();
                }

                getSessions().remove(sessionName);
            }
        }

        if (session == null) {
            logAndThrowException(SessionLog.WARNING, ValidationException.noSessionFound(sessionName, ""));
        } else if (shouldLoginSession && !session.isConnected()) {
            ((DatabaseSession)session).login();
        }

        return session;
    
public java.util.HashtablegetSessions()
INTERNAL: Return a hashtable on all sessions.

        return sessions;
    
private voidlogAndThrowException(int level, java.lang.RuntimeException exception)
INTERNAL: Log exceptions to the default log then throw them.

        AbstractSessionLog.getLog().logThrowable(level, exception);
        throw exception;
    
public voidsetDefaultSession(oracle.toplink.essentials.sessions.Session defaultSession)
PUBLIC: Set the default session. Other sessions are supported through the getSession by name API.

        this.defaultSession = (AbstractSession)defaultSession;
        addSession("default", defaultSession);
    
public static voidsetManager(oracle.toplink.essentials.tools.sessionmanagement.SessionManager theManager)
INTERNAL: Set the singleton session manager. This allows global access to a set of named sessions.

        manager = theManager;
    
public voidsetSessions(java.util.Hashtable sessions)
INTERNAL: Set a hashtable of all sessions

        this.sessions = sessions;