JAL-976 pick an available instance of a service based on server-independent URI string
[jalview.git] / src / jalview / ws / jws2 / Jws2Discoverer.java
index b440de8..b6e7493 100644 (file)
@@ -31,6 +31,8 @@ import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.StringTokenizer;
@@ -701,4 +703,33 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     }
     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;
+  }
 }