From 3b36243749ee8b03dd84ade4983e17001b0ca20c Mon Sep 17 00:00:00 2001
From: Hallvard Traetteberg <hal@ntnu.no>
Date: Mon, 30 Sep 2019 16:07:17 +0200
Subject: [PATCH] Forenklet oppstart av serveren (la til venting).

---
 .../fxui/src/main/java/simpleex/ui/FxApp.java |  6 ++--
 .../java/simpleex/ui/FxAppUsingRestTest.java  |  9 ++----
 .../restserver/LatLongGrizzlyApp.java         | 32 +++++++++++++++++--
 3 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java b/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java
index aa0b92a..203d8dc 100644
--- a/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java
+++ b/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java
@@ -28,7 +28,7 @@ public class FxApp extends Application {
         // json of initial data
         serverArgs.add(args.get(1));
       }
-      restServer = LatLongGrizzlyApp.startServer(serverArgs.toArray(new String[serverArgs.size()]));
+      restServer = LatLongGrizzlyApp.startServer(serverArgs.toArray(new String[serverArgs.size()]), 5);
     }
     final String fxml = (baseUri != null ? "FxAppUsingRest.fxml" : "FxApp.fxml");
     final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxml));
@@ -48,7 +48,9 @@ public class FxApp extends Application {
 
   @Override
   public void stop() throws Exception {
-    restServer.shutdown();
+    if (restServer != null) {
+      restServer.shutdown();
+    }
     super.stop();
   }
 
diff --git a/simpleexample2/fxui/src/test/java/simpleex/ui/FxAppUsingRestTest.java b/simpleexample2/fxui/src/test/java/simpleex/ui/FxAppUsingRestTest.java
index 893452e..4517690 100644
--- a/simpleexample2/fxui/src/test/java/simpleex/ui/FxAppUsingRestTest.java
+++ b/simpleexample2/fxui/src/test/java/simpleex/ui/FxAppUsingRestTest.java
@@ -33,13 +33,8 @@ public class FxAppUsingRestTest extends AbstractFxAppTest {
       currentServer = LatLongGrizzlyApp.startServer(new String[] {
           serverUrlString,
           "[[63.1, 11.2], [63.2, 11.0]]"
-      });
-      // wait for server to start
-      int tryCount = 10;
-      while (tryCount-- > 0 && dataAccess.getAllLatLongs().isEmpty()) {
-        Thread.sleep(500);
-      }
-    } catch (final IOException | InterruptedException e) {
+      }, 5);
+    } catch (final IOException e) {
       throw new IllegalStateException("Couldn't setup server");
     }
   }
diff --git a/simpleexample2/restserver/src/main/java/simpleex/restserver/LatLongGrizzlyApp.java b/simpleexample2/restserver/src/main/java/simpleex/restserver/LatLongGrizzlyApp.java
index e6c14a2..dbd2be3 100644
--- a/simpleexample2/restserver/src/main/java/simpleex/restserver/LatLongGrizzlyApp.java
+++ b/simpleexample2/restserver/src/main/java/simpleex/restserver/LatLongGrizzlyApp.java
@@ -1,21 +1,47 @@
 package simpleex.restserver;
 
 import java.io.IOException;
+import java.net.HttpURLConnection;
 import java.net.URI;
+import java.net.URL;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import org.glassfish.grizzly.http.server.HttpServer;
 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
 import org.glassfish.jersey.server.ResourceConfig;
+import simpleex.restapi.LatLongsService;
 
 public class LatLongGrizzlyApp {
 
   private static URI BASE_URI = URI.create("http://localhost:8080/");
 
-  public static HttpServer startServer(final String[] args) throws IOException {
+  public static HttpServer startServer(final String[] args, int waitSecondsForServer) throws IOException {
     final URI baseUri = (args.length >= 1 ? URI.create(args[0]) : BASE_URI);
     final ResourceConfig resourceConfig = (args.length >= 2 ? new LatLongConfig(args[1]) : new LatLongConfig());
-    return GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
+    final HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
+    if (waitSecondsForServer < 0) {
+      return httpServer;
+    }
+    while (waitSecondsForServer > 0) {
+      try {
+        final URL clientUrl = new URL(baseUri + LatLongsService.LAT_LONG_SERVICE_PATH);
+        final HttpURLConnection connection = (HttpURLConnection) clientUrl.openConnection();
+        final int responseCode = connection.getResponseCode();
+        System.out.println("Trying " + clientUrl + ": " + responseCode);
+        connection.disconnect();
+        if (responseCode == 200) {
+          return httpServer;
+        }
+      } catch (final Exception e) {
+      }
+      try {
+        Thread.sleep(1000);
+        waitSecondsForServer -= 1;
+      } catch (final InterruptedException e) {
+        return null;
+      }
+    }
+    return null;
   }
 
   public static void stopServer(final HttpServer server) throws IOException {
@@ -24,7 +50,7 @@ public class LatLongGrizzlyApp {
 
   public static void main(final String[] args) throws IOException {
     try {
-      final HttpServer server = startServer(args);
+      final HttpServer server = startServer(args, -1);
       Runtime.getRuntime().addShutdownHook(new Thread(server::shutdownNow));
       Thread.currentThread().join();
     } catch (final InterruptedException ex) {
-- 
GitLab