Skip to content

Commit

Permalink
Making ServletContextHandler use util.Decorators
Browse files Browse the repository at this point in the history
+ Deprecating methods that are just awkward now.
   ServletContextHandler.getDecorators() - as it expects the
     ServletContextHandler.Decorator version
  • Loading branch information
joakime committed Mar 12, 2015
1 parent 2b7b5ef commit fb88bc4
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 61 deletions.
Expand Up @@ -60,6 +60,7 @@
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -248,6 +249,13 @@ private void relinkHandlers()

}

@Override
protected void doStart() throws Exception
{
setAttribute(Decorators.class.getName(), _decorators);
super.doStart();
}

/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.handler.ContextHandler#doStop()
Expand All @@ -256,8 +264,7 @@ private void relinkHandlers()
protected void doStop() throws Exception
{
super.doStop();
if (_decorators != null)
_decorators.clear();
_decorators.clear();
}

/* ------------------------------------------------------------ */
Expand Down Expand Up @@ -318,18 +325,13 @@ protected void startContext() throws Exception

if (_servletHandler != null)
{
//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--)
// Call decorators on all holders, and also on any EventListeners before
// decorators are called on any other classes (like servlets and filters)
if(_servletHandler.getListeners() != null)
{
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());
}
for (ListenerHolder holder:_servletHandler.getListeners())
{
_decorators.decorate(holder.getListener());
}
}
}
Expand Down Expand Up @@ -649,20 +651,26 @@ public void insertHandler(HandlerWrapper handler)
/* ------------------------------------------------------------ */
/**
* @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);
}

/* ------------------------------------------------------------ */
Expand All @@ -671,21 +679,19 @@ public void setDecorators(List<Decorator> decorators)
*/
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);
}

/* ------------------------------------------------------------ */
Expand Down Expand Up @@ -1238,11 +1244,7 @@ public <T extends Filter> T createFilter(Class<T> c) throws ServletException
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)
Expand All @@ -1258,11 +1260,7 @@ public <T extends Servlet> T createServlet(Class<T> c) throws ServletException
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)
Expand Down Expand Up @@ -1405,11 +1403,7 @@ public <T extends EventListener> T createListener(Class<T> clazz) throws Servlet
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)
Expand Down Expand Up @@ -1450,11 +1444,36 @@ public void declareRoles(String... roleNames)

/* ------------------------------------------------------------ */
/**
* 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);
}
}
}
63 changes: 41 additions & 22 deletions jetty-util/src/main/java/org/eclipse/jetty/util/Decorators.java
Expand Up @@ -20,59 +20,78 @@

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()
public void addDecorator(Decorator decorator)
{
return Collections.unmodifiableList(decorators);
this.decorators.add(decorator);
}

public void setDecorators(List<Decorator> decorators)
public void clear()
{
this.decorators.clear();
if (decorators != null)
{
this.decorators.addAll(decorators);
}
}

public void addDecorator(Decorator decorator)
public <T> T createDecoratedInstance(Class<T> clazz) throws Exception
{
this.decorators.add(decorator);
T o = clazz.newInstance();
return decorate(o);
}

public void destroy(Object obj)
public <T> T createInstance(Class<T> clazz) throws Exception
{
for (Decorator decorator : this.decorators)
{
decorator.destroy(obj);
}
T o = clazz.newInstance();
return o;
}

public <T> T decorate(T obj)
{
T f = obj;
for (Decorator decorator : this.decorators)
// Decorate is always backwards
for (int i = decorators.size() - 1; i >= 0; i--)
{
f = decorator.decorate(f);
f = decorators.get(i).decorate(f);
}
return f;
}

public <T> T createInstance(Class<T> clazz) throws Exception
public void destroy(Object obj)
{
T o = clazz.newInstance();
return o;
for (Decorator decorator : this.decorators)
{
decorator.destroy(obj);
}
}

public List<Decorator> getDecorators()
{
return Collections.unmodifiableList(decorators);
}

@Override
public Iterator<Decorator> iterator()
{
return this.decorators.iterator();
}

public void setDecorators(List<? extends Decorator> decorators)
{
this.decorators.clear();
if (decorators != null)
{
this.decorators.addAll(decorators);
}
}
}

0 comments on commit fb88bc4

Please sign in to comment.