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;
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;
* @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";
/*
private final static String JWS2HOSTURLS = "JWS2HOSTURLS";
/*
- * Singleton instance
- */
- private static Jws2Discoverer discoverer;
-
- /*
* Override for testing only
*/
- private static List<String> testUrls = null;
+ private List<String> testUrls = null;
// preferred url has precedence over others
private String preferredUrl;
-
- private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
- this);
-
+
+ private Set<ServiceChangeListener> serviceListeners = new CopyOnWriteArraySet<>();
private Vector<String> invalidServiceUrls = null;
private Vector<String> urlsWithoutServices = null;
private volatile boolean running = false;
private volatile boolean aborted = false;
-
- private Thread oldthread = null;
+
+ private volatile Thread oldthread = null;
/**
* holds list of services.
{
}
- /**
- * 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<? extends ServiceWithParameters> services)
+ {
+ if (services == null) services = this.services;
+ for (var listener : serviceListeners) {
+ listener.servicesChanged(this, services);
+ }
}
/**
ignoredServices.add(ignored);
}
- changeSupport.firePropertyChange("services", services,
- new Vector<Jws2Instance>());
+ 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(
}
oldthread = null;
running = false;
- changeSupport.firePropertyChange("services", new Vector<Jws2Instance>(),
- services);
+ notifyServiceListeners(services);
}
/**
}
/**
- * attach all available web services to the appropriate submenu in the given
- * JMenu
- */
- @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);
- }
-
- /**
*
* @param args
* @j2sIgnore
*/
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
{
}
}
- /**
- * Returns the singleton instance of this class.
- *
- * @return
- */
- public static Jws2Discoverer getDiscoverer()
- {
- if (discoverer == null)
- {
- discoverer = new Jws2Discoverer();
- }
- return discoverer;
- }
@Override
public boolean hasServices()
@Override
public Vector<ServiceWithParameters> getServices()
{
- return (services == null) ? new Vector<>()
- : new Vector<>(services);
+ return (services == null) ? new Vector<>() : new Vector<>(services);
}
/**
* @return new thread
*/
@Override
- public Thread startDiscoverer(PropertyChangeListener changeSupport2)
+ public CompletableFuture<WSDiscovererI> startDiscoverer()
{
/* if (restart())
{
{
setAborted(true);
}
- addPropertyChangeListener(changeSupport2);
- Thread thr = new Thread(this);
- thr.start();
- return thr;
+ CompletableFuture<WSDiscovererI> task = CompletableFuture
+ .supplyAsync(() -> {
+ run();
+ return Jws2Discoverer.this;
+ });
+ return task;
}
/**