[java-idp-integration-tests] 07/07: Enable ability to shutdown Jetty by sending stop command

Tom Zeller tzeller at dragonacea.biz
Fri Mar 23 19:36:31 EDT 2018


This is an automated email from the git hooks/post-receive script.

tzeller pushed a commit to branch master
in repository java-idp-integration-tests.

View the commit online:
http://git.shibboleth.net/view/?p=java-idp-integration-tests.git;a=commit;h=cd9244693c334ea90df24c46d1af801f349346b6

commit cd9244693c334ea90df24c46d1af801f349346b6
Author: Tom Zeller <tzeller at dragonacea.biz>
AuthorDate: Fri Mar 23 18:31:57 2018 -0500

    Enable ability to shutdown Jetty by sending stop command
---
 .../shibboleth/idp/test/JettyServerProcess.java    | 92 ++++++++++++++++++++++
 1 file changed, 92 insertions(+)

diff --git a/src/test/java/net/shibboleth/idp/test/JettyServerProcess.java b/src/test/java/net/shibboleth/idp/test/JettyServerProcess.java
index 1dbb46f..0a2d565 100644
--- a/src/test/java/net/shibboleth/idp/test/JettyServerProcess.java
+++ b/src/test/java/net/shibboleth/idp/test/JettyServerProcess.java
@@ -17,14 +17,36 @@
 
 package net.shibboleth.idp.test;
 
+import java.io.OutputStream;
+import java.net.ConnectException;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 
+import javax.annotation.Nonnull;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.util.SocketUtils;
+
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
 import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.logic.Constraint;
 
 /** Start Jetty via start.jar. */
 public class JettyServerProcess extends AbstractServerProcess {
 
+    /** Class logger. */
+    @Nonnull private final Logger log = LoggerFactory.getLogger(JettyServerProcess.class);
+
+    /** Port to use to shutdown Jetty. Defaults to 8005. */
+    @Nonnull private int shutdownPort = 8005;
+
+    /** Passphrase to use to shutdown Jetty. Defaults to SHUTDOWN. */
+    @Nonnull private String shutdownKey = "SHUTDOWN";
+
     /** {@inheritDoc} */
     @Override
     protected void doInitialize() throws ComponentInitializationException {
@@ -38,6 +60,76 @@ public class JettyServerProcess extends AbstractServerProcess {
         getCommands().add(pathToJava.toAbsolutePath().toString());
         getCommands().add("-jar");
         getCommands().add(getServletContainerHomePath().toAbsolutePath().toString() + "/start.jar");
+        getCommands().add("STOP.KEY=" + shutdownKey);
+        nextAvailableShutdownPort();
+        getCommands().add("STOP.PORT=" + Integer.toString(shutdownPort));
+    }
+
+    /**
+     * Set Jetty shutdown passphrase.
+     * 
+     * Passed to start.jar as STOP.KEY.
+     * 
+     * @param key key to use to shutdown Jetty
+     */
+    @Nonnull
+    public void setShutdownKey(@Nonnull @NotEmpty final String key) {
+        shutdownKey = key;
+    }
+
+    /**
+     * Configure the next available port in the range 20000-30000 to shutdown Jetty.
+     * 
+     * @return the next available port to use to shutdown Tomcat, by default '8005' if the '8080' system property is
+     *         <code>true</code>
+     * @throws ComponentInitializationException if catalina.properties cannot be modified
+     */
+    @Nonnull
+    public int nextAvailableShutdownPort() throws ComponentInitializationException {
+        if (Boolean.getBoolean("8080")) {
+            log.debug("System property '8080' is true, using default shutdown port {}", shutdownPort);
+            return shutdownPort;
+        }
+        shutdownPort = SocketUtils.findAvailableTcpPort(20000, 30000);
+        log.debug("Selecting Jetty shutdown port {}", shutdownPort);
+        return shutdownPort;
+    }
+
+    /**
+     * Attempt to shutdown Jetty.
+     * 
+     * Send the stop command to the STOP.PORT.
+     * 
+     * @param hostname the name of the remote host
+     * @param port the port to connect to on the remote host
+     * @param password the shutdown password
+     */
+    public void shutdown(@Nonnull final String hostname, final int port, @Nonnull final String password) {
+        Constraint.isNotNull(hostname, "Hostname cannot be null");
+        Constraint.isNotNull(password, "Password cannot be null");
+
+        log.debug("Attempting to shutdown Jetty at '{}:{}'", hostname, port);
+
+        try {
+            try (final Socket s = new Socket(InetAddress.getByName(hostname), port)) {
+                try (OutputStream out = s.getOutputStream()) {
+                    out.write((password + "\r\nstop\r\n").getBytes());
+                    out.flush();
+                }
+            }
+        } catch (SocketTimeoutException e) {
+            log.warn("Timed out waiting for stop confirmation");
+        } catch (ConnectException e) {
+            log.warn("Server might be stopped", e);
+        } catch (Exception e) {
+            log.warn("An error occurred waiting for server to stop", e);
+        }
     }
 
+    /** {@inheritDoc} */
+    @Override
+    public void stop() {
+        shutdown("127.0.0.1", shutdownPort, shutdownKey);
+        super.stop();
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list