JAL-976 pick an available instance of a service based on server-independent URI string
[jalview.git] / src / jalview / ws / jws2 / Jws2Discoverer.java
index afeb53a..b6e7493 100644 (file)
  */
 package jalview.ws.jws2;
 
+import jalview.bin.Cache;
+import jalview.gui.AlignFrame;
+import jalview.gui.Desktop;
+import jalview.gui.JvSwingUtils;
+import jalview.ws.WSMenuEntryProviderI;
+import jalview.ws.jws2.jabaws2.Jws2Instance;
+
 import java.awt.Color;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
-import java.net.ConnectException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
@@ -33,24 +40,7 @@ import java.util.Vector;
 
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
-import javax.swing.event.MenuEvent;
-import javax.swing.event.MenuListener;
-
-import org.apache.log4j.Level;
 
-import jalview.bin.Cache;
-import jalview.datamodel.AlignmentView;
-import jalview.gui.AlignFrame;
-import jalview.gui.Desktop;
-import jalview.gui.JalviewChangeSupport;
-import jalview.gui.JvSwingUtils;
-import jalview.util.jarInputStreamProvider;
-import jalview.ws.WSMenuEntryProviderI;
-import jalview.ws.jws2.jabaws2.Jws2Instance;
-import compbio.data.msa.JABAService;
-import compbio.metadata.Option;
-import compbio.metadata.Preset;
-import compbio.ws.client.Jws2Client;
 import compbio.ws.client.Services;
 
 /**
@@ -160,6 +150,10 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     {
       invalidServiceUrls.removeAllElements();
     }
+    if (validServiceUrls != null)
+    {
+      validServiceUrls.removeAllElements();
+    }
     ArrayList<String> svctypes=new ArrayList<String>();
 
     List<JabaWsServerQuery> qrys = new ArrayList<JabaWsServerQuery>();
@@ -253,6 +247,11 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     // retrieve the presets and parameter set and cache now
     service.getParamStore().getPresets();
     service.hasParameters();
+    if (validServiceUrls==null)
+    {
+      validServiceUrls=new Vector();
+    }
+    validServiceUrls.add(jwsservers);
   }
 
   /**
@@ -583,7 +582,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     return thr;
   }
 
-  Vector<String> invalidServiceUrls = null, urlsWithoutServices = null;
+  Vector<String> invalidServiceUrls = null, urlsWithoutServices = null, validServiceUrls=null;
 
   /**
    * @return the invalidServiceUrls
@@ -689,4 +688,48 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     }
     return null;
   }
+
+  public int getServerStatusFor(String url)
+  {
+    if (validServiceUrls!=null && validServiceUrls.contains(url))
+    {
+      return 1;
+    }
+    if (urlsWithoutServices!=null && urlsWithoutServices.contains(url))
+    return 0;
+    if (invalidServiceUrls!=null && invalidServiceUrls.contains(url))
+    {
+      return -1;
+    }
+    return -2;
+  }
+
+  /**
+   * pick the user's preferred service based on a set of URLs (jaba server locations) and service URIs (specifying version and service interface class)
+   * 
+   * @param serviceURL
+   * @return null or best match for given uri/ls.
+   */
+  public Jws2Instance getPreferredServiceFor(String[] serviceURLs)
+  {
+    HashSet<String> urls=new HashSet<String>();
+    urls.addAll(Arrays.asList(serviceURLs));
+    Jws2Instance match=null;
+    for (Jws2Instance svc:services)
+    {
+      if (urls.contains(svc.getServiceTypeURI()))
+      {
+        if (match==null) {
+          // for moment we always pick service from server ordered first in user's preferences
+          match=svc;
+        }
+        if (urls.contains(svc.getUri()))
+        {
+          // stop and return - we've matched type URI and URI for service endpoint
+          return svc;
+        }
+      }
+    }
+    return match;
+  }
 }