Improved dump output of ServletHandler and ContextHandler Fixed duplicate listeners Removed unused fields Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
parent
baca9cae39
commit
9d37feba3c
|
@ -20,8 +20,23 @@ package org.eclipse.jetty.embedded;
|
|||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.DefaultServlet;
|
||||
import org.eclipse.jetty.servlet.ListenerHolder;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletRequestEvent;
|
||||
import javax.servlet.ServletRequestListener;
|
||||
import javax.servlet.ServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.EnumSet;
|
||||
|
||||
public class OneServletContext
|
||||
{
|
||||
public static void main( String[] args ) throws Exception
|
||||
|
@ -35,11 +50,72 @@ public class OneServletContext
|
|||
server.setHandler(context);
|
||||
|
||||
// Add dump servlet
|
||||
context.addServlet(DumpServlet.class, "/dump/*");
|
||||
// Add default servlet
|
||||
context.addServlet(
|
||||
context.addServlet(DumpServlet.class, "/dump/*"),
|
||||
"*.dump");
|
||||
context.addServlet(HelloServlet.class, "/hello/*");
|
||||
context.addServlet(DefaultServlet.class, "/");
|
||||
|
||||
context.addFilter(TestFilter.class,"/*", EnumSet.of(DispatcherType.REQUEST));
|
||||
context.addFilter(TestFilter.class,"/test", EnumSet.of(DispatcherType.REQUEST,DispatcherType.ASYNC));
|
||||
context.addFilter(TestFilter.class,"*.test", EnumSet.of(DispatcherType.REQUEST,DispatcherType.INCLUDE,DispatcherType.FORWARD));
|
||||
|
||||
context.getServletHandler().addListener(new ListenerHolder(InitListener.class));
|
||||
context.getServletHandler().addListener(new ListenerHolder(RequestListener.class));
|
||||
|
||||
server.start();
|
||||
server.dumpStdErr();
|
||||
server.join();
|
||||
}
|
||||
|
||||
|
||||
public static class TestFilter implements Filter
|
||||
{
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
|
||||
{
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static class InitListener implements ServletContextListener
|
||||
{
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce)
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class RequestListener implements ServletRequestListener
|
||||
{
|
||||
@Override
|
||||
public void requestDestroyed(ServletRequestEvent sre)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestInitialized(ServletRequestEvent sre)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -201,7 +201,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
private final List<ServletRequestAttributeListener> _servletRequestAttributeListeners = new CopyOnWriteArrayList<>();
|
||||
private final List<ContextScopeListener> _contextListeners = new CopyOnWriteArrayList<>();
|
||||
private final List<EventListener> _durableListeners = new CopyOnWriteArrayList<>();
|
||||
private Map<String, Object> _managedAttributes;
|
||||
private String[] _protectedTargets;
|
||||
private final CopyOnWriteArrayList<AliasCheck> _aliasChecks = new CopyOnWriteArrayList<ContextHandler.AliasCheck>();
|
||||
|
||||
|
@ -258,9 +257,10 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
public void dump(Appendable out, String indent) throws IOException
|
||||
{
|
||||
dumpBeans(out,indent,Collections.singletonList(new ClassLoaderDump(getClassLoader())),
|
||||
Collections.singletonList(new DumpableCollection("Handler attributes " + this,((AttributesMap)getAttributes()).getAttributeEntrySet())),
|
||||
Collections.singletonList(new DumpableCollection("Context attributes " + this,((Context)getServletContext()).getAttributeEntrySet())),
|
||||
Collections.singletonList(new DumpableCollection("Initparams " + this,getInitParams().entrySet())));
|
||||
Collections.singletonList(new DumpableCollection("eventListeners "+this,_eventListeners)),
|
||||
Collections.singletonList(new DumpableCollection("handler attributes " + this,((AttributesMap)getAttributes()).getAttributeEntrySet())),
|
||||
Collections.singletonList(new DumpableCollection("context attributes " + this,((Context)getServletContext()).getAttributeEntrySet())),
|
||||
Collections.singletonList(new DumpableCollection("initparams " + this,getInitParams().entrySet())));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -1553,10 +1553,9 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
|||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Deprecated
|
||||
public void setManagedAttribute(String name, Object value)
|
||||
{
|
||||
Object old = _managedAttributes.put(name,value);
|
||||
updateBean(old,value);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
|
|
@ -19,7 +19,9 @@
|
|||
package org.eclipse.jetty.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.EnumSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
|
||||
|
@ -86,11 +88,31 @@ public class FilterMapping implements Dumpable
|
|||
throw new IllegalArgumentException(type.toString());
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Dispatch type from name
|
||||
* @param type the dispatcher type
|
||||
* @return the type constant ({@link #REQUEST}, {@link #ASYNC}, {@link #FORWARD}, {@link #INCLUDE}, or {@link #ERROR})
|
||||
*/
|
||||
public static DispatcherType dispatch(int type)
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
case REQUEST:
|
||||
return DispatcherType.REQUEST;
|
||||
case ASYNC:
|
||||
return DispatcherType.ASYNC;
|
||||
case FORWARD:
|
||||
return DispatcherType.FORWARD;
|
||||
case INCLUDE:
|
||||
return DispatcherType.INCLUDE;
|
||||
case ERROR:
|
||||
return DispatcherType.ERROR;
|
||||
}
|
||||
throw new IllegalArgumentException(Integer.toString(type));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------ */
|
||||
|
||||
|
||||
private int _dispatches=DEFAULT;
|
||||
private String _filterName;
|
||||
private transient FilterHolder _holder;
|
||||
|
@ -122,7 +144,7 @@ public class FilterMapping implements Dumpable
|
|||
/* ------------------------------------------------------------ */
|
||||
/** Check if this filter applies to a particular dispatch type.
|
||||
* @param type The type of request:
|
||||
* {@link Handler#REQUEST}, {@link Handler#FORWARD}, {@link Handler#INCLUDE} or {@link Handler#ERROR}.
|
||||
* {@link #REQUEST}, {@link #FORWARD}, {@link #INCLUDE} or {@link #ERROR}.
|
||||
* @return <code>true</code> if this filter applies
|
||||
*/
|
||||
boolean appliesTo(int type)
|
||||
|
@ -295,9 +317,9 @@ public class FilterMapping implements Dumpable
|
|||
public String toString()
|
||||
{
|
||||
return
|
||||
TypeUtil.asList(_pathSpecs)+"/"+
|
||||
TypeUtil.asList(_servletNames)+"=="+
|
||||
_dispatches+"=>"+
|
||||
TypeUtil.asList(_pathSpecs)+"/"+
|
||||
TypeUtil.asList(_servletNames)+"/"+
|
||||
Arrays.stream(DispatcherType.values()).filter(this::appliesTo).collect(Collectors.toSet())+"=>"+
|
||||
_filterName;
|
||||
}
|
||||
|
||||
|
|
|
@ -46,7 +46,12 @@ public class ListenerHolder extends BaseHolder<EventListener>
|
|||
{
|
||||
super(source);
|
||||
}
|
||||
|
||||
|
||||
public ListenerHolder(Class<? extends EventListener> listenerClass)
|
||||
{
|
||||
super(Source.EMBEDDED);
|
||||
setHeldClass(listenerClass);
|
||||
}
|
||||
|
||||
public EventListener getListener()
|
||||
{
|
||||
|
|
|
@ -153,7 +153,7 @@ public class ServletContextHandler extends ContextHandler
|
|||
/* ------------------------------------------------------------ */
|
||||
public ServletContextHandler(HandlerContainer parent, String contextPath, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler,int options)
|
||||
{
|
||||
super((ContextHandler.Context)null);
|
||||
super(parent, contextPath);
|
||||
_options=options;
|
||||
_scontext = new Context();
|
||||
_sessionHandler = sessionHandler;
|
||||
|
@ -163,15 +163,6 @@ public class ServletContextHandler extends ContextHandler
|
|||
_objFactory = new DecoratedObjectFactory();
|
||||
_objFactory.addDecorator(new DeprecationWarning());
|
||||
|
||||
if (contextPath!=null)
|
||||
setContextPath(contextPath);
|
||||
|
||||
if (parent instanceof HandlerWrapper)
|
||||
((HandlerWrapper)parent).setHandler(this);
|
||||
else if (parent instanceof HandlerCollection)
|
||||
((HandlerCollection)parent).addHandler(this);
|
||||
|
||||
|
||||
// Link the handlers
|
||||
relinkHandlers();
|
||||
|
||||
|
|
|
@ -32,6 +32,7 @@ import java.util.Set;
|
|||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
import javax.servlet.Filter;
|
||||
|
@ -67,6 +68,7 @@ import org.eclipse.jetty.util.MultiMap;
|
|||
import org.eclipse.jetty.util.URIUtil;
|
||||
import org.eclipse.jetty.util.annotation.ManagedAttribute;
|
||||
import org.eclipse.jetty.util.annotation.ManagedObject;
|
||||
import org.eclipse.jetty.util.component.DumpableCollection;
|
||||
import org.eclipse.jetty.util.component.LifeCycle;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
|
@ -124,8 +126,6 @@ public class ServletHandler extends ScopedHandler
|
|||
@SuppressWarnings("unchecked")
|
||||
protected final Queue<String>[] _chainLRU = new Queue[FilterMapping.ALL];
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Constructor.
|
||||
*/
|
||||
|
@ -133,6 +133,18 @@ public class ServletHandler extends ScopedHandler
|
|||
{
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public void dump(Appendable out, String indent) throws IOException
|
||||
{
|
||||
dumpBeans(out,indent,
|
||||
Collections.singletonList(new DumpableCollection("listeners "+this,_listeners)),
|
||||
Collections.singletonList(new DumpableCollection("filters "+this,_filters)),
|
||||
Collections.singletonList(new DumpableCollection("filterMappings "+this,_filterMappings)),
|
||||
Collections.singletonList(new DumpableCollection("servlets "+this,_servlets)),
|
||||
Collections.singletonList(new DumpableCollection("servletMappings "+this,_servletMappings)));
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------- */
|
||||
@Override
|
||||
protected synchronized void doStart()
|
||||
|
@ -178,7 +190,7 @@ public class ServletHandler extends ScopedHandler
|
|||
|
||||
if (_contextHandler==null)
|
||||
initialize();
|
||||
|
||||
|
||||
super.doStart();
|
||||
}
|
||||
|
||||
|
@ -257,10 +269,8 @@ public class ServletHandler extends ScopedHandler
|
|||
|
||||
//Retain only filters and mappings that were added using jetty api (ie Source.EMBEDDED)
|
||||
FilterHolder[] fhs = (FilterHolder[]) LazyList.toArray(filterHolders, FilterHolder.class);
|
||||
updateBeans(_filters, fhs);
|
||||
_filters = fhs;
|
||||
FilterMapping[] fms = (FilterMapping[]) LazyList.toArray(filterMappings, FilterMapping.class);
|
||||
updateBeans(_filterMappings, fms);
|
||||
_filterMappings = fms;
|
||||
|
||||
_matchAfterIndex = (_filterMappings == null || _filterMappings.length == 0 ? -1 : _filterMappings.length-1);
|
||||
|
@ -302,10 +312,8 @@ public class ServletHandler extends ScopedHandler
|
|||
|
||||
//Retain only Servlets and mappings added via jetty apis (ie Source.EMBEDDED)
|
||||
ServletHolder[] shs = (ServletHolder[]) LazyList.toArray(servletHolders, ServletHolder.class);
|
||||
updateBeans(_servlets, shs);
|
||||
_servlets = shs;
|
||||
ServletMapping[] sms = (ServletMapping[])LazyList.toArray(servletMappings, ServletMapping.class);
|
||||
updateBeans(_servletMappings, sms);
|
||||
_servletMappings = sms;
|
||||
|
||||
//Retain only Listeners added via jetty apis (is Source.EMBEDDED)
|
||||
|
@ -327,7 +335,6 @@ public class ServletHandler extends ScopedHandler
|
|||
}
|
||||
}
|
||||
ListenerHolder[] listeners = (ListenerHolder[])LazyList.toArray(listenerHolders, ListenerHolder.class);
|
||||
updateBeans(_listeners, listeners);
|
||||
_listeners = listeners;
|
||||
|
||||
//will be regenerated on next start
|
||||
|
@ -730,60 +737,26 @@ public class ServletHandler extends ScopedHandler
|
|||
{
|
||||
MultiException mx = new MultiException();
|
||||
|
||||
//start filter holders now
|
||||
if (_filters != null)
|
||||
{
|
||||
for (FilterHolder f: _filters)
|
||||
{
|
||||
Stream.concat(Stream.concat(
|
||||
Arrays.stream(_filters),
|
||||
Arrays.stream(_servlets).sorted()),
|
||||
Arrays.stream(_listeners))
|
||||
.forEach(h->{
|
||||
try
|
||||
{
|
||||
f.start();
|
||||
f.initialize();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
mx.add(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort and Initialize servlets
|
||||
if (_servlets!=null)
|
||||
{
|
||||
ServletHolder[] servlets = _servlets.clone();
|
||||
Arrays.sort(servlets);
|
||||
for (ServletHolder servlet : servlets)
|
||||
{
|
||||
try
|
||||
{
|
||||
servlet.start();
|
||||
servlet.initialize();
|
||||
if (!h.isStarted())
|
||||
{
|
||||
h.start();
|
||||
h.initialize();
|
||||
}
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
LOG.debug(Log.EXCEPTION, e);
|
||||
mx.add(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//any other beans
|
||||
for (Holder<?> h: getBeans(Holder.class))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!h.isStarted())
|
||||
{
|
||||
h.start();
|
||||
h.initialize();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
mx.add(e);
|
||||
}
|
||||
}
|
||||
|
||||
mx.ifExceptionThrow();
|
||||
}
|
||||
|
||||
|
@ -820,7 +793,6 @@ public class ServletHandler extends ScopedHandler
|
|||
for (ListenerHolder holder:listeners)
|
||||
holder.setServletHandler(this);
|
||||
|
||||
updateBeans(_listeners,listeners);
|
||||
_listeners = listeners;
|
||||
}
|
||||
|
||||
|
@ -1537,7 +1509,6 @@ public class ServletHandler extends ScopedHandler
|
|||
*/
|
||||
public void setFilterMappings(FilterMapping[] filterMappings)
|
||||
{
|
||||
updateBeans(_filterMappings,filterMappings);
|
||||
_filterMappings = filterMappings;
|
||||
if (isStarted()) updateMappings();
|
||||
invalidateChainsCache();
|
||||
|
@ -1550,7 +1521,6 @@ public class ServletHandler extends ScopedHandler
|
|||
for (FilterHolder holder:holders)
|
||||
holder.setServletHandler(this);
|
||||
|
||||
updateBeans(_filters,holders);
|
||||
_filters=holders;
|
||||
updateNameMappings();
|
||||
invalidateChainsCache();
|
||||
|
@ -1562,7 +1532,6 @@ public class ServletHandler extends ScopedHandler
|
|||
*/
|
||||
public void setServletMappings(ServletMapping[] servletMappings)
|
||||
{
|
||||
updateBeans(_servletMappings,servletMappings);
|
||||
_servletMappings = servletMappings;
|
||||
if (isStarted()) updateMappings();
|
||||
invalidateChainsCache();
|
||||
|
@ -1578,7 +1547,6 @@ public class ServletHandler extends ScopedHandler
|
|||
for (ServletHolder holder:holders)
|
||||
holder.setServletHandler(this);
|
||||
|
||||
updateBeans(_servlets,holders);
|
||||
_servlets=holders;
|
||||
updateNameMappings();
|
||||
invalidateChainsCache();
|
||||
|
|
|
@ -19,7 +19,9 @@
|
|||
package org.eclipse.jetty.util.component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
public class DumpableCollection implements Dumpable
|
||||
{
|
||||
|
@ -31,7 +33,12 @@ public class DumpableCollection implements Dumpable
|
|||
_name=name;
|
||||
_collection=collection;
|
||||
}
|
||||
|
||||
|
||||
public DumpableCollection(String name,Object... items)
|
||||
{
|
||||
this(name, items==null?Collections.emptyList():Arrays.asList(items));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String dump()
|
||||
{
|
||||
|
|
|
@ -1900,14 +1900,10 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
|
|||
{
|
||||
//Servlet Spec 3.0 p 74
|
||||
//Duplicate listener declarations don't result in duplicate listener instances
|
||||
EventListener[] listeners=context.getEventListeners();
|
||||
if (listeners!=null)
|
||||
for (ListenerHolder holder : context.getServletHandler().getListeners())
|
||||
{
|
||||
for (EventListener l : listeners)
|
||||
{
|
||||
if (l.getClass().getName().equals(className))
|
||||
return;
|
||||
}
|
||||
if (holder.getClassName().equals(className))
|
||||
return;
|
||||
}
|
||||
|
||||
((WebDescriptor)descriptor).addClassName(className);
|
||||
|
|
Loading…
Reference in New Issue