2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.jws2;
23 import jalview.bin.Cache;
24 import jalview.bin.Console;
25 import jalview.bin.ApplicationSingletonProvider;
26 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
27 import jalview.gui.AlignFrame;
28 import jalview.util.MessageManager;
29 import jalview.ws.ServiceChangeListener;
30 import jalview.ws.WSDiscovererI;
31 import jalview.ws.api.ServiceWithParameters;
32 import jalview.ws.jws2.jabaws2.Jws2Instance;
33 import jalview.ws.params.ParamDatastoreI;
35 import java.beans.PropertyChangeEvent;
36 import java.beans.PropertyChangeListener;
37 import java.beans.PropertyChangeSupport;
38 import java.net.MalformedURLException;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.HashSet;
43 import java.util.List;
45 import java.util.StringTokenizer;
46 import java.util.Vector;
47 import java.util.concurrent.CompletableFuture;
48 import java.util.concurrent.CopyOnWriteArraySet;
49 import java.util.concurrent.ExecutionException;
50 import java.util.concurrent.Future;
51 import java.util.concurrent.FutureTask;
53 import javax.swing.JMenu;
55 import compbio.ws.client.Services;
58 * discoverer for jws2 services. Follows the lightweight service discoverer
59 * pattern (archetyped by EnfinEnvision2OneWay)
64 public class Jws2Discoverer implements WSDiscovererI, Runnable, ApplicationSingletonI
67 * Returns the singleton instance of this class.
71 public static Jws2Discoverer getInstance()
73 return ApplicationSingletonProvider.getInstance(Jws2Discoverer.class);
75 public static final String COMPBIO_JABAWS = "http://www.compbio.dundee.ac.uk/jabaws";
78 * the .jalview_properties entry for JWS2 URLS
80 private final static String JWS2HOSTURLS = "JWS2HOSTURLS";
83 * Override for testing only
85 private List<String> testUrls = null;
87 // preferred url has precedence over others
88 private String preferredUrl;
91 private Set<ServiceChangeListener> serviceListeners = new CopyOnWriteArraySet<>();
93 private Vector<String> invalidServiceUrls = null;
95 private Vector<String> urlsWithoutServices = null;
97 private Vector<String> validServiceUrls = null;
99 private volatile boolean running = false;
101 private volatile boolean aborted = false;
104 private volatile Thread oldthread = null;
107 * holds list of services.
109 protected Vector<Jws2Instance> services;
112 * Private constructor enforces use of singleton via getDiscoverer()
114 private Jws2Discoverer()
119 public void addServiceChangeListener(ServiceChangeListener listener)
121 serviceListeners.add(listener);
125 public void removeServiceChangeListener(ServiceChangeListener listener)
127 serviceListeners.remove(listener);
130 private void notifyServiceListeners(List<? extends ServiceWithParameters> services)
132 if (services == null) services = this.services;
133 for (var listener : serviceListeners) {
134 listener.servicesChanged(this, services);
139 * @return the aborted
141 public boolean isAborted()
150 public void setAborted(boolean aborted)
152 this.aborted = aborted;
159 if (running && oldthread != null && oldthread.isAlive())
170 "Waiting around for old discovery thread to finish.");
171 // wait around until old discoverer dies
173 } catch (Exception e)
178 Console.debug("Old discovery thread has finished.");
182 // first set up exclusion list if needed
183 final Set<String> ignoredServices = new HashSet<>();
184 for (String ignored : Cache
185 .getDefault("IGNORED_JABAWS_SERVICETYPES", "").split("\\|"))
187 ignoredServices.add(ignored);
190 notifyServiceListeners(Collections.emptyList());
191 oldthread = Thread.currentThread();
194 getClass().getClassLoader().loadClass("compbio.ws.client.Jws2Client");
195 } catch (ClassNotFoundException e)
198 "Not enabling JABA Webservices : client jar is not available."
199 + "\nPlease check that your webstart JNLP file is up to date!");
203 // reinitialise records of good and bad service URLs
204 if (services != null)
206 services.removeAllElements();
208 if (urlsWithoutServices != null)
210 urlsWithoutServices.removeAllElements();
212 if (invalidServiceUrls != null)
214 invalidServiceUrls.removeAllElements();
216 if (validServiceUrls != null)
218 validServiceUrls.removeAllElements();
220 ArrayList<String> svctypes = new ArrayList<>();
222 List<JabaWsServerQuery> qrys = new ArrayList<>();
223 for (final String jwsserver : getServiceUrls())
225 JabaWsServerQuery squery = new JabaWsServerQuery(this, jwsserver);
226 if (svctypes.size() == 0)
228 // TODO: remove this ugly hack to get Canonical JABA service ordering
229 // for all possible services
230 for (Services sv : squery.JABAWS2SERVERS)
232 if (!ignoredServices.contains(sv.toString()))
234 svctypes.add(sv.toString());
240 new Thread(squery).start();
242 boolean finished = true;
249 } catch (Exception e)
252 for (JabaWsServerQuery squery : qrys)
254 if (squery.isRunning())
262 "Aborting " + qrys.size() + " JABAWS discovery threads.");
263 for (JabaWsServerQuery squery : qrys)
265 squery.setQuit(true);
268 } while (!aborted && !finished);
271 // resort services according to order found in jabaws service list
272 // also ensure services for each host are ordered in same way.
274 if (services != null && services.size() > 0)
276 Jws2Instance[] svcs = new Jws2Instance[services.size()];
277 int[] spos = new int[services.size()];
279 List<String> svcUrls = getServiceUrls();
280 for (Jws2Instance svc : services)
283 spos[ipos++] = 1000 * svcUrls.indexOf(svc.getHostURL()) + 1
284 + svctypes.indexOf(svc.getName());
286 jalview.util.QuickSort.sort(spos, svcs);
287 services = new Vector<>();
288 for (Jws2Instance svc : svcs)
290 if (!ignoredServices.contains(svc.getName()))
299 notifyServiceListeners(services);
303 * record this service endpoint so we can use it
309 synchronized void addService(String jwsservers, Jws2Instance service)
311 if (services == null)
313 services = new Vector<>();
316 "Discovered service: " + jwsservers + " " + service.toString());
317 // Jws2Instance service = new Jws2Instance(jwsservers, srv.toString(),
320 services.add(service);
321 // retrieve the presets and parameter set and cache now
322 ParamDatastoreI pds = service.getParamStore();
327 service.hasParameters();
328 if (validServiceUrls == null)
330 validServiceUrls = new Vector<>();
332 validServiceUrls.add(jwsservers);
340 public static void main(String[] args)
342 Jws2Discoverer instance = getInstance();
345 instance.testUrls = new ArrayList<>();
346 for (String url : args)
348 instance.testUrls.add(url);
351 var discoverer = getInstance();
352 discoverer.addServiceChangeListener((_discoverer, _services) -> {
353 if (discoverer.services != null)
355 System.out.println("Changesupport: There are now "
356 + discoverer.services.size() + " services");
358 for (ServiceWithParameters s_instance : discoverer.services)
361 "Service " + i++ + " " + s_instance.getClass()
362 + "@" + s_instance.getHostURL() + ": "
363 + s_instance.getActionText());
370 discoverer.startDiscoverer().get();
371 } catch (InterruptedException | ExecutionException e)
377 } catch (InterruptedException x)
384 public boolean hasServices()
386 return !running && services != null && services.size() > 0;
390 public boolean isRunning()
396 public void setServiceUrls(List<String> wsUrls)
398 if (wsUrls != null && !wsUrls.isEmpty())
400 StringBuilder urls = new StringBuilder(128);
402 for (String url : wsUrls)
408 Cache.setProperty(JWS2HOSTURLS, urls.toString());
412 Cache.removeProperty(JWS2HOSTURLS);
417 * Returns web service URLs, in the order in which they should be tried (or an
423 public List<String> getServiceUrls()
425 if (testUrls != null)
427 // return test urls, if there are any, instead of touching cache
430 List<String> urls = new ArrayList<>();
432 if (this.preferredUrl != null)
434 urls.add(preferredUrl);
437 String surls = Cache.getDefault(JWS2HOSTURLS, COMPBIO_JABAWS);
440 StringTokenizer st = new StringTokenizer(surls, ",");
441 while (st.hasMoreElements())
446 url = st.nextToken();
448 if (!urls.contains(url))
454 Console.warn("Ignoring duplicate url " + url + " in "
455 + JWS2HOSTURLS + " list");
457 } catch (MalformedURLException ex)
459 Console.warn("Problem whilst trying to make a URL from '"
460 + ((url != null) ? url : "<null>") + "'");
462 "This was probably due to a malformed comma separated list"
463 + " in the " + JWS2HOSTURLS
464 + " entry of $(HOME)/.jalview_properties)");
465 Console.debug("Exception was ", ex);
468 } catch (Exception ex)
470 Console.warn("Error parsing comma separated list of urls in "
471 + JWS2HOSTURLS + " preference.", ex);
477 public Vector<ServiceWithParameters> getServices()
479 return (services == null) ? new Vector<>() : new Vector<>(services);
483 * test the given URL with the JabaWS test code
489 public boolean testServiceUrl(URL foo)
493 compbio.ws.client.WSTester
495 { "-h=" + foo.toString() });
496 } catch (Exception e)
500 } catch (OutOfMemoryError e)
513 public boolean restart()
530 * Start a fresh discovery thread and notify the given object when we're
531 * finished. Any known existing threads will be killed before this one is
534 * @param changeSupport2
538 public CompletableFuture<WSDiscovererI> startDiscoverer()
546 Thread thr = new Thread(this);
554 CompletableFuture<WSDiscovererI> task = CompletableFuture
557 return Jws2Discoverer.this;
563 * @return the invalidServiceUrls
565 public Vector<String> getInvalidServiceUrls()
567 return invalidServiceUrls;
571 * @return the urlsWithoutServices
573 public Vector<String> getUrlsWithoutServices()
575 return urlsWithoutServices;
579 * add an 'empty' JABA server to the list. Only servers not already in the
580 * 'bad URL' list will be added to this list.
584 public synchronized void addUrlwithnoservices(String jwsservers)
586 if (urlsWithoutServices == null)
588 urlsWithoutServices = new Vector<>();
591 if ((invalidServiceUrls == null
592 || !invalidServiceUrls.contains(jwsservers))
593 && !urlsWithoutServices.contains(jwsservers))
595 urlsWithoutServices.add(jwsservers);
600 * add a bad URL to the list
604 public synchronized void addInvalidServiceUrl(String jwsservers)
606 if (invalidServiceUrls == null)
608 invalidServiceUrls = new Vector<>();
610 if (!invalidServiceUrls.contains(jwsservers))
612 invalidServiceUrls.add(jwsservers);
618 * @return a human readable report of any problems with the service URLs used
622 public String getErrorMessages()
624 if (!isRunning() && !isAborted())
626 StringBuffer ermsg = new StringBuffer();
627 boolean list = false;
628 if (getInvalidServiceUrls() != null
629 && getInvalidServiceUrls().size() > 0)
631 ermsg.append(MessageManager.getString("warn.urls_not_contacted")
633 for (String svcurl : getInvalidServiceUrls())
640 ermsg.append(svcurl);
642 ermsg.append("\n\n");
645 if (getUrlsWithoutServices() != null
646 && getUrlsWithoutServices().size() > 0)
649 MessageManager.getString("warn.urls_no_jaba") + ": \n");
650 for (String svcurl : getUrlsWithoutServices())
657 ermsg.append(svcurl);
661 if (ermsg.length() > 1)
663 return ermsg.toString();
671 public int getServerStatusFor(String url)
673 if (validServiceUrls != null && validServiceUrls.contains(url))
677 if (urlsWithoutServices != null && urlsWithoutServices.contains(url))
679 return STATUS_NO_SERVICES;
681 if (invalidServiceUrls != null && invalidServiceUrls.contains(url))
683 return STATUS_INVALID;
685 return STATUS_UNKNOWN;
689 * Set a URL to try before any others. For use with command-line parameter to
690 * configure a local Jabaws installation without the need to add to property
694 * @throws MalformedURLException
696 public void setPreferredUrl(String value) throws MalformedURLException
698 if (value != null && value.trim().length() > 0)
701 preferredUrl = value;