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
This commit is contained in:
Greg Wilkins 2010-04-26 20:15:12 +00:00
parent 84e467659e
commit 937d475839
10 changed files with 43 additions and 24 deletions

View File

@ -12,6 +12,7 @@ jetty-7.1.0-SNAPSHOT
+ 309765 Added JSP module + 309765 Added JSP module
+ 310051 _configurationClasses now defaults to null in WebAppContext + 310051 _configurationClasses now defaults to null in WebAppContext
+ 310094 Improved start.jar usage and config files + 310094 Improved start.jar usage and config files
+ 310431 Default ErrorHandler as server Bean
+ JETTY-903 Stop both caches + JETTY-903 Stop both caches
+ JETTY-1200 SSL NIO Endpoint wraps non NIO buffers + JETTY-1200 SSL NIO Endpoint wraps non NIO buffers
+ JETTY-1202 Use platform default algorithm for SecureRandom + JETTY-1202 Use platform default algorithm for SecureRandom

View File

@ -261,9 +261,7 @@ public class XmlConfiguredJetty
public DeploymentManager getActiveDeploymentManager() public DeploymentManager getActiveDeploymentManager()
{ {
List<DeploymentManager> depmans = server.getBeans(DeploymentManager.class); return server.getBean(DeploymentManager.class);
Assert.assertEquals("DeploymentManager bean count",1,depmans.size());
return depmans.get(0);
} }
public File getJettyDir(String name) public File getJettyDir(String name)

View File

@ -256,15 +256,13 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
protected IdentityService findIdentityService() protected IdentityService findIdentityService()
{ {
List<IdentityService> services = getServer().getBeans(IdentityService.class); return getServer().getBean(IdentityService.class);
if (services!=null && services.size()>0)
return services.get(0);
return null;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
*/ */
@Override
protected void doStart() protected void doStart()
throws Exception throws Exception
{ {
@ -354,6 +352,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
} }
/* ------------------------------------------------------------ */
protected boolean checkSecurity(Request request) protected boolean checkSecurity(Request request)
{ {
switch(request.getDispatcherType()) switch(request.getDispatcherType())

View File

@ -17,6 +17,7 @@ import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
@ -288,14 +289,14 @@ public class Response implements HttpServletResponse
ContextHandler.Context context = request.getContext(); ContextHandler.Context context = request.getContext();
if (context!=null) if (context!=null)
error_handler=context.getContextHandler().getErrorHandler(); error_handler=context.getContextHandler().getErrorHandler();
if (error_handler==null)
error_handler = _connection.getConnector().getServer().getBean(ErrorHandler.class);
if (error_handler!=null) 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_STATUS_CODE,new Integer(code));
request.setAttribute(Dispatcher.ERROR_MESSAGE, message); request.setAttribute(Dispatcher.ERROR_MESSAGE, message);
request.setAttribute(Dispatcher.ERROR_REQUEST_URI, request.getRequestURI()); request.setAttribute(Dispatcher.ERROR_REQUEST_URI, request.getRequestURI());
request.setAttribute(Dispatcher.ERROR_SERVLET_NAME,request.getServletName()); request.setAttribute(Dispatcher.ERROR_SERVLET_NAME,request.getServletName());
error_handler.handle(null,_connection.getRequest(),_connection.getRequest(),this ); error_handler.handle(null,_connection.getRequest(),_connection.getRequest(),this );
} }
else else

View File

@ -14,15 +14,11 @@
package org.eclipse.jetty.server; package org.eclipse.jetty.server;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -499,6 +495,34 @@ public class Server extends HandlerWrapper implements Attributes
return beans; 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> T getBean(Class<T> 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 * Remove a LifeCycle object to be started/stopped
* along with the Server * along with the Server

View File

@ -578,14 +578,9 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
old_context=__context.get(); old_context=__context.get();
__context.set(_scontext); __context.set(_scontext);
if (_errorHandler==null)
setErrorHandler(new ErrorHandler());
// defers the calling of super.doStart() // defers the calling of super.doStart()
startContext(); startContext();
_availability=_shutdown?__SHUTDOWN:_available?__AVAILABLE:__UNAVAILABLE; _availability=_shutdown?__SHUTDOWN:_available?__AVAILABLE:__UNAVAILABLE;
} }
finally finally

View File

@ -27,13 +27,11 @@ import org.eclipse.jetty.server.HttpConnection;
import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.ByteArrayISO8859Writer; import org.eclipse.jetty.util.ByteArrayISO8859Writer;
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** Handler for Error pages /** Handler for Error pages
* A handler that is registered at the org.eclipse.http.ErrorHandler * An ErrorHandler is registered with {@link ContextHandler#setErrorHandler(ErrorHandler)} or
* context attributed and called by the HttpResponse.sendError method to write a * {@link org.eclipse.jetty.server.Server#addBean(Object)}.
* error page. * It is called by the HttpResponse.sendError method to write a error page.
*
* *
*/ */
public class ErrorHandler extends AbstractHandler public class ErrorHandler extends AbstractHandler

View File

@ -113,7 +113,6 @@ public class ServletContextHandler extends ContextHandler
else if (parent instanceof HandlerCollection) else if (parent instanceof HandlerCollection)
((HandlerCollection)parent).addHandler(this); ((HandlerCollection)parent).addHandler(this);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** Get the defaultSecurityHandlerClass. /** Get the defaultSecurityHandlerClass.

View File

@ -21,6 +21,7 @@ import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.server.LocalConnector; import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector; 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.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
@ -60,6 +61,7 @@ public class ServletTester
{ {
try try
{ {
_server.addBean(new ErrorHandler());
_server.setSendServerVersion(false); _server.setSendServerVersion(false);
_server.addConnector(_connector); _server.addConnector(_connector);
_server.setHandler(_context); _server.setHandler(_context);

View File

@ -253,6 +253,8 @@ public class ServletTest extends TestCase
out.put(request0.getBytes("iso8859-1")); out.put(request0.getBytes("iso8859-1"));
String responses = tester.getResponses(out).toString(); String responses = tester.getResponses(out).toString();
System.err.println(responses);
int offset = responses.indexOf("HTTP/1.1 500"); int offset = responses.indexOf("HTTP/1.1 500");
assertTrue(offset>=0); assertTrue(offset>=0);
offset = responses.indexOf("Content-Length: ",offset); offset = responses.indexOf("Content-Length: ",offset);