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.gui.AlignFrame;
25 import jalview.util.MessageManager;
26 import jalview.ws.ServiceChangeListener;
27 import jalview.ws.WSDiscovererI;
28 import jalview.ws.api.ServiceWithParameters;
29 import jalview.ws.jws2.jabaws2.Jws2Instance;
30 import jalview.ws.params.ParamDatastoreI;
32 import java.beans.PropertyChangeEvent;
33 import java.beans.PropertyChangeListener;
34 import java.beans.PropertyChangeSupport;
35 import java.net.MalformedURLException;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.HashSet;
40 import java.util.List;
42 import java.util.StringTokenizer;
43 import java.util.Vector;
44 import java.util.concurrent.CompletableFuture;
45 import java.util.concurrent.CopyOnWriteArraySet;
46 import java.util.concurrent.ExecutionException;
47 import java.util.concurrent.Future;
48 import java.util.concurrent.FutureTask;
50 import javax.swing.JMenu;
52 import compbio.ws.client.Services;
55 * discoverer for jws2 services. Follows the lightweight service discoverer
56 * pattern (archetyped by EnfinEnvision2OneWay)
61 public class Jws2Discoverer implements WSDiscovererI, Runnable
63 public static final String COMPBIO_JABAWS = "http://www.compbio.dundee.ac.uk/jabaws";
66 * the .jalview_properties entry for JWS2 URLS
68 private final static String JWS2HOSTURLS = "JWS2HOSTURLS";
73 private static Jws2Discoverer discoverer;
76 * Override for testing only
78 private static List<String> testUrls = null;
80 // preferred url has precedence over others
81 private String preferredUrl;
83 private Set<ServiceChangeListener> serviceListeners = new CopyOnWriteArraySet<>();
85 private Vector<String> invalidServiceUrls = null;
87 private Vector<String> urlsWithoutServices = null;
89 private Vector<String> validServiceUrls = null;
91 private volatile boolean running = false;
93 private volatile boolean aborted = false;
95 private volatile Thread oldthread = null;
98 * holds list of services.
100 protected Vector<Jws2Instance> services;
103 * Private constructor enforces use of singleton via getDiscoverer()
105 private Jws2Discoverer()
111 public void addServiceChangeListener(ServiceChangeListener listener)
113 serviceListeners.add(listener);
117 public void removeServiceChangeListener(ServiceChangeListener listener)
119 serviceListeners.remove(listener);
122 private void notifyServiceListeners(List<? extends ServiceWithParameters> services)
124 if (services == null) services = this.services;
125 for (var listener : serviceListeners) {
126 listener.servicesChanged(this, services);
131 * @return the aborted
133 public boolean isAborted()
142 public void setAborted(boolean aborted)
144 this.aborted = aborted;
151 if (running && oldthread != null && oldthread.isAlive())
162 "Waiting around for old discovery thread to finish.");
163 // wait around until old discoverer dies
165 } catch (Exception e)
170 Cache.log.debug("Old discovery thread has finished.");
174 // first set up exclusion list if needed
175 final Set<String> ignoredServices = new HashSet<>();
176 for (String ignored : Cache
177 .getDefault("IGNORED_JABAWS_SERVICETYPES", "").split("\\|"))
179 ignoredServices.add(ignored);
182 notifyServiceListeners(Collections.emptyList());
183 oldthread = Thread.currentThread();
186 getClass().getClassLoader().loadClass("compbio.ws.client.Jws2Client");
187 } catch (ClassNotFoundException e)
190 "Not enabling JABA Webservices : client jar is not available."
191 + "\nPlease check that your webstart JNLP file is up to date!");
195 // reinitialise records of good and bad service URLs
196 if (services != null)
198 services.removeAllElements();
200 if (urlsWithoutServices != null)
202 urlsWithoutServices.removeAllElements();
204 if (invalidServiceUrls != null)
206 invalidServiceUrls.removeAllElements();
208 if (validServiceUrls != null)
210 validServiceUrls.removeAllElements();
212 ArrayList<String> svctypes = new ArrayList<>();
214 List<JabaWsServerQuery> qrys = new ArrayList<>();
215 for (final String jwsserver : getServiceUrls())
217 JabaWsServerQuery squery = new JabaWsServerQuery(this, jwsserver);
218 if (svctypes.size() == 0)
220 // TODO: remove this ugly hack to get Canonical JABA service ordering
221 // for all possible services
222 for (Services sv : squery.JABAWS2SERVERS)
224 if (!ignoredServices.contains(sv.toString()))
226 svctypes.add(sv.toString());
232 new Thread(squery).start();
234 boolean finished = true;
241 } catch (Exception e)
244 for (JabaWsServerQuery squery : qrys)
246 if (squery.isRunning())
254 "Aborting " + qrys.size() + " JABAWS discovery threads.");
255 for (JabaWsServerQuery squery : qrys)
257 squery.setQuit(true);
260 } while (!aborted && !finished);
263 // resort services according to order found in jabaws service list
264 // also ensure services for each host are ordered in same way.
266 if (services != null && services.size() > 0)
268 Jws2Instance[] svcs = new Jws2Instance[services.size()];
269 int[] spos = new int[services.size()];
271 List<String> svcUrls = getServiceUrls();
272 for (Jws2Instance svc : services)
275 spos[ipos++] = 1000 * svcUrls.indexOf(svc.getHostURL()) + 1
276 + svctypes.indexOf(svc.getName());
278 jalview.util.QuickSort.sort(spos, svcs);
279 services = new Vector<>();
280 for (Jws2Instance svc : svcs)
282 if (!ignoredServices.contains(svc.getName()))
291 notifyServiceListeners(services);
295 * record this service endpoint so we can use it
301 synchronized void addService(String jwsservers, Jws2Instance service)
303 if (services == null)
305 services = new Vector<>();
308 "Discovered service: " + jwsservers + " " + service.toString());
309 // Jws2Instance service = new Jws2Instance(jwsservers, srv.toString(),
312 services.add(service);
313 // retrieve the presets and parameter set and cache now
314 ParamDatastoreI pds = service.getParamStore();
319 service.hasParameters();
320 if (validServiceUrls == null)
322 validServiceUrls = new Vector<>();
324 validServiceUrls.add(jwsservers);
332 public static void main(String[] args)
336 testUrls = new ArrayList<>();
337 for (String url : args)
342 var discoverer = getDiscoverer();
343 discoverer.addServiceChangeListener((_discoverer, _services) -> {
344 if (discoverer.services != null)
346 System.out.println("Changesupport: There are now "
347 + discoverer.services.size() + " services");
349 for (ServiceWithParameters instance : discoverer.services)
352 "Service " + i++ + " " + instance.getClass()
353 + "@" + instance.getHostURL() + ": "
354 + instance.getActionText());
361 discoverer.startDiscoverer().get();
362 } catch (InterruptedException | ExecutionException e)
368 } catch (InterruptedException x)
374 * Returns the singleton instance of this class.
378 public static Jws2Discoverer getDiscoverer()
380 if (discoverer == null)
382 discoverer = new Jws2Discoverer();
388 public boolean hasServices()
390 return !running && services != null && services.size() > 0;
394 public boolean isRunning()
400 public void setServiceUrls(List<String> wsUrls)
402 if (wsUrls != null && !wsUrls.isEmpty())
404 StringBuilder urls = new StringBuilder(128);
406 for (String url : wsUrls)
412 Cache.setProperty(JWS2HOSTURLS, urls.toString());
416 Cache.removeProperty(JWS2HOSTURLS);
421 * Returns web service URLs, in the order in which they should be tried (or an
427 public List<String> getServiceUrls()
429 if (testUrls != null)
431 // return test urls, if there are any, instead of touching cache
434 List<String> urls = new ArrayList<>();
436 if (this.preferredUrl != null)
438 urls.add(preferredUrl);
441 String surls = Cache.getDefault(JWS2HOSTURLS, COMPBIO_JABAWS);
444 StringTokenizer st = new StringTokenizer(surls, ",");
445 while (st.hasMoreElements())
450 url = st.nextToken();
452 if (!urls.contains(url))
458 Cache.log.warn("Ignoring duplicate url " + url + " in "
459 + JWS2HOSTURLS + " list");
461 } catch (MalformedURLException ex)
463 Cache.log.warn("Problem whilst trying to make a URL from '"
464 + ((url != null) ? url : "<null>") + "'");
466 "This was probably due to a malformed comma separated list"
467 + " in the " + JWS2HOSTURLS
468 + " entry of $(HOME)/.jalview_properties)");
469 Cache.log.debug("Exception was ", ex);
472 } catch (Exception ex)
474 Cache.log.warn("Error parsing comma separated list of urls in "
475 + JWS2HOSTURLS + " preference.", ex);
481 public Vector<ServiceWithParameters> getServices()
483 return (services == null) ? new Vector<>() : new Vector<>(services);
487 * test the given URL with the JabaWS test code
493 public boolean testServiceUrl(URL foo)
497 compbio.ws.client.WSTester
499 { "-h=" + foo.toString() });
500 } catch (Exception e)
504 } catch (OutOfMemoryError e)
517 public boolean restart()
534 * Start a fresh discovery thread and notify the given object when we're
535 * finished. Any known existing threads will be killed before this one is
538 * @param changeSupport2
542 public CompletableFuture<WSDiscovererI> startDiscoverer()
550 Thread thr = new Thread(this);
558 CompletableFuture<WSDiscovererI> task = CompletableFuture
561 return Jws2Discoverer.this;
567 * @return the invalidServiceUrls
569 public Vector<String> getInvalidServiceUrls()
571 return invalidServiceUrls;
575 * @return the urlsWithoutServices
577 public Vector<String> getUrlsWithoutServices()
579 return urlsWithoutServices;
583 * add an 'empty' JABA server to the list. Only servers not already in the
584 * 'bad URL' list will be added to this list.
588 public synchronized void addUrlwithnoservices(String jwsservers)
590 if (urlsWithoutServices == null)
592 urlsWithoutServices = new Vector<>();
595 if ((invalidServiceUrls == null
596 || !invalidServiceUrls.contains(jwsservers))
597 && !urlsWithoutServices.contains(jwsservers))
599 urlsWithoutServices.add(jwsservers);
604 * add a bad URL to the list
608 public synchronized void addInvalidServiceUrl(String jwsservers)
610 if (invalidServiceUrls == null)
612 invalidServiceUrls = new Vector<>();
614 if (!invalidServiceUrls.contains(jwsservers))
616 invalidServiceUrls.add(jwsservers);
622 * @return a human readable report of any problems with the service URLs used
626 public String getErrorMessages()
628 if (!isRunning() && !isAborted())
630 StringBuffer ermsg = new StringBuffer();
631 boolean list = false;
632 if (getInvalidServiceUrls() != null
633 && getInvalidServiceUrls().size() > 0)
635 ermsg.append(MessageManager.getString("warn.urls_not_contacted")
637 for (String svcurl : getInvalidServiceUrls())
644 ermsg.append(svcurl);
646 ermsg.append("\n\n");
649 if (getUrlsWithoutServices() != null
650 && getUrlsWithoutServices().size() > 0)
653 MessageManager.getString("warn.urls_no_jaba") + ": \n");
654 for (String svcurl : getUrlsWithoutServices())
661 ermsg.append(svcurl);
665 if (ermsg.length() > 1)
667 return ermsg.toString();
675 public int getServerStatusFor(String url)
677 if (validServiceUrls != null && validServiceUrls.contains(url))
681 if (urlsWithoutServices != null && urlsWithoutServices.contains(url))
683 return STATUS_NO_SERVICES;
685 if (invalidServiceUrls != null && invalidServiceUrls.contains(url))
687 return STATUS_INVALID;
689 return STATUS_UNKNOWN;
693 * Set a URL to try before any others. For use with command-line parameter to
694 * configure a local Jabaws installation without the need to add to property
698 * @throws MalformedURLException
700 public void setPreferredUrl(String value) throws MalformedURLException
702 if (value != null && value.trim().length() > 0)
705 preferredUrl = value;