temp push
[jalview.git] / src / jalview / ws / jws2 / Jws2Discoverer.java
index 5319eab..bd4d352 100644 (file)
@@ -21,6 +21,8 @@
 package jalview.ws.jws2;
 
 import jalview.bin.Cache;
+import jalview.bin.ApplicationSingletonProvider;
+import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
 import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
 import jalview.gui.JvSwingUtils;
@@ -60,8 +62,29 @@ import compbio.ws.client.Services;
  * @author JimP
  * 
  */
-public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
+public class Jws2Discoverer
+        implements Runnable, WSMenuEntryProviderI, ApplicationSingletonI
 {
+
+  /**
+   * Returns the singleton instance of this class.
+   * 
+   * @return
+   */
+  public static Jws2Discoverer getInstance()
+  {
+    return (Jws2Discoverer) ApplicationSingletonProvider
+            .getInstance(Jws2Discoverer.class);
+  }
+
+  /**
+   * Private constructor enforces use of singleton via getDiscoverer()
+   */
+  private Jws2Discoverer()
+  {
+    // use getInstance();
+  }
+
   public static final String COMPBIO_JABAWS = "http://www.compbio.dundee.ac.uk/jabaws";
 
   /*
@@ -70,11 +93,6 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
   private final static String JWS2HOSTURLS = "JWS2HOSTURLS";
 
   /*
-   * Singleton instance
-   */
-  private static Jws2Discoverer discoverer;
-
-  /*
    * Override for testing only
    */
   private static List<String> testUrls = null;
@@ -82,7 +100,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
   // preferred url has precedence over others
   private String preferredUrl;
 
-  private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
+  protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
           this);
 
   private Vector<String> invalidServiceUrls = null;
@@ -103,13 +121,6 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
   protected Vector<Jws2Instance> services;
 
   /**
-   * Private constructor enforces use of singleton via getDiscoverer()
-   */
-  private Jws2Discoverer()
-  {
-  }
-
-  /**
    * change listeners are notified of "services" property changes
    * 
    * @param listener
@@ -178,7 +189,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     running = true;
 
     // first set up exclusion list if needed
-    final Set<String> ignoredServices = new HashSet<String>();
+    final Set<String> ignoredServices = new HashSet<>();
     for (String ignored : Cache
             .getDefault("IGNORED_JABAWS_SERVICETYPES", "").split("\\|"))
     {
@@ -190,7 +201,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     oldthread = Thread.currentThread();
     try
     {
-      Class foo = getClass().getClassLoader()
+      getClass().getClassLoader()
               .loadClass("compbio.ws.client.Jws2Client");
     } catch (ClassNotFoundException e)
     {
@@ -217,9 +228,9 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     {
       validServiceUrls.removeAllElements();
     }
-    ArrayList<String> svctypes = new ArrayList<String>();
+    ArrayList<String> svctypes = new ArrayList<>();
 
-    List<JabaWsServerQuery> qrys = new ArrayList<JabaWsServerQuery>();
+    List<JabaWsServerQuery> qrys = new ArrayList<>();
     for (final String jwsserver : getServiceUrls())
     {
       JabaWsServerQuery squery = new JabaWsServerQuery(this, jwsserver);
@@ -249,7 +260,6 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
       } catch (Exception e)
       {
       }
-      ;
       for (JabaWsServerQuery squery : qrys)
       {
         if (squery.isRunning())
@@ -285,7 +295,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
                   + svctypes.indexOf(svc.serviceType);
         }
         jalview.util.QuickSort.sort(spos, svcs);
-        services = new Vector<Jws2Instance>();
+        services = new Vector<>();
         for (Jws2Instance svc : svcs)
         {
           if (!ignoredServices.contains(svc.serviceType))
@@ -312,7 +322,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
   {
     if (services == null)
     {
-      services = new Vector<Jws2Instance>();
+      services = new Vector<>();
     }
     System.out.println(
             "Discovered service: " + jwsservers + " " + service.toString());
@@ -329,7 +339,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     service.hasParameters();
     if (validServiceUrls == null)
     {
-      validServiceUrls = new Vector<String>();
+      validServiceUrls = new Vector<>();
     }
     validServiceUrls.add(jwsservers);
   }
@@ -363,10 +373,10 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
      * for moment we keep them separate.
      */
     JMenu atpoint;
-    List<Jws2Instance> enumerableServices = new ArrayList<Jws2Instance>();
+    List<Jws2Instance> enumerableServices = new ArrayList<>();
     // jws2al.removeAll();
-    Map<String, Jws2Instance> preferredHosts = new HashMap<String, Jws2Instance>();
-    Map<String, List<Jws2Instance>> alternates = new HashMap<String, List<Jws2Instance>>();
+    Map<String, Jws2Instance> preferredHosts = new HashMap<>();
+    Map<String, List<Jws2Instance>> alternates = new HashMap<>();
     for (Jws2Instance service : services.toArray(new Jws2Instance[0]))
     {
       if (!isRecalculable(service.action))
@@ -395,7 +405,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
         {
           if (ph == null)
           {
-            ph = new ArrayList<Jws2Instance>();
+            ph = new ArrayList<>();
           }
           ph.add(service);
           alternates.put(service.serviceType, ph);
@@ -457,7 +467,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
                           sv.action, sv);
                   changeSupport.firePropertyChange("services",
                           new Vector<Jws2Instance>(), services);
-                };
+                }
               }).start();
 
             }
@@ -483,17 +493,17 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
      */
     JMenu atpoint;
 
-    List<String> hostLabels = new ArrayList<String>();
-    Hashtable<String, String> lasthostFor = new Hashtable<String, String>();
-    Hashtable<String, ArrayList<Jws2Instance>> hosts = new Hashtable<String, ArrayList<Jws2Instance>>();
-    ArrayList<String> hostlist = new ArrayList<String>();
+    List<String> hostLabels = new ArrayList<>();
+    Hashtable<String, String> lasthostFor = new Hashtable<>();
+    Hashtable<String, ArrayList<Jws2Instance>> hosts = new Hashtable<>();
+    ArrayList<String> hostlist = new ArrayList<>();
     for (Jws2Instance service : enumerableServices)
     {
       ArrayList<Jws2Instance> hostservices = hosts.get(service.getHost());
       if (hostservices == null)
       {
         hosts.put(service.getHost(),
-                hostservices = new ArrayList<Jws2Instance>());
+                hostservices = new ArrayList<>());
         hostlist.add(service.getHost());
       }
       hostservices.add(service);
@@ -577,30 +587,34 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     }
   }
 
+  /**
+   * 
+   * @param args
+   * @j2sIgnore
+   */
   public static void main(String[] args)
   {
     if (args.length > 0)
     {
-      testUrls = new ArrayList<String>();
+      testUrls = new ArrayList<>();
       for (String url : args)
       {
         testUrls.add(url);
       }
-      ;
     }
-    Thread runner = getDiscoverer()
+    Thread runner = getInstance()
             .startDiscoverer(new PropertyChangeListener()
             {
 
               @Override
               public void propertyChange(PropertyChangeEvent evt)
               {
-                if (getDiscoverer().services != null)
+                if (getInstance().services != null)
                 {
                   System.out.println("Changesupport: There are now "
-                          + getDiscoverer().services.size() + " services");
+                          + getInstance().services.size() + " services");
                   int i = 1;
-                  for (Jws2Instance instance : getDiscoverer().services)
+                  for (Jws2Instance instance : getInstance().services)
                   {
                     System.out.println("Service " + i++ + " "
                             + instance.getClass() + "@" + instance.getHost()
@@ -618,7 +632,6 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
       } catch (InterruptedException e)
       {
       }
-      ;
     }
     try
     {
@@ -628,20 +641,6 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     }
   }
 
-  /**
-   * Returns the singleton instance of this class.
-   * 
-   * @return
-   */
-  public static Jws2Discoverer getDiscoverer()
-  {
-    if (discoverer == null)
-    {
-      discoverer = new Jws2Discoverer();
-    }
-    return discoverer;
-  }
-
   public boolean hasServices()
   {
     return !running && services != null && services.size() > 0;
@@ -685,7 +684,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
       // return test urls, if there are any, instead of touching cache
       return testUrls;
     }
-    List<String> urls = new ArrayList<String>();
+    List<String> urls = new ArrayList<>();
 
     if (this.preferredUrl != null)
     {
@@ -733,8 +732,8 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
 
   public Vector<Jws2Instance> getServices()
   {
-    return (services == null) ? new Vector<Jws2Instance>()
-            : new Vector<Jws2Instance>(services);
+    return (services == null) ? new Vector<>()
+            : new Vector<>(services);
   }
 
   /**
@@ -839,7 +838,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
   {
     if (urlsWithoutServices == null)
     {
-      urlsWithoutServices = new Vector<String>();
+      urlsWithoutServices = new Vector<>();
     }
 
     if ((invalidServiceUrls == null
@@ -859,7 +858,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
   {
     if (invalidServiceUrls == null)
     {
-      invalidServiceUrls = new Vector<String>();
+      invalidServiceUrls = new Vector<>();
     }
     if (!invalidServiceUrls.contains(jwsservers))
     {
@@ -947,7 +946,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
    */
   public Jws2Instance getPreferredServiceFor(String[] serviceURLs)
   {
-    HashSet<String> urls = new HashSet<String>();
+    HashSet<String> urls = new HashSet<>();
     urls.addAll(Arrays.asList(serviceURLs));
     Jws2Instance match = null;
     if (services != null)
@@ -974,7 +973,7 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     return match;
   }
 
-  Map<String, Map<String, String>> preferredServiceMap = new HashMap<String, Map<String, String>>();;
+  Map<String, Map<String, String>> preferredServiceMap = new HashMap<>();
 
   /**
    * get current preferred service of the given type, or global default
@@ -1026,12 +1025,12 @@ public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
     String afid = (af == null) ? "" : af.getViewport().getSequenceSetId();
     if (preferredServiceMap == null)
     {
-      preferredServiceMap = new HashMap<String, Map<String, String>>();
+      preferredServiceMap = new HashMap<>();
     }
     Map<String, String> prefmap = preferredServiceMap.get(afid);
     if (prefmap == null)
     {
-      prefmap = new HashMap<String, String>();
+      prefmap = new HashMap<>();
       preferredServiceMap.put(afid, prefmap);
     }
     prefmap.put(serviceType, selectedServer.getHost());