X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2Fslivkaws%2FSlivkaWSDiscoverer.java;h=d290024937c859f62319382be3a0ad8060c4e6a7;hb=95f4ae66fbaecbbdc1c3ab3c6c9c611a3a9a3e46;hp=7a41431ba8482970085d103b438d50344573f850;hpb=076b3b34bc3e02a033213cdac4300444e3c57b74;p=jalview.git diff --git a/src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java b/src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java index 7a41431..d290024 100644 --- a/src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java +++ b/src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java @@ -1,21 +1,24 @@ package jalview.ws.slivkaws; import jalview.bin.Cache; -import jalview.gui.AlignFrame; +import jalview.ws.ServiceChangeListener; import jalview.ws.WSDiscovererI; import jalview.ws.api.ServiceWithParameters; -import jalview.ws.jws2.PreferredServiceRegistry; - -import java.beans.PropertyChangeListener; -import java.beans.PropertyChangeSupport; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; +import java.util.Collections; import java.util.List; - -import javax.swing.JMenu; - +import java.util.Set; +import java.util.Vector; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import compbio.data.msa.Category; import uk.ac.dundee.compbio.slivkaclient.SlivkaClient; import uk.ac.dundee.compbio.slivkaclient.SlivkaService; @@ -42,124 +45,104 @@ public class SlivkaWSDiscoverer implements WSDiscovererI return instance; } - private PropertyChangeSupport changeSupport = new PropertyChangeSupport( - this); + private Set serviceListeners = new CopyOnWriteArraySet<>(); @Override - public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame) + public void addServiceChangeListener(ServiceChangeListener l) { - JMenu slivkaMenu = new JMenu("Slivka"); - wsmenu.add(slivkaMenu); - - JMenu alignmentMenu = new JMenu("Sequence Alignment"); - slivkaMenu.add(alignmentMenu); - JMenu disorderMenu = new JMenu("Protein sequence analysis"); - slivkaMenu.add(disorderMenu); - JMenu conservationMenu = new JMenu("Conservation"); - slivkaMenu.add(conservationMenu); - PreferredServiceRegistry.getRegistry().populateWSMenuEntry(services, - changeSupport, slivkaMenu, alignFrame, null); - + serviceListeners.add(l); } - volatile boolean ready = false; - - volatile Thread discovererThread = null; - - private class DiscovererThread extends Thread + @Override + public void removeServiceChangeListener(ServiceChangeListener l) { - private Thread oldThread; - - DiscovererThread(Thread oldThread) - { - super(); - this.oldThread = oldThread; - } + serviceListeners.remove(l); + } - @Override - public void run() + public void notifyServiceListeners(List services) + { + for (var listener : serviceListeners) { - if (oldThread != null) - { - oldThread.interrupt(); - try - { - oldThread.join(); - } catch (InterruptedException e) - { - return; - } finally - { - oldThread = null; - } - } - ready = false; - reloadServices(); - ready = !isInterrupted(); + listener.servicesChanged(this, services); } } - Thread discoverer = null; + private final ExecutorService executor = Executors + .newSingleThreadExecutor(); - @Override - public Thread startDiscoverer(PropertyChangeListener changeListener) + private Vector> discoveryTasks = new Vector<>(); + + public CompletableFuture startDiscoverer() { - changeSupport.addPropertyChangeListener(changeListener); - ready = false; - (discovererThread = new DiscovererThread(discovererThread)).start(); - return discovererThread; + CompletableFuture task = CompletableFuture + .supplyAsync(() -> { + reloadServices(); + return SlivkaWSDiscoverer.this; + }, executor); + discoveryTasks.add(task); + return task; } - private void reloadServices() + private List reloadServices() { Cache.log.info("Reloading Slivka services"); - changeSupport.firePropertyChange("services", services, List.of()); + notifyServiceListeners(Collections.emptyList()); ArrayList instances = new ArrayList<>(); for (String url : getServiceUrls()) { - Cache.log.info(url); - SlivkaClient client; - client = new SlivkaClient(url); + SlivkaClient client = new SlivkaClient(url); + + List services; try { - for (SlivkaService service : client.getServices()) + services = client.getServices(); + } catch (IOException e) + { + e.printStackTrace(); + continue; + } + for (SlivkaService service : services) + { + SlivkaWSInstance newInstance = null; + for (String classifier : service.classifiers) { - SlivkaWSInstance newinstance = null; - for (String classifier : service.classifiers) + String[] path = classifier.split("\\s*::\\s*"); + if (path.length >= 3 && path[0].toLowerCase().equals("operation") + && path[1].toLowerCase().equals("analysis")) { - if (classifier.contains("Multiple sequence alignment")) - { - newinstance = new SlivkaMsaServiceInstance(client, service); - } - if (classifier.contains("Protein sequence analysis") - && newinstance == null) - { - newinstance = new SlivkaAnnotationServiceInstance(client, - service, false); - } - if (classifier - .contains("Sequence alignment analysis (conservation)")) + switch (path[path.length - 1].toLowerCase()) { - newinstance = new SlivkaAnnotationServiceInstance(client, - service, true); + case "sequence alignment analysis (conservation)": + newInstance = new SlivkaAnnotationServiceInstance(client, + service, Category.CATEGORY_CONSERVATION); + break; + case "protein sequence analysis": + newInstance = new SlivkaAnnotationServiceInstance(client, + service, Category.CATEGORY_DISORDER); + break; + case "protein secondary structure prediction": + newInstance = new SlivkaJPredServiceInstance(client, + service, "Secondary Structure Prediction"); + break; + case "multiple sequence alignment": + newInstance = new SlivkaMsaServiceInstance(client, service, + Category.CATEGORY_ALIGNMENT); + break; } } - if (newinstance != null) - { - instances.add(newinstance); - } + if (newInstance != null) + break; } - } catch (IOException e) - { - continue; + if (newInstance != null) + instances.add(newInstance); } } services = instances; - changeSupport.firePropertyChange("services", List.of(), services); - Cache.log.info("Slivka services reloading finished"); + notifyServiceListeners(instances); + return instances; } @Override @@ -171,14 +154,13 @@ public class SlivkaWSDiscoverer implements WSDiscovererI @Override public boolean hasServices() { - return ready == true && services.size() > 0; + return !isRunning() && services.size() > 0; } @Override public boolean isRunning() { - return discovererThread == null || discovererThread.isAlive() - || discovererThread.getState() == Thread.State.NEW; + return !discoveryTasks.stream().allMatch(Future::isDone); } @Override @@ -209,11 +191,11 @@ public class SlivkaWSDiscoverer implements WSDiscovererI } catch (MalformedURLException e) { Cache.log.warn("Problem whilst trying to make a URL from '" - + ((url != null) ? url : "") + "'"); + + ((url != null) ? url : "") + "'"); Cache.log.warn( - "This was probably due to a malformed comma separated list" - + " in the " + SLIVKA_HOST_URLS - + " entry of $(HOME)/.jalview_properties)"); + "This was probably due to a malformed comma separated list" + + " in the " + SLIVKA_HOST_URLS + + " entry of $(HOME)/.jalview_properties)"); Cache.log.debug("Exception was ", e); } }