X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2Fjws2%2FJws2Discoverer.java;fp=src%2Fjalview%2Fws%2Fjws2%2FJws2Discoverer.java;h=d391f8b379a4bfa8cbaf006f8a4e48fb879e91ac;hb=4898f0ae429e0c61ddba72ca46be89b34bb4df8b;hp=197d0ee9efad9fd066abab00ad60b1b1baa789ae;hpb=5a6ac5b535856903629234ad43a71319a91ebee5;p=jalview.git diff --git a/src/jalview/ws/jws2/Jws2Discoverer.java b/src/jalview/ws/jws2/Jws2Discoverer.java index 197d0ee..d391f8b 100644 --- a/src/jalview/ws/jws2/Jws2Discoverer.java +++ b/src/jalview/ws/jws2/Jws2Discoverer.java @@ -21,8 +21,11 @@ package jalview.ws.jws2; import jalview.bin.Cache; +import jalview.bin.ApplicationSingletonProvider; +import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI; import jalview.gui.AlignFrame; import jalview.util.MessageManager; +import jalview.ws.ServiceChangeListener; import jalview.ws.WSDiscovererI; import jalview.ws.api.ServiceWithParameters; import jalview.ws.jws2.jabaws2.Jws2Instance; @@ -34,11 +37,17 @@ import java.beans.PropertyChangeSupport; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.StringTokenizer; import java.util.Vector; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; import javax.swing.JMenu; @@ -51,8 +60,20 @@ import compbio.ws.client.Services; * @author JimP * */ -public class Jws2Discoverer implements WSDiscovererI, Runnable +public class Jws2Discoverer implements WSDiscovererI, Runnable, ApplicationSingletonI { + + /** + * Returns the singleton instance of this class. + * + * @return + */ + public static Jws2Discoverer getInstance() + { + return (Jws2Discoverer) ApplicationSingletonProvider + .getInstance(Jws2Discoverer.class); + } + public static final String COMPBIO_JABAWS = "http://www.compbio.dundee.ac.uk/jabaws"; /* @@ -61,21 +82,14 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable private final static String JWS2HOSTURLS = "JWS2HOSTURLS"; /* - * Singleton instance - */ - private static Jws2Discoverer discoverer; - - /* * Override for testing only */ - private static List testUrls = null; + private List testUrls = null; // preferred url has precedence over others private String preferredUrl; - - private PropertyChangeSupport changeSupport = new PropertyChangeSupport( - this); - + + private Set serviceListeners = new CopyOnWriteArraySet<>(); private Vector invalidServiceUrls = null; private Vector urlsWithoutServices = null; @@ -85,8 +99,8 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable private volatile boolean running = false; private volatile boolean aborted = false; - - private Thread oldthread = null; + + private volatile Thread oldthread = null; /** * holds list of services. @@ -100,28 +114,25 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable { } - /** - * change listeners are notified of "services" property changes - * - * @param listener - * to be added that consumes new services Hashtable object. - */ - public void addPropertyChangeListener( - java.beans.PropertyChangeListener listener) + + @Override + public void addServiceChangeListener(ServiceChangeListener listener) { - changeSupport.addPropertyChangeListener(listener); + serviceListeners.add(listener); } - /** - * - * - * @param listener - * to be removed - */ - public void removePropertyChangeListener( - java.beans.PropertyChangeListener listener) + @Override + public void removeServiceChangeListener(ServiceChangeListener listener) { - changeSupport.removePropertyChangeListener(listener); + serviceListeners.remove(listener); + } + + private void notifyServiceListeners(List services) + { + if (services == null) services = this.services; + for (var listener : serviceListeners) { + listener.servicesChanged(this, services); + } } /** @@ -176,13 +187,11 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable ignoredServices.add(ignored); } - changeSupport.firePropertyChange("services", services, - new Vector()); + notifyServiceListeners(Collections.emptyList()); oldthread = Thread.currentThread(); try { - Class foo = getClass().getClassLoader() - .loadClass("compbio.ws.client.Jws2Client"); + getClass().getClassLoader().loadClass("compbio.ws.client.Jws2Client"); } catch (ClassNotFoundException e) { System.err.println( @@ -240,7 +249,6 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable } catch (Exception e) { } - ; for (JabaWsServerQuery squery : qrys) { if (squery.isRunning()) @@ -288,8 +296,7 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable } oldthread = null; running = false; - changeSupport.firePropertyChange("services", new Vector(), - services); + notifyServiceListeners(services); } /** @@ -326,72 +333,43 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable } /** - * attach all available web services to the appropriate submenu in the given - * JMenu + * + * @param args + * @j2sIgnore */ - @Override - public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame) - { - if (running || services == null || services.size() == 0) - { - return; - } - // dynamically regenerate service list. - populateWSMenuEntry(wsmenu, alignFrame, null); - } - - private void populateWSMenuEntry(JMenu jws2al, - final AlignFrame alignFrame, String typeFilter) - { - PreferredServiceRegistry.getRegistry().populateWSMenuEntry( - getServices(), - changeSupport, jws2al, - alignFrame, typeFilter); - } - public static void main(String[] args) { + Jws2Discoverer instance = getInstance(); if (args.length > 0) { - testUrls = new ArrayList<>(); + instance.testUrls = new ArrayList<>(); for (String url : args) { - testUrls.add(url); + instance.testUrls.add(url); } - ; - } - Thread runner = getDiscoverer() - .startDiscoverer(new PropertyChangeListener() - { - - @Override - public void propertyChange(PropertyChangeEvent evt) - { - if (getDiscoverer().services != null) - { - System.out.println("Changesupport: There are now " - + getDiscoverer().services.size() + " services"); - int i = 1; - for (ServiceWithParameters instance : getDiscoverer().services) - { - System.out.println("Service " + i++ + " " - + instance.getClass() + "@" - + instance.getHostURL() - + ": " + instance.getActionText()); - } - - } - } - }); - while (runner.isAlive()) - { - try - { - Thread.sleep(50); - } catch (InterruptedException e) + } + var discoverer = getInstance(); + discoverer.addServiceChangeListener((_discoverer, _services) -> { + if (discoverer.services != null) { + System.out.println("Changesupport: There are now " + + discoverer.services.size() + " services"); + int i = 1; + for (ServiceWithParameters s_instance : discoverer.services) + { + System.out.println( + "Service " + i++ + " " + s_instance.getClass() + + "@" + s_instance.getHostURL() + ": " + + s_instance.getActionText()); + } + } - ; + }); + try + { + discoverer.startDiscoverer().get(); + } catch (InterruptedException | ExecutionException e) + { } try { @@ -401,19 +379,6 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable } } - /** - * Returns the singleton instance of this class. - * - * @return - */ - public static Jws2Discoverer getDiscoverer() - { - if (discoverer == null) - { - discoverer = new Jws2Discoverer(); - } - return discoverer; - } @Override public boolean hasServices() @@ -511,8 +476,7 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable @Override public Vector getServices() { - return (services == null) ? new Vector<>() - : new Vector<>(services); + return (services == null) ? new Vector<>() : new Vector<>(services); } /** @@ -571,7 +535,7 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable * @return new thread */ @Override - public Thread startDiscoverer(PropertyChangeListener changeSupport2) + public CompletableFuture startDiscoverer() { /* if (restart()) { @@ -587,10 +551,12 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable { setAborted(true); } - addPropertyChangeListener(changeSupport2); - Thread thr = new Thread(this); - thr.start(); - return thr; + CompletableFuture task = CompletableFuture + .supplyAsync(() -> { + run(); + return Jws2Discoverer.this; + }); + return task; } /**