407708 - HttpUpgradeHandler must support injection

This commit is contained in:
Jan Bartel 2013-05-13 17:47:53 +10:00
parent 215d2e2095
commit 73bee3af4b
11 changed files with 117 additions and 253 deletions

View File

@ -18,19 +18,11 @@
package org.eclipse.jetty.annotations;
import java.util.EventListener;
import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler.Decorator;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
/**
* WebAppDecoratorWrapper
* AnnotationDecorator
*
*
*/
@ -53,99 +45,6 @@ public class AnnotationDecorator implements Decorator
_introspector.registerHandler(new ServletSecurityAnnotationHandler(context));
}
/* ------------------------------------------------------------ */
/**
* @param filter
* @throws ServletException
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateFilterHolder(org.eclipse.jetty.servlet.FilterHolder)
*/
public void decorateFilterHolder(FilterHolder filter) throws ServletException
{
}
/* ------------------------------------------------------------ */
/**
* @param <T>
* @param filter
* @return the decorated filter
* @throws ServletException
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateFilterInstance(javax.servlet.Filter)
*/
public <T extends Filter> T decorateFilterInstance(T filter) throws ServletException
{
introspect(filter);
return filter;
}
/* ------------------------------------------------------------ */
/**
* @param <T>
* @param listener
* @return the decorated event listener instance
* @throws ServletException
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateListenerInstance(java.util.EventListener)
*/
public <T extends EventListener> T decorateListenerInstance(T listener) throws ServletException
{
introspect(listener);
return listener;
}
/* ------------------------------------------------------------ */
/**
* @param servlet
* @throws ServletException
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateServletHolder(org.eclipse.jetty.servlet.ServletHolder)
*/
public void decorateServletHolder(ServletHolder servlet) throws ServletException
{
}
/* ------------------------------------------------------------ */
/**
* @param <T>
* @param servlet
* @return the decorated servlet instance
* @throws ServletException
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateServletInstance(javax.servlet.Servlet)
*/
public <T extends Servlet> T decorateServletInstance(T servlet) throws ServletException
{
introspect(servlet);
return servlet;
}
/* ------------------------------------------------------------ */
/**
* @param f
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#destroyFilterInstance(javax.servlet.Filter)
*/
public void destroyFilterInstance(Filter f)
{
}
/* ------------------------------------------------------------ */
/**
* @param s
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#destroyServletInstance(javax.servlet.Servlet)
*/
public void destroyServletInstance(Servlet s)
{
}
/* ------------------------------------------------------------ */
/**
* @param f
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#destroyListenerInstance(java.util.EventListener)
*/
public void destroyListenerInstance(EventListener f)
{
}
/**
* Look for annotations that can be discovered with introspection:
* <ul>
@ -161,4 +60,17 @@ public class AnnotationDecorator implements Decorator
{
_introspector.introspect(o.getClass());
}
@Override
public Object decorate(Object o)
{
introspect(o);
return o;
}
@Override
public void destroy(Object o)
{
}
}

View File

@ -44,7 +44,7 @@ public class PostConstructAnnotationHandler extends AbstractIntrospectableAnnota
public void doHandle(Class clazz)
{
//Check that the PostConstruct is on a class that we're interested in
if (Util.isServletType(clazz))
if (Util.supportsPostConstructPreDestroy(clazz))
{
Method[] methods = clazz.getDeclaredMethods();
for (int i=0; i<methods.length; i++)

View File

@ -43,7 +43,7 @@ public class PreDestroyAnnotationHandler extends AbstractIntrospectableAnnotatio
public void doHandle(Class clazz)
{
//Check that the PreDestroy is on a class that we're interested in
if (Util.isServletType(clazz))
if (Util.supportsPostConstructPreDestroy(clazz))
{
Method[] methods = clazz.getDeclaredMethods();
for (int i=0; i<methods.length; i++)

View File

@ -57,7 +57,7 @@ public class ResourceAnnotationHandler extends AbstractIntrospectableAnnotationH
*/
public void doHandle(Class<?> clazz)
{
if (Util.isServletType(clazz))
if (Util.supportsResourceInjection(clazz))
{
handleClass(clazz);

View File

@ -68,6 +68,42 @@ public class Util
return isServlet;
}
public static boolean supportsResourceInjection (Class c)
{
if (javax.servlet.Servlet.class.isAssignableFrom(c) ||
javax.servlet.Filter.class.isAssignableFrom(c) ||
javax.servlet.ServletContextListener.class.isAssignableFrom(c) ||
javax.servlet.ServletContextAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.ServletRequestListener.class.isAssignableFrom(c) ||
javax.servlet.ServletRequestAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.AsyncListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpUpgradeHandler.class.isAssignableFrom(null))
return true;
return false;
}
public static boolean supportsPostConstructPreDestroy (Class c)
{
if (javax.servlet.Servlet.class.isAssignableFrom(c) ||
javax.servlet.Filter.class.isAssignableFrom(c) ||
javax.servlet.ServletContextListener.class.isAssignableFrom(c) ||
javax.servlet.ServletContextAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.ServletRequestListener.class.isAssignableFrom(c) ||
javax.servlet.ServletRequestAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpSessionAttributeListener.class.isAssignableFrom(c) ||
javax.servlet.AsyncListener.class.isAssignableFrom(c) ||
javax.servlet.http.HttpUpgradeHandler.class.isAssignableFrom(null))
return true;
return false;
}
public static boolean isEnvEntryType (Class type)
{

View File

@ -18,8 +18,6 @@
package org.eclipse.jetty.plus.annotation;
import javax.servlet.ServletException;
import org.eclipse.jetty.servlet.ServletHolder;
/**
@ -57,8 +55,10 @@ public class RunAs
}
/**
* @param holder
*/
public void setRunAs (ServletHolder holder)
throws ServletException
{
if (holder == null)
return;

View File

@ -52,7 +52,6 @@ public class RunAsCollection
}
public RunAs getRunAs (Object o)
throws ServletException
{
if (o==null)
return null;
@ -61,7 +60,6 @@ public class RunAsCollection
}
public void setRunAs(Object o)
throws ServletException
{
if (o == null)
return;

View File

@ -18,24 +18,18 @@
package org.eclipse.jetty.plus.webapp;
import java.util.EventListener;
import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.eclipse.jetty.plus.annotation.InjectionCollection;
import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
import org.eclipse.jetty.plus.annotation.RunAsCollection;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler.Decorator;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.webapp.WebAppContext;
/**
* WebAppDecorator
* PlusDecorator
*
*
*/
@ -50,83 +44,7 @@ public class PlusDecorator implements Decorator
_context = context;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateFilterHolder(org.eclipse.jetty.servlet.FilterHolder)
*/
public void decorateFilterHolder(FilterHolder filter) throws ServletException
{
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateFilterInstance(javax.servlet.Filter)
*/
public <T extends Filter> T decorateFilterInstance(T filter) throws ServletException
{
decorate(filter);
return filter;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateListenerInstance(java.util.EventListener)
*/
public <T extends EventListener> T decorateListenerInstance(T listener) throws ServletException
{
decorate(listener);
return listener;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateServletHolder(org.eclipse.jetty.servlet.ServletHolder)
*/
public void decorateServletHolder(ServletHolder holder) throws ServletException
{
decorate(holder);
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#decorateServletInstance(javax.servlet.Servlet)
*/
public <T extends Servlet> T decorateServletInstance(T servlet) throws ServletException
{
decorate(servlet);
return servlet;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#destroyFilterInstance(javax.servlet.Filter)
*/
public void destroyFilterInstance(Filter f)
{
destroy(f);
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#destroyServletInstance(javax.servlet.Servlet)
*/
public void destroyServletInstance(Servlet s)
{
destroy(s);
}
/**
* @see org.eclipse.jetty.servlet.ServletContextHandler.Decorator#destroyListenerInstance(java.util.EventListener)
*/
public void destroyListenerInstance(EventListener l)
{
destroy(l);
}
protected void decorate (Object o)
throws ServletException
public Object decorate (Object o)
{
RunAsCollection runAses = (RunAsCollection)_context.getAttribute(RunAsCollection.RUNAS_COLLECTION);
@ -146,12 +64,13 @@ public class PlusDecorator implements Decorator
}
catch (Exception e)
{
throw new ServletException(e);
throw new RuntimeException(e);
}
}
return o;
}
protected void destroy (Object o)
public void destroy (Object o)
{
LifeCycleCallbackCollection callbacks = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);
if (callbacks != null)

View File

@ -2218,7 +2218,23 @@ public class Request implements HttpServletRequest
@Override
public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException
{
// TODO Auto-generated method stub
return null;
if (getContext() == null)
throw new ServletException ("Unable to instantiate "+handlerClass);
try
{
//Instantiate an instance and inject it
T h = getContext().createInstance(handlerClass);
//TODO handle the rest of the upgrade process
return h;
}
catch (Exception e)
{
if (e instanceof ServletException)
throw (ServletException)e;
throw new ServletException(e);
}
}
}

View File

@ -2131,13 +2131,9 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
{
try
{
return clazz.newInstance();
return createInstance(clazz);
}
catch (InstantiationException e)
{
throw new ServletException(e);
}
catch (IllegalAccessException e)
catch (Exception e)
{
throw new ServletException(e);
}
@ -2180,6 +2176,13 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
{
return _enabled;
}
public <T> T createInstance (Class<T> clazz) throws Exception
{
T o = clazz.newInstance();
return o;
}
}

View File

@ -46,6 +46,7 @@ import javax.servlet.SessionTrackingMode;
import javax.servlet.descriptor.JspConfigDescriptor;
import javax.servlet.descriptor.JspPropertyGroupDescriptor;
import javax.servlet.descriptor.TaglibDescriptor;
import javax.servlet.http.HttpUpgradeHandler;
import org.eclipse.jetty.security.ConstraintAware;
import org.eclipse.jetty.security.ConstraintMapping;
@ -265,10 +266,10 @@ public class ServletContextHandler extends ContextHandler
Decorator decorator = _decorators.get(i);
if (_servletHandler.getFilters()!=null)
for (FilterHolder holder:_servletHandler.getFilters())
decorator.decorateFilterHolder(holder);
decorator.decorate(holder);
if(_servletHandler.getServlets()!=null)
for (ServletHolder holder:_servletHandler.getServlets())
decorator.decorateServletHolder(holder);
decorator.decorate(holder);
}
}
@ -521,14 +522,14 @@ public class ServletContextHandler extends ContextHandler
void destroyServlet(Servlet servlet)
{
for (Decorator decorator : _decorators)
decorator.destroyServletInstance(servlet);
decorator.destroy(servlet);
}
/* ------------------------------------------------------------ */
void destroyFilter(Filter filter)
{
for (Decorator decorator : _decorators)
decorator.destroyFilterInstance(filter);
decorator.destroy(filter);
}
/* ------------------------------------------------------------ */
@ -1082,19 +1083,10 @@ public class ServletContextHandler extends ContextHandler
{
try
{
T f = c.newInstance();
for (int i=_decorators.size()-1; i>=0; i--)
{
Decorator decorator = _decorators.get(i);
f=decorator.decorateFilterInstance(f);
}
T f = createInstance(c);
return f;
}
catch (InstantiationException e)
{
throw new ServletException(e);
}
catch (IllegalAccessException e)
catch (Exception e)
{
throw new ServletException(e);
}
@ -1106,23 +1098,29 @@ public class ServletContextHandler extends ContextHandler
{
try
{
T s = c.newInstance();
for (int i=_decorators.size()-1; i>=0; i--)
{
Decorator decorator = _decorators.get(i);
s=decorator.decorateServletInstance(s);
}
T s = createInstance(c);
return s;
}
catch (InstantiationException e)
{
throw new ServletException(e);
}
catch (IllegalAccessException e)
catch (Exception e)
{
throw new ServletException(e);
}
}
public <T> T createInstance (Class<T> c) throws Exception
{
T o = super.createInstance(c);
for (int i=_decorators.size()-1; i>=0; i--)
{
Decorator decorator = _decorators.get(i);
o=decorator.decorate(o);
}
return o;
}
@Override
public Set<SessionTrackingMode> getDefaultSessionTrackingModes()
@ -1253,20 +1251,10 @@ public class ServletContextHandler extends ContextHandler
{
try
{
T l = super.createListener(clazz);
for (int i=_decorators.size()-1; i>=0; i--)
{
Decorator decorator = _decorators.get(i);
l=decorator.decorateListenerInstance(l);
}
T l = createInstance(clazz);
return l;
}
catch(ServletException e)
{
throw e;
}
catch(Exception e)
}
catch (Exception e)
{
throw new ServletException(e);
}
@ -1307,15 +1295,7 @@ public class ServletContextHandler extends ContextHandler
*/
public interface Decorator
{
<T extends Filter> T decorateFilterInstance(T filter) throws ServletException;
<T extends Servlet> T decorateServletInstance(T servlet) throws ServletException;
<T extends EventListener> T decorateListenerInstance(T listener) throws ServletException;
void decorateFilterHolder(FilterHolder filter) throws ServletException;
void decorateServletHolder(ServletHolder servlet) throws ServletException;
void destroyServletInstance(Servlet s);
void destroyFilterInstance(Filter f);
void destroyListenerInstance(EventListener f);
<T> T decorate (T o);
void destroy (Object o);
}
}