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 (Jws2Discoverer) ApplicationSingletonProvider
74 .getInstance(Jws2Discoverer.class);
76 public static final String COMPBIO_JABAWS = "http://www.compbio.dundee.ac.uk/jabaws";
79 * the .jalview_properties entry for JWS2 URLS
81 private final static String JWS2HOSTURLS = "JWS2HOSTURLS";
84 * Override for testing only
86 private List<String> testUrls = null;
88 // preferred url has precedence over others
89 private String preferredUrl;
92 private Set<ServiceChangeListener> serviceListeners = new CopyOnWriteArraySet<>();
94 private Vector<String> invalidServiceUrls = null;
96 private Vector<String> urlsWithoutServices = null;
98 private Vector<String> validServiceUrls = null;
100 private volatile boolean running = false;
102 private volatile boolean aborted = false;
105 private volatile Thread oldthread = null;
108 * holds list of services.
110 protected Vector<Jws2Instance> services;
113 * Private constructor enforces use of singleton via getDiscoverer()
115 private Jws2Discoverer()
120 public void addServiceChangeListener(ServiceChangeListener listener)
122 serviceListeners.add(listener);
126 public void removeServiceChangeListener(ServiceChangeListener listener)
128 serviceListeners.remove(listener);
131 private void notifyServiceListeners(List<? extends ServiceWithParameters> services)
133 if (services == null) services = this.services;
134 for (var listener : serviceListeners) {
135 listener.servicesChanged(this, services);
140 * @return the aborted
142 public boolean isAborted()
151 public void setAborted(boolean aborted)
153 this.aborted = aborted;
160 if (running && oldthread != null && oldthread.isAlive())
171 "Waiting around for old discovery thread to finish.");
172 // wait around until old discoverer dies
174 } catch (Exception e)
179 Console.debug("Old discovery thread has finished.");
183 // first set up exclusion list if needed
184 final Set<String> ignoredServices = new HashSet<>();
185 for (String ignored : Cache
186 .getDefault("IGNORED_JABAWS_SERVICETYPES", "").split("\\|"))
188 ignoredServices.add(ignored);
191 notifyServiceListeners(Collections.emptyList());
192 oldthread = Thread.currentThread();
195 getClass().getClassLoader().loadClass("compbio.ws.client.Jws2Client");
196 } catch (ClassNotFoundException e)
199 "Not enabling JABA Webservices : client jar is not available."
200 + "\nPlease check that your webstart JNLP file is up to date!");
204 // reinitialise records of good and bad service URLs
205 if (services != null)
207 services.removeAllElements();
209 if (urlsWithoutServices != null)
211 urlsWithoutServices.removeAllElements();
213 if (invalidServiceUrls != null)
215 invalidServiceUrls.removeAllElements();
217 if (validServiceUrls != null)
219 validServiceUrls.removeAllElements();
221 ArrayList<String> svctypes = new ArrayList<>();
223 List<JabaWsServerQuery> qrys = new ArrayList<>();
224 for (final String jwsserver : getServiceUrls())
226 JabaWsServerQuery squery = new JabaWsServerQuery(this, jwsserver);
227 if (svctypes.size() == 0)
229 // TODO: remove this ugly hack to get Canonical JABA service ordering
230 // for all possible services
231 for (Services sv : squery.JABAWS2SERVERS)
233 if (!ignoredServices.contains(sv.toString()))
235 svctypes.add(sv.toString());
241 new Thread(squery).start();
243 boolean finished = true;
250 } catch (Exception e)
253 for (JabaWsServerQuery squery : qrys)
255 if (squery.isRunning())
263 "Aborting " + qrys.size() + " JABAWS discovery threads.");
264 for (JabaWsServerQuery squery : qrys)
266 squery.setQuit(true);
269 } while (!aborted && !finished);
272 // resort services according to order found in jabaws service list
273 // also ensure services for each host are ordered in same way.
275 if (services != null && services.size() > 0)
277 Jws2Instance[] svcs = new Jws2Instance[services.size()];
278 int[] spos = new int[services.size()];
280 List<String> svcUrls = getServiceUrls();
281 for (Jws2Instance svc : services)
284 spos[ipos++] = 1000 * svcUrls.indexOf(svc.getHostURL()) + 1
285 + svctypes.indexOf(svc.getName());
287 jalview.util.QuickSort.sort(spos, svcs);
288 services = new Vector<>();
289 for (Jws2Instance svc : svcs)
291 if (!ignoredServices.contains(svc.getName()))
300 notifyServiceListeners(services);
304 * record this service endpoint so we can use it
310 synchronized void addService(String jwsservers, Jws2Instance service)
312 if (services == null)
314 services = new Vector<>();
317 "Discovered service: " + jwsservers + " " + service.toString());
318 // Jws2Instance service = new Jws2Instance(jwsservers, srv.toString(),
321 services.add(service);
322 // retrieve the presets and parameter set and cache now
323 ParamDatastoreI pds = service.getParamStore();
328 service.hasParameters();
329 if (validServiceUrls == null)
331 validServiceUrls = new Vector<>();
333 validServiceUrls.add(jwsservers);
341 public static void main(String[] args)
343 Jws2Discoverer instance = getInstance();
346 instance.testUrls = new ArrayList<>();
347 for (String url : args)
349 instance.testUrls.add(url);
352 var discoverer = getInstance();
353 discoverer.addServiceChangeListener((_discoverer, _services) -> {
354 if (discoverer.services != null)
356 System.out.println("Changesupport: There are now "
357 + discoverer.services.size() + " services");
359 for (ServiceWithParameters s_instance : discoverer.services)
362 "Service " + i++ + " " + s_instance.getClass()
363 + "@" + s_instance.getHostURL() + ": "
364 + s_instance.getActionText());
371 discoverer.startDiscoverer().get();
372 } catch (InterruptedException | ExecutionException e)
378 } catch (InterruptedException x)
385 public boolean hasServices()
387 return !running && services != null && services.size() > 0;
391 public boolean isRunning()
397 public void setServiceUrls(List<String> wsUrls)
399 if (wsUrls != null && !wsUrls.isEmpty())
401 StringBuilder urls = new StringBuilder(128);
403 for (String url : wsUrls)
409 Cache.setProperty(JWS2HOSTURLS, urls.toString());
413 Cache.removeProperty(JWS2HOSTURLS);
418 * Returns web service URLs, in the order in which they should be tried (or an
424 public List<String> getServiceUrls()
426 if (testUrls != null)
428 // return test urls, if there are any, instead of touching cache
431 List<String> urls = new ArrayList<>();
433 if (this.preferredUrl != null)
435 urls.add(preferredUrl);
438 String surls = Cache.getDefault(JWS2HOSTURLS, COMPBIO_JABAWS);
441 StringTokenizer st = new StringTokenizer(surls, ",");
442 while (st.hasMoreElements())
447 url = st.nextToken();
449 if (!urls.contains(url))
455 Console.warn("Ignoring duplicate url " + url + " in "
456 + JWS2HOSTURLS + " list");
458 } catch (MalformedURLException ex)
460 Console.warn("Problem whilst trying to make a URL from '"
461 + ((url != null) ? url : "<null>") + "'");
463 "This was probably due to a malformed comma separated list"
464 + " in the " + JWS2HOSTURLS
465 + " entry of $(HOME)/.jalview_properties)");
466 Console.debug("Exception was ", ex);
469 } catch (Exception ex)
471 Console.warn("Error parsing comma separated list of urls in "
472 + JWS2HOSTURLS + " preference.", ex);
478 public Vector<ServiceWithParameters> getServices()
480 return (services == null) ? new Vector<>() : new Vector<>(services);
484 * test the given URL with the JabaWS test code
490 public boolean testServiceUrl(URL foo)
494 compbio.ws.client.WSTester
496 { "-h=" + foo.toString() });
497 } catch (Exception e)
501 } catch (OutOfMemoryError e)
514 public boolean restart()
531 * Start a fresh discovery thread and notify the given object when we're
532 * finished. Any known existing threads will be killed before this one is
535 * @param changeSupport2
539 public CompletableFuture<WSDiscovererI> startDiscoverer()
547 Thread thr = new Thread(this);
555 CompletableFuture<WSDiscovererI> task = CompletableFuture
558 return Jws2Discoverer.this;
564 * @return the invalidServiceUrls
566 public Vector<String> getInvalidServiceUrls()
568 return invalidServiceUrls;
572 * @return the urlsWithoutServices
574 public Vector<String> getUrlsWithoutServices()
576 return urlsWithoutServices;
580 * add an 'empty' JABA server to the list. Only servers not already in the
581 * 'bad URL' list will be added to this list.
585 public synchronized void addUrlwithnoservices(String jwsservers)
587 if (urlsWithoutServices == null)
589 urlsWithoutServices = new Vector<>();
592 if ((invalidServiceUrls == null
593 || !invalidServiceUrls.contains(jwsservers))
594 && !urlsWithoutServices.contains(jwsservers))
596 urlsWithoutServices.add(jwsservers);
601 * add a bad URL to the list
605 public synchronized void addInvalidServiceUrl(String jwsservers)
607 if (invalidServiceUrls == null)
609 invalidServiceUrls = new Vector<>();
611 if (!invalidServiceUrls.contains(jwsservers))
613 invalidServiceUrls.add(jwsservers);
619 * @return a human readable report of any problems with the service URLs used
623 public String getErrorMessages()
625 if (!isRunning() && !isAborted())
627 StringBuffer ermsg = new StringBuffer();
628 boolean list = false;
629 if (getInvalidServiceUrls() != null
630 && getInvalidServiceUrls().size() > 0)
632 ermsg.append(MessageManager.getString("warn.urls_not_contacted")
634 for (String svcurl : getInvalidServiceUrls())
641 ermsg.append(svcurl);
643 ermsg.append("\n\n");
646 if (getUrlsWithoutServices() != null
647 && getUrlsWithoutServices().size() > 0)
650 MessageManager.getString("warn.urls_no_jaba") + ": \n");
651 for (String svcurl : getUrlsWithoutServices())
658 ermsg.append(svcurl);
662 if (ermsg.length() > 1)
664 return ermsg.toString();
672 public int getServerStatusFor(String url)
674 if (validServiceUrls != null && validServiceUrls.contains(url))
678 if (urlsWithoutServices != null && urlsWithoutServices.contains(url))
680 return STATUS_NO_SERVICES;
682 if (invalidServiceUrls != null && invalidServiceUrls.contains(url))
684 return STATUS_INVALID;
686 return STATUS_UNKNOWN;
690 * Set a URL to try before any others. For use with command-line parameter to
691 * configure a local Jabaws installation without the need to add to property
695 * @throws MalformedURLException
697 public void setPreferredUrl(String value) throws MalformedURLException
699 if (value != null && value.trim().length() > 0)
702 preferredUrl = value;