From 937d475839bfec3877c73c07e620bd4d9185c160 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 26 Apr 2010 20:15:12 +0000 Subject: [PATCH] 310431 Default ErrorHandler as server Bean git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1566 7e9141cc-0065-0410-87d8-b60c137991c4 --- VERSION.txt | 1 + .../jetty/deploy/test/XmlConfiguredJetty.java | 4 +-- .../jetty/security/SecurityHandler.java | 7 ++-- .../org/eclipse/jetty/server/Response.java | 5 +-- .../java/org/eclipse/jetty/server/Server.java | 32 ++++++++++++++++--- .../jetty/server/handler/ContextHandler.java | 5 --- .../jetty/server/handler/ErrorHandler.java | 8 ++--- .../jetty/servlet/ServletContextHandler.java | 1 - .../eclipse/jetty/testing/ServletTester.java | 2 ++ .../eclipse/jetty/testing/ServletTest.java | 2 ++ 10 files changed, 43 insertions(+), 24 deletions(-) diff --git a/VERSION.txt b/VERSION.txt index 001d2e8eec2..63f9acb29c8 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -12,6 +12,7 @@ jetty-7.1.0-SNAPSHOT + 309765 Added JSP module + 310051 _configurationClasses now defaults to null in WebAppContext + 310094 Improved start.jar usage and config files + + 310431 Default ErrorHandler as server Bean + JETTY-903 Stop both caches + JETTY-1200 SSL NIO Endpoint wraps non NIO buffers + JETTY-1202 Use platform default algorithm for SecureRandom diff --git a/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/test/XmlConfiguredJetty.java b/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/test/XmlConfiguredJetty.java index e2e18f911e5..954c776397e 100644 --- a/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/test/XmlConfiguredJetty.java +++ b/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/test/XmlConfiguredJetty.java @@ -261,9 +261,7 @@ public class XmlConfiguredJetty public DeploymentManager getActiveDeploymentManager() { - List depmans = server.getBeans(DeploymentManager.class); - Assert.assertEquals("DeploymentManager bean count",1,depmans.size()); - return depmans.get(0); + return server.getBean(DeploymentManager.class); } public File getJettyDir(String name) diff --git a/jetty-security/src/main/java/org/eclipse/jetty/security/SecurityHandler.java b/jetty-security/src/main/java/org/eclipse/jetty/security/SecurityHandler.java index b3f9e8ac7af..0d78853666a 100644 --- a/jetty-security/src/main/java/org/eclipse/jetty/security/SecurityHandler.java +++ b/jetty-security/src/main/java/org/eclipse/jetty/security/SecurityHandler.java @@ -256,15 +256,13 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti /* ------------------------------------------------------------ */ protected IdentityService findIdentityService() { - List services = getServer().getBeans(IdentityService.class); - if (services!=null && services.size()>0) - return services.get(0); - return null; + return getServer().getBean(IdentityService.class); } /* ------------------------------------------------------------ */ /** */ + @Override protected void doStart() throws Exception { @@ -354,6 +352,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti } + /* ------------------------------------------------------------ */ protected boolean checkSecurity(Request request) { switch(request.getDispatcherType()) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java index ee9db9dfaf4..02634931906 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.util.Collections; import java.util.Enumeration; +import java.util.List; import java.util.Locale; import javax.servlet.ServletOutputStream; @@ -288,14 +289,14 @@ public class Response implements HttpServletResponse ContextHandler.Context context = request.getContext(); if (context!=null) error_handler=context.getContextHandler().getErrorHandler(); + if (error_handler==null) + error_handler = _connection.getConnector().getServer().getBean(ErrorHandler.class); if (error_handler!=null) { - // TODO - probably should reset these after the request? request.setAttribute(Dispatcher.ERROR_STATUS_CODE,new Integer(code)); request.setAttribute(Dispatcher.ERROR_MESSAGE, message); request.setAttribute(Dispatcher.ERROR_REQUEST_URI, request.getRequestURI()); request.setAttribute(Dispatcher.ERROR_SERVLET_NAME,request.getServletName()); - error_handler.handle(null,_connection.getRequest(),_connection.getRequest(),this ); } else diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java index 344f4463ef2..69b7fb72ea3 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java @@ -14,15 +14,11 @@ package org.eclipse.jetty.server; import java.io.IOException; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.ListIterator; -import java.util.Set; -import java.util.concurrent.CopyOnWriteArraySet; - import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -499,6 +495,34 @@ public class Server extends HandlerWrapper implements Attributes return beans; } + /* ------------------------------------------------------------ */ + /** Get dependent bean of a specific class. + * If more than one bean of the type exist, the first is returned. + * @see #addBean(Object) + * @param clazz + * @return bean or null + */ + public T getBean(Class clazz) + { + Iterator iter = _dependentBeans.iterator(); + T t=null; + int count=0; + while (iter.hasNext()) + { + Object o = iter.next(); + if (clazz.isInstance(o)) + { + count++; + if (t==null) + t=(T)o; + } + } + if (count>1) + Log.debug("getBean({}) 1 of {}",clazz.getName(),count); + + return t; + } + /** * Remove a LifeCycle object to be started/stopped * along with the Server diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index 91ca000710e..b9844be2630 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -578,14 +578,9 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server. old_context=__context.get(); __context.set(_scontext); - if (_errorHandler==null) - setErrorHandler(new ErrorHandler()); - - // defers the calling of super.doStart() startContext(); - _availability=_shutdown?__SHUTDOWN:_available?__AVAILABLE:__UNAVAILABLE; } finally diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java index a8dabdaf56c..6a422e9be84 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java @@ -27,13 +27,11 @@ import org.eclipse.jetty.server.HttpConnection; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.util.ByteArrayISO8859Writer; - /* ------------------------------------------------------------ */ /** Handler for Error pages - * A handler that is registered at the org.eclipse.http.ErrorHandler - * context attributed and called by the HttpResponse.sendError method to write a - * error page. - * + * An ErrorHandler is registered with {@link ContextHandler#setErrorHandler(ErrorHandler)} or + * {@link org.eclipse.jetty.server.Server#addBean(Object)}. + * It is called by the HttpResponse.sendError method to write a error page. * */ public class ErrorHandler extends AbstractHandler diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java index b70d1bef589..03ba5b22d43 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java @@ -113,7 +113,6 @@ public class ServletContextHandler extends ContextHandler else if (parent instanceof HandlerCollection) ((HandlerCollection)parent).addHandler(this); } - /* ------------------------------------------------------------ */ /** Get the defaultSecurityHandlerClass. diff --git a/test-jetty-servlet/src/main/java/org/eclipse/jetty/testing/ServletTester.java b/test-jetty-servlet/src/main/java/org/eclipse/jetty/testing/ServletTester.java index eac3e1f4ba4..b31636fec86 100644 --- a/test-jetty-servlet/src/main/java/org/eclipse/jetty/testing/ServletTester.java +++ b/test-jetty-servlet/src/main/java/org/eclipse/jetty/testing/ServletTester.java @@ -21,6 +21,7 @@ import org.eclipse.jetty.io.ByteArrayBuffer; import org.eclipse.jetty.server.LocalConnector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.bio.SocketConnector; +import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -60,6 +61,7 @@ public class ServletTester { try { + _server.addBean(new ErrorHandler()); _server.setSendServerVersion(false); _server.addConnector(_connector); _server.setHandler(_context); diff --git a/test-jetty-servlet/src/test/java/org/eclipse/jetty/testing/ServletTest.java b/test-jetty-servlet/src/test/java/org/eclipse/jetty/testing/ServletTest.java index 22e2bba9b01..5e8bda0f0f6 100644 --- a/test-jetty-servlet/src/test/java/org/eclipse/jetty/testing/ServletTest.java +++ b/test-jetty-servlet/src/test/java/org/eclipse/jetty/testing/ServletTest.java @@ -253,6 +253,8 @@ public class ServletTest extends TestCase out.put(request0.getBytes("iso8859-1")); String responses = tester.getResponses(out).toString(); + System.err.println(responses); + int offset = responses.indexOf("HTTP/1.1 500"); assertTrue(offset>=0); offset = responses.indexOf("Content-Length: ",offset);