FileDocCategorySizeDatePackage
SessionTracker.javaAPI DocGlassfish v2 API6706Fri May 04 22:32:44 BST 2007org.apache.coyote.tomcat5

SessionTracker

public class SessionTracker extends Object implements org.apache.catalina.SessionListener
Class responsible for keeping track of the total number of active sessions associated with an HTTP request.

A given HTTP request may be associated with more than one active session if it has been dispatched to one or more foreign contexts.

The number of active sessions being tracked is used to determine whether or not any session information needs to be reflected in the response (e.g., in the form of a response cookie). If no active sessions are associated with the request by the time the response is committed, no session information will be reflected in the response. See GlassFish Issue 896 for additional details.

Fields Summary
private int
count
private String
trackedSessionId
private CoyoteResponse
response
private ArrayList
contextNames
Constructors Summary
Methods Summary
public intgetActiveSessions()
Gets the number of active sessions that are being tracked.

return
The number of active sessions being tracked

        return count;
    
public java.lang.StringgetSessionId()
Gets the id of the sessions that are being tracked. Notice that since all sessions are associated with the same request, albeit in different context, they all share the same id.

return
The id of the sessions that are being tracked

        return trackedSessionId;
    
public synchronized voidreset()
Resets this session tracker.

        count = 0;
        trackedSessionId = null;
        contextNames.clear();
    
public voidsessionEvent(org.apache.catalina.SessionEvent event)
Processes the given session event, by unregistering this SessionTracker as a session listener from the session that is the source of the event, and by decrementing the counter of currently tracked sessions.

param
event The session event


                                              
        

        if (!Session.SESSION_DESTROYED_EVENT.equals(event.getType())) {
            return;
        }

        Session session = event.getSession();

        synchronized (this) {
            if (session.getIdInternal() != null
                    && session.getIdInternal().equals(trackedSessionId)
                    && session.getManager() != null
                    && session.getManager().getContainer() != null
                    && contextNames.contains(
                            session.getManager().getContainer().getName())) {
                count--;
                if (count == 0) {
                    trackedSessionId = null;
                    if (response != null) {
                        response.removeSessionCookies();
                    }
                }
            }

            session.removeSessionListener(this);
        }
    
public synchronized voidsetResponse(CoyoteResponse response)
Associates the given response with this SessionTracker. If the number of tracked sessions drops to zero, this SessionTracker will remove the Set-Cookie from the given response.

param
response The response from which to remove the Set-Cookie header if the number of tracked sessions drops to zero

        this.response = response;
    
public synchronized voidtrack(org.apache.catalina.Session session)
Tracks the given session, by registering this SessionTracker as a listener with the given session, and by incrementing the counter of currently tracked sessions.

param
session The session to track


        if (trackedSessionId == null) {
            trackedSessionId = session.getIdInternal();
        } else if (!trackedSessionId.equals(session.getIdInternal())) {
            throw new IllegalArgumentException("Should never reach here");
        }

        count++;

        if (session.getManager() != null
                && session.getManager().getContainer() != null
                && session.getManager().getContainer().getName() != null) {
            contextNames.add(session.getManager().getContainer().getName());
        }

        session.addSessionListener(this);