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.CopyOnWriteArraySet;
45 import java.util.concurrent.ExecutionException;
46 import java.util.concurrent.Future;
47 import java.util.concurrent.FutureTask;
49 import javax.swing.JMenu;
51 import compbio.ws.client.Services;
54 * discoverer for jws2 services. Follows the lightweight service discoverer
55 * pattern (archetyped by EnfinEnvision2OneWay)
60 public class Jws2Discoverer implements WSDiscovererI, Runnable
62 public static final String COMPBIO_JABAWS = "http://www.compbio.dundee.ac.uk/jabaws";
65 * the .jalview_properties entry for JWS2 URLS
67 private final static String JWS2HOSTURLS = "JWS2HOSTURLS";
72 private static Jws2Discoverer discoverer;
75 * Override for testing only
77 private static List<String> testUrls = null;
79 // preferred url has precedence over others
80 private String preferredUrl;
82 private Set<ServiceChangeListener> serviceListeners = new CopyOnWriteArraySet<>();
84 private Vector<String> invalidServiceUrls = null;
86 private Vector<String> urlsWithoutServices = null;
88 private Vector<String> validServiceUrls = null;
90 private volatile boolean running = false;
92 private volatile boolean aborted = false;
94 private volatile Thread oldthread = null;
97 * holds list of services.
99 protected Vector<Jws2Instance> services;
102 * Private constructor enforces use of singleton via getDiscoverer()
104 private Jws2Discoverer()
110 public void addServiceChangeListener(ServiceChangeListener listener)
112 serviceListeners.add(listener);
116 public void removeServiceChangeListener(ServiceChangeListener listener)
118 serviceListeners.remove(listener);
121 private void notifyServiceListeners(List<? extends ServiceWithParameters> services)
123 if (services == null) services = this.services;
124 for (var listener : serviceListeners) {
125 listener.servicesChanged(this, services);
130 * @return the aborted
132 public boolean isAborted()
141 public void setAborted(boolean aborted)
143 this.aborted = aborted;
150 if (running && oldthread != null && oldthread.isAlive())
161 "Waiting around for old discovery thread to finish.");
162 // wait around until old discoverer dies
164 } catch (Exception e)
169 Cache.log.debug("Old discovery thread has finished.");
173 // first set up exclusion list if needed
174 final Set<String> ignoredServices = new HashSet<>();
175 for (String ignored : Cache
176 .getDefault("IGNORED_JABAWS_SERVICETYPES", "").split("\\|"))
178 ignoredServices.add(ignored);
181 notifyServiceListeners(Collections.emptyList());
182 oldthread = Thread.currentThread();
185 getClass().getClassLoader().loadClass("compbio.ws.client.Jws2Client");
186 } catch (ClassNotFoundException e)
189 "Not enabling JABA Webservices : client jar is not available."
190 + "\nPlease check that your webstart JNLP file is up to date!");
194 // reinitialise records of good and bad service URLs
195 if (services != null)
197 services.removeAllElements();
199 if (urlsWithoutServices != null)
201 urlsWithoutServices.removeAllElements();
203 if (invalidServiceUrls != null)
205 invalidServiceUrls.removeAllElements();
207 if (validServiceUrls != null)
209 validServiceUrls.removeAllElements();
211 ArrayList<String> svctypes = new ArrayList<>();
213 List<JabaWsServerQuery> qrys = new ArrayList<>();
214 for (final String jwsserver : getServiceUrls())
216 JabaWsServerQuery squery = new JabaWsServerQuery(this, jwsserver);
217 if (svctypes.size() == 0)
219 // TODO: remove this ugly hack to get Canonical JABA service ordering
220 // for all possible services
221 for (Services sv : squery.JABAWS2SERVERS)
223 if (!ignoredServices.contains(sv.toString()))
225 svctypes.add(sv.toString());
231 new Thread(squery).start();
233 boolean finished = true;
240 } catch (Exception e)
243 for (JabaWsServerQuery squery : qrys)
245 if (squery.isRunning())
253 "Aborting " + qrys.size() + " JABAWS discovery threads.");
254 for (JabaWsServerQuery squery : qrys)
256 squery.setQuit(true);
259 } while (!aborted && !finished);
262 // resort services according to order found in jabaws service list
263 // also ensure services for each host are ordered in same way.
265 if (services != null && services.size() > 0)
267 Jws2Instance[] svcs = new Jws2Instance[services.size()];
268 int[] spos = new int[services.size()];
270 List<String> svcUrls = getServiceUrls();
271 for (Jws2Instance svc : services)
274 spos[ipos++] = 1000 * svcUrls.indexOf(svc.getHostURL()) + 1
275 + svctypes.indexOf(svc.getName());
277 jalview.util.QuickSort.sort(spos, svcs);
278 services = new Vector<>();
279 for (Jws2Instance svc : svcs)
281 if (!ignoredServices.contains(svc.getName()))
290 notifyServiceListeners(services);
294 * record this service endpoint so we can use it
300 synchronized void addService(String jwsservers, Jws2Instance service)
302 if (services == null)
304 services = new Vector<>();
307 "Discovered service: " + jwsservers + " " + service.toString());
308 // Jws2Instance service = new Jws2Instance(jwsservers, srv.toString(),
311 services.add(service);
312 // retrieve the presets and parameter set and cache now
313 ParamDatastoreI pds = service.getParamStore();
318 service.hasParameters();
319 if (validServiceUrls == null)
321 validServiceUrls = new Vector<>();
323 validServiceUrls.add(jwsservers);
331 public static void main(String[] args)
335 testUrls = new ArrayList<>();
336 for (String url : args)
341 var discoverer = getDiscoverer();
342 discoverer.addServiceChangeListener((_discoverer, _services) -> {
343 if (discoverer.services != null)
345 System.out.println("Changesupport: There are now "
346 + discoverer.services.size() + " services");
348 for (ServiceWithParameters instance : discoverer.services)
351 "Service " + i++ + " " + instance.getClass()
352 + "@" + instance.getHostURL() + ": "
353 + instance.getActionText());
360 discoverer.startDiscoverer().get();
361 } catch (InterruptedException | ExecutionException e)
367 } catch (InterruptedException x)
373 * Returns the singleton instance of this class.
377 public static Jws2Discoverer getDiscoverer()
379 if (discoverer == null)
381 discoverer = new Jws2Discoverer();
387 public boolean hasServices()
389 return !running && services != null && services.size() > 0;
393 public boolean isRunning()
399 public void setServiceUrls(List<String> wsUrls)
401 if (wsUrls != null && !wsUrls.isEmpty())
403 StringBuilder urls = new StringBuilder(128);
405 for (String url : wsUrls)
411 Cache.setProperty(JWS2HOSTURLS, urls.toString());
415 Cache.removeProperty(JWS2HOSTURLS);
420 * Returns web service URLs, in the order in which they should be tried (or an
426 public List<String> getServiceUrls()
428 if (testUrls != null)
430 // return test urls, if there are any, instead of touching cache
433 List<String> urls = new ArrayList<>();
435 if (this.preferredUrl != null)
437 urls.add(preferredUrl);
440 String surls = Cache.getDefault(JWS2HOSTURLS, COMPBIO_JABAWS);
443 StringTokenizer st = new StringTokenizer(surls, ",");
444 while (st.hasMoreElements())
449 url = st.nextToken();
451 if (!urls.contains(url))
457 Cache.log.warn("Ignoring duplicate url " + url + " in "
458 + JWS2HOSTURLS + " list");
460 } catch (MalformedURLException ex)
462 Cache.log.warn("Problem whilst trying to make a URL from '"
463 + ((url != null) ? url : "<null>") + "'");
465 "This was probably due to a malformed comma separated list"
466 + " in the " + JWS2HOSTURLS
467 + " entry of $(HOME)/.jalview_properties)");
468 Cache.log.debug("Exception was ", ex);
471 } catch (Exception ex)
473 Cache.log.warn("Error parsing comma separated list of urls in "
474 + JWS2HOSTURLS + " preference.", ex);
480 public Vector<ServiceWithParameters> getServices()
482 return (services == null) ? new Vector<>() : new Vector<>(services);
486 * test the given URL with the JabaWS test code
492 public boolean testServiceUrl(URL foo)
496 compbio.ws.client.WSTester
498 { "-h=" + foo.toString() });
499 } catch (Exception e)
503 } catch (OutOfMemoryError e)
516 public boolean restart()
533 * Start a fresh discovery thread and notify the given object when we're
534 * finished. Any known existing threads will be killed before this one is
537 * @param changeSupport2
541 public FutureTask<WSDiscovererI> startDiscoverer()
549 Thread thr = new Thread(this);
557 FutureTask<WSDiscovererI> task = new FutureTask<>(this, this);
558 new Thread(task).start();
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;