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
// 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
\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