1 package jalview.ws2.slivka;
3 import java.io.IOException;
4 import java.net.MalformedURLException;
7 import java.util.concurrent.*;
9 import jalview.bin.Cache;
11 import jalview.ws2.operations.AlignmentOperation;
12 import jalview.ws2.operations.Operation;
13 import uk.ac.dundee.compbio.slivkaclient.SlivkaClient;
14 import uk.ac.dundee.compbio.slivkaclient.SlivkaService;
16 public class SlivkaWSDiscoverer implements WebServiceDiscoverer
18 private static final String SLIVKA_HOST_URLS = "SLIVKSHOSTURLS";
20 private static final String DEFAULT_URL = "https://www.compbio.dundee.ac.uk/slivka/";
22 private static SlivkaWSDiscoverer instance = null;
24 private List<WebServiceI> services = List.of();
26 private SlivkaWSDiscoverer()
30 public static SlivkaWSDiscoverer getInstance()
34 instance = new SlivkaWSDiscoverer();
40 public List<String> getUrls()
42 String surls = Cache.getDefault(SLIVKA_HOST_URLS, DEFAULT_URL);
43 String urls[] = surls.split(",");
44 ArrayList<String> valid = new ArrayList<>(urls.length);
45 for (String url : urls)
51 } catch (MalformedURLException e)
53 Cache.log.warn("Problem whilst trying to make a URL from '"
54 + Objects.toString(url, "<null>") + "'. "
55 + "This was probably due to malformed comma-separated-list "
56 + "in the " + SLIVKA_HOST_URLS
57 + " entry of ${HOME}/.jalview_properties");
58 Cache.log.debug("Exception occurred while reading url list", e);
65 public void setUrls(List<String> wsUrls)
67 if (wsUrls != null && !wsUrls.isEmpty())
69 Cache.setProperty(SLIVKA_HOST_URLS, String.join(",", wsUrls));
73 Cache.removeProperty(SLIVKA_HOST_URLS);
78 public boolean testUrl(URL url)
80 return getStatusForUrl(url.toString()) == STATUS_OK;
84 public int getStatusForUrl(String url)
88 List<?> services = new SlivkaClient(url).getServices();
89 return services.isEmpty() ? STATUS_NO_SERVICES : STATUS_OK;
90 } catch (IOException e)
92 Cache.log.error("Slivka could not retrieve services list from " + url,
94 return STATUS_INVALID;
98 public List<WebServiceI> getServices()
100 return Collections.unmodifiableList(services);
103 public boolean hasServices()
105 return !isRunning() && services.size() > 0;
108 public boolean isRunning()
110 for (Future<?> task : discoveryTasks)
120 public boolean isDone()
122 return !isRunning() && discoveryTasks.size() > 0;
125 private Vector<Future<?>> discoveryTasks = new Vector<>();
128 public CompletableFuture<WebServiceDiscoverer> startDiscoverer()
130 CompletableFuture<WebServiceDiscoverer> task = CompletableFuture
133 return SlivkaWSDiscoverer.this;
135 discoveryTasks.add(task);
139 private List<WebServiceI> reloadServices()
141 Cache.log.info("Reloading Slivka services");
142 fireServicesChanged(Collections.emptyList());
143 ArrayList<WebServiceI> allServices = new ArrayList<>();
144 for (String url : getUrls())
146 SlivkaClient client = new SlivkaClient(url);
147 List<SlivkaService> services;
150 services = client.getServices();
151 } catch (IOException e)
153 Cache.log.error("Unable to fetch services from " + url, e);
156 for (SlivkaService service : services)
158 SlivkaWebService instance = new SlivkaWebService(client, service,
160 for (String classifier : service.classifiers)
162 String[] path = classifier.split("\\s*::\\s*");
163 if (path.length >= 3 && path[0].toLowerCase().equals("operation")
164 && path[1].toLowerCase().equals("analysis"))
167 switch (path[path.length - 1].toLowerCase())
169 case "multiple sequence alignment":
170 op = new AlignmentOperation(instance, instance::getAlignment);
173 instance.addOperation(op);
176 if (instance.operations.size() > 0)
178 allServices.add(instance);
182 this.services = allServices;
183 Cache.log.info("Reloading slivka services finished");
184 fireServicesChanged(getServices());
189 public String getErrorMessages()