import jalview.bin.Cache;
import jalview.gui.AlignFrame;
import jalview.util.MessageManager;
-import jalview.ws.WSMenuEntryProviderI;
+import jalview.ws.ServiceChangeListener;
+import jalview.ws.WSDiscovererI;
import jalview.ws.api.ServiceWithParameters;
import jalview.ws.jws2.jabaws2.Jws2Instance;
import jalview.ws.params.ParamDatastoreI;
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.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 Runnable, WSMenuEntryProviderI
+public class Jws2Discoverer implements WSDiscovererI, Runnable
{
public static final String COMPBIO_JABAWS = "http://www.compbio.dundee.ac.uk/jabaws";
// 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 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(
} catch (Exception e)
{
}
- ;
for (JabaWsServerQuery squery : qrys)
{
if (squery.isRunning())
}
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
+ *
+ * @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)
{
if (args.length > 0)
{
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 = getDiscoverer();
+ discoverer.addServiceChangeListener((_discoverer, _services) -> {
+ if (discoverer.services != null)
{
+ System.out.println("Changesupport: There are now "
+ + discoverer.services.size() + " services");
+ int i = 1;
+ for (ServiceWithParameters instance : discoverer.services)
+ {
+ System.out.println(
+ "Service " + i++ + " " + instance.getClass()
+ + "@" + instance.getHostURL() + ": "
+ + instance.getActionText());
+ }
+
}
- ;
+ });
+ try
+ {
+ discoverer.startDiscoverer().get();
+ } catch (InterruptedException | ExecutionException e)
+ {
}
try
{
return discoverer;
}
+ @Override
public boolean hasServices()
{
return !running && services != null && services.size() > 0;
}
+ @Override
public boolean isRunning()
{
return running;
}
+ @Override
public void setServiceUrls(List<String> wsUrls)
{
if (wsUrls != null && !wsUrls.isEmpty())
*
* @return
*/
+ @Override
public List<String> getServiceUrls()
{
if (testUrls != null)
return urls;
}
+ @Override
public Vector<ServiceWithParameters> getServices()
{
- return (services == null) ? new Vector<>()
- : new Vector<>(services);
+ return (services == null) ? new Vector<>() : new Vector<>(services);
}
/**
* @param foo
* @return
*/
- public static boolean testServiceUrl(URL foo)
+ @Override
+ public boolean testServiceUrl(URL foo)
{
try
{
* @param changeSupport2
* @return new thread
*/
- public Thread startDiscoverer(PropertyChangeListener changeSupport2)
+ @Override
+ public FutureTask<WSDiscovererI> startDiscoverer()
{
/* if (restart())
{
{
setAborted(true);
}
- addPropertyChangeListener(changeSupport2);
- Thread thr = new Thread(this);
- thr.start();
- return thr;
+ FutureTask<WSDiscovererI> task = new FutureTask<>(this, this);
+ new Thread(task).start();
+ return task;
}
/**
* @return a human readable report of any problems with the service URLs used
* for discovery
*/
+ @Override
public String getErrorMessages()
{
if (!isRunning() && !isAborted())
return null;
}
+ @Override
public int getServerStatusFor(String url)
{
if (validServiceUrls != null && validServiceUrls.contains(url))
{
- return 1;
+ return STATUS_OK;
}
if (urlsWithoutServices != null && urlsWithoutServices.contains(url))
{
- return 0;
+ return STATUS_NO_SERVICES;
}
if (invalidServiceUrls != null && invalidServiceUrls.contains(url))
{
- return -1;
+ return STATUS_INVALID;
}
- return -2;
+ return STATUS_UNKNOWN;
}
/**