2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server.handler;
21 import java.io.IOException;
22 import java.net.HttpURLConnection;
23 import java.net.InetSocketAddress;
24 import java.net.SocketException;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
31 import org.eclipse.jetty.server.Connector;
32 import org.eclipse.jetty.server.NetworkConnector;
33 import org.eclipse.jetty.server.Request;
34 import org.eclipse.jetty.server.Server;
35 import org.eclipse.jetty.util.log.Log;
36 import org.eclipse.jetty.util.log.Logger;
38 /* ------------------------------------------------------------ */
40 * 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
43 * This handler is a contribution from Johannes Brodwall: https://bugs.eclipse.org/bugs/show_bug.cgi?id=357687
48 Server server = new Server(8080);
49 HandlerList handlers = new HandlerList();
50 handlers.setHandlers(new Handler[]
51 { someOtherHandler, new ShutdownHandler("secret password") });
52 server.setHandler(handlers);
57 public static void attemptShutdown(int port, String shutdownCookie) {
59 URL url = new URL("http://localhost:" + port + "/shutdown?token=" + shutdownCookie);
60 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
61 connection.setRequestMethod("POST");
62 connection.getResponseCode();
63 logger.info("Shutting down " + url + ": " + connection.getResponseMessage());
64 } catch (SocketException e) {
65 logger.debug("Not running");
66 // Okay - the server is not running
67 } catch (IOException e) {
68 throw new RuntimeException(e);
73 public class ShutdownHandler extends HandlerWrapper
75 private static final Logger LOG = Log.getLogger(ShutdownHandler.class);
77 private final String _shutdownToken;
78 private boolean _sendShutdownAtStart;
79 private boolean _exitJvm = false;
83 * Creates a listener that lets the server be shut down remotely (but only from localhost).
86 * the Jetty instance that should be shut down
87 * @param shutdownToken
88 * a secret password to avoid unauthorized shutdown attempts
91 public ShutdownHandler(Server server, String shutdownToken)
96 public ShutdownHandler(String shutdownToken)
98 this(shutdownToken,false,false);
102 * @param shutdownToken
103 * @param sendShutdownAtStart If true, a shutdown is sent as a HTTP post
104 * during startup, which will shutdown any previously running instances of
105 * this server with an identically configured ShutdownHandler
107 public ShutdownHandler(String shutdownToken, boolean exitJVM, boolean sendShutdownAtStart)
109 this._shutdownToken = shutdownToken;
111 setSendShutdownAtStart(sendShutdownAtStart);
115 public void sendShutdown() throws IOException
117 URL url = new URL(getServerUrl() + "/shutdown?token=" + _shutdownToken);
120 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
121 connection.setRequestMethod("POST");
122 connection.getResponseCode();
123 LOG.info("Shutting down " + url + ": " + connection.getResponseMessage());
125 catch (SocketException e)
127 LOG.debug("Not running");
128 // Okay - the server is not running
130 catch (IOException e)
132 throw new RuntimeException(e);
136 private String getServerUrl()
138 NetworkConnector connector=null;
139 for (Connector c: getServer().getConnectors())
141 if (c instanceof NetworkConnector)
143 connector=(NetworkConnector)c;
149 return "http://localhost";
151 return "http://localhost:" + connector.getPort();
156 protected void doStart() throws Exception
159 if (_sendShutdownAtStart)
164 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
166 if (!target.equals("/shutdown"))
168 super.handle(target,baseRequest,request,response);
172 if (!request.getMethod().equals("POST"))
174 response.sendError(HttpServletResponse.SC_BAD_REQUEST);
177 if (!hasCorrectSecurityToken(request))
179 LOG.warn("Unauthorized tokenless shutdown attempt from " + request.getRemoteAddr());
180 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
183 if (!requestFromLocalhost(baseRequest))
185 LOG.warn("Unauthorized non-loopback shutdown attempt from " + request.getRemoteAddr());
186 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
190 LOG.info("Shutting down by request from " + request.getRemoteAddr());
192 final Server server=getServer();
200 shutdownServer(server);
202 catch (InterruptedException e)
208 throw new RuntimeException("Shutting down server",e);
214 private boolean requestFromLocalhost(Request request)
216 InetSocketAddress addr = request.getRemoteInetSocketAddress();
221 return addr.getAddress().isLoopbackAddress();
224 private boolean hasCorrectSecurityToken(HttpServletRequest request)
226 String tok = request.getParameter("token");
227 LOG.debug("Token: {}", tok);
228 return _shutdownToken.equals(tok);
231 private void shutdownServer(Server server) throws Exception
241 public void setExitJvm(boolean exitJvm)
243 this._exitJvm = exitJvm;
246 public boolean isSendShutdownAtStart()
248 return _sendShutdownAtStart;
251 public void setSendShutdownAtStart(boolean sendShutdownAtStart)
253 _sendShutdownAtStart = sendShutdownAtStart;
256 public String getShutdownToken()
258 return _shutdownToken;
261 public boolean isExitJvm()