Merge remote-tracking branch 'origin/jetty-9.4.x' into jetty-10.0.x

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2018-10-03 10:21:14 +10:00
commit 147f96ff4e
8 changed files with 156 additions and 88 deletions

View File

@ -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)
{
}
}
}

View File

@ -202,7 +202,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>();
@ -237,7 +236,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
}
/* ------------------------------------------------------------ */
private ContextHandler(Context context, HandlerContainer parent, String contextPath)
protected ContextHandler(Context context, HandlerContainer parent, String contextPath)
{
_scontext = context == null?new Context():context;
_attributes = new AttributesMap();
@ -259,9 +258,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())));
}
/* ------------------------------------------------------------ */
@ -1554,10 +1554,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);
}
/* ------------------------------------------------------------ */

View File

@ -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;
}

View File

@ -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()
{

View File

@ -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(null, parent, contextPath);
_options=options;
_scontext = new Context();
_sessionHandler = sessionHandler;
@ -163,11 +163,6 @@ public class ServletContextHandler extends ContextHandler
_objFactory = new DecoratedObjectFactory();
_objFactory.addDecorator(new DeprecationWarning());
if (contextPath!=null)
setContextPath(contextPath);
setParent(parent);
// Link the handlers
relinkHandlers();

View File

@ -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;
@ -123,8 +125,6 @@ public class ServletHandler extends ScopedHandler
@SuppressWarnings("unchecked")
protected final Queue<String>[] _chainLRU = new Queue[FilterMapping.ALL];
/* ------------------------------------------------------------ */
/** Constructor.
*/
@ -132,6 +132,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()
@ -177,7 +189,7 @@ public class ServletHandler extends ScopedHandler
if (_contextHandler==null)
initialize();
super.doStart();
}
@ -256,10 +268,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);
@ -301,10 +311,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)
@ -326,7 +334,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
@ -732,60 +739,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();
}
@ -822,7 +795,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();

View File

@ -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()
{

View File

@ -1902,14 +1902,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);