FileDocCategorySizeDatePackage
PESessionLockingStandardPipeline.javaAPI DocGlassfish v2 API8384Fri May 04 22:36:02 BST 2007com.sun.enterprise.web

PESessionLockingStandardPipeline

public class PESessionLockingStandardPipeline extends WebPipeline
author
Larry White

Fields Summary
protected static final Logger
_logger
The logger to use for logging ALL web container related messages.
Constructors Summary
public PESessionLockingStandardPipeline(org.apache.catalina.Container container)
creates an instance of PESessionLockingStandardPipeline

param
container

    
                        
       
        super(container);
    
Methods Summary
protected org.apache.catalina.SessiongetSession(org.apache.catalina.Request request)
get the session associated with this request

param
request

        ServletRequest servletReq = request.getRequest();
        HttpServletRequest httpReq = 
            (HttpServletRequest) servletReq;
        HttpSession httpSess = httpReq.getSession(false);
        if(httpSess == null)
            //need to null out session
            //httpReq.setSession(null);
            return null;
        String id = httpSess.getId();
        if(_logger.isLoggable(Level.FINEST)) {
            _logger.finest("SESSION_ID=" + id);
        }
        Manager mgr = this.getContainer().getManager();
        PersistentManagerBase pmb = (PersistentManagerBase)mgr;
        Session sess = null;
        try {
            sess = pmb.findSession(id);
        } catch (java.io.IOException ex) {}
        if(_logger.isLoggable(Level.FINEST)) {
            _logger.finest("RETRIEVED_SESSION=" + sess);
        }
         return sess;
    
public voidinvoke(org.apache.catalina.Request request, org.apache.catalina.Response response)
Cause the specified request and response to be processed by the Valves associated with this pipeline, until one of these valves causes the response to be created and returned. The implementation must ensure that multiple simultaneous requests (on different threads) can be processed through the same Pipeline without interfering with each other's control flow.

param
request The servlet request we are processing
param
response The servlet response we are creating
exception
IOException if an input/output error occurs
exception
ServletException if a servlet exception is thrown

        
        this.lockSession(request);
        try {
            super.invoke(request, response);
        } finally {
            this.unlockSession(request);
        }
    
protected booleanlockSession(org.apache.catalina.Request request)
lock the session associated with this request this will be a foreground lock checks for background lock to clear and does a decay poll loop to wait until it is clear; after 5 times it takes control for the foreground

param
request

        boolean result = false;
        Session sess = this.getSession(request);
        if(_logger.isLoggable(Level.FINEST)) {
            _logger.finest("IN LOCK_SESSION: sess =" + sess);
        }
        //now lock the session
        if(sess != null) {
            long pollTime = 200L;
            int maxNumberOfRetries = 7;
            int tryNumber = 0;
            boolean keepTrying = true;
            boolean lockResult = false;
            if(_logger.isLoggable(Level.FINEST)) {
                _logger.finest("locking session: sess =" + sess);
            }
            StandardSession haSess = (StandardSession) sess;
            //try to lock up to maxNumberOfRetries times
            //poll and wait starting with 200 ms
            while(keepTrying) {
                lockResult = haSess.lockForeground();
                if(lockResult) {
                    keepTrying = false;
                    result = true;
                    break;
                }
                tryNumber++;
                if(tryNumber < maxNumberOfRetries) {
                    pollTime = pollTime * 2L;
                    threadSleep(pollTime);
                } else {
                    //tried to wait and lock maxNumberOfRetries times; throw an exception
                    //throw new ServletException("unable to acquire session lock");
                    //instead of above; unlock the background so we can take over
                    _logger.warning("this should not happen-breaking background lock: sess =" + sess);
                    haSess.unlockBackground();
                }              
            }
            if(_logger.isLoggable(Level.FINEST)) {
                _logger.finest("finished locking session: sess =" + sess);
                _logger.finest("LOCK = " + haSess.getSessionLock());
            }
        }
        return result;
    
protected voidthreadSleep(long sleepTime)


        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            ;
        }

    
protected voidunlockSession(org.apache.catalina.Request request)
unlock the session associated with this request

param
request

        Session sess = this.getSession(request);
        if(_logger.isLoggable(Level.FINEST)) {
            _logger.finest("IN UNLOCK_SESSION: sess = " + sess);
        }
        //now unlock the session
        if(sess != null) {
            if(_logger.isLoggable(Level.FINEST)) {
                _logger.finest("unlocking session: sess =" + sess);
            }
            StandardSession haSess = (StandardSession) sess;
            haSess.unlockForeground();
            if(_logger.isLoggable(Level.FINEST)) {
                _logger.finest("finished unlocking session: sess =" + sess);
                _logger.finest("LOCK = " + haSess.getSessionLock());
            }
        }