//
// ========================================================================
-// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
+// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
/* ------------------------------------------------------------ */
/**
- * A handler that shuts the server down on a valid request. Used to do "soft" restarts from Java. If _exitJvm ist set to true a hard System.exit() call is being
- * made.
+ * A handler that shuts the server down on a valid request. Used to do "soft" restarts from Java.
+ * If _exitJvm is set to true a hard System.exit() call is being made.
+ * If _sendShutdownAtStart is set to true, starting the server will try to shut down an existing server at the same port.
+ * If _sendShutdownAtStart is set to true, make a http call to
+ * "http://localhost:" + port + "/shutdown?token=" + shutdownCookie
+ * in order to shut down the server.
*
* This handler is a contribution from Johannes Brodwall: https://bugs.eclipse.org/bugs/show_bug.cgi?id=357687
*
Server server = new Server(8080);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]
- { someOtherHandler, new ShutdownHandler("secret password") });
+ { someOtherHandler, new ShutdownHandler("secret password", false, true) });
server.setHandler(handlers);
server.start();
</pre>
/**
* @param shutdownToken
+ * a secret password to avoid unauthorized shutdown attempts
+ * @param exitJVM If true, when the shutdown is executed, the handler class System.exit()
* @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
+ * 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)
{
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.getResponseCode();
- LOG.info("Shutting down " + url + ": " + connection.getResponseMessage());
+ LOG.info("Shutting down " + url + ": " + connection.getResponseCode() + " " + connection.getResponseMessage());
}
catch (SocketException e)
{
}
}
+ @SuppressWarnings("resource")
private String getServerUrl()
{
NetworkConnector connector=null;
}
LOG.info("Shutting down by request from " + request.getRemoteAddr());
+ doShutdown(baseRequest, response);
+ }
+
+ protected void doShutdown(Request baseRequest, HttpServletResponse response) throws IOException {
+ for (Connector connector : getServer().getConnectors()) {
+ connector.shutdown();
+ }
+
+ response.sendError(200, "Connectors closed, commencing full shutdown");
+ baseRequest.setHandled(true);
final Server server=getServer();
new Thread()
private boolean hasCorrectSecurityToken(HttpServletRequest request)
{
String tok = request.getParameter("token");
- LOG.debug("Token: {}", tok);
+ if (LOG.isDebugEnabled())
+ LOG.debug("Token: {}", tok);
return _shutdownToken.equals(tok);
}