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.WSMenuEntryProviderI;
27 import jalview.ws.api.ServiceWithParameters;
28 import jalview.ws.jws2.jabaws2.Jws2Instance;
29 import jalview.ws.params.ParamDatastoreI;
31 import java.beans.PropertyChangeEvent;
32 import java.beans.PropertyChangeListener;
33 import java.beans.PropertyChangeSupport;
34 import java.net.MalformedURLException;
36 import java.util.ArrayList;
37 import java.util.HashSet;
38 import java.util.List;
40 import java.util.StringTokenizer;
41 import java.util.Vector;
43 import javax.swing.JMenu;
45 import compbio.ws.client.Services;
48 * discoverer for jws2 services. Follows the lightweight service discoverer
49 * pattern (archetyped by EnfinEnvision2OneWay)
54 public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
56 public static final String COMPBIO_JABAWS = "http://www.compbio.dundee.ac.uk/jabaws";
59 * the .jalview_properties entry for JWS2 URLS
61 private final static String JWS2HOSTURLS = "JWS2HOSTURLS";
66 private static Jws2Discoverer discoverer;
69 * Override for testing only
71 private static List<String> testUrls = null;
73 // preferred url has precedence over others
74 private String preferredUrl;
76 private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
79 private Vector<String> invalidServiceUrls = null;
81 private Vector<String> urlsWithoutServices = null;
83 private Vector<String> validServiceUrls = null;
85 private volatile boolean running = false;
87 private volatile boolean aborted = false;
89 private Thread oldthread = null;
92 * holds list of services.
94 protected Vector<Jws2Instance> services;
97 * Private constructor enforces use of singleton via getDiscoverer()
99 private Jws2Discoverer()
104 * change listeners are notified of "services" property changes
107 * to be added that consumes new services Hashtable object.
109 public void addPropertyChangeListener(
110 java.beans.PropertyChangeListener listener)
112 changeSupport.addPropertyChangeListener(listener);
121 public void removePropertyChangeListener(
122 java.beans.PropertyChangeListener listener)
124 changeSupport.removePropertyChangeListener(listener);
128 * @return the aborted
130 public boolean isAborted()
139 public void setAborted(boolean aborted)
141 this.aborted = aborted;
148 if (running && oldthread != null && oldthread.isAlive())
159 "Waiting around for old discovery thread to finish.");
160 // wait around until old discoverer dies
162 } catch (Exception e)
167 Cache.log.debug("Old discovery thread has finished.");
171 // first set up exclusion list if needed
172 final Set<String> ignoredServices = new HashSet<>();
173 for (String ignored : Cache
174 .getDefault("IGNORED_JABAWS_SERVICETYPES", "").split("\\|"))
176 ignoredServices.add(ignored);
179 changeSupport.firePropertyChange("services", services,
180 new Vector<Jws2Instance>());
181 oldthread = Thread.currentThread();
184 Class foo = getClass().getClassLoader()
185 .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)
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 changeSupport.firePropertyChange("services", new Vector<Jws2Instance>(),
296 * record this service endpoint so we can use it
302 synchronized void addService(String jwsservers, Jws2Instance service)
304 if (services == null)
306 services = new Vector<>();
309 "Discovered service: " + jwsservers + " " + service.toString());
310 // Jws2Instance service = new Jws2Instance(jwsservers, srv.toString(),
313 services.add(service);
314 // retrieve the presets and parameter set and cache now
315 ParamDatastoreI pds = service.getParamStore();
320 service.hasParameters();
321 if (validServiceUrls == null)
323 validServiceUrls = new Vector<>();
325 validServiceUrls.add(jwsservers);
329 * attach all available web services to the appropriate submenu in the given
333 public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
335 if (running || services == null || services.size() == 0)
339 // dynamically regenerate service list.
340 populateWSMenuEntry(wsmenu, alignFrame, null);
343 private void populateWSMenuEntry(JMenu jws2al,
344 final AlignFrame alignFrame, String typeFilter)
346 PreferredServiceRegistry.getRegistry().populateWSMenuEntry(
348 changeSupport, jws2al,
349 alignFrame, typeFilter);
352 public static void main(String[] args)
356 testUrls = new ArrayList<>();
357 for (String url : args)
363 Thread runner = getDiscoverer()
364 .startDiscoverer(new PropertyChangeListener()
368 public void propertyChange(PropertyChangeEvent evt)
370 if (getDiscoverer().services != null)
372 System.out.println("Changesupport: There are now "
373 + getDiscoverer().services.size() + " services");
375 for (ServiceWithParameters instance : getDiscoverer().services)
377 System.out.println("Service " + i++ + " "
378 + instance.getClass() + "@"
379 + instance.getHostURL()
380 + ": " + instance.getActionText());
386 while (runner.isAlive())
391 } catch (InterruptedException e)
399 } catch (InterruptedException x)
405 * Returns the singleton instance of this class.
409 public static Jws2Discoverer getDiscoverer()
411 if (discoverer == null)
413 discoverer = new Jws2Discoverer();
418 public boolean hasServices()
420 return !running && services != null && services.size() > 0;
423 public boolean isRunning()
428 public void setServiceUrls(List<String> wsUrls)
430 if (wsUrls != null && !wsUrls.isEmpty())
432 StringBuilder urls = new StringBuilder(128);
434 for (String url : wsUrls)
440 Cache.setProperty(JWS2HOSTURLS, urls.toString());
444 Cache.removeProperty(JWS2HOSTURLS);
449 * Returns web service URLs, in the order in which they should be tried (or an
454 public List<String> getServiceUrls()
456 if (testUrls != null)
458 // return test urls, if there are any, instead of touching cache
461 List<String> urls = new ArrayList<>();
463 if (this.preferredUrl != null)
465 urls.add(preferredUrl);
468 String surls = Cache.getDefault(JWS2HOSTURLS, COMPBIO_JABAWS);
471 StringTokenizer st = new StringTokenizer(surls, ",");
472 while (st.hasMoreElements())
477 url = st.nextToken();
479 if (!urls.contains(url))
485 Cache.log.warn("Ignoring duplicate url " + url + " in "
486 + JWS2HOSTURLS + " list");
488 } catch (MalformedURLException ex)
490 Cache.log.warn("Problem whilst trying to make a URL from '"
491 + ((url != null) ? url : "<null>") + "'");
493 "This was probably due to a malformed comma separated list"
494 + " in the " + JWS2HOSTURLS
495 + " entry of $(HOME)/.jalview_properties)");
496 Cache.log.debug("Exception was ", ex);
499 } catch (Exception ex)
501 Cache.log.warn("Error parsing comma separated list of urls in "
502 + JWS2HOSTURLS + " preference.", ex);
507 public Vector<ServiceWithParameters> getServices()
509 return (services == null) ? new Vector<>()
510 : new Vector<>(services);
514 * test the given URL with the JabaWS test code
519 public static boolean testServiceUrl(URL foo)
523 compbio.ws.client.WSTester
525 { "-h=" + foo.toString() });
526 } catch (Exception e)
530 } catch (OutOfMemoryError e)
543 public boolean restart()
560 * Start a fresh discovery thread and notify the given object when we're
561 * finished. Any known existing threads will be killed before this one is
564 * @param changeSupport2
567 public Thread startDiscoverer(PropertyChangeListener changeSupport2)
575 Thread thr = new Thread(this);
583 addPropertyChangeListener(changeSupport2);
584 Thread thr = new Thread(this);
590 * @return the invalidServiceUrls
592 public Vector<String> getInvalidServiceUrls()
594 return invalidServiceUrls;
598 * @return the urlsWithoutServices
600 public Vector<String> getUrlsWithoutServices()
602 return urlsWithoutServices;
606 * add an 'empty' JABA server to the list. Only servers not already in the
607 * 'bad URL' list will be added to this list.
611 public synchronized void addUrlwithnoservices(String jwsservers)
613 if (urlsWithoutServices == null)
615 urlsWithoutServices = new Vector<>();
618 if ((invalidServiceUrls == null
619 || !invalidServiceUrls.contains(jwsservers))
620 && !urlsWithoutServices.contains(jwsservers))
622 urlsWithoutServices.add(jwsservers);
627 * add a bad URL to the list
631 public synchronized void addInvalidServiceUrl(String jwsservers)
633 if (invalidServiceUrls == null)
635 invalidServiceUrls = new Vector<>();
637 if (!invalidServiceUrls.contains(jwsservers))
639 invalidServiceUrls.add(jwsservers);
645 * @return a human readable report of any problems with the service URLs used
648 public String getErrorMessages()
650 if (!isRunning() && !isAborted())
652 StringBuffer ermsg = new StringBuffer();
653 boolean list = false;
654 if (getInvalidServiceUrls() != null
655 && getInvalidServiceUrls().size() > 0)
657 ermsg.append(MessageManager.getString("warn.urls_not_contacted")
659 for (String svcurl : getInvalidServiceUrls())
666 ermsg.append(svcurl);
668 ermsg.append("\n\n");
671 if (getUrlsWithoutServices() != null
672 && getUrlsWithoutServices().size() > 0)
675 MessageManager.getString("warn.urls_no_jaba") + ": \n");
676 for (String svcurl : getUrlsWithoutServices())
683 ermsg.append(svcurl);
687 if (ermsg.length() > 1)
689 return ermsg.toString();
696 public int getServerStatusFor(String url)
698 if (validServiceUrls != null && validServiceUrls.contains(url))
702 if (urlsWithoutServices != null && urlsWithoutServices.contains(url))
706 if (invalidServiceUrls != null && invalidServiceUrls.contains(url))
714 * Set a URL to try before any others. For use with command-line parameter to
715 * configure a local Jabaws installation without the need to add to property
719 * @throws MalformedURLException
721 public void setPreferredUrl(String value) throws MalformedURLException
723 if (value != null && value.trim().length() > 0)
726 preferredUrl = value;