Mercurial Hosting > luan
changeset 829:dfa742c663f9
remove ContinuationFilter and FauxContinuation
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 15 Sep 2016 18:16:25 -0600 |
parents | 8579194add85 |
children | 7c737c376bc3 |
files | src/luan/modules/http/LuanHandler.java src/org/eclipse/jetty/continuation/ContinuationFilter.java src/org/eclipse/jetty/continuation/ContinuationSupport.java src/org/eclipse/jetty/continuation/FauxContinuation.java |
diffstat | 4 files changed, 20 insertions(+), 810 deletions(-) [+] |
line wrap: on
line diff
diff -r 8579194add85 -r dfa742c663f9 src/luan/modules/http/LuanHandler.java --- a/src/luan/modules/http/LuanHandler.java Thu Sep 15 16:47:49 2016 -0600 +++ b/src/luan/modules/http/LuanHandler.java Thu Sep 15 18:16:25 2016 -0600 @@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.AbstractHandler; +import org.eclipse.jetty.continuation.ContinuationSupport; import luan.Luan; import luan.LuanState; import luan.LuanTable; @@ -52,6 +53,7 @@ @Override public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) throws IOException { +//System.out.println("getContinuation = "+ContinuationSupport.getContinuation(request)); if( target.endsWith("/") ) target += welcomeFile; Thread thread = Thread.currentThread();
diff -r 8579194add85 -r dfa742c663f9 src/org/eclipse/jetty/continuation/ContinuationFilter.java --- a/src/org/eclipse/jetty/continuation/ContinuationFilter.java Thu Sep 15 16:47:49 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,174 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 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; - -import java.io.IOException; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - - - -/* ------------------------------------------------------------ */ -/** - * <p>ContinuationFilter must be applied to servlet paths that make use of - * the asynchronous features provided by {@link Continuation} APIs, but that - * are deployed in servlet containers that are neither Jetty (>= 7) nor a - * compliant Servlet 3.0 container.</p> - * <p>The following init parameters may be used to configure the filter (these are mostly for testing):</p> - * <dl> - * <dt>debug</dt><dd>Boolean controlling debug output</dd> - * <dt>jetty6</dt><dd>Boolean to force use of Jetty 6 continuations</dd> - * <dt>faux</dt><dd>Boolean to force use of faux continuations</dd> - * </dl> - * <p>If the servlet container is not Jetty (either 6 or 7) nor a Servlet 3 - * container, then "faux" continuations will be used.</p> - * <p>Faux continuations will just put the thread that called {@link Continuation#suspend()} - * in wait, and will notify that thread when {@link Continuation#resume()} or - * {@link Continuation#complete()} is called.</p> - * <p>Faux continuations are not threadless continuations (they are "faux" - fake - for this reason) - * and as such they will scale less than proper continuations.</p> - */ -public class ContinuationFilter implements Filter -{ - static boolean _initialized; - static boolean __debug; // shared debug status - private boolean _faux; - private boolean _jetty6; - private boolean _filtered; - ServletContext _context; - private boolean _debug; - - public void init(FilterConfig filterConfig) throws ServletException - { - boolean jetty_7_or_greater="org.eclipse.jetty.servlet".equals(filterConfig.getClass().getPackage().getName()); - _context = filterConfig.getServletContext(); - - String param=filterConfig.getInitParameter("debug"); - _debug=param!=null&&Boolean.parseBoolean(param); - if (_debug) - __debug=true; - - param=filterConfig.getInitParameter("jetty6"); - if (param==null) - param=filterConfig.getInitParameter("partial"); - if (param!=null) - _jetty6=Boolean.parseBoolean(param); - else - _jetty6=ContinuationSupport.__jetty6 && !jetty_7_or_greater; - - param=filterConfig.getInitParameter("faux"); - if (param!=null) - _faux=Boolean.parseBoolean(param); - else - _faux=!(jetty_7_or_greater || _jetty6 || _context.getMajorVersion()>=3); - - _filtered=_faux||_jetty6; - if (_debug) - _context.log("ContinuationFilter "+ - " jetty="+jetty_7_or_greater+ - " jetty6="+_jetty6+ - " faux="+_faux+ - " filtered="+_filtered+ - " servlet3="+ContinuationSupport.__servlet3); - _initialized=true; - } - - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException - { - if (_filtered) - { - Continuation c = (Continuation) request.getAttribute(Continuation.ATTRIBUTE); - FilteredContinuation fc; - if (_faux && (c==null || !(c instanceof FauxContinuation))) - { - fc = new FauxContinuation(request); - request.setAttribute(Continuation.ATTRIBUTE,fc); - } - else - fc=(FilteredContinuation)c; - - boolean complete=false; - while (!complete) - { - try - { - if (fc==null || (fc).enter(response)) - chain.doFilter(request,response); - } - catch (ContinuationThrowable e) - { - debug("faux",e); - } - finally - { - if (fc==null) - fc = (FilteredContinuation) request.getAttribute(Continuation.ATTRIBUTE); - - complete=fc==null || (fc).exit(); - } - } - } - else - { - try - { - chain.doFilter(request,response); - } - catch (ContinuationThrowable e) - { - debug("caught",e); - } - } - } - - private void debug(String string) - { - if (_debug) - { - _context.log(string); - } - } - - private void debug(String string, Throwable th) - { - if (_debug) - { - if (th instanceof ContinuationThrowable) - _context.log(string+":"+th); - else - _context.log(string,th); - } - } - - public void destroy() - { - } - - public interface FilteredContinuation extends Continuation - { - boolean enter(ServletResponse response); - boolean exit(); - } -}
diff -r 8579194add85 -r dfa742c663f9 src/org/eclipse/jetty/continuation/ContinuationSupport.java --- a/src/org/eclipse/jetty/continuation/ContinuationSupport.java Thu Sep 15 16:47:49 2016 -0600 +++ b/src/org/eclipse/jetty/continuation/ContinuationSupport.java Thu Sep 15 18:16:25 2016 -0600 @@ -32,133 +32,23 @@ */ public class ContinuationSupport { - static final boolean __jetty6; - static final boolean __servlet3; - static final Class<?> __waitingContinuation; - static final Constructor<? extends Continuation> __newServlet3Continuation; - static final Constructor<? extends Continuation> __newJetty6Continuation; - static - { - boolean servlet3Support=false; - Constructor<? extends Continuation>s3cc=null; - try - { - boolean servlet3=ServletRequest.class.getMethod("startAsync")!=null; - if (servlet3) - { - Class<? extends Continuation> s3c = ContinuationSupport.class.getClassLoader().loadClass("org.eclipse.jetty.continuation.Servlet3Continuation").asSubclass(Continuation.class); - s3cc=s3c.getConstructor(ServletRequest.class); - servlet3Support=true; - } - } - catch (Exception e) - {} - finally - { - __servlet3=servlet3Support; - __newServlet3Continuation=s3cc; - } - - boolean jetty6Support=false; - Constructor<? extends Continuation>j6cc=null; - try - { - Class<?> jetty6ContinuationClass = ContinuationSupport.class.getClassLoader().loadClass("org.mortbay.util.ajax.Continuation"); - boolean jetty6=jetty6ContinuationClass!=null; - if (jetty6) - { - Class<? extends Continuation> j6c = ContinuationSupport.class.getClassLoader().loadClass("org.eclipse.jetty.continuation.Jetty6Continuation").asSubclass(Continuation.class); - j6cc=j6c.getConstructor(ServletRequest.class, jetty6ContinuationClass); - jetty6Support=true; - } - } - catch (Exception e) - {} - finally - { - __jetty6=jetty6Support; - __newJetty6Continuation=j6cc; - } - - Class<?> waiting=null; - try - { - waiting=ContinuationSupport.class.getClassLoader().loadClass("org.mortbay.util.ajax.WaitingContinuation"); - } - catch (Exception e) - { - } - finally - { - __waitingContinuation=waiting; - } - } + /* ------------------------------------------------------------ */ + /** + * Get a Continuation. The type of the Continuation returned may + * vary depending on the container in which the application is + * deployed. It may be an implementation native to the container (eg + * org.eclipse.jetty.server.AsyncContinuation) or one of the utility + * implementations provided such as an internal <code>FauxContinuation</code> + * or a real implementation like {@link org.eclipse.jetty.continuation.Servlet3Continuation}. + * @param request The request + * @return a Continuation instance + */ + public static Continuation getContinuation(ServletRequest request) + { + Continuation continuation = (Continuation) request.getAttribute(Continuation.ATTRIBUTE); + if (continuation!=null) + return continuation; - /* ------------------------------------------------------------ */ - /** - * Get a Continuation. The type of the Continuation returned may - * vary depending on the container in which the application is - * deployed. It may be an implementation native to the container (eg - * org.eclipse.jetty.server.AsyncContinuation) or one of the utility - * implementations provided such as an internal <code>FauxContinuation</code> - * or a real implementation like {@link org.eclipse.jetty.continuation.Servlet3Continuation}. - * @param request The request - * @return a Continuation instance - */ - public static Continuation getContinuation(ServletRequest request) - { - Continuation continuation = (Continuation) request.getAttribute(Continuation.ATTRIBUTE); - if (continuation!=null) - return continuation; - - while (request instanceof ServletRequestWrapper) - request=((ServletRequestWrapper)request).getRequest(); - - if (__servlet3 ) - { - try - { - continuation=__newServlet3Continuation.newInstance(request); - request.setAttribute(Continuation.ATTRIBUTE,continuation); - return continuation; - } - catch(Exception e) - { - throw new RuntimeException(e); - } - } - - if (__jetty6) - { - Object c=request.getAttribute("org.mortbay.jetty.ajax.Continuation"); - try - { - if (c==null || __waitingContinuation==null || __waitingContinuation.isInstance(c)) - continuation=new FauxContinuation(request); - else - continuation= __newJetty6Continuation.newInstance(request,c); - request.setAttribute(Continuation.ATTRIBUTE,continuation); - return continuation; - } - catch(Exception e) - { - throw new RuntimeException(e); - } - } - - throw new IllegalStateException("!(Jetty || Servlet 3.0 || ContinuationFilter)"); - } - - /* ------------------------------------------------------------ */ - /** - * @param request the servlet request - * @param response the servlet response - * @deprecated use {@link #getContinuation(ServletRequest)} - * @return the continuation - */ - @Deprecated - public static Continuation getContinuation(final ServletRequest request, final ServletResponse response) - { - return getContinuation(request); - } + throw new IllegalStateException("!(Jetty || Servlet 3.0 || ContinuationFilter)"); + } }
diff -r 8579194add85 -r dfa742c663f9 src/org/eclipse/jetty/continuation/FauxContinuation.java --- a/src/org/eclipse/jetty/continuation/FauxContinuation.java Thu Sep 15 16:47:49 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,508 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 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; - -import java.util.ArrayList; - -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.ServletResponseWrapper; - -import org.eclipse.jetty.continuation.ContinuationFilter.FilteredContinuation; - - -/* ------------------------------------------------------------ */ -/** - * A blocking implementation of Continuation. - * This implementation of Continuation is used by the {@link ContinuationFilter} - * when there are is no native or asynchronous continuation type available. - */ -class FauxContinuation implements FilteredContinuation -{ - // 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=__HANDLING; - private boolean _initial=true; - private boolean _resumed=false; - private boolean _timeout=false; - private boolean _responseWrapped=false; - private long _timeoutMs=30000; // TODO configure - - private ArrayList<ContinuationListener> _listeners; - - FauxContinuation(final ServletRequest request) - { - _request=request; - } - - /* ------------------------------------------------------------ */ - public void onComplete() - { - if (_listeners!=null) - for (ContinuationListener l:_listeners) - l.onComplete(this); - } - - /* ------------------------------------------------------------ */ - public void onTimeout() - { - if (_listeners!=null) - for (ContinuationListener l:_listeners) - l.onTimeout(this); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.continuation.Continuation#isResponseWrapped() - */ - public boolean isResponseWrapped() - { - return _responseWrapped; - } - - /* ------------------------------------------------------------ */ - public boolean isInitial() - { - synchronized(this) - { - return _initial; - } - } - - /* ------------------------------------------------------------ */ - public boolean isResumed() - { - synchronized(this) - { - return _resumed; - } - } - - /* ------------------------------------------------------------ */ - public boolean isSuspended() - { - synchronized(this) - { - switch(_state) - { - case __HANDLING: - return false; - case __SUSPENDING: - case __RESUMING: - case __COMPLETING: - case __SUSPENDED: - return true; - case __UNSUSPENDING: - default: - return false; - } - } - } - - /* ------------------------------------------------------------ */ - public boolean isExpired() - { - synchronized(this) - { - return _timeout; - } - } - - /* ------------------------------------------------------------ */ - public void setTimeout(long timeoutMs) - { - _timeoutMs = timeoutMs; - } - - /* ------------------------------------------------------------ */ - public void suspend(ServletResponse response) - { - _response=response; - _responseWrapped=response instanceof ServletResponseWrapper; - suspend(); - } - - /* ------------------------------------------------------------ */ - public void suspend() - { - synchronized (this) - { - switch(_state) - { - case __HANDLING: - _timeout=false; - _resumed=false; - _state=__SUSPENDING; - return; - - case __SUSPENDING: - case __RESUMING: - return; - - case __COMPLETING: - case __SUSPENDED: - case __UNSUSPENDING: - throw new IllegalStateException(this.getStatusString()); - - default: - throw new IllegalStateException(""+_state); - } - - } - } - - - /* ------------------------------------------------------------ */ - /* (non-Javadoc) - * @see org.mortbay.jetty.Suspendor#resume() - */ - public void resume() - { - synchronized (this) - { - switch(_state) - { - case __HANDLING: - _resumed=true; - return; - - case __SUSPENDING: - _resumed=true; - _state=__RESUMING; - return; - - case __RESUMING: - case __COMPLETING: - return; - - case __SUSPENDED: - fauxResume(); - _resumed=true; - _state=__UNSUSPENDING; - break; - - case __UNSUSPENDING: - _resumed=true; - return; - - default: - throw new IllegalStateException(this.getStatusString()); - } - } - - } - - - /* ------------------------------------------------------------ */ - public void complete() - { - // just like resume, except don't set _resumed=true; - synchronized (this) - { - switch(_state) - { - case __HANDLING: - throw new IllegalStateException(this.getStatusString()); - - case __SUSPENDING: - _state=__COMPLETING; - break; - - case __RESUMING: - break; - - case __COMPLETING: - return; - - case __SUSPENDED: - _state=__COMPLETING; - fauxResume(); - break; - - case __UNSUSPENDING: - return; - - default: - throw new IllegalStateException(this.getStatusString()); - } - } - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.continuation.Continuation#getServletResponse() - */ - public boolean enter(ServletResponse response) - { - _response=response; - return true; - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.continuation.Continuation#getServletResponse() - */ - public ServletResponse getServletResponse() - { - return _response; - } - - - /* ------------------------------------------------------------ */ - void handling() - { - synchronized (this) - { - _responseWrapped=false; - switch(_state) - { - case __HANDLING: - throw new IllegalStateException(this.getStatusString()); - - case __SUSPENDING: - case __RESUMING: - throw new IllegalStateException(this.getStatusString()); - - case __COMPLETING: - return; - - case __SUSPENDED: - fauxResume(); - case __UNSUSPENDING: - _state=__HANDLING; - return; - - default: - throw new IllegalStateException(""+_state); - } - - } - } - - /* ------------------------------------------------------------ */ - /** - * @return true if handling is complete - */ - public boolean exit() - { - synchronized (this) - { - switch(_state) - { - case __HANDLING: - _state=__COMPLETE; - onComplete(); - return true; - - case __SUSPENDING: - _initial=false; - _state=__SUSPENDED; - fauxSuspend(); // could block and change state. - if (_state==__SUSPENDED || _state==__COMPLETING) - { - onComplete(); - return true; - } - - _initial=false; - _state=__HANDLING; - return false; - - case __RESUMING: - _initial=false; - _state=__HANDLING; - return false; - - case __COMPLETING: - _initial=false; - _state=__COMPLETE; - onComplete(); - return true; - - case __SUSPENDED: - case __UNSUSPENDING: - default: - throw new IllegalStateException(this.getStatusString()); - } - } - } - - /* ------------------------------------------------------------ */ - protected void expire() - { - // just like resume, except don't set _resumed=true; - - synchronized (this) - { - _timeout=true; - } - - onTimeout(); - - synchronized (this) - { - switch(_state) - { - case __HANDLING: - return; - - case __SUSPENDING: - _timeout=true; - _state=__RESUMING; - fauxResume(); - return; - - case __RESUMING: - return; - - case __COMPLETING: - return; - - case __SUSPENDED: - _timeout=true; - _state=__UNSUSPENDING; - break; - - case __UNSUSPENDING: - _timeout=true; - return; - - default: - throw new IllegalStateException(this.getStatusString()); - } - } - } - - private void fauxSuspend() - { - long expire_at = System.currentTimeMillis()+_timeoutMs; - long wait=_timeoutMs; - while (_timeoutMs>0 && wait>0) - { - try - { - this.wait(wait); - } - catch (InterruptedException e) - { - break; - } - wait=expire_at-System.currentTimeMillis(); - } - - if (_timeoutMs>0 && wait<=0) - expire(); - } - - private void fauxResume() - { - _timeoutMs=0; - this.notifyAll(); - } - - @Override - public String toString() - { - return getStatusString(); - } - - String getStatusString() - { - synchronized (this) - { - return - ((_state==__HANDLING)?"HANDLING": - (_state==__SUSPENDING)?"SUSPENDING": - (_state==__SUSPENDED)?"SUSPENDED": - (_state==__RESUMING)?"RESUMING": - (_state==__UNSUSPENDING)?"UNSUSPENDING": - (_state==__COMPLETING)?"COMPLETING": - ("???"+_state))+ - (_initial?",initial":"")+ - (_resumed?",resumed":"")+ - (_timeout?",timeout":""); - } - } - - - public void addContinuationListener(ContinuationListener listener) - { - if (_listeners==null) - _listeners=new ArrayList<ContinuationListener>(); - _listeners.add(listener); - - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.continuation.Continuation#getAttribute(java.lang.String) - */ - public Object getAttribute(String name) - { - return _request.getAttribute(name); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.continuation.Continuation#removeAttribute(java.lang.String) - */ - public void removeAttribute(String name) - { - _request.removeAttribute(name); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.continuation.Continuation#setAttribute(java.lang.String, java.lang.Object) - */ - public void setAttribute(String name, Object attribute) - { - _request.setAttribute(name,attribute); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.continuation.Continuation#undispatch() - */ - public void undispatch() - { - if (isSuspended()) - { - if (ContinuationFilter.__debug) - throw new ContinuationThrowable(); - throw __exception; - } - throw new IllegalStateException("!suspended"); - - } -}