Making ServletContextHandler use util.Decorators

+ Deprecating methods that are just awkward now.
   ServletContextHandler.getDecorators() - as it expects the
     ServletContextHandler.Decorator version
This commit is contained in:
Joakim Erdfelt 2014-11-21 12:01:32 -07:00
parent 2b7b5ef495
commit fb88bc4c19
2 changed files with 104 additions and 66 deletions

View File

@ -60,6 +60,7 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.server.handler.gzip.GzipHandler; import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.server.session.SessionHandler; import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.BaseHolder.Source; import org.eclipse.jetty.servlet.BaseHolder.Source;
import org.eclipse.jetty.util.Decorators;
import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.component.LifeCycle;
@ -87,7 +88,7 @@ public class ServletContextHandler extends ContextHandler
public interface ServletContainerInitializerCaller extends LifeCycle {}; public interface ServletContainerInitializerCaller extends LifeCycle {};
protected final List<Decorator> _decorators= new ArrayList<>(); protected final Decorators _decorators= new Decorators();
protected Class<? extends SecurityHandler> _defaultSecurityHandlerClass=org.eclipse.jetty.security.ConstraintSecurityHandler.class; protected Class<? extends SecurityHandler> _defaultSecurityHandlerClass=org.eclipse.jetty.security.ConstraintSecurityHandler.class;
protected SessionHandler _sessionHandler; protected SessionHandler _sessionHandler;
protected SecurityHandler _securityHandler; protected SecurityHandler _securityHandler;
@ -248,6 +249,13 @@ public class ServletContextHandler extends ContextHandler
} }
@Override
protected void doStart() throws Exception
{
setAttribute(Decorators.class.getName(), _decorators);
super.doStart();
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* @see org.eclipse.jetty.server.handler.ContextHandler#doStop() * @see org.eclipse.jetty.server.handler.ContextHandler#doStop()
@ -256,8 +264,7 @@ public class ServletContextHandler extends ContextHandler
protected void doStop() throws Exception protected void doStop() throws Exception
{ {
super.doStop(); super.doStop();
if (_decorators != null) _decorators.clear();
_decorators.clear();
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -318,18 +325,13 @@ public class ServletContextHandler extends ContextHandler
if (_servletHandler != null) if (_servletHandler != null)
{ {
//Call decorators on all holders, and also on any EventListeners before // Call decorators on all holders, and also on any EventListeners before
//decorators are called on any other classes (like servlets and filters) // decorators are called on any other classes (like servlets and filters)
for (int i=_decorators.size()-1;i>=0; i--) if(_servletHandler.getListeners() != null)
{ {
Decorator decorator = _decorators.get(i); for (ListenerHolder holder:_servletHandler.getListeners())
//Do any decorations on the ListenerHolders AND the listener instances first up {
if (_servletHandler.getListeners()!=null) _decorators.decorate(holder.getListener());
{
for (ListenerHolder holder:_servletHandler.getListeners())
{
decorator.decorate(holder.getListener());
}
} }
} }
} }
@ -649,20 +651,26 @@ public class ServletContextHandler extends ContextHandler
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* @return The decorator list used to resource inject new Filters, Servlets and EventListeners * @return The decorator list used to resource inject new Filters, Servlets and EventListeners
* @deprecated use getAttribute("org.eclipse.jetty.util.Decorators") instead
*/ */
@Deprecated
public List<Decorator> getDecorators() public List<Decorator> getDecorators()
{ {
return Collections.unmodifiableList(_decorators); List<Decorator> ret = new ArrayList<ServletContextHandler.Decorator>();
for (org.eclipse.jetty.util.Decorator decorator : _decorators)
{
ret.add(new LegacyDecorator(decorator));
}
return Collections.unmodifiableList(ret);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* @param decorators The lis of {@link Decorator}s * @param decorators The list of {@link Decorator}s
*/ */
public void setDecorators(List<Decorator> decorators) public void setDecorators(List<Decorator> decorators)
{ {
_decorators.clear(); _decorators.setDecorators(decorators);
_decorators.addAll(decorators);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -671,21 +679,19 @@ public class ServletContextHandler extends ContextHandler
*/ */
public void addDecorator(Decorator decorator) public void addDecorator(Decorator decorator)
{ {
_decorators.add(decorator); _decorators.addDecorator(decorator);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
void destroyServlet(Servlet servlet) void destroyServlet(Servlet servlet)
{ {
for (Decorator decorator : _decorators) _decorators.destroy(servlet);
decorator.destroy(servlet);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
void destroyFilter(Filter filter) void destroyFilter(Filter filter)
{ {
for (Decorator decorator : _decorators) _decorators.destroy(filter);
decorator.destroy(filter);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -1238,11 +1244,7 @@ public class ServletContextHandler extends ContextHandler
try try
{ {
T f = createInstance(c); T f = createInstance(c);
for (int i=_decorators.size()-1; i>=0; i--) f = _decorators.decorate(f);
{
Decorator decorator = _decorators.get(i);
f=decorator.decorate(f);
}
return f; return f;
} }
catch (Exception e) catch (Exception e)
@ -1258,11 +1260,7 @@ public class ServletContextHandler extends ContextHandler
try try
{ {
T s = createInstance(c); T s = createInstance(c);
for (int i=_decorators.size()-1; i>=0; i--) s = _decorators.decorate(s);
{
Decorator decorator = _decorators.get(i);
s=decorator.decorate(s);
}
return s; return s;
} }
catch (Exception e) catch (Exception e)
@ -1405,11 +1403,7 @@ public class ServletContextHandler extends ContextHandler
try try
{ {
T l = createInstance(clazz); T l = createInstance(clazz);
for (int i=_decorators.size()-1; i>=0; i--) l = _decorators.decorate(l);
{
Decorator decorator = _decorators.get(i);
l=decorator.decorate(l);
}
return l; return l;
} }
catch (Exception e) catch (Exception e)
@ -1450,11 +1444,36 @@ public class ServletContextHandler extends ContextHandler
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* Interface to decorate loaded classes. * Legacy Interface to decorate loaded classes.
* <p> * <p>
* Left for backwards compatibility with Weld / CDI * Left for backwards compatibility with Weld / CDI
*/ */
public interface Decorator extends org.eclipse.jetty.util.Decorator public interface Decorator extends org.eclipse.jetty.util.Decorator
{ {
} }
/**
* Implementation of the legacy interface to decorate loaded classes.
*/
private static class LegacyDecorator implements Decorator
{
private org.eclipse.jetty.util.Decorator decorator;
public LegacyDecorator(org.eclipse.jetty.util.Decorator decorator)
{
this.decorator = decorator;
}
@Override
public <T> T decorate(T o)
{
return decorator.decorate(o);
}
@Override
public void destroy(Object o)
{
decorator.destroy(o);
}
}
} }

View File

@ -20,38 +20,53 @@ package org.eclipse.jetty.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
* Represents a collection of {@link Decorator} instances that apply to a known * Represents a collection of {@link Decorator} instances that apply to a known
* state, such as a WebAppContext, WebSocketServerFactory, or WebSocketClient. * state, such as a WebAppContext, WebSocketServerFactory, or WebSocketClient.
* <p> * <p>
* Consistent single location for all Decorator behavior, with equal behavior in * Consistent single location for all Decorator behavior, with equal behavior in a ServletContext and also for a stand
* a ServletContext and also for a stand alone client. * alone client.
*/ */
public class Decorators public class Decorators implements Iterable<Decorator>
{ {
private List<Decorator> decorators = new ArrayList<>(); private List<Decorator> decorators = new ArrayList<>();
public List<Decorator> getDecorators()
{
return Collections.unmodifiableList(decorators);
}
public void setDecorators(List<Decorator> decorators)
{
this.decorators.clear();
if (decorators != null)
{
this.decorators.addAll(decorators);
}
}
public void addDecorator(Decorator decorator) public void addDecorator(Decorator decorator)
{ {
this.decorators.add(decorator); this.decorators.add(decorator);
} }
public void clear()
{
this.decorators.clear();
}
public <T> T createDecoratedInstance(Class<T> clazz) throws Exception
{
T o = clazz.newInstance();
return decorate(o);
}
public <T> T createInstance(Class<T> clazz) throws Exception
{
T o = clazz.newInstance();
return o;
}
public <T> T decorate(T obj)
{
T f = obj;
// Decorate is always backwards
for (int i = decorators.size() - 1; i >= 0; i--)
{
f = decorators.get(i).decorate(f);
}
return f;
}
public void destroy(Object obj) public void destroy(Object obj)
{ {
for (Decorator decorator : this.decorators) for (Decorator decorator : this.decorators)
@ -60,19 +75,23 @@ public class Decorators
} }
} }
public <T> T decorate(T obj) public List<Decorator> getDecorators()
{ {
T f = obj; return Collections.unmodifiableList(decorators);
for (Decorator decorator : this.decorators)
{
f = decorator.decorate(f);
}
return f;
} }
public <T> T createInstance(Class<T> clazz) throws Exception @Override
public Iterator<Decorator> iterator()
{ {
T o = clazz.newInstance(); return this.decorators.iterator();
return o; }
public void setDecorators(List<? extends Decorator> decorators)
{
this.decorators.clear();
if (decorators != null)
{
this.decorators.addAll(decorators);
}
} }
} }