]> WPIA git - gigi.git/blobdiff - lib/jetty/org/eclipse/jetty/server/handler/ShutdownHandler.java
updating jetty to jetty-9.2.16.v2016040
[gigi.git] / lib / jetty / org / eclipse / jetty / server / handler / ShutdownHandler.java
index e946fb670e704b5822a7431f8335116bbba1e5b2..6a4daa37ef249aaa308d8afa9e3439bb6d114a1a 100644 (file)
@@ -1,6 +1,6 @@
 //
 //  ========================================================================
 //
 //  ========================================================================
-//  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
 //  ------------------------------------------------------------------------
 //  All rights reserved. This program and the accompanying materials
 //  are made available under the terms of the Eclipse Public License v1.0
@@ -37,8 +37,12 @@ import org.eclipse.jetty.util.log.Logger;
 
 /* ------------------------------------------------------------ */
 /**
 
 /* ------------------------------------------------------------ */
 /**
- * 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
  *
  *
  * This handler is a contribution from Johannes Brodwall: https://bugs.eclipse.org/bugs/show_bug.cgi?id=357687
  *
@@ -48,7 +52,7 @@ import org.eclipse.jetty.util.log.Logger;
     Server server = new Server(8080);
     HandlerList handlers = new HandlerList();
     handlers.setHandlers(new Handler[]
     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>
     server.setHandler(handlers);
     server.start();
    </pre>
@@ -100,9 +104,11 @@ public class ShutdownHandler extends HandlerWrapper
     
     /**
      * @param shutdownToken
     
     /**
      * @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
      * @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)
     {
      */
     public ShutdownHandler(String shutdownToken, boolean exitJVM, boolean sendShutdownAtStart)
     {
@@ -120,7 +126,7 @@ public class ShutdownHandler extends HandlerWrapper
             HttpURLConnection connection = (HttpURLConnection)url.openConnection();
             connection.setRequestMethod("POST");
             connection.getResponseCode();
             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)
         {
         }
         catch (SocketException e)
         {
@@ -133,6 +139,7 @@ public class ShutdownHandler extends HandlerWrapper
         }
     }
 
         }
     }
 
+    @SuppressWarnings("resource")
     private String getServerUrl()
     {
         NetworkConnector connector=null;
     private String getServerUrl()
     {
         NetworkConnector connector=null;
@@ -188,6 +195,16 @@ public class ShutdownHandler extends HandlerWrapper
         }
 
         LOG.info("Shutting down by request from " + request.getRemoteAddr());
         }
 
         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()
 
         final Server server=getServer();
         new Thread()
@@ -224,7 +241,8 @@ public class ShutdownHandler extends HandlerWrapper
     private boolean hasCorrectSecurityToken(HttpServletRequest request)
     {
         String tok = request.getParameter("token");
     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);
     }
 
         return _shutdownToken.equals(tok);
     }