JAL-4199 Update slivka client
[jalview.git] / src / jalview / ws / slivkaws / SlivkaWSDiscoverer.java
index cd296c6..104560a 100644 (file)
@@ -1,9 +1,12 @@
 package jalview.ws.slivkaws;
 
 import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.ws.ServiceChangeListener;
 import jalview.ws.WSDiscovererI;
 import jalview.ws.api.ServiceWithParameters;
+import javajs.http.HttpClientFactory;
+
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -17,6 +20,8 @@ import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+
+import compbio.data.msa.Category;
 import uk.ac.dundee.compbio.slivkaclient.SlivkaClient;
 import uk.ac.dundee.compbio.slivkaclient.SlivkaService;
 
@@ -65,7 +70,9 @@ public class SlivkaWSDiscoverer implements WSDiscovererI
     }
   }
 
-  private final ExecutorService executor = Executors.newSingleThreadExecutor();
+  private final ExecutorService executor = Executors
+          .newSingleThreadExecutor();
+
   private Vector<Future<?>> discoveryTasks = new Vector<>();
 
   public CompletableFuture<WSDiscovererI> startDiscoverer()
@@ -81,54 +88,67 @@ public class SlivkaWSDiscoverer implements WSDiscovererI
 
   private List<ServiceWithParameters> reloadServices()
   {
-    Cache.log.info("Reloading Slivka services");
+    Console.info("Reloading Slivka services");
     notifyServiceListeners(Collections.emptyList());
     ArrayList<ServiceWithParameters> instances = new ArrayList<>();
 
     for (String url : getServiceUrls())
     {
-      Cache.log.info(url);
-      SlivkaClient client;
-      client = new SlivkaClient(url);
+      SlivkaClient client = SlivkaClient.newInstance(url);
+
+      List<SlivkaService> services;
       try
       {
-        for (SlivkaService service : client.getServices())
+        services = client.getServices();
+      } catch (IOException e)
+      {
+        e.printStackTrace();
+        continue;
+      }
+      for (SlivkaService service : services)
+      {
+        SlivkaWSInstance newInstance = null;
+        for (String classifier : service.classifiers)
         {
-          SlivkaWSInstance newinstance = null;
-          for (String classifier : service.classifiers)
+          String[] path = classifier.split("\\s*::\\s*");
+          if (path.length >= 3 && path[0].toLowerCase().equals("operation")
+                  && path[1].toLowerCase().equals("analysis"))
           {
-            if (classifier.contains("Multiple sequence alignment"))
+            switch (path[path.length - 1].toLowerCase())
             {
-              newinstance = new SlivkaMsaServiceInstance(client, service);
+            case "rna secondary structure prediction":
+              newInstance = new RNAalifoldServiceInstance(client,
+                      service, "Secondary Structure Prediction");
+              break;
+            case "sequence alignment analysis (conservation)":
+              newInstance = new SlivkaAnnotationServiceInstance(client,
+                      service, Category.CATEGORY_CONSERVATION);
+              break;
+            case "protein sequence analysis":
+              newInstance = new SlivkaAnnotationServiceInstance(client,
+                      service, Category.CATEGORY_DISORDER);
+              break;
+            case "protein secondary structure prediction":
+              newInstance = new SlivkaAnnotationServiceInstance(client,
+                      service, "Secondary Structure Prediction");
+              break;
+            case "multiple sequence alignment":
+              newInstance = new SlivkaMsaServiceInstance(client, service,
+                      Category.CATEGORY_ALIGNMENT);
+              break;
             }
-            if (classifier.contains("Protein sequence analysis")
-                && newinstance == null)
-            {
-              newinstance = new SlivkaAnnotationServiceInstance(client,
-                  service, false);
-            }
-            if (classifier
-                .contains("Sequence alignment analysis (conservation)"))
-            {
-              newinstance = new SlivkaAnnotationServiceInstance(client,
-                  service, true);
-            }
-          }
-          if (newinstance != null)
-          {
-            instances.add(newinstance);
           }
+          if (newInstance != null)
+            break;
         }
-      } catch (IOException e)
-      {
-        e.printStackTrace();
-        continue;
+        if (newInstance != null)
+          instances.add(newInstance);
       }
     }
 
     services = instances;
+    Console.info("Slivka services reloading finished");
     notifyServiceListeners(instances);
-    Cache.log.info("Slivka services reloading finished");
     return instances;
   }
 
@@ -177,13 +197,13 @@ public class SlivkaWSDiscoverer implements WSDiscovererI
         valid.add(url);
       } catch (MalformedURLException e)
       {
-        Cache.log.warn("Problem whilst trying to make a URL from '"
-            + ((url != null) ? url : "<null>") + "'");
-        Cache.log.warn(
-            "This was probably due to a malformed comma separated list"
-                + " in the " + SLIVKA_HOST_URLS
-                + " entry of $(HOME)/.jalview_properties)");
-        Cache.log.debug("Exception was ", e);
+        Console.warn("Problem whilst trying to make a URL from '"
+                + ((url != null) ? url : "<null>") + "'");
+        Console.warn(
+                "This was probably due to a malformed comma separated list"
+                        + " in the " + SLIVKA_HOST_URLS
+                        + " entry of $(HOME)/.jalview_properties)");
+        Console.debug("Exception was ", e);
       }
     }
     return valid;
@@ -200,11 +220,11 @@ public class SlivkaWSDiscoverer implements WSDiscovererI
   {
     try
     {
-      List<?> services = new SlivkaClient(url).getServices();
+      List<?> services = SlivkaClient.newInstance(url).getServices();
       return services.isEmpty() ? STATUS_NO_SERVICES : STATUS_OK;
-    } catch (IOException e)
+    } catch (IOException | org.json.JSONException e)
     {
-      Cache.log.error("Slivka could not retrieve services list", e);
+      Console.error("Slivka could not retrieve services list", e);
       return STATUS_INVALID;
     }
   }