Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'jetty-servlet')
-rw-r--r--jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java41
-rw-r--r--jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java124
2 files changed, 146 insertions, 19 deletions
diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java
index 29cbed496c..42f4e548a9 100644
--- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java
+++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java
@@ -88,7 +88,7 @@ public class ServletContextHandler extends ContextHandler
public interface ServletContainerInitializerCaller extends LifeCycle {};
- protected final DecoratedObjectFactory _instantiator= new DecoratedObjectFactory();
+ protected final DecoratedObjectFactory _objFactory = new DecoratedObjectFactory();
protected Class<? extends SecurityHandler> _defaultSecurityHandlerClass=org.eclipse.jetty.security.ConstraintSecurityHandler.class;
protected SessionHandler _sessionHandler;
protected SecurityHandler _securityHandler;
@@ -252,7 +252,7 @@ public class ServletContextHandler extends ContextHandler
@Override
protected void doStart() throws Exception
{
- setAttribute(DecoratedObjectFactory.ATTR, _instantiator);
+ getServletContext().setAttribute(DecoratedObjectFactory.ATTR, _objFactory);
super.doStart();
}
@@ -264,7 +264,7 @@ public class ServletContextHandler extends ContextHandler
protected void doStop() throws Exception
{
super.doStop();
- _instantiator.clear();
+ _objFactory.clear();
}
/* ------------------------------------------------------------ */
@@ -331,7 +331,7 @@ public class ServletContextHandler extends ContextHandler
{
for (ListenerHolder holder:_servletHandler.getListeners())
{
- _instantiator.decorate(holder.getListener());
+ _objFactory.decorate(holder.getListener());
}
}
}
@@ -647,17 +647,28 @@ public class ServletContextHandler extends ContextHandler
h.setHandler(handler);
relinkHandlers();
}
+
+ /* ------------------------------------------------------------ */
+ /**
+ * The DecoratedObjectFactory for use by IoC containers (weld / spring / etc)
+ *
+ * @return The DecoratedObjectFactory
+ */
+ public DecoratedObjectFactory getObjectFactory()
+ {
+ return _objFactory;
+ }
/* ------------------------------------------------------------ */
/**
* @return The decorator list used to resource inject new Filters, Servlets and EventListeners
- * @deprecated use getAttribute("org.eclipse.jetty.util.DecoratedObjectFactory") instead
+ * @deprecated use the {@link DecoratedObjectFactory} from getAttribute("org.eclipse.jetty.util.DecoratedObjectFactory") or {@link #getObjectFactory()} instead
*/
@Deprecated
public List<Decorator> getDecorators()
{
List<Decorator> ret = new ArrayList<ServletContextHandler.Decorator>();
- for (org.eclipse.jetty.util.Decorator decorator : _instantiator)
+ for (org.eclipse.jetty.util.Decorator decorator : _objFactory)
{
ret.add(new LegacyDecorator(decorator));
}
@@ -667,31 +678,35 @@ public class ServletContextHandler extends ContextHandler
/* ------------------------------------------------------------ */
/**
* @param decorators The list of {@link Decorator}s
+ * @deprecated use the {@link DecoratedObjectFactory} from getAttribute("org.eclipse.jetty.util.DecoratedObjectFactory") or {@link #getObjectFactory()} instead
*/
+ @Deprecated
public void setDecorators(List<Decorator> decorators)
{
- _instantiator.setDecorators(decorators);
+ _objFactory.setDecorators(decorators);
}
/* ------------------------------------------------------------ */
/**
* @param decorator The decorator to add
+ * @deprecated use the {@link DecoratedObjectFactory} from getAttribute("org.eclipse.jetty.util.DecoratedObjectFactory") or {@link #getObjectFactory()} instead
*/
+ @Deprecated
public void addDecorator(Decorator decorator)
{
- _instantiator.addDecorator(decorator);
+ _objFactory.addDecorator(decorator);
}
/* ------------------------------------------------------------ */
void destroyServlet(Servlet servlet)
{
- _instantiator.destroy(servlet);
+ _objFactory.destroy(servlet);
}
/* ------------------------------------------------------------ */
void destroyFilter(Filter filter)
{
- _instantiator.destroy(filter);
+ _objFactory.destroy(filter);
}
/* ------------------------------------------------------------ */
@@ -1244,7 +1259,7 @@ public class ServletContextHandler extends ContextHandler
try
{
T f = createInstance(c);
- f = _instantiator.decorate(f);
+ f = _objFactory.decorate(f);
return f;
}
catch (Exception e)
@@ -1260,7 +1275,7 @@ public class ServletContextHandler extends ContextHandler
try
{
T s = createInstance(c);
- s = _instantiator.decorate(s);
+ s = _objFactory.decorate(s);
return s;
}
catch (Exception e)
@@ -1403,7 +1418,7 @@ public class ServletContextHandler extends ContextHandler
try
{
T l = createInstance(clazz);
- l = _instantiator.decorate(l);
+ l = _objFactory.decorate(l);
return l;
}
catch (Exception e)
diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java
index 729e88e30d..3bc61e67f0 100644
--- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java
+++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java
@@ -18,15 +18,12 @@
package org.eclipse.jetty.servlet;
-import static org.hamcrest.Matchers.containsString;
-import static org.hamcrest.Matchers.notNullValue;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.*;
import java.io.IOException;
import java.io.PrintWriter;
+import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
@@ -52,6 +49,8 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.server.session.SessionHandler;
+import org.eclipse.jetty.util.DecoratedObjectFactory;
+import org.eclipse.jetty.util.Decorator;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
@@ -361,6 +360,59 @@ public class ServletContextHandlerTest
Assert.assertThat(response, Matchers.containsString("404 Fell Through"));
}
+
+ /**
+ * Test behavior of legacy {@link ServletContextHandler.Decorator}, with
+ * new DecoratedObjectFactory class
+ */
+ @SuppressWarnings("deprecation")
+ @Test
+ public void testLegacyDecorator() throws Exception
+ {
+ ServletContextHandler context = new ServletContextHandler();
+ context.addDecorator(new DummyLegacyDecorator());
+ _server.setHandler(context);
+
+ context.addServlet(DecoratedObjectFactoryServlet.class, "/objfactory/*");
+ _server.start();
+
+ String response= _connector.getResponses("GET /objfactory/ HTTP/1.0\r\n\r\n");
+ assertThat("Response status code", response, containsString("200 OK"));
+
+ String expected = String.format("Attribute[%s] = %s", DecoratedObjectFactory.ATTR, DecoratedObjectFactory.class.getName());
+ assertThat("Has context attribute", response, containsString(expected));
+
+ assertThat("Decorators size", response, containsString("Decorators.size = [1]"));
+
+ expected = String.format("decorator[] = %s", DummyLegacyDecorator.class.getName());
+ assertThat("Specific Legacy Decorator", response, containsString(expected));
+ }
+
+ /**
+ * Test behavior of new {@link org.eclipse.jetty.util.Decorator}, with
+ * new DecoratedObjectFactory class
+ */
+ @Test
+ public void testUtilDecorator() throws Exception
+ {
+ ServletContextHandler context = new ServletContextHandler();
+ context.getObjectFactory().addDecorator(new DummyUtilDecorator());
+ _server.setHandler(context);
+
+ context.addServlet(DecoratedObjectFactoryServlet.class, "/objfactory/*");
+ _server.start();
+
+ String response= _connector.getResponses("GET /objfactory/ HTTP/1.0\r\n\r\n");
+ assertThat("Response status code", response, containsString("200 OK"));
+
+ String expected = String.format("Attribute[%s] = %s", DecoratedObjectFactory.ATTR, DecoratedObjectFactory.class.getName());
+ assertThat("Has context attribute", response, containsString(expected));
+
+ assertThat("Decorators size", response, containsString("Decorators.size = [1]"));
+
+ expected = String.format("decorator[] = %s", DummyUtilDecorator.class.getName());
+ assertThat("Specific Legacy Decorator", response, containsString(expected));
+ }
private int assertResponseContains(String expected, String response)
{
@@ -391,6 +443,66 @@ public class ServletContextHandlerTest
writer.write("Hello World");
}
}
+
+ public static class DummyUtilDecorator implements org.eclipse.jetty.util.Decorator
+ {
+ @Override
+ public <T> T decorate(T o)
+ {
+ return o;
+ }
+
+ @Override
+ public void destroy(Object o)
+ {
+ }
+ }
+
+ public static class DummyLegacyDecorator implements org.eclipse.jetty.servlet.ServletContextHandler.Decorator
+ {
+ @Override
+ public <T> T decorate(T o)
+ {
+ return o;
+ }
+
+ @Override
+ public void destroy(Object o)
+ {
+ }
+ }
+
+ public static class DecoratedObjectFactoryServlet extends HttpServlet
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
+ resp.setContentType("text/plain");
+ resp.setStatus(HttpServletResponse.SC_OK);
+ PrintWriter out = resp.getWriter();
+
+ Object obj = req.getServletContext().getAttribute(DecoratedObjectFactory.ATTR);
+ out.printf("Attribute[%s] = %s%n",DecoratedObjectFactory.ATTR,obj.getClass().getName());
+
+ if (obj instanceof DecoratedObjectFactory)
+ {
+ out.printf("Object is a DecoratedObjectFactory%n");
+ DecoratedObjectFactory objFactory = (DecoratedObjectFactory)obj;
+ List<Decorator> decorators = objFactory.getDecorators();
+ out.printf("Decorators.size = [%d]%n",decorators.size());
+ for (Decorator decorator : decorators)
+ {
+ out.printf(" decorator[] = %s%n",decorator.getClass().getName());
+ }
+ }
+ else
+ {
+ out.printf("Object is NOT a DecoratedObjectFactory%n");
+ }
+ }
+ }
public static class TestServlet extends HttpServlet
{

Back to the top