changeset 993:d9d95acded81

remove ScopedHandler
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 18 Oct 2016 15:54:54 -0600
parents 0608a6664bee
children 4e9d373bf6e9
files src/org/eclipse/jetty/server/handler/ContextHandler.java src/org/eclipse/jetty/server/handler/HandlerWrapper.java src/org/eclipse/jetty/server/handler/ScopedHandler.java
diffstat 3 files changed, 130 insertions(+), 372 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/eclipse/jetty/server/handler/ContextHandler.java	Tue Oct 18 15:23:25 2016 -0600
+++ b/src/org/eclipse/jetty/server/handler/ContextHandler.java	Tue Oct 18 15:54:54 2016 -0600
@@ -93,7 +93,7 @@
  * 
  * @org.apache.xbean.XBean description="Creates a basic HTTP context"
  */
-public final class ContextHandler extends ScopedHandler implements Server.Graceful
+public final class ContextHandler extends HandlerWrapper implements Server.Graceful
 {
 	private static final Logger LOG = LoggerFactory.getLogger(ContextHandler.class);
 
@@ -229,18 +229,6 @@
 	}
 
 	/* ------------------------------------------------------------ */
-	public void callContextInitialized (ServletContextListener l, ServletContextEvent e)
-	{
-		l.contextInitialized(e);
-	}
-
-	/* ------------------------------------------------------------ */
-	public void callContextDestroyed (ServletContextListener l, ServletContextEvent e)
-	{
-		l.contextDestroyed(e);
-	}
-	
-	/* ------------------------------------------------------------ */
 	/*
 	 * @see org.eclipse.thread.AbstractLifeCycle#doStop()
 	 */
@@ -267,10 +255,6 @@
 		_contextAttributes.clearAttributes();
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
-	 */
 	private boolean checkContext(final String target, final Request baseRequest, final HttpServletResponse response) throws IOException, ServletException
 	{
 		switch (_availability)
@@ -308,13 +292,8 @@
 		return true;
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @see org.eclipse.jetty.server.handler.ScopedHandler#doScope(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest,
-	 *      javax.servlet.http.HttpServletResponse)
-	 */
 	@Override
-	public void doScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
+	public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
 	{
 		if (LOG.isDebugEnabled())
 			LOG.debug("scope {}|{}|{} @ {}",baseRequest.getContextPath(),baseRequest.getServletPath(),baseRequest.getPathInfo(),this);
@@ -375,16 +354,16 @@
 			if (LOG.isDebugEnabled())
 				LOG.debug("context={}|{}|{} @ {}",baseRequest.getContextPath(),baseRequest.getServletPath(), baseRequest.getPathInfo(),this);
 
-			// start manual inline of nextScope(target,baseRequest,request,response);
-			if (never())
-				nextScope(target,baseRequest,request,response);
-			else if (_nextScope != null)
-				_nextScope.doScope(target,baseRequest,request,response);
-			else if (_outerScope != null)
-				_outerScope.doHandle(target,baseRequest,request,response);
-			else
-				doHandle(target,baseRequest,request,response);
-			// end manual inline (pathentic attempt to reduce stack depth)
+			try
+			{
+				super.handle(target,baseRequest,request,response);
+			}
+			catch (HttpException e)
+			{
+				LOG.debug("",e);
+				baseRequest.setHandled(true);
+				response.sendError(e.getStatus(),e.getReason());
+			}
 		}
 		finally
 		{
@@ -400,34 +379,6 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @see org.eclipse.jetty.server.handler.ScopedHandler#doHandle(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest,
-	 *      javax.servlet.http.HttpServletResponse)
-	 */
-	@Override
-	public void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
-	{
-		try
-		{
-			// start manual inline of nextHandle(target,baseRequest,request,response);
-			// noinspection ConstantIfStatement
-			if (never())
-				nextHandle(target,baseRequest,request,response);
-			else if (_nextScope != null && _nextScope == _handler)
-				_nextScope.doHandle(target,baseRequest,request,response);
-			else if (_handler != null)
-				_handler.handle(target,baseRequest,request,response);
-			// end manual inline
-		}
-		catch (HttpException e)
-		{
-			LOG.debug("",e);
-			baseRequest.setHandled(true);
-			response.sendError(e.getStatus(),e.getReason());
-		}
-	}
-
 	public void setContextPath(String contextPath)
 	{
 		if (contextPath != null && contextPath.length() > 1 && contextPath.endsWith("/"))
--- a/src/org/eclipse/jetty/server/handler/HandlerWrapper.java	Tue Oct 18 15:23:25 2016 -0600
+++ b/src/org/eclipse/jetty/server/handler/HandlerWrapper.java	Tue Oct 18 15:54:54 2016 -0600
@@ -36,141 +36,141 @@
  */
 public class HandlerWrapper extends AbstractHandlerContainer
 {
-    protected Handler _handler;
+	protected Handler _handler;
 
-    /* ------------------------------------------------------------ */
-    /**
-     *
-     */
-    public HandlerWrapper()
-    {
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 *
+	 */
+	public HandlerWrapper()
+	{
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * @return Returns the handlers.
-     */
-    public Handler getHandler()
-    {
-        return _handler;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * @return Returns the handlers.
+	 */
+	public Handler getHandler()
+	{
+		return _handler;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * @return Returns the handlers.
-     */
-    public Handler[] getHandlers()
-    {
-        if (_handler==null)
-            return new Handler[0];
-        return new Handler[] {_handler};
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * @return Returns the handlers.
+	 */
+	public Handler[] getHandlers()
+	{
+		if (_handler==null)
+			return new Handler[0];
+		return new Handler[] {_handler};
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * @param handler Set the {@link Handler} which should be wrapped.
-     */
-    public void setHandler(Handler handler)
-    {
-        if (isStarted())
-            throw new IllegalStateException(STARTED);
+	/* ------------------------------------------------------------ */
+	/**
+	 * @param handler Set the {@link Handler} which should be wrapped.
+	 */
+	public void setHandler(Handler handler)
+	{
+		if (isStarted())
+			throw new IllegalStateException(STARTED);
 
-        Handler old_handler = _handler;
-        _handler = handler;
-        if (handler!=null)
-            handler.setServer(getServer());
-    }
+		Handler old_handler = _handler;
+		_handler = handler;
+		if (handler!=null)
+			handler.setServer(getServer());
+	}
 
-    /* ------------------------------------------------------------ */
-    /*
-     * @see org.eclipse.thread.AbstractLifeCycle#doStart()
-     */
-    @Override
-    protected void doStart() throws Exception
-    {
-        if (_handler!=null)
-            _handler.start();
-        super.doStart();
-    }
+	/* ------------------------------------------------------------ */
+	/*
+	 * @see org.eclipse.thread.AbstractLifeCycle#doStart()
+	 */
+	@Override
+	protected void doStart() throws Exception
+	{
+		if (_handler!=null)
+			_handler.start();
+		super.doStart();
+	}
 
-    /* ------------------------------------------------------------ */
-    /*
-     * @see org.eclipse.thread.AbstractLifeCycle#doStop()
-     */
-    @Override
-    protected void doStop() throws Exception
-    {
-        if (_handler!=null)
-            _handler.stop();
-        super.doStop();
-    }
+	/* ------------------------------------------------------------ */
+	/*
+	 * @see org.eclipse.thread.AbstractLifeCycle#doStop()
+	 */
+	@Override
+	protected void doStop() throws Exception
+	{
+		if (_handler!=null)
+			_handler.stop();
+		super.doStop();
+	}
 
-    /* ------------------------------------------------------------ */
-    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
-    {
-        if (_handler!=null && isStarted())
-        {
-            _handler.handle(target,baseRequest, request, response);
-        }
-    }
+	/* ------------------------------------------------------------ */
+	public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
+	{
+		if (_handler!=null && isStarted())
+		{
+			_handler.handle(target,baseRequest, request, response);
+		}
+	}
 
 
-    /* ------------------------------------------------------------ */
-    @Override
-    public void setServer(Server server)
-    {
-        Server old_server=getServer();
-        if (server==old_server)
-            return;
+	/* ------------------------------------------------------------ */
+	@Override
+	public void setServer(Server server)
+	{
+		Server old_server=getServer();
+		if (server==old_server)
+			return;
 
-        if (isStarted())
-            throw new IllegalStateException(STARTED);
+		if (isStarted())
+			throw new IllegalStateException(STARTED);
 
-        super.setServer(server);
+		super.setServer(server);
 
-        Handler h=getHandler();
-        if (h!=null)
-            h.setServer(server);
-    }
+		Handler h=getHandler();
+		if (h!=null)
+			h.setServer(server);
+	}
 
 
-    /* ------------------------------------------------------------ */
-    @Override
-    protected Object expandChildren(Object list, Class byClass)
-    {
-        return expandHandler(_handler,list,byClass);
-    }
+	/* ------------------------------------------------------------ */
+	@Override
+	protected Object expandChildren(Object list, Class byClass)
+	{
+		return expandHandler(_handler,list,byClass);
+	}
 
-    /* ------------------------------------------------------------ */
-    public <H extends Handler> H getNestedHandlerByClass(Class<H> byclass)
-    {
-        HandlerWrapper h=this;
-        while (h!=null)
-        {
-            if (byclass.isInstance(h))
-                return (H)h;
-            Handler w = h.getHandler();
-            if (w instanceof HandlerWrapper)
-                h=(HandlerWrapper)w;
-            else break;
-        }
-        return null;
+	/* ------------------------------------------------------------ */
+	public <H extends Handler> H getNestedHandlerByClass(Class<H> byclass)
+	{
+		HandlerWrapper h=this;
+		while (h!=null)
+		{
+			if (byclass.isInstance(h))
+				return (H)h;
+			Handler w = h.getHandler();
+			if (w instanceof HandlerWrapper)
+				h=(HandlerWrapper)w;
+			else break;
+		}
+		return null;
 
-    }
+	}
 
-    /* ------------------------------------------------------------ */
-    @Override
-    public void destroy()
-    {
-        if (!isStopped())
-            throw new IllegalStateException("!STOPPED");
-        Handler child=getHandler();
-        if (child!=null)
-        {
-            setHandler(null);
-            child.destroy();
-        }
-        super.destroy();
-    }
+	/* ------------------------------------------------------------ */
+	@Override
+	public void destroy()
+	{
+		if (!isStopped())
+			throw new IllegalStateException("!STOPPED");
+		Handler child=getHandler();
+		if (child!=null)
+		{
+			setHandler(null);
+			child.destroy();
+		}
+		super.destroy();
+	}
 
 }
--- a/src/org/eclipse/jetty/server/handler/ScopedHandler.java	Tue Oct 18 15:23:25 2016 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,193 +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.server.handler;
-
-import java.io.IOException;
-
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.eclipse.jetty.server.Request;
-
-
-/* ------------------------------------------------------------ */
-/** ScopedHandler.
- * 
- * A ScopedHandler is a HandlerWrapper where the wrapped handlers
- * each define a scope.   When {@link #handle(String, Request, HttpServletRequest, HttpServletResponse)}
- * is called on the first ScopedHandler in a chain of HandlerWrappers,
- * the {@link #doScope(String, Request, HttpServletRequest, HttpServletResponse)} method is 
- * called on all contained ScopedHandlers, before the 
- * {@link #doHandle(String, Request, HttpServletRequest, HttpServletResponse)} method 
- * is called on all contained handlers.
- * 
- * <p>For example if Scoped handlers A, B & C were chained together, then 
- * the calling order would be:<pre>
- * A.handle(...)
- *   A.doScope(...)
- *     B.doScope(...)
- *       C.doScope(...)
- *         A.doHandle(...)
- *           B.doHandle(...)
- *              C.doHandle(...)   
- * <pre>
- * 
- * <p>If non scoped handler X was in the chained A, B, X & C, then 
- * the calling order would be:<pre>
- * A.handle(...)
- *   A.doScope(...)
- *     B.doScope(...)
- *       C.doScope(...)
- *         A.doHandle(...)
- *           B.doHandle(...)
- *             X.handle(...)
- *               C.handle(...)
- *                 C.doHandle(...)   
- * <pre>
- * 
- * <p>A typical usage pattern is:<pre>
- *     private static class MyHandler extends ScopedHandler
- *     {
- *         public void doScope(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
- *         {
- *             try
- *             {
- *                 setUpMyScope();
- *                 super.doScope(target,request,response);
- *             }
- *             finally
- *             {
- *                 tearDownMyScope();
- *             }
- *         }
- *         
- *         public void doHandle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
- *         {
- *             try
- *             {
- *                 doMyHandling();
- *                 super.doHandle(target,request,response);
- *             }
- *             finally
- *             {
- *                 cleanupMyHandling();
- *             }
- *         }
- *     }
- * </pre>
- */
-public abstract class ScopedHandler extends HandlerWrapper
-{
-    private static final ThreadLocal<ScopedHandler> __outerScope= new ThreadLocal<ScopedHandler>();
-    protected ScopedHandler _outerScope;
-    protected ScopedHandler _nextScope;
-    
-    /* ------------------------------------------------------------ */
-    /**
-     * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStart()
-     */
-    @Override
-    protected void doStart() throws Exception
-    {
-        try
-        {
-            _outerScope=__outerScope.get();
-            if (_outerScope==null)
-                __outerScope.set(this);
-            
-            super.doStart();
-            
-            _nextScope= (ScopedHandler)getChildHandlerByClass(ScopedHandler.class);
-            
-        }
-        finally
-        {
-            if (_outerScope==null)
-                __outerScope.set(null);
-        }
-    }
-
-
-    /* ------------------------------------------------------------ */
-    /* 
-     */
-    @Override
-    public final void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
-    {
-        if (_outerScope==null)  
-            doScope(target,baseRequest,request, response);
-        else 
-            doHandle(target,baseRequest,request, response);
-    }
-    
-    /* ------------------------------------------------------------ */
-    /* 
-     * Scope the handler
-     */
-    public abstract void doScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) 
-        throws IOException, ServletException;
-    
-    /* ------------------------------------------------------------ */
-    /* 
-     * Scope the handler
-     */
-    public final void nextScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) 
-        throws IOException, ServletException
-    {
-        // this method has been manually inlined in several locations, but
-        // is called protected by an if(never()), so your IDE can find those
-        // locations if this code is changed.
-        if (_nextScope!=null)
-            _nextScope.doScope(target,baseRequest,request, response);
-        else if (_outerScope!=null)
-            _outerScope.doHandle(target,baseRequest,request, response);
-        else 
-            doHandle(target,baseRequest,request, response);
-    }
-
-    /* ------------------------------------------------------------ */
-    /* 
-     * Do the handler work within the scope.
-     */
-    public abstract void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) 
-        throws IOException, ServletException;
-    
-    /* ------------------------------------------------------------ */
-    /* 
-     * Do the handler work within the scope.
-     */
-    public final void nextHandle(String target, final Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
-    {
-        // this method has been manually inlined in several locations, but
-        // is called protected by an if(never()), so your IDE can find those
-        // locations if this code is changed.
-        if (_nextScope!=null && _nextScope==_handler)
-            _nextScope.doHandle(target,baseRequest,request, response);
-        else if (_handler!=null)
-            _handler.handle(target,baseRequest, request, response);
-    }
-    
-    /* ------------------------------------------------------------ */
-    protected boolean never()
-    {
-        return false;
-    }
-    
-}