416577 enhanced shutdown handler to send shutdown at startup

This commit is contained in:
Greg Wilkins 2013-09-19 09:19:55 +10:00
parent c6a29339ab
commit ba5a709245
1 changed files with 102 additions and 9 deletions

View File

@ -19,11 +19,16 @@
package org.eclipse.jetty.server.handler;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.log.Log;
@ -42,7 +47,7 @@ import org.eclipse.jetty.util.log.Logger;
Server server = new Server(8080);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]
{ someOtherHandler, new ShutdownHandler(server,"secret password") });
{ someOtherHandler, new ShutdownHandler("secret password") });
server.setHandler(handlers);
server.start();
</pre>
@ -64,18 +69,15 @@ import org.eclipse.jetty.util.log.Logger;
}
</pre>
*/
public class ShutdownHandler extends AbstractHandler
public class ShutdownHandler extends HandlerWrapper
{
private static final Logger LOG = Log.getLogger(ShutdownHandler.class);
private final String _shutdownToken;
private final Server _server;
private boolean _sendShutdownAtStart;
private boolean _exitJvm = false;
/**
* Creates a listener that lets the server be shut down remotely (but only from localhost).
*
@ -84,16 +86,86 @@ public class ShutdownHandler extends AbstractHandler
* @param shutdownToken
* a secret password to avoid unauthorized shutdown attempts
*/
@Deprecated
public ShutdownHandler(Server server, String shutdownToken)
{
this._server = server;
this._shutdownToken = shutdownToken;
this(shutdownToken);
}
public ShutdownHandler(String shutdownToken)
{
this(shutdownToken,false,false);
}
/**
* @param shutdownToken
* @param sendShutdownAtStart If true, a shutdown is sent as a HTTP post
* during startup, which will shutdown any previously running instances of
* this server with an identically configured ShutdownHandler
*/
public ShutdownHandler(String shutdownToken, boolean exitJVM, boolean sendShutdownAtStart)
{
this._shutdownToken = shutdownToken;
setExitJvm(exitJVM);
setSendShutdownAtStart(sendShutdownAtStart);
}
public void sendShutdown() throws IOException
{
URL url = new URL(getServerUrl() + "/shutdown?token=" + _shutdownToken);
try
{
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.getResponseCode();
LOG.info("Shutting down " + url + ": " + connection.getResponseMessage());
}
catch (SocketException e)
{
LOG.debug("Not running");
// Okay - the server is not running
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@SuppressWarnings("resource")
private String getServerUrl()
{
NetworkConnector connector=null;
for (Connector c: getServer().getConnectors())
{
if (c instanceof NetworkConnector)
{
connector=(NetworkConnector)c;
break;
}
}
if (connector==null)
return "http://localhost";
return "http://localhost:" + connector.getPort();
}
@Override
protected void doStart() throws Exception
{
super.doStart();
if (_sendShutdownAtStart)
sendShutdown();
}
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
if (!target.equals("/shutdown"))
{
super.handle(target,baseRequest,request,response);
return;
}
@ -119,6 +191,7 @@ public class ShutdownHandler extends AbstractHandler
new Thread()
{
@Override
public void run ()
{
try
@ -156,7 +229,7 @@ public class ShutdownHandler extends AbstractHandler
private void shutdownServer() throws Exception
{
_server.stop();
getServer().stop();
if (_exitJvm)
{
@ -169,4 +242,24 @@ public class ShutdownHandler extends AbstractHandler
this._exitJvm = exitJvm;
}
public boolean isSendShutdownAtStart()
{
return _sendShutdownAtStart;
}
public void setSendShutdownAtStart(boolean sendShutdownAtStart)
{
_sendShutdownAtStart = sendShutdownAtStart;
}
public String getShutdownToken()
{
return _shutdownToken;
}
public boolean isExitJvm()
{
return _exitJvm;
}
}