Skip to content

Commit

Permalink
Issue #5832 - deprecate and remove usage of the ContainerInitializer …
Browse files Browse the repository at this point in the history
…utility

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Jan 12, 2021
1 parent 774dac6 commit 9e19e87
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 16 deletions.
Expand Up @@ -696,6 +696,27 @@ else if (handler instanceof ServletHandler)
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)
*
Expand Down
Expand Up @@ -25,7 +25,9 @@
/**
* 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
{
/**
Expand Down
Expand Up @@ -15,6 +15,7 @@

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;
Expand All @@ -28,7 +29,6 @@

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;
Expand All @@ -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).
Expand Down Expand Up @@ -102,7 +104,7 @@ public static void configure(ServletContextHandler context, Configurator configu
// 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);
Expand Down Expand Up @@ -270,6 +272,9 @@ public void onStartup(Set<Class<?>> c, ServletContext context) throws ServletExc
}
}
}

if (afterStartupConsumer != null)
afterStartupConsumer.accept(context);
}

@SuppressWarnings("unchecked")
Expand All @@ -296,4 +301,17 @@ private void filterClasses(Set<Class<?>> c, Set<Class<? extends Endpoint>> disco
}
}
}

/**
* 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;
}
}
Expand Up @@ -30,7 +30,6 @@
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;
Expand Down Expand Up @@ -73,8 +72,7 @@ public void before() throws Exception
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());
Expand Down
Expand Up @@ -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;
Expand All @@ -32,6 +32,7 @@
public class JettyWebSocketServletContainerInitializer implements ServletContainerInitializer
{
private static final Logger LOG = LoggerFactory.getLogger(JettyWebSocketServletContainerInitializer.class);
private Consumer<ServletContext> afterStartupConsumer;

public interface Configurator
{
Expand All @@ -51,17 +52,15 @@ public static void configure(ServletContextHandler context, Configurator configu
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);
}
}));
}

/**
Expand Down Expand Up @@ -101,5 +100,21 @@ public void onStartup(Set<Class<?>> c, ServletContext context)
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;
}
}

0 comments on commit 9e19e87

Please sign in to comment.