package jalview.ws.slivkaws;
-import jalview.datamodel.AlignmentView;
-import jalview.gui.AlignFrame;
-import jalview.ws.WSMenuEntryProviderI;
-import jalview.ws.jws2.MsaWSClient;
-
-import java.awt.event.ActionEvent;
-import java.io.IOError;
+import jalview.bin.Cache;
+import jalview.ws.ServiceChangeListener;
+import jalview.ws.WSDiscovererI;
+import jalview.ws.api.ServiceWithParameters;
import java.io.IOException;
-import java.net.URISyntaxException;
-
-import javax.swing.JMenu;
-import javax.swing.JMenuItem;
-
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+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 uk.ac.dundee.compbio.slivkaclient.SlivkaClient;
+import uk.ac.dundee.compbio.slivkaclient.SlivkaService;
-public class SlivkaWSDiscoverer
- implements Runnable, WSMenuEntryProviderI
+public class SlivkaWSDiscoverer implements WSDiscovererI
{
+ private static final String SLIVKA_HOST_URLS = "SLIVKAHOSTURLS";
+
+ private static final String COMPBIO_SLIVKA = "https://www.compbio.dundee.ac.uk/slivka/";
+
private static SlivkaWSDiscoverer instance = null;
- private SlivkaClient client;
- private SlivkaWSDiscoverer() {
- try
- {
- client = new SlivkaClient("gjb-www-1.cluster.lifesci.dundee.ac.uk", 3203);
- } catch (URISyntaxException e)
- {
- throw new RuntimeException(e);
- }
+ private List<ServiceWithParameters> services = List.of();
+
+ private SlivkaWSDiscoverer()
+ {
}
public static SlivkaWSDiscoverer getInstance()
{
- if (instance == null) {
- instance = new SlivkaWSDiscoverer();
- }
+ if (instance == null)
+ {
+ instance = new SlivkaWSDiscoverer();
+ }
return instance;
}
+ private Set<ServiceChangeListener> serviceListeners = new CopyOnWriteArraySet<>();
+
@Override
- public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
+ public void addServiceChangeListener(ServiceChangeListener l)
{
- JMenu submenu = new JMenu("Slivka");
+ serviceListeners.add(l);
+ }
- String[] services = { "clustalo", "clustalw", "probcons", "muscle", "mafft", "tcoffee" };
- String[] names = { "ClustalO", "ClustalW2", "Probcons", "Muscle", "Mafft", "TCoffe" };
+ @Override
+ public void removeServiceChangeListener(ServiceChangeListener l)
+ {
+ serviceListeners.remove(l);
+ }
- for (int i = 0; i < services.length; i++)
+ public void notifyServiceListeners(List<ServiceWithParameters> services)
+ {
+ for (var listener : serviceListeners)
{
+ listener.servicesChanged(this, services);
+ }
+ }
+
+ private final ExecutorService executor = Executors.newSingleThreadExecutor();
+ private Vector<Future<?>> discoveryTasks = new Vector<>();
+
+ public CompletableFuture<WSDiscovererI> startDiscoverer()
+ {
+ CompletableFuture<WSDiscovererI> task = CompletableFuture
+ .supplyAsync(() -> {
+ reloadServices();
+ return SlivkaWSDiscoverer.this;
+ }, executor);
+ discoveryTasks.add(task);
+ return task;
+ }
+
+ private List<ServiceWithParameters> reloadServices()
+ {
+ Cache.log.info("Reloading Slivka services");
+ notifyServiceListeners(Collections.emptyList());
+ ArrayList<ServiceWithParameters> instances = new ArrayList<>();
+
+ for (String url : getServiceUrls())
+ {
+ Cache.log.info(url);
+ SlivkaClient client;
+ client = new SlivkaClient(url);
try
{
- SlivkaWSInstance instance = new SlivkaWSInstance(client,
- client.getService(services[i]));
-
- JMenuItem defaultMenuItem = new JMenuItem(
- String.format("%s with defaults", names[i]));
- defaultMenuItem.addActionListener((ActionEvent e) -> {
- AlignmentView msa = alignFrame.gatherSequencesForAlignment();
- if (msa != null)
+ for (SlivkaService service : client.getServices())
+ {
+ SlivkaWSInstance newinstance = null;
+ for (String classifier : service.classifiers)
{
- new MsaWSClient(instance, alignFrame.getTitle(), msa, false, true,
- alignFrame.getViewport().getAlignment().getDataset(),
- alignFrame);
- }
- });
- submenu.add(defaultMenuItem);
-
- JMenuItem customMenuItem = new JMenuItem(
- String.format("%s with custom parameters", names[i]));
- customMenuItem.addActionListener((ActionEvent e) -> {
- AlignmentView msa = alignFrame.gatherSequencesForAlignment();
- if (msa != null)
- {
- try
+ if (classifier.contains("Multiple sequence alignment"))
{
- SlivkaParamSet paramSet = new SlivkaParamSet(
- instance.getService());
- new MsaWSClient(instance, paramSet, null, true,
- alignFrame.getTitle(), msa, false, true,
- alignFrame.getViewport().getAlignment().getDataset(),
- alignFrame);
- } catch (IOException e1)
+ newinstance = new SlivkaMsaServiceInstance(client, service);
+ }
+ if (classifier.contains("Protein sequence analysis")
+ && newinstance == null)
{
- throw new IOError(e1);
+ newinstance = new SlivkaAnnotationServiceInstance(client,
+ service, false);
+ }
+ if (classifier
+ .contains("Sequence alignment analysis (conservation)"))
+ {
+ newinstance = new SlivkaAnnotationServiceInstance(client,
+ service, true);
}
-
}
- });
- submenu.add(customMenuItem);
+ if (newinstance != null)
+ {
+ instances.add(newinstance);
+ }
+ }
} catch (IOException e)
{
- // TODO Auto-generated catch block
e.printStackTrace();
+ continue;
+ }
+ }
+
+ services = instances;
+ notifyServiceListeners(instances);
+ Cache.log.info("Slivka services reloading finished");
+ return instances;
+ }
+
+ @Override
+ public List<ServiceWithParameters> getServices()
+ {
+ return services;
+ }
+
+ @Override
+ public boolean hasServices()
+ {
+ return !isRunning() && services.size() > 0;
+ }
+
+ @Override
+ public boolean isRunning()
+ {
+ return !discoveryTasks.stream().allMatch(Future::isDone);
+ }
+
+ @Override
+ public void setServiceUrls(List<String> wsUrls)
+ {
+ if (wsUrls != null && !wsUrls.isEmpty())
+ {
+ Cache.setProperty(SLIVKA_HOST_URLS, String.join(",", wsUrls));
+ }
+ else
+ {
+ Cache.removeProperty(SLIVKA_HOST_URLS);
+ }
+ }
+
+ @Override
+ public List<String> getServiceUrls()
+ {
+ String surls = Cache.getDefault(SLIVKA_HOST_URLS, COMPBIO_SLIVKA);
+ String[] urls = surls.split(",");
+ ArrayList<String> valid = new ArrayList<>(urls.length);
+ for (String url : urls)
+ {
+ try
+ {
+ new URL(url);
+ valid.add(url);
+ } catch (MalformedURLException e)
+ {
+ Cache.log.warn("Problem whilst trying to make a URL from '"
+ + ((url != null) ? url : "<null>") + "'");
+ Cache.log.warn(
+ "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);
}
}
+ return valid;
+ }
- wsmenu.add(submenu);
+ @Override
+ public boolean testServiceUrl(URL url)
+ {
+ return getServerStatusFor(url.toString()) == STATUS_OK;
}
@Override
- public void run()
+ public int getServerStatusFor(String url)
{
+ try
+ {
+ List<?> services = new SlivkaClient(url).getServices();
+ return services.isEmpty() ? STATUS_NO_SERVICES : STATUS_OK;
+ } catch (IOException e)
+ {
+ Cache.log.error("Slivka could not retrieve services list", e);
+ return STATUS_INVALID;
+ }
+ }
+ @Override
+ public String getErrorMessages()
+ {
+ // TODO Auto-generated method stub
+ return "";
}
}