]> source.dussan.org Git - vaadin-framework.git/commitdiff
#7375 Close previous DevelopmentServerLauncher when a new one is opened
authorLeif Åstrand <leif@vaadin.com>
Tue, 9 Aug 2011 06:50:03 +0000 (06:50 +0000)
committerLeif Åstrand <leif@vaadin.com>
Tue, 9 Aug 2011 06:50:03 +0000 (06:50 +0000)
svn changeset:20193/svn branch:6.7

src/com/vaadin/launcher/DevelopmentServerLauncher.java

index 256712c9635f9d6a6683d1bbff5015249aba669e..8a4b356ae7488e2575f057af95a47d9b023c674e 100644 (file)
@@ -5,6 +5,11 @@
 package com.vaadin.launcher;\r
 \r
 import java.io.File;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.net.InetAddress;\r
+import java.net.ServerSocket;\r
+import java.net.Socket;\r
 import java.util.HashMap;\r
 import java.util.Map;\r
 \r
@@ -39,6 +44,20 @@ public class DevelopmentServerLauncher {
         // Pass-through of arguments for Jetty\r
         final Map<String, String> serverArgs = parseArguments(args);\r
 \r
+        if (serverArgs.containsKey("shutdownPort")) {\r
+            int port = Integer.parseInt(serverArgs.get("shutdownPort"));\r
+            try {\r
+                // Try to notify another instance that it's time to close\r
+                Socket socket = new Socket((String) null, port);\r
+                // Wait until the other instance says it has closed\r
+                socket.getInputStream().read();\r
+                // Then tidy up\r
+                socket.close();\r
+            } catch (IOException e) {\r
+                // Ignore if port is not open\r
+            }\r
+        }\r
+\r
         // Start Jetty\r
         System.out.println("Starting Jetty servlet container.");\r
         String url;\r
@@ -118,6 +137,41 @@ public class DevelopmentServerLauncher {
 \r
         try {\r
             server.start();\r
+\r
+            if (serverArgs.containsKey("shutdownPort")) {\r
+                int shutdownPort = Integer.parseInt(serverArgs\r
+                        .get("shutdownPort"));\r
+                final ServerSocket serverSocket = new ServerSocket(\r
+                        shutdownPort, 1, InetAddress.getByName("127.0.0.1"));\r
+                new Thread() {\r
+                    @Override\r
+                    public void run() {\r
+                        try {\r
+                            System.out\r
+                                    .println("Waiting for shutdown signal on port "\r
+                                            + serverSocket.getLocalPort());\r
+                            // Start waiting for a close signal\r
+                            Socket accept = serverSocket.accept();\r
+                            // First stop listening to the port\r
+                            serverSocket.close();\r
+                            // Then stop the jetty server\r
+                            server.stop();\r
+                            // Send a byte to tell the other process that it can\r
+                            // start jetty\r
+                            OutputStream outputStream = accept\r
+                                    .getOutputStream();\r
+                            outputStream.write(0);\r
+                            outputStream.flush();\r
+                            // Finally close the socket\r
+                            accept.close();\r
+                        } catch (Exception e) {\r
+                            e.printStackTrace();\r
+                        }\r
+                    };\r
+\r
+                }.start();\r
+\r
+            }\r
         } catch (Exception e) {\r
             server.stop();\r
             throw e;\r