Issue #5832 - deprecate and remove usage of the ContainerInitializer utility

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2021-01-12 17:08:53 +11:00
parent 774dac66a2
commit 9e19e875f5
5 changed files with 70 additions and 16 deletions

View File

@ -696,6 +696,27 @@ public class ServletContextHandler extends ContextHandler
relinkHandlers();
}
/**
* Utility Method to allow for manual execution of {@link javax.servlet.ServletContainerInitializer} when using Embedded Jetty.
* @param containerInitializer the ServletContainerInitializer to register.
* @see Initializer
*/
public void addServletContainerInitializer(ServletContainerInitializer containerInitializer)
{
addManaged(new Initializer(this, containerInitializer));
}
/**
* Utility Method to allow for manual execution of {@link javax.servlet.ServletContainerInitializer} when using Embedded Jetty.
* @param containerInitializer the ServletContainerInitializer to register.
* @param classes the Set of application classes.
* @see Initializer
*/
public void addServletContainerInitializer(ServletContainerInitializer containerInitializer, Set<Class<?>> classes)
{
addManaged(new Initializer(this, containerInitializer, classes));
}
/**
* The DecoratedObjectFactory for use by IoC containers (weld / spring / etc)
*

View File

@ -25,7 +25,9 @@ import javax.servlet.ServletContextListener;
/**
* Utility Methods for manual execution of {@link javax.servlet.ServletContainerInitializer} when
* using Embedded Jetty.
* @deprecated use {@link org.eclipse.jetty.servlet.ServletContextHandler#addServletContainerInitializer(ServletContainerInitializer)}.
*/
@Deprecated
public final class ContainerInitializer
{
/**

View File

@ -15,6 +15,7 @@ package org.eclipse.jetty.websocket.javax.server.config;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
@ -28,7 +29,6 @@ import javax.websocket.server.ServerEndpointConfig;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.listener.ContainerInitializer;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.thread.ThreadClassLoaderScope;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
@ -52,6 +52,8 @@ public class JavaxWebSocketServletContainerInitializer implements ServletContain
public static final String HTTPCLIENT_ATTRIBUTE = "org.eclipse.jetty.websocket.javax.HttpClient";
private static final Logger LOG = LoggerFactory.getLogger(JavaxWebSocketServletContainerInitializer.class);
private Consumer<ServletContext> afterStartupConsumer;
/**
* Test a ServletContext for {@code init-param} or {@code attribute} at {@code keyName} for
* true or false setting that determines if the specified feature is enabled (or not).
@ -102,7 +104,7 @@ public class JavaxWebSocketServletContainerInitializer implements ServletContain
// the initialization phase is over. (important for this SCI to function)
context.getServletContext().setExtendedListenerTypes(true);
context.addEventListener(ContainerInitializer.asContextListener(new JavaxWebSocketServletContainerInitializer())
context.addServletContainerInitializer(new JavaxWebSocketServletContainerInitializer()
.afterStartup((servletContext) ->
{
JavaxWebSocketServerContainer serverContainer = JavaxWebSocketServerContainer.getContainer(servletContext);
@ -270,6 +272,9 @@ public class JavaxWebSocketServletContainerInitializer implements ServletContain
}
}
}
if (afterStartupConsumer != null)
afterStartupConsumer.accept(context);
}
@SuppressWarnings("unchecked")
@ -296,4 +301,17 @@ public class JavaxWebSocketServletContainerInitializer implements ServletContain
}
}
}
/**
* Add a optional consumer to execute once the {@link ServletContainerInitializer#onStartup(Set, ServletContext)} has
* been called successfully.
*
* @param consumer the consumer to execute after the SCI has executed
* @return this configured {@link JavaxWebSocketServletContainerInitializer} instance.
*/
public JavaxWebSocketServletContainerInitializer afterStartup(Consumer<ServletContext> consumer)
{
this.afterStartupConsumer = consumer;
return this;
}
}

View File

@ -30,7 +30,6 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.listener.ContainerInitializer;
import org.eclipse.jetty.websocket.javax.client.JavaxWebSocketClientShutdown;
import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
import org.junit.jupiter.api.AfterEach;
@ -73,8 +72,7 @@ public class ClientInWebappTest
server.setHandler(contextHandler);
// Because we are using embedded we must manually add the Javax WS Client Shutdown SCI.
// TODO: fix to not use ContainerInitializer.asContextListener
contextHandler.addEventListener(ContainerInitializer.asContextListener(new JavaxWebSocketClientShutdown()));
contextHandler.addServletContainerInitializer(new JavaxWebSocketClientShutdown());
server.start();
serverUri = WSURI.toWebsocket(server.getURI());

View File

@ -14,11 +14,11 @@
package org.eclipse.jetty.websocket.server.config;
import java.util.Set;
import java.util.function.Consumer;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.listener.ContainerInitializer;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
import org.eclipse.jetty.websocket.core.server.WebSocketMappings;
import org.eclipse.jetty.websocket.core.server.WebSocketServerComponents;
@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory;
public class JettyWebSocketServletContainerInitializer implements ServletContainerInitializer
{
private static final Logger LOG = LoggerFactory.getLogger(JettyWebSocketServletContainerInitializer.class);
private Consumer<ServletContext> afterStartupConsumer;
public interface Configurator
{
@ -51,17 +52,15 @@ public class JettyWebSocketServletContainerInitializer implements ServletContain
if (!context.isStopped())
throw new IllegalStateException("configure should be called before starting");
context.addEventListener(
ContainerInitializer
.asContextListener(new JettyWebSocketServletContainerInitializer())
.afterStartup((servletContext) ->
context.addServletContainerInitializer(new JettyWebSocketServletContainerInitializer()
.afterStartup((servletContext) ->
{
if (configurator != null)
{
if (configurator != null)
{
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
configurator.accept(servletContext, container);
}
}));
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
configurator.accept(servletContext, container);
}
}));
}
/**
@ -101,5 +100,21 @@ public class JettyWebSocketServletContainerInitializer implements ServletContain
JettyWebSocketServerContainer container = JettyWebSocketServletContainerInitializer.initialize(contextHandler);
if (LOG.isDebugEnabled())
LOG.debug("onStartup {}", container);
if (afterStartupConsumer != null)
afterStartupConsumer.accept(context);
}
/**
* Add a optional consumer to execute once the {@link ServletContainerInitializer#onStartup(Set, ServletContext)} has
* been called successfully.
*
* @param consumer the consumer to execute after the SCI has executed
* @return this configured {@link JettyWebSocketServletContainerInitializer} instance.
*/
public JettyWebSocketServletContainerInitializer afterStartup(Consumer<ServletContext> consumer)
{
this.afterStartupConsumer = consumer;
return this;
}
}