This commit is contained in:
Jan Bartel 2016-07-08 13:22:34 +10:00
parent 5c6e271b2c
commit 5b5c8c3791
15 changed files with 137 additions and 38 deletions

View File

@ -28,7 +28,7 @@ import javax.servlet.annotation.WebInitParam;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
import org.eclipse.jetty.servlet.Holder;
import org.eclipse.jetty.servlet.Source;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
@ -94,7 +94,7 @@ public class WebFilterAnnotation extends DiscoveredAnnotation
if (holder == null)
{
//Filter with this name does not already exist, so add it
holder = _context.getServletHandler().newFilterHolder(Holder.Source.ANNOTATION);
holder = _context.getServletHandler().newFilterHolder(new Source (Source.Origin.ANNOTATION, clazz.getName()));
holder.setName(name);
holder.setHeldClass(clazz);

View File

@ -28,8 +28,8 @@ import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionIdListener;
import javax.servlet.http.HttpSessionListener;
import org.eclipse.jetty.servlet.BaseHolder.Source;
import org.eclipse.jetty.servlet.ListenerHolder;
import org.eclipse.jetty.servlet.Source;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
@ -82,7 +82,7 @@ public class WebListenerAnnotation extends DiscoveredAnnotation
MetaData metaData = _context.getMetaData();
if (metaData.getOrigin(clazz.getName()+".listener") == Origin.NotSet)
{
ListenerHolder h = _context.getServletHandler().newListenerHolder(Source.ANNOTATION);
ListenerHolder h = _context.getServletHandler().newListenerHolder(new Source(Source.Origin.ANNOTATION, clazz.getName()));
h.setListener(listener);
_context.getServletHandler().addListener(h);
_context.addEventListener(listener);

View File

@ -27,9 +27,9 @@ import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import org.eclipse.jetty.servlet.Holder;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.ServletMapping;
import org.eclipse.jetty.servlet.Source;
import org.eclipse.jetty.util.ArrayUtil;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.log.Log;
@ -130,7 +130,9 @@ public class WebServletAnnotation extends DiscoveredAnnotation
{
//No servlet of this name has already been defined, either by a descriptor
//or another annotation (which would be impossible).
holder = _context.getServletHandler().newServletHolder(Holder.Source.ANNOTATION);
Source source = new Source(Source.Origin.ANNOTATION, clazz.getName());
holder = _context.getServletHandler().newServletHolder(source);
holder.setHeldClass(clazz);
metaData.setOrigin(servletName+".servlet.servlet-class",annotation,clazz);
@ -153,7 +155,7 @@ public class WebServletAnnotation extends DiscoveredAnnotation
_context.getServletHandler().addServlet(holder);
mapping = new ServletMapping();
mapping = new ServletMapping(source);
mapping.setServletName(holder.getName());
mapping.setPathSpecs( LazyList.toStringArray(urlPatternList));
}
@ -190,7 +192,7 @@ public class WebServletAnnotation extends DiscoveredAnnotation
//about processing these url mappings
if (existingMappings.isEmpty() || !containsNonDefaultMappings(existingMappings))
{
mapping = new ServletMapping();
mapping = new ServletMapping(new Source(Source.Origin.ANNOTATION, clazz.getName()));
mapping.setServletName(servletName);
mapping.setPathSpecs(LazyList.toStringArray(urlPatternList));
}

View File

@ -49,10 +49,10 @@ import org.eclipse.jetty.plus.webapp.EnvConfiguration;
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
import org.eclipse.jetty.servlet.Holder;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.ServletMapping;
import org.eclipse.jetty.servlet.Source;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
@ -413,7 +413,7 @@ public class AntWebAppContext extends WebAppContext
{
@Override
public ServletHolder newServletHolder(Holder.Source source)
public ServletHolder newServletHolder(Source source)
{
return new AntServletHolder();
}

View File

@ -45,8 +45,6 @@ public abstract class BaseHolder<T> extends AbstractLifeCycle implements Dumpabl
private static final Logger LOG = Log.getLogger(BaseHolder.class);
public enum Source { EMBEDDED, JAVAX_API, DESCRIPTOR, ANNOTATION };
final protected Source _source;
protected transient Class<? extends T> _class;
protected String _className;

View File

@ -59,7 +59,7 @@ public class FilterHolder extends Holder<Filter>
/** Constructor
* @param source the holder source
*/
public FilterHolder(Holder.Source source)
public FilterHolder(Source source)
{
super(source);
}

View File

@ -59,7 +59,7 @@ public class Holder<T> extends BaseHolder<T>
protected Holder(Source source)
{
super(source);
switch(_source)
switch(_source.getOrigin())
{
case JAVAX_API:
case DESCRIPTOR:

View File

@ -60,7 +60,6 @@ import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.BaseHolder.Source;
import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.DeprecationWarning;
import org.eclipse.jetty.util.annotation.ManagedAttribute;

View File

@ -63,7 +63,6 @@ import org.eclipse.jetty.server.ServletResponseHttpWrapper;
import org.eclipse.jetty.server.UserIdentity;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ScopedHandler;
import org.eclipse.jetty.servlet.BaseHolder.Source;
import org.eclipse.jetty.util.ArrayUtil;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.MultiException;
@ -802,7 +801,7 @@ public class ServletHandler extends ScopedHandler
}
/* ------------------------------------------------------------ */
public ListenerHolder newListenerHolder(Holder.Source source)
public ListenerHolder newListenerHolder(Source source)
{
return new ListenerHolder(source);
}
@ -822,7 +821,7 @@ public class ServletHandler extends ScopedHandler
* @param source the holder source
* @return the servlet holder
*/
public ServletHolder newServletHolder(Holder.Source source)
public ServletHolder newServletHolder(Source source)
{
return new ServletHolder(source);
}
@ -915,7 +914,7 @@ public class ServletHandler extends ScopedHandler
}
/* ------------------------------------------------------------ */
public FilterHolder newFilterHolder(Holder.Source source)
public FilterHolder newFilterHolder(Source source)
{
return new FilterHolder(source);
}
@ -1361,16 +1360,27 @@ public class ServletHandler extends ScopedHandler
else
{
//existing candidate isn't a default, if the one we're looking at isn't a default either, then its an error
if (!mapping.isDefault())
throw new IllegalStateException("Multiple servlets map to path: "+pathSpec+": "+finalMapping.getServletName()+","+mapping.getServletName());
if (!mapping.isDefault())
{
ServletHolder finalMappedServlet = _servletNameMap.get(finalMapping.getServletName());
throw new IllegalStateException("Multiple servlets map to path "
+pathSpec+": "
+finalMappedServlet.getName()+"[mapped:"+finalMapping.getSource()+"],"
+mapping.getServletName()+"[mapped:"+mapping.getSource()+"]");
}
}
}
}
if (finalMapping == null)
throw new IllegalStateException ("No acceptable servlet mappings for "+pathSpec);
if (LOG.isDebugEnabled()) LOG.debug("Chose path={} mapped to servlet={} from default={}", pathSpec, finalMapping.getServletName(), finalMapping.isDefault());
if (LOG.isDebugEnabled())
LOG.debug("Path={}[{}] mapped to servlet={}[{}]",
pathSpec,
finalMapping.getSource(),
finalMapping.getServletName(),
_servletNameMap.get(finalMapping.getServletName()).getSource());
servletPathMappings.put(pathSpec, finalMapping);
pm.put(new ServletPathSpec(pathSpec),_servletNameMap.get(finalMapping.getServletName()));
}

View File

@ -105,7 +105,7 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
/** Constructor .
* @param creator the holder source
*/
public ServletHolder(Holder.Source creator)
public ServletHolder(Source creator)
{
super(creator);
}
@ -1022,7 +1022,7 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
return clash;
//otherwise apply all of them
ServletMapping mapping = new ServletMapping();
ServletMapping mapping = new ServletMapping(Source.JAVAX_API);
mapping.setServletName(ServletHolder.this.getName());
mapping.setPathSpecs(urlPatterns);
_servletHandler.addServletMapping(mapping);

View File

@ -30,11 +30,19 @@ public class ServletMapping
private String[] _pathSpecs;
private String _servletName;
private boolean _default;
private Source _source;
/* ------------------------------------------------------------ */
public ServletMapping()
{
this (Source.EMBEDDED);
}
public ServletMapping(Source source)
{
_source = source;
}
/* ------------------------------------------------------------ */
@ -117,6 +125,12 @@ public class ServletMapping
{
_default = fromDefault;
}
/* ------------------------------------------------------------ */
public Source getSource ()
{
return _source;
}
/* ------------------------------------------------------------ */
public String toString()

View File

@ -0,0 +1,78 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.servlet;
/**
* Source
*
* The source of a web artifact: servlet, filter, mapping etc
*/
public class Source
{
public static final Source EMBEDDED = new Source (Origin.EMBEDDED, null);
public static final Source JAVAX_API = new Source (Origin.JAVAX_API, null);
public enum Origin { EMBEDDED, JAVAX_API, DESCRIPTOR, ANNOTATION };
public Origin _origin;
public String _resource;
/**
* @param o the Origin of the artifact (servlet, filter, mapping etc)
* @param resource the location where the artifact was declared
*/
public Source (Origin o, String resource)
{
if (o == null)
throw new IllegalArgumentException ("Origin is null");
_origin = o;
_resource = resource;
}
/**
* @return the origin
*/
public Origin getOrigin()
{
return _origin;
}
/**
* @return the resource
*/
public String getResource()
{
return _resource;
}
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return _origin +":"+ _resource;
}
}

View File

@ -48,7 +48,6 @@ import java.util.Set;
import javax.servlet.ServletRegistration;
import org.eclipse.jetty.servlet.BaseHolder.Source;
import org.junit.Test;
/**

View File

@ -27,16 +27,15 @@ import java.util.EnumSet;
import javax.servlet.DispatcherType;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.servlet.BaseHolder.Source;
import org.junit.Before;
import org.junit.Test;
public class ServletHandlerTest
{
FilterHolder fh1 = new FilterHolder(Source.DESCRIPTOR);
FilterHolder fh1 = new FilterHolder( new Source (Source.Origin.DESCRIPTOR, "foo.xml"));
FilterMapping fm1 = new FilterMapping();
FilterHolder fh2 = new FilterHolder(Source.DESCRIPTOR);
FilterHolder fh2 = new FilterHolder(new Source (Source.Origin.DESCRIPTOR, "foo.xml"));
FilterMapping fm2 = new FilterMapping();
FilterHolder fh3 = new FilterHolder(Source.JAVAX_API);
@ -91,7 +90,7 @@ public class ServletHandlerTest
assertTrue(fm2 == mappings[1]);
//add another ordinary mapping
FilterHolder of1 = new FilterHolder(Source.DESCRIPTOR);
FilterHolder of1 = new FilterHolder(new Source (Source.Origin.DESCRIPTOR, "foo.xml"));
FilterMapping ofm1 = new FilterMapping();
ofm1.setFilterHolder(of1);
ofm1.setPathSpec("/*");

View File

@ -39,7 +39,6 @@ import javax.servlet.SessionTrackingMode;
import org.eclipse.jetty.security.ConstraintAware;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.authentication.FormAuthenticator;
import org.eclipse.jetty.servlet.BaseHolder.Source;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
@ -49,6 +48,7 @@ import org.eclipse.jetty.servlet.ServletContextHandler.JspPropertyGroup;
import org.eclipse.jetty.servlet.ServletContextHandler.TagLib;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.ServletMapping;
import org.eclipse.jetty.servlet.Source;
import org.eclipse.jetty.util.ArrayUtil;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.StringUtil;
@ -213,7 +213,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
//If servlet of that name does not already exist, create it.
if (holder == null)
{
holder = context.getServletHandler().newServletHolder(Source.DESCRIPTOR);
holder = context.getServletHandler().newServletHolder(new Source (Source.Origin.DESCRIPTOR, descriptor.getResource().toString()));
holder.setName(name);
_servletHolderMap.put(name,holder);
_servletHolders.add(holder);
@ -1181,7 +1181,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
public ServletMapping addServletMapping (String servletName, XmlParser.Node node, WebAppContext context, Descriptor descriptor)
{
ServletMapping mapping = new ServletMapping();
ServletMapping mapping = new ServletMapping(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString()));
mapping.setServletName(servletName);
mapping.setDefault(descriptor instanceof DefaultsDescriptor);
@ -1415,7 +1415,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
else
{
//no mapping for jsp yet, make one
ServletMapping mapping = new ServletMapping();
ServletMapping mapping = new ServletMapping(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString()));
mapping.setServletName("jsp");
mapping.setPathSpecs(paths.toArray(new String[paths.size()]));
_servletMappings.add(mapping);
@ -1710,7 +1710,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
FilterHolder holder = _filterHolderMap.get(name);
if (holder == null)
{
holder = context.getServletHandler().newFilterHolder(Source.DESCRIPTOR);
holder = context.getServletHandler().newFilterHolder(new Source (Source.Origin.DESCRIPTOR, descriptor.getResource().toString()));
holder.setName(name);
_filterHolderMap.put(name,holder);
_filterHolders.add(holder);
@ -1896,7 +1896,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
((WebDescriptor)descriptor).addClassName(className);
Class<? extends EventListener> listenerClass = (Class<? extends EventListener>)context.loadClass(className);
listener = newListenerInstance(context,listenerClass);
listener = newListenerInstance(context,listenerClass, descriptor);
if (!(listener instanceof EventListener))
{
LOG.warn("Not an EventListener: " + listener);
@ -1938,9 +1938,9 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
((ConstraintAware)context.getSecurityHandler()).setDenyUncoveredHttpMethods(true);
}
public EventListener newListenerInstance(WebAppContext context,Class<? extends EventListener> clazz) throws Exception
public EventListener newListenerInstance(WebAppContext context,Class<? extends EventListener> clazz, Descriptor descriptor) throws Exception
{
ListenerHolder h = context.getServletHandler().newListenerHolder(Source.DESCRIPTOR);
ListenerHolder h = context.getServletHandler().newListenerHolder(new Source (Source.Origin.DESCRIPTOR, descriptor.getResource().toString()));
EventListener l = context.getServletContext().createInstance(clazz);
h.setListener(l);
context.getServletHandler().addListener(h);