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.session.SessionHandler;
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.ManagedObject;
import org.eclipse.jetty.util.component.LifeCycle;
@ -87,7 +88,7 @@ public class ServletContextHandler extends ContextHandler
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 SessionHandler _sessionHandler;
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()
@ -256,7 +264,6 @@ public class ServletContextHandler extends ContextHandler
protected void doStop() throws Exception
{
super.doStop();
if (_decorators != null)
_decorators.clear();
}
@ -320,16 +327,11 @@ public class ServletContextHandler extends ContextHandler
{
// Call decorators on all holders, and also on any EventListeners before
// decorators are called on any other classes (like servlets and filters)
for (int i=_decorators.size()-1;i>=0; i--)
{
Decorator decorator = _decorators.get(i);
//Do any decorations on the ListenerHolders AND the listener instances first up
if(_servletHandler.getListeners() != null)
{
for (ListenerHolder holder:_servletHandler.getListeners())
{
decorator.decorate(holder.getListener());
}
_decorators.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
* @deprecated use getAttribute("org.eclipse.jetty.util.Decorators") instead
*/
@Deprecated
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)
{
_decorators.clear();
_decorators.addAll(decorators);
_decorators.setDecorators(decorators);
}
/* ------------------------------------------------------------ */
@ -671,21 +679,19 @@ public class ServletContextHandler extends ContextHandler
*/
public void addDecorator(Decorator decorator)
{
_decorators.add(decorator);
_decorators.addDecorator(decorator);
}
/* ------------------------------------------------------------ */
void destroyServlet(Servlet servlet)
{
for (Decorator decorator : _decorators)
decorator.destroy(servlet);
_decorators.destroy(servlet);
}
/* ------------------------------------------------------------ */
void destroyFilter(Filter filter)
{
for (Decorator decorator : _decorators)
decorator.destroy(filter);
_decorators.destroy(filter);
}
/* ------------------------------------------------------------ */
@ -1238,11 +1244,7 @@ public class ServletContextHandler extends ContextHandler
try
{
T f = createInstance(c);
for (int i=_decorators.size()-1; i>=0; i--)
{
Decorator decorator = _decorators.get(i);
f=decorator.decorate(f);
}
f = _decorators.decorate(f);
return f;
}
catch (Exception e)
@ -1258,11 +1260,7 @@ public class ServletContextHandler extends ContextHandler
try
{
T s = createInstance(c);
for (int i=_decorators.size()-1; i>=0; i--)
{
Decorator decorator = _decorators.get(i);
s=decorator.decorate(s);
}
s = _decorators.decorate(s);
return s;
}
catch (Exception e)
@ -1405,11 +1403,7 @@ public class ServletContextHandler extends ContextHandler
try
{
T l = createInstance(clazz);
for (int i=_decorators.size()-1; i>=0; i--)
{
Decorator decorator = _decorators.get(i);
l=decorator.decorate(l);
}
l = _decorators.decorate(l);
return l;
}
catch (Exception e)
@ -1450,11 +1444,36 @@ public class ServletContextHandler extends ContextHandler
/* ------------------------------------------------------------ */
/**
* Interface to decorate loaded classes.
* Legacy Interface to decorate loaded classes.
* <p>
* Left for backwards compatibility with Weld / CDI
*/
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.Collections;
import java.util.Iterator;
import java.util.List;
/**
* Represents a collection of {@link Decorator} instances that apply to a known
* state, such as a WebAppContext, WebSocketServerFactory, or WebSocketClient.
* <p>
* Consistent single location for all Decorator behavior, with equal behavior in
* a ServletContext and also for a stand alone client.
* Consistent single location for all Decorator behavior, with equal behavior in a ServletContext and also for a stand
* alone client.
*/
public class Decorators
public class Decorators implements Iterable<Decorator>
{
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)
{
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)
{
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;
for (Decorator decorator : this.decorators)
{
f = decorator.decorate(f);
}
return f;
return Collections.unmodifiableList(decorators);
}
public <T> T createInstance(Class<T> clazz) throws Exception
@Override
public Iterator<Decorator> iterator()
{
T o = clazz.newInstance();
return o;
return this.decorators.iterator();
}
public void setDecorators(List<? extends Decorator> decorators)
{
this.decorators.clear();
if (decorators != null)
{
this.decorators.addAll(decorators);
}
}
}