JAL-3690 refactoring web-services discovery
[jalview.git] / src / jalview / gui / Desktop.java
index a99bab9..d8e3bbb 100644 (file)
@@ -59,8 +59,11 @@ import java.util.Hashtable;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Vector;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.FutureTask;
 import java.util.concurrent.Semaphore;
 
 import javax.swing.AbstractAction;
@@ -2631,10 +2634,9 @@ public class Desktop extends jalview.jbgui.GDesktop
 
   public void startServiceDiscovery(boolean blocking)
   {
-    boolean alive = true;
-    Thread t0 = null, t1 = null, t2 = null, t3 = null;
+    var tasks = new ArrayList<Future<?>>();
     // JAL-940 - JALVIEW 1 services are now being EOLed as of JABA 2.1 release
-    if (true)
+
     {
       // todo: changesupport handlers need to be transferred
       if (discoverer == null)
@@ -2645,53 +2647,32 @@ public class Desktop extends jalview.jbgui.GDesktop
       }
       // JAL-940 - disabled JWS1 service configuration - always start discoverer
       // until we phase out completely
-      (t0 = new Thread(discoverer)).start();
+      var f = new FutureTask<Void>(discoverer, null);
+      new Thread(f).start();
+      tasks.add(f);
     }
 
     if (Cache.getDefault("SHOW_JWS2_SERVICES", true))
     {
-      t2 = startServiceDiscovery(
-          jalview.ws.jws2.Jws2Discoverer.getDiscoverer(), false);
+      tasks.add(jalview.ws.jws2.Jws2Discoverer.getDiscoverer().startDiscoverer());
     }
     if (Cache.getDefault("SHOW_SLIVKA_SERVICES", true))
     {
-      // start slivka discovery
-      t3 = startServiceDiscovery(
-          jalview.ws.slivkaws.SlivkaWSDiscoverer.getInstance(), false);
+      tasks.add(jalview.ws.slivkaws.SlivkaWSDiscoverer.getInstance().startDiscoverer());
     }
     if (blocking)
     {
-      while (alive)
-      {
+      for (Future<?> task : tasks) {
         try
         {
-          Thread.sleep(15);
+          // block until all discovery tasks are done
+          task.get();
         } catch (Exception e)
         {
+          e.printStackTrace();
         }
-        // FIXME: Condition should check the discoverer's isRunning rather than
-        // threads
-        alive = (t1 != null && t1.isAlive()) || (t2 != null && t2.isAlive())
-            || (t3 != null && t3.isAlive()) || (t0 != null && t0.isAlive());
-      }
-    }
-  }
-
-  public Thread startServiceDiscovery(WSDiscovererI discoverer,
-      boolean blocking)
-  {
-    Thread thread = discoverer.startDiscoverer(changeSupport);
-    if (blocking)
-    {
-      try
-      {
-        thread.join();
-      } catch (InterruptedException e)
-      {
-        e.printStackTrace();
       }
     }
-    return thread;
   }
 
   /**