Merged HotSwapHandler from ee9 to common

This commit is contained in:
Greg Wilkins 2022-09-23 17:21:54 +10:00
parent c4200aee9f
commit 2ca6dce08e
3 changed files with 104 additions and 18 deletions

View File

@ -534,9 +534,7 @@ public interface Handler extends LifeCycle, Destroyable, Invocable
public List<Handler> getHandlers()
{
Handler next = getHandler();
if (next == null)
return Collections.emptyList();
return List.of(next);
return (next == null) ? Collections.emptyList() : Collections.singletonList(next);
}
@Override

View File

@ -13,13 +13,113 @@
package org.eclipse.jetty.server.handler;
import java.util.Collections;
import java.util.List;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.thread.Invocable;
/**
* A <code>HandlerContainer</code> that allows a hot swap of a wrapped handler.
* @deprecated
*/
@Deprecated
public class HotSwapHandler extends Handler.Wrapper
public class HotSwapHandler extends Handler.AbstractContainer implements Handler.Nested
{
// TODO unit tests
private volatile Handler _handler;
/**
*
*/
public HotSwapHandler()
{
}
/**
* @return Returns the handlers.
*/
public Handler getHandler()
{
return _handler;
}
/**
* @return Returns the handlers.
*/
@Override
public List<Handler> getHandlers()
{
Handler next = _handler;
return (next == null) ? Collections.emptyList() : Collections.singletonList(next);
}
/**
* @param handler Set the {@link Handler} which should be wrapped.
*/
public void setHandler(Handler handler)
{
// check state
Server server1 = ((Nested)this).getServer();
if (server1 != null && server1.isStarted() && handler != null &&
server1.getInvocationType() != Invocable.combine(server1.getInvocationType(), handler.getInvocationType()))
throw new IllegalArgumentException("Cannot change invocation type of started server");
// Check for loops.
if (handler == this || (handler instanceof Container container &&
container.getDescendants().contains(this)))
throw new IllegalStateException("setHandler loop");
try
{
Server server = getServer();
if (handler == _handler)
return;
Handler oldHandler = _handler;
if (handler != null)
{
handler.setServer(server);
addBean(handler, true);
if (oldHandler != null && oldHandler.isStarted())
handler.start();
}
_handler = handler;
if (oldHandler != null)
removeBean(oldHandler);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
@Override
public Request.Processor handle(Request request) throws Exception
{
Handler next = _handler;
return next == null ? null : next.handle(request);
}
@Override
public InvocationType getInvocationType()
{
Handler next = getHandler();
return next == null ? InvocationType.NON_BLOCKING : next.getInvocationType();
}
@Override
public void destroy()
{
if (!isStopped())
throw new IllegalStateException("!STOPPED");
Handler child = getHandler();
if (child != null)
{
setHandler((Handler)null);
child.destroy();
}
super.destroy();
}
}

View File

@ -84,18 +84,6 @@ public class HotSwapHandler extends AbstractHandlerContainer
}
}
@Override
protected void doStart() throws Exception
{
super.doStart();
}
@Override
protected void doStop() throws Exception
{
super.doStop();
}
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{