]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/DevelLauncher.java
Fix: SimpleSigner Thread safety
[gigi.git] / src / org / cacert / gigi / DevelLauncher.java
index 431b922d298b76137884029b7b04e651db775079..97b4b3d52e7b6bd1332aea76ae6b4a679f81bb78 100644 (file)
@@ -43,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);
@@ -70,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");
@@ -77,7 +81,12 @@ public class DevelLauncher {
             instF.setAccessible(true);
             pageF.setAccessible(true);
             Object gigi = instF.get(null);
-            HashMap<String, Page> pages = new HashMap<>((Map<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
@@ -92,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);
@@ -105,6 +124,7 @@ public class DevelLauncher {
                     return false;
                 }
             });
+
             pageF.set(gigi, Collections.unmodifiableMap(pages));
         } catch (ReflectiveOperationException e) {
             e.printStackTrace();