Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 
 package org.eclipse.jetty.continuation;
 
 
 
 
 
 /* ------------------------------------------------------------ */
A blocking implementation of Continuation. This implementation of Continuation is used by the ContinuationFilter when there are is no native or asynchronous continuation type available.
 
 {
     // common exception used for all continuations.  
     // Turn on debug in ContinuationFilter to see real stack trace.
     private final static ContinuationThrowable __exception = new ContinuationThrowable();
     
     private static final int __HANDLING=1;   // Request dispatched to filter/servlet
     private static final int __SUSPENDING=2;   // Suspend called, but not yet returned to container
     private static final int __RESUMING=3;     // resumed while suspending
     private static final int __COMPLETING=4;   // resumed while suspending or suspended
     private static final int __SUSPENDED=5;    // Suspended and parked
     private static final int __UNSUSPENDING=6;
     private static final int __COMPLETE=7;
 
     private final ServletRequest _request;
     private ServletResponse _response;
     
     private int _state=;
     private boolean _initial=true;
     private boolean _resumed=false;
     private boolean _timeout=false;
     private boolean _responseWrapped=false;
     private  long _timeoutMs=30000; // TODO configure
     
 
     FauxContinuation(final ServletRequest request)
     {
         =request;
     }
 
     /* ------------------------------------------------------------ */
     public void onComplete()
     {
         if (!=null)
             for (ContinuationListener l:)
                 l.onComplete(this);
     }
     
     /* ------------------------------------------------------------ */
     public void onTimeout()
     {
         if (!=null)
             for (ContinuationListener l:)
                 l.onTimeout(this);
     }
 
     /* ------------------------------------------------------------ */
    
 
     public boolean isResponseWrapped()
     {
         return ;
     }
 
     /* ------------------------------------------------------------ */
     public boolean isInitial()
     {
         synchronized(this)
         {
             return ;
         }
    }
    /* ------------------------------------------------------------ */
    public boolean isResumed()
    {
        synchronized(this)
        {
            return ;
        }
    }
    /* ------------------------------------------------------------ */
    public boolean isSuspended()
    {
        synchronized(this)
        {
            switch()
            {
                case :
                    return false;
                case :
                case :
                case :
                case :
                    return true;
                case :
                default:
                    return false;   
            }
        }
    }
    /* ------------------------------------------------------------ */
    public boolean isExpired()
    {
        synchronized(this)
        {
            return ;
        }
    }
    /* ------------------------------------------------------------ */
    public void setTimeout(long timeoutMs)
    {
         = timeoutMs;
    }
    /* ------------------------------------------------------------ */
    public void suspend(ServletResponse response)
    {
        =response;
        =response instanceof ServletResponseWrapper;
        suspend();
    }
    
    /* ------------------------------------------------------------ */
    public void suspend()
    {
        synchronized (this)
        {
            switch()
            {
                case :
                    =false;
                    =false;
                    =;
                    return;
                case :
                case :
                    return;
                case :
                case :
                case :
                    throw new IllegalStateException(this.getStatusString());
                default:
                    throw new IllegalStateException(""+);
            }
        }
    }
    /* ------------------------------------------------------------ */
    /* (non-Javadoc)
     * @see org.mortbay.jetty.Suspendor#resume()
     */
    public void resume()
    {
        synchronized (this)
        {
            switch()
            {
                case :
                    =true;
                    return;
                    
                case :
                    =true;
                    =;
                    return;
                case :
                case :
                    return;
                    
                case :
                    fauxResume();
                    =true;
                    =;
                    break;
                    
                case :
                    =true;
                    return;
                    
                default:
                    throw new IllegalStateException(this.getStatusString());
            }
        }
        
    }
    
    /* ------------------------------------------------------------ */
    public void complete()
    {
        // just like resume, except don't set _resumed=true;
        synchronized (this)
        {
            switch()
            {
                case :
                    throw new IllegalStateException(this.getStatusString());
                    
                case :
                    =;
                    break;
                    
                case :
                    break;
                case :
                    return;
                    
                case :
                    =;
                    fauxResume();
                    break;
                    
                case :
                    return;
                    
                default:
                    throw new IllegalStateException(this.getStatusString());
            }
        }
    }
    /* ------------------------------------------------------------ */
    
    public boolean enter(ServletResponse response)
    {
        =response;
        return true;
    }
    /* ------------------------------------------------------------ */
    
    {
        return ;
    }
    
    /* ------------------------------------------------------------ */
    void handling()
    {
        synchronized (this)
        {
            =false;
            switch()
            {
                case :
                    throw new IllegalStateException(this.getStatusString());
                case :
                case :
                    throw new IllegalStateException(this.getStatusString());
                case :
                    return;
                case :
                    fauxResume();
                case :
                    =;
                    return;
                default:
                    throw new IllegalStateException(""+);
            }
        }
    }
    /* ------------------------------------------------------------ */
    

Returns:
true if handling is complete
    public boolean exit()
    {
        synchronized (this)
        {
            switch()
            {
                case :
                    =;
                    onComplete();
                    return true;
                case :
                    =false;
                    =;
                    fauxSuspend(); // could block and change state.
                    if (== || ==)
                    {
                        onComplete();
                        return true;
                    }
                    
                    =false;
                    =;
                    return false
                case :
                    =false;
                    =;
                    return false
                case :
                    =false;
                    =;
                    onComplete();
                    return true;
                case :
                case :
                default:
                    throw new IllegalStateException(this.getStatusString());
            }
        }
    }
    /* ------------------------------------------------------------ */
    protected void expire()
    {
        // just like resume, except don't set _resumed=true;
        synchronized (this)
        {
            =true;
        }
        
        onTimeout();
        
        synchronized (this)
        {
            switch()
            {
                case :
                    return;
                    
                case :
                    =true;
                    =;
                    fauxResume();
                    return;
                    
                case :
                    return;
                    
                case :
                    return;
                    
                case :
                    =true;
                    =;
                    break;
                    
                case :
                    =true;
                    return;
                    
                default:
                    throw new IllegalStateException(this.getStatusString());
            }
        }
    }
    private void fauxSuspend()
    {
        long expire_at = System.currentTimeMillis()+;
        long wait=;
        while (>0 && wait>0)
        {
            try
            {
                this.wait(wait);
            }
            catch (InterruptedException e)
            {
                break;
            }
            wait=expire_at-System.currentTimeMillis();
        }
        if (>0 && wait<=0)
            expire();
    }
    
    private void fauxResume()
    {
        =0;
        this.notifyAll();
    }
    
    @Override
    public String toString()
    {
        return getStatusString();
    }
    
    {
        synchronized (this)
        {
            return
            ((==)?"HANDLING":
                    (==)?"SUSPENDING":
                        (==)?"SUSPENDED":
                            (==)?"RESUMING":
                                (==)?"UNSUSPENDING":
                                    (==)?"COMPLETING":
                                    ("???"+))+
            (?",initial":"")+
            (?",resumed":"")+
            (?",timeout":"");
        }
    }
    
    public void addContinuationListener(ContinuationListener listener)
    {
        if (==null)
            =new ArrayList<ContinuationListener>();
        .add(listener);
        
    }
    /* ------------------------------------------------------------ */
    
    public Object getAttribute(String name)
    {
        return .getAttribute(name);
    }
    /* ------------------------------------------------------------ */
    
    public void removeAttribute(String name)
    {
        .removeAttribute(name);
    }
    /* ------------------------------------------------------------ */
    
    public void setAttribute(String nameObject attribute)
    {
        .setAttribute(name,attribute);
    }
    /* ------------------------------------------------------------ */
    
    public void undispatch()
    {
        if (isSuspended())
        {
            if (.)
                throw new ContinuationThrowable();
            throw ;
        }
        throw new IllegalStateException("!suspended");
        
    }
New to GrepCode? Check out our FAQ X