]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/DevelLauncher.java
UPD: document and justify call to System.exit in /kill
[gigi.git] / src / org / cacert / gigi / DevelLauncher.java
index fe19ebd479250362b90e22afa29927f34ae1be4c..97b4b3d52e7b6bd1332aea76ae6b4a679f81bb78 100644 (file)
@@ -14,7 +14,9 @@ import java.lang.reflect.Field;
 import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.Map;
 import java.util.Properties;
 
 import javax.servlet.http.HttpServletRequest;
@@ -41,13 +43,7 @@ public class DevelLauncher {
             }
             i++;
         }
-        try {
-            String targetPort = mainProps.getProperty("http.port");
-            String targetHost = mainProps.getProperty("name.www");
-            URL u = new URL("http://" + targetHost + ":" + targetPort + "/kill");
-            u.openStream();
-        } catch (IOException e) {
-        }
+        killPreviousInstance(mainProps);
 
         ByteArrayOutputStream chunkConfig = new ByteArrayOutputStream();
         DataOutputStream dos = new DataOutputStream(chunkConfig);
@@ -68,6 +64,16 @@ public class DevelLauncher {
         System.exit(0);
     }
 
+    private static void killPreviousInstance(Properties mainProps) {
+        try {
+            String targetPort = mainProps.getProperty("http.port");
+            String targetHost = mainProps.getProperty("name.www");
+            URL u = new URL("http://" + targetHost + ":" + targetPort + "/kill");
+            u.openStream();
+        } catch (IOException e) {
+        }
+    }
+
     public static void addDevelPage() {
         try {
             Field instF = Gigi.class.getDeclaredField("instance");
@@ -75,7 +81,12 @@ public class DevelLauncher {
             instF.setAccessible(true);
             pageF.setAccessible(true);
             Object gigi = instF.get(null);
-            HashMap<String, Page> pages = (HashMap<String, Page>) pageF.get(gigi);
+
+            // Check if we got a proper map (as much as we can tell)
+            Object pagesObj = pageF.get(gigi);
+            @SuppressWarnings("unchecked")
+            HashMap<String, Page> pages = pagesObj instanceof Map ? new HashMap<>((Map<String, Page>) pagesObj) : null;
+
             pages.put("/manage", new Page("Page-manager") {
 
                 @Override
@@ -90,9 +101,19 @@ public class DevelLauncher {
                 public boolean needsLogin() {
                     return false;
                 }
+
             });
+
             pages.put("/kill", new Page("Kill") {
 
+                /**
+                 * The contained call to {@link System#exit(int)} is mainly
+                 * needed to kill this instance immediately if another
+                 * {@link DevelLauncher} is booting up to free all ports This is
+                 * required for fast development cycles.
+                 * 
+                 * @see #killPreviousInstance(Properties)
+                 */
                 @Override
                 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
                     System.exit(0);
@@ -103,6 +124,8 @@ public class DevelLauncher {
                     return false;
                 }
             });
+
+            pageF.set(gigi, Collections.unmodifiableMap(pages));
         } catch (ReflectiveOperationException e) {
             e.printStackTrace();
         }