From c869aad1450fa2f597f2ab81b123006e3cccad6c Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Thu, 30 May 2013 15:16:43 +1000 Subject: [PATCH] 409436 NPE on context restart using dynamic servlet registration --- .../annotations/AnnotationConfiguration.java | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationConfiguration.java b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationConfiguration.java index 85c366073f0..2d46715c4a2 100644 --- a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationConfiguration.java +++ b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationConfiguration.java @@ -23,24 +23,17 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ServiceLoader; -import java.util.Set; import javax.servlet.ServletContainerInitializer; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; import javax.servlet.annotation.HandlesTypes; - import org.eclipse.jetty.annotations.AnnotationParser.DiscoverableAnnotationHandler; import org.eclipse.jetty.plus.annotation.ContainerInitializer; import org.eclipse.jetty.util.MultiMap; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.resource.Resource; -import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.AbstractConfiguration; -import org.eclipse.jetty.webapp.Descriptor; -import org.eclipse.jetty.webapp.DiscoveredAnnotation; import org.eclipse.jetty.webapp.FragmentDescriptor; import org.eclipse.jetty.webapp.MetaDataComplete; import org.eclipse.jetty.webapp.WebAppContext; @@ -56,18 +49,31 @@ public class AnnotationConfiguration extends AbstractConfiguration private static final Logger LOG = Log.getLogger(AnnotationConfiguration.class); public static final String CLASS_INHERITANCE_MAP = "org.eclipse.jetty.classInheritanceMap"; public static final String CONTAINER_INITIALIZERS = "org.eclipse.jetty.containerInitializers"; + public static final String CONTAINER_INITIALIZER_LISTENER = "org.eclipse.jetty.containerInitializerListener"; protected List _discoverableAnnotationHandlers = new ArrayList(); protected ClassInheritanceHandler _classInheritanceHandler; protected List _containerInitializerAnnotationHandlers = new ArrayList(); + - + @Override public void preConfigure(final WebAppContext context) throws Exception { } - + @Override + public void deconfigure(WebAppContext context) throws Exception + { + context.removeAttribute(CLASS_INHERITANCE_MAP); + context.removeAttribute(CONTAINER_INITIALIZERS); + ServletContainerInitializerListener listener = (ServletContainerInitializerListener)context.getAttribute(CONTAINER_INITIALIZER_LISTENER); + if (listener != null) + { + context.removeBean(listener); + context.removeAttribute(CONTAINER_INITIALIZER_LISTENER); + } + } /** * @see org.eclipse.jetty.webapp.AbstractConfiguration#configure(org.eclipse.jetty.webapp.WebAppContext) @@ -225,8 +231,12 @@ public class AnnotationConfiguration extends AbstractConfiguration //add a bean which will call the servletcontainerinitializers when appropriate - ServletContainerInitializerListener listener = new ServletContainerInitializerListener(); + ServletContainerInitializerListener listener = (ServletContainerInitializerListener)context.getAttribute(CONTAINER_INITIALIZER_LISTENER); + if (listener != null) + throw new IllegalStateException("ServletContainerInitializerListener already exists"); + listener = new ServletContainerInitializerListener(); listener.setWebAppContext(context); + context.setAttribute(CONTAINER_INITIALIZER_LISTENER, listener); context.addBean(listener, true); }