Issue #39078 Duplicated programmatic listeners. (#3101)

Signed-off-by: Jan Bartel <janb@webtide.com>
This commit is contained in:
Jan Bartel 2018-11-13 14:29:12 +01:00 committed by GitHub
parent 6f34541bfd
commit 1845e6ea48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 1 deletions

View File

@ -1485,10 +1485,14 @@ public class ServletContextHandler extends ContextHandler
throw new IllegalStateException();
if (!_enabled)
throw new UnsupportedOperationException();
super.addListener(t);
checkListener(t.getClass());
ListenerHolder holder = getServletHandler().newListenerHolder(Source.JAVAX_API);
holder.setListener(t);
getServletHandler().addListener(holder);
addProgrammaticListener(t);
}
@Override

View File

@ -24,15 +24,20 @@ import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.Servlet;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
@ -60,6 +65,7 @@ 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.eclipse.jetty.util.component.AbstractLifeCycle;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@ -72,6 +78,66 @@ public class ServletContextHandlerTest
private static final AtomicInteger __testServlets = new AtomicInteger();
public static class MySCI implements ServletContainerInitializer
{
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException
{
//add a programmatic listener
if (ctx.getAttribute("MySCI.startup") != null)
throw new IllegalStateException("MySCI already called");
ctx.setAttribute("MySCI.startup", Boolean.TRUE);
ctx.addListener(new MyContextListener());
}
}
public static class MySCIStarter extends AbstractLifeCycle implements ServletContextHandler.ServletContainerInitializerCaller
{
MySCI _sci = new MySCI();
ContextHandler.Context _ctx;
MySCIStarter (ContextHandler.Context ctx)
{
_ctx = ctx;
}
@Override
protected void doStart() throws Exception
{
super.doStart();
//call the SCI
try
{
_ctx.setExtendedListenerTypes(true);
_sci.onStartup(Collections.emptySet(), _ctx);
}
finally
{
}
}
}
public static class MyContextListener implements ServletContextListener
{
@Override
public void contextInitialized(ServletContextEvent sce)
{
assertNull(sce.getServletContext().getAttribute("MyContextListener.contextInitialized"));
sce.getServletContext().setAttribute("MyContextListener.contextInitialized", Boolean.TRUE);
}
@Override
public void contextDestroyed(ServletContextEvent sce)
{
assertNull(sce.getServletContext().getAttribute("MyContextListener.contextDestroyed"));
sce.getServletContext().setAttribute("MyContextListener.contextDestroyed", Boolean.TRUE);
}
}
public static class MySessionHandler extends SessionHandler
{
public void checkSessionListeners (int size)
@ -158,6 +224,20 @@ public class ServletContextHandlerTest
sessions.checkSessionListeners(1);
}
@Test
public void testListenerFromSCI() throws Exception
{
ContextHandlerCollection contexts = new ContextHandlerCollection();
_server.setHandler(contexts);
ServletContextHandler root = new ServletContextHandler(contexts,"/");
root.addBean(new MySCIStarter(root.getServletContext()), true);
_server.start();
assertTrue((Boolean)root.getServletContext().getAttribute("MySCI.startup"));
assertTrue((Boolean)root.getServletContext().getAttribute("MyContextListener.contextInitialized"));
}
@Test
public void testFindContainer() throws Exception
{

View File

@ -63,6 +63,9 @@ public class FooInitializer implements ServletContainerInitializer
@Override
public void contextInitialized(ServletContextEvent sce)
{
if (sce.getServletContext().getAttribute("com.acme.AnnotationTest.listenerTest") != null)
throw new IllegalStateException("FooListener already initialized");
//Can add a ServletContextListener from a ServletContainerInitializer
sce.getServletContext().setAttribute("com.acme.AnnotationTest.listenerTest", Boolean.TRUE);
@ -95,6 +98,9 @@ public class FooInitializer implements ServletContainerInitializer
@Override
public void onStartup(Set<Class<?>> classes, ServletContext context)
{
if (context.getAttribute("com.acme.Foo") != null)
throw new IllegalStateException ("FooInitializer on Startup already called");
context.setAttribute("com.acme.Foo", new ArrayList<Class>(classes));
ServletRegistration.Dynamic reg = context.addServlet("AnnotationTest", "com.acme.AnnotationTest");
context.setAttribute("com.acme.AnnotationTest.complete", (reg == null));

View File

@ -76,6 +76,9 @@ public class AnnotatedListener implements HttpSessionListener, HttpSessionAttri
@Override
public void contextInitialized(ServletContextEvent sce)
{
if (sce.getServletContext().getAttribute("com.acme.AnnotationTest.sclInjectWebListenerTest") != null)
throw new IllegalStateException("AnnotatedListener already initialized");
sce.getServletContext().setAttribute("com.acme.AnnotationTest.sclInjectWebListenerTest", Boolean.valueOf(maxAmount!=null));
}

View File

@ -117,6 +117,9 @@ public class TestListener implements HttpSessionListener, HttpSessionAttributeL
@Override
public void contextInitialized(ServletContextEvent sce)
{
if (sce.getServletContext().getAttribute("com.acme.AnnotationTest.sclInjectTest") != null)
throw new IllegalStateException("TestListener already initialized");
sce.getServletContext().setAttribute("com.acme.AnnotationTest.sclInjectTest", Boolean.valueOf(maxAmount != null));
//Can't add a ServletContextListener from a ServletContextListener even if it is declared in web.xml