Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Root extends ContainerLifeCycle
public Root()
{
// The Monitor life cycle is managed by Root.
addManaged(monitor);
addBeanAndEnsure(this, monitor);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public Destination resolveDestination(Origin origin)
{
HttpDestination newDestination = (HttpDestination)getHttpClientTransport().newDestination(k);
// Start the destination before it's published to other threads.
addManaged(newDestination);
addBeanAndEnsure(this, newDestination);
if (destinationSweeper != null)
destinationSweeper.offer(newDestination);
if (LOG.isDebugEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.eclipse.jetty.toolchain.test.Net;
import org.eclipse.jetty.util.Pool;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.ExecutorThreadPool;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
Expand Down Expand Up @@ -1370,7 +1371,7 @@ public void onClosed(Connection connection)
latch.countDown();
}
};
connector.addManaged(serverStats);
ContainerLifeCycle.addBeanAndEnsure(connector, serverStats);

SslContextFactory.Client clientTLSFactory = createClientSslContextFactory();
startClient(clientTLSFactory);
Expand All @@ -1383,7 +1384,7 @@ public void onClosed(Connection connection)
latch.countDown();
}
};
client.addManaged(clientStats);
ContainerLifeCycle.addBeanAndEnsure(client, clientStats);

ContentResponse response = client.newRequest("https://localhost:" + connector.getLocalPort())
.headers(httpFields -> httpFields.put(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.jetty.quic.util.ErrorCode;
import org.eclipse.jetty.util.Promise;
import org.eclipse.jetty.util.component.Container;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -114,7 +115,7 @@ public void onOpen(Session session)
{
ClientQuicheSession qSession = (ClientQuicheSession)session;
ProtocolSession pSession = newProtocolSession(qSession);
qSession.addManaged(pSession);
ContainerLifeCycle.addBeanAndEnsure(qSession, pSession);
protocolSession.set(pSession);
context.put(ClientConnector.APPLICATION_PROTOCOL_CONTEXT_KEY, qSession.getNegotiatedProtocol());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void onOpen(Session session)
{
ServerQuicheSession qSession = (ServerQuicheSession)session;
ProtocolSession pSession = newProtocolSession(qSession);
qSession.addManaged(pSession);
addBeanAndEnsure(qSession, pSession);
protocolSession.set(pSession);
}
catch (Throwable x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@ public void addAliasCheck(AliasCheck check)
{
_aliasChecks.add(check);
if (check instanceof LifeCycle)
addManaged((LifeCycle)check);
addBeanAndEnsure(this, (LifeCycle)check);
else
addBean(check);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void setHandler(Handler handler)
if (handler != null)
{
handler.setServer(server);
addManaged(handler);
addBeanAndEnsure(this, handler);
}
_handler = handler;
if (oldHandler != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,30 @@ private boolean installBean(Object o, Managed managed)
* wrapped as RuntimeExceptions
*
* @param lifecycle the managed lifecycle to add
* @deprecated use {@link #addBeanAndEnsure(ContainerLifeCycle, LifeCycle)} instead.
*/
@Deprecated(forRemoval = true, since = "jetty-12.1.3")
public void addManaged(LifeCycle lifecycle)
{
addBean(lifecycle, true);
addBeanAndEnsure(this, lifecycle);
}

/**
* Adds a managed lifecycle.
* <p>This is a convenience method that uses addBean(lifecycle,true)
* and then ensures that the added bean is started iff this container
* is running. Exception from nested calls to start are caught and
* wrapped as RuntimeExceptions
*
* @param lifecycle the managed lifecycle to add
*/
public static void addBeanAndEnsure(ContainerLifeCycle container, LifeCycle lifecycle)
{
container.addBean(lifecycle, true);
try
{
if (isRunning() && !lifecycle.isRunning())
start(lifecycle);
if (container.isRunning() && !lifecycle.isRunning())
container.start(lifecycle);
}
catch (RuntimeException | Error e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,22 @@ public static WebSocketMappings ensureMappings(ContextHandler contextHandler)
{
WebSocketMappings mappings = getMappings(contextHandler);
if (mappings == null)
{
mappings = contextHandler.getBean(WebSocketMappings.class);
if (mappings != null)
{
contextHandler.setAttribute(WEBSOCKET_MAPPING_ATTRIBUTE, mappings);

// The listener only persists through restarts if it is "durable" (if it is added before starting).
if (!contextHandler.getEventListeners().contains(mappings))
contextHandler.addEventListener(mappings);
}
}
if (mappings == null)
{
mappings = new WebSocketMappings(WebSocketServerComponents.getWebSocketComponents(contextHandler));
contextHandler.setAttribute(WEBSOCKET_MAPPING_ATTRIBUTE, mappings);
contextHandler.addBean(mappings);
WebSocketMappings m = mappings;
contextHandler.addEventListener(new LifeCycle.Listener()
{
@Override
public void lifeCycleStopping(LifeCycle event)
{
contextHandler.removeAttribute(WEBSOCKET_MAPPING_ATTRIBUTE);
contextHandler.removeEventListener(this);
contextHandler.removeBean(m);
}
});
}

return mappings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.compression.DeflaterPool;
import org.eclipse.jetty.util.compression.InflaterPool;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
Expand Down Expand Up @@ -99,6 +98,13 @@ private static WebSocketComponents ensureWebSocketComponents(Server server, Attr
if (components != null)
return components;

components = container.getBean(WebSocketServerComponents.class);
if (components != null)
{
attributes.setAttribute(WEBSOCKET_COMPONENTS_ATTRIBUTE, components);
return components;
}

InflaterPool inflaterPool = (InflaterPool)attributes.getAttribute(WEBSOCKET_INFLATER_POOL_ATTRIBUTE);
if (inflaterPool == null)
inflaterPool = InflaterPool.ensurePool(server);
Expand All @@ -116,44 +122,24 @@ private static WebSocketComponents ensureWebSocketComponents(Server server, Attr
executor = server.getThreadPool();

DecoratedObjectFactory objectFactory = (DecoratedObjectFactory)attributes.getAttribute(DecoratedObjectFactory.ATTR);
WebSocketComponents serverComponents = new WebSocketServerComponents(inflaterPool, deflaterPool, bufferPool, objectFactory, executor);
components = new WebSocketServerComponents(inflaterPool, deflaterPool, bufferPool, objectFactory, executor);
if (objectFactory != null)
serverComponents.unmanage(objectFactory);
components.unmanage(objectFactory);

// These components may be managed by the server but not yet started.
// In this case we don't want them to be managed by the components as well.
if (server.contains(inflaterPool))
serverComponents.unmanage(inflaterPool);
components.unmanage(inflaterPool);
if (server.contains(deflaterPool))
serverComponents.unmanage(deflaterPool);
components.unmanage(deflaterPool);
if (server.contains(bufferPool))
serverComponents.unmanage(bufferPool);
components.unmanage(bufferPool);
if (executor != null)
serverComponents.unmanage(executor);
components.unmanage(executor);

// Set to be managed as persistent attribute and bean on ContextHandler.
container.addManaged(serverComponents);
attributes.setAttribute(WEBSOCKET_COMPONENTS_ATTRIBUTE, serverComponents);

// Stop the WebSocketComponents when the ContextHandler stops and remove the WebSocketComponents attribute.
container.addEventListener(new LifeCycle.Listener()
{
@Override
public void lifeCycleStopping(LifeCycle event)
{
attributes.removeAttribute(WEBSOCKET_COMPONENTS_ATTRIBUTE);
container.removeBean(serverComponents);
container.removeEventListener(this);
}

@Override
public String toString()
{
return String.format("%sCleanupListener", WebSocketServerComponents.class.getSimpleName());
}
});

return serverComponents;
addBeanAndEnsure(container, components);
attributes.setAttribute(WEBSOCKET_COMPONENTS_ATTRIBUTE, components);
return components;
}

public static WebSocketComponents getWebSocketComponents(ContextHandler contextHandler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public WebSocketClient()
public WebSocketClient(HttpClient httpClient)
{
coreClient = new WebSocketCoreClient(httpClient, null);
addManaged(coreClient);
addBeanAndEnsure(this, coreClient);
frameHandlerFactory = new JettyWebSocketFrameHandlerFactory(this, coreClient.getWebSocketComponents());
sessionListeners.add(sessionTracker);
installBean(sessionTracker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public static ServerWebSocketContainer ensure(Server server, ContextHandler cont
? WebSocketServerComponents.ensureWebSocketComponents(server)
: WebSocketServerComponents.ensureWebSocketComponents(server, contextHandler);
WebSocketMappings mappings = new WebSocketMappings(components);
container = new ServerWebSocketContainer(context, mappings);
container = new ServerWebSocketContainer(context, components, mappings);
ContainerLifeCycle parent = contextHandler == null ? server : contextHandler;
parent.addManaged(container);
addBeanAndEnsure(parent, container);
}
return container;
}
Expand Down Expand Up @@ -127,13 +127,14 @@ public static ServerWebSocketContainer get(Context context)
private final FrameHandlerFactory factory;
private InvocationType invocationType = InvocationType.BLOCKING;

ServerWebSocketContainer(Context context, WebSocketMappings mappings)
ServerWebSocketContainer(Context context, WebSocketComponents components, WebSocketMappings mappings)
{
this.context = context;
this.mappings = mappings;
installBean(mappings);
this.factory = new ServerFrameHandlerFactory(this, mappings.getWebSocketComponents());
addSessionListener(sessionTracker);
installBean(components);
installBean(mappings);
installBean(sessionTracker);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ public boolean removeEventListener(EventListener listener)
{
if (super.removeEventListener(listener))
{
_durableListeners.remove(listener);

if (listener instanceof ServletContextScopeListener)
_contextListeners.remove(listener);

Expand Down Expand Up @@ -911,6 +913,9 @@ public boolean addEventListener(EventListener listener)
{
if (super.addEventListener(listener))
{
if (isRunning() && contains(listener))
_durableListeners.add(listener);

if ((listener instanceof HttpSessionActivationListener) ||
(listener instanceof HttpSessionAttributeListener) ||
(listener instanceof HttpSessionBindingListener) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.toolchain.test.MavenPaths;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
Expand Down Expand Up @@ -151,7 +152,7 @@ public void load() throws Exception

public void start() throws Exception
{
_server.addManaged(_resourceFactory);
ContainerLifeCycle.addBeanAndEnsure(_server, _resourceFactory);
_server.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected WebSocketCoreClient getWebSocketCoreClient()
if (coreClient == null)
{
coreClient = coreClientFactory.apply(components);
addManaged(coreClient);
addBeanAndEnsure(this, coreClient);
}

return coreClient;
Expand Down Expand Up @@ -332,7 +332,7 @@ protected void doClientStart()
ContainerLifeCycle container = SHUTDOWN_MAP.get(cl);
if (container != null)
{
container.addManaged(this);
addBeanAndEnsure(container, this);
if (LOG.isDebugEnabled())
LOG.debug("{} registered for WebApp shutdown to {}", this, container);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public JakartaWebSocketContainer(WebSocketComponents components)
this.components = components;
addSessionListener(sessionTracker);
installBean(sessionTracker);
installBean(components);
}

public abstract Executor getExecutor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static JakartaWebSocketServerContainer ensureContainer(ServletContext ser
coreClientSupplier);

// Manage the lifecycle of the Container.
contextHandler.addManaged(container);
addBeanAndEnsure(contextHandler, container);
contextHandler.addEventListener(container);
contextHandler.addEventListener(new LifeCycle.Listener()
{
Expand Down
Loading