(JAL-978) discover JABA1 and JABA2 services and generate GUI elements for each servic...
[jalview.git] / src / jalview / ws / jws2 / JabaWsServerQuery.java
index 74a5df1..61810e8 100644 (file)
@@ -1,6 +1,6 @@
 /*******************************************************************************
  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
- * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
+ * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
  *
  * This file is part of Jalview.
  *
 package jalview.ws.jws2;
 
 import jalview.bin.Cache;
+import jalview.ws.jws2.jabaws2.Jws2Instance;
 
-import compbio.data.msa.MsaWS;
+import java.util.HashSet;
+import java.util.Set;
+
+import compbio.data.msa.Category;
+import compbio.data.msa.JABAService;
 import compbio.ws.client.Jws2Client;
 import compbio.ws.client.Services;
 
 /**
  * @author JimP
- *
+ * 
  */
 public class JabaWsServerQuery implements Runnable
 {
 
-  Jws2Discoverer jws2Discoverer=null;
-  String jwsservers=null;
-  boolean quit=false,
-  running=false;
+  Jws2Discoverer jws2Discoverer = null;
+
+  String jwsservers = null;
+
+  boolean quit = false, running = false;
+
   /**
    * @return the running
    */
-  public  boolean isRunning()
+  public boolean isRunning()
   {
     return running;
   }
 
   /**
-   * @param quit the quit to set
+   * @param quit
+   *          the quit to set
    */
   public void setQuit(boolean quit)
   {
@@ -56,52 +64,118 @@ public class JabaWsServerQuery implements Runnable
   public JabaWsServerQuery(Jws2Discoverer jws2Discoverer, String jwsservers)
   {
     this.jws2Discoverer = jws2Discoverer;
-    this.jwsservers=jwsservers;
+    this.jwsservers = jwsservers;
   }
 
-  /* (non-Javadoc)
+  /*
+   * (non-Javadoc)
+   * 
    * @see java.lang.Runnable#run()
    */
   @Override
   public void run()
   {
-    running=true;
-        try
+    running = true;
+    try
     {
       if (Jws2Client.validURL(jwsservers))
       {
-        boolean noservices=true;
+        compbio.data.msa.RegistryWS registry = null;
+        Set svccategories = null;
+        boolean noservices = true;
         // look for services
-        for (Services srv : Services.values())
+        boolean jabasws2 = false;
+        // If we are dealing with a JABAWS2 service, then just go and ask the
+        // JABAWS 2 service registry
+        Set<Services> srv_set = new HashSet<Services>();
+
+        Set<Category> categories = Category.getCategories();
+        String svc_cat;
+
+        try
         {
-          if (quit)
+          // JBPNote: why is RegistryWS in compbio.data.msa ?
+          registry = Jws2Client.connectToRegistry(jwsservers);
+          if (registry != null)
           {
-            running=false;
-            return;
+            // System.err.println("Test Services Output\n"
+            // + registry.testAllServices());
+            // TODO: enumerate services and test those that haven't been tested
+            // in the last n-days/hours/etc.
+
+            jabasws2 = true;
+            srv_set = registry.getSupportedServices();
+            svccategories = registry.getServiceCategories();
+
           }
-          MsaWS service = null;
-          try
+        } catch (Exception ex)
+        {
+          System.err.println("Exception whilst trying to get at registry:");
+          ex.printStackTrace();
+          // if that failed, then we are probably working with a JABAWS1 server.
+          // in that case, look for each service endpoint
+          System.err.println("JWS2 Discoverer: " + jwsservers
+                  + " is a JABAWS1 server. Using hardwired list.");
+          for (Services srv : new Services[]
+          { Services.ClustalWS, Services.MuscleWS, Services.MafftWS,
+              Services.ProbconsWS, Services.TcoffeeWS })
           {
-            service = Jws2Client.connect(jwsservers, srv);
-          } catch (Exception e)
+            srv_set.add(srv);
+          }
+        }
+        for (Category cat : categories)
+        {
+          for (Services srv : cat.getServices())
           {
-            System.err.println("Jws2 Discoverer: Problem on "
-                    + jwsservers + " with service " + srv + ":\n"
-                    + e.getMessage());
-            if (!(e instanceof javax.xml.ws.WebServiceException))
+            if (quit)
             {
-              e.printStackTrace();
+              running = false;
+              return;
             }
-            // For moment, report service as a problem.
-            jws2Discoverer.addInvalidServiceUrl(jwsservers);
-          }
-          ;
-          if (service != null)
-          {
-            noservices=false;
-            jws2Discoverer.addService(jwsservers, srv, service);
+            if (!srv_set.contains(srv))
+            {
+              continue;
+            }
+            JABAService service = null;
+            try
+            {
+              service = Jws2Client.connect(jwsservers, srv);
+            } catch (Exception e)
+            {
+              System.err.println("Jws2 Discoverer: Problem on "
+                      + jwsservers + " with service " + srv + ":\n"
+                      + e.getMessage());
+              if (!(e instanceof javax.xml.ws.WebServiceException))
+              {
+                e.printStackTrace();
+              }
+              // For moment, report service as a problem.
+              jws2Discoverer.addInvalidServiceUrl(jwsservers);
+            }
+            ;
+            if (service != null)
+            {
+              noservices = false;
+              Jws2Instance svc = null;
+              if (registry != null)
+              {
+
+                String description = registry.getServiceDescription(srv);
+
+                svc = new Jws2Instance(jwsservers, srv.toString(),
+                        cat.name, description, service);
+              }
+              if (svc == null)
+              {
+                svc = new Jws2Instance(jwsservers, srv.toString(), cat.name,
+                        "JABAWS 1 Alignment Service", service);
+              }
+              jws2Discoverer.addService(jwsservers, svc);
+            }
+
           }
         }
+
         if (noservices)
         {
           jws2Discoverer.addUrlwithnoservices(jwsservers);
@@ -122,7 +196,7 @@ public class JabaWsServerQuery implements Runnable
       Cache.log.error("Exception when discovering Jws2 services.", e);
       jws2Discoverer.addInvalidServiceUrl(jwsservers);
     }
-    running=false;
+    running = false;
   }
 
 }