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.WSDiscovererI;
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 WSDiscovererI, Runnable
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();
419 public boolean hasServices()
421 return !running && services != null && services.size() > 0;
425 public boolean isRunning()
431 public void setServiceUrls(List<String> wsUrls)
433 if (wsUrls != null && !wsUrls.isEmpty())
435 StringBuilder urls = new StringBuilder(128);
437 for (String url : wsUrls)
443 Cache.setProperty(JWS2HOSTURLS, urls.toString());
447 Cache.removeProperty(JWS2HOSTURLS);
452 * Returns web service URLs, in the order in which they should be tried (or an
458 public List<String> getServiceUrls()
460 if (testUrls != null)
462 // return test urls, if there are any, instead of touching cache
465 List<String> urls = new ArrayList<>();
467 if (this.preferredUrl != null)
469 urls.add(preferredUrl);
472 String surls = Cache.getDefault(JWS2HOSTURLS, COMPBIO_JABAWS);
475 StringTokenizer st = new StringTokenizer(surls, ",");
476 while (st.hasMoreElements())
481 url = st.nextToken();
483 if (!urls.contains(url))
489 Cache.log.warn("Ignoring duplicate url " + url + " in "
490 + JWS2HOSTURLS + " list");
492 } catch (MalformedURLException ex)
494 Cache.log.warn("Problem whilst trying to make a URL from '"
495 + ((url != null) ? url : "<null>") + "'");
497 "This was probably due to a malformed comma separated list"
498 + " in the " + JWS2HOSTURLS
499 + " entry of $(HOME)/.jalview_properties)");
500 Cache.log.debug("Exception was ", ex);
503 } catch (Exception ex)
505 Cache.log.warn("Error parsing comma separated list of urls in "
506 + JWS2HOSTURLS + " preference.", ex);
512 public Vector<ServiceWithParameters> getServices()
514 return (services == null) ? new Vector<>()
515 : new Vector<>(services);
519 * test the given URL with the JabaWS test code
525 public boolean testServiceUrl(URL foo)
529 compbio.ws.client.WSTester
531 { "-h=" + foo.toString() });
532 } catch (Exception e)
536 } catch (OutOfMemoryError e)
549 public boolean restart()
566 * Start a fresh discovery thread and notify the given object when we're
567 * finished. Any known existing threads will be killed before this one is
570 * @param changeSupport2
574 public Thread startDiscoverer(PropertyChangeListener changeSupport2)
582 Thread thr = new Thread(this);
590 addPropertyChangeListener(changeSupport2);
591 Thread thr = new Thread(this);
597 * @return the invalidServiceUrls
599 public Vector<String> getInvalidServiceUrls()
601 return invalidServiceUrls;
605 * @return the urlsWithoutServices
607 public Vector<String> getUrlsWithoutServices()
609 return urlsWithoutServices;
613 * add an 'empty' JABA server to the list. Only servers not already in the
614 * 'bad URL' list will be added to this list.
618 public synchronized void addUrlwithnoservices(String jwsservers)
620 if (urlsWithoutServices == null)
622 urlsWithoutServices = new Vector<>();
625 if ((invalidServiceUrls == null
626 || !invalidServiceUrls.contains(jwsservers))
627 && !urlsWithoutServices.contains(jwsservers))
629 urlsWithoutServices.add(jwsservers);
634 * add a bad URL to the list
638 public synchronized void addInvalidServiceUrl(String jwsservers)
640 if (invalidServiceUrls == null)
642 invalidServiceUrls = new Vector<>();
644 if (!invalidServiceUrls.contains(jwsservers))
646 invalidServiceUrls.add(jwsservers);
652 * @return a human readable report of any problems with the service URLs used
656 public String getErrorMessages()
658 if (!isRunning() && !isAborted())
660 StringBuffer ermsg = new StringBuffer();
661 boolean list = false;
662 if (getInvalidServiceUrls() != null
663 && getInvalidServiceUrls().size() > 0)
665 ermsg.append(MessageManager.getString("warn.urls_not_contacted")
667 for (String svcurl : getInvalidServiceUrls())
674 ermsg.append(svcurl);
676 ermsg.append("\n\n");
679 if (getUrlsWithoutServices() != null
680 && getUrlsWithoutServices().size() > 0)
683 MessageManager.getString("warn.urls_no_jaba") + ": \n");
684 for (String svcurl : getUrlsWithoutServices())
691 ermsg.append(svcurl);
695 if (ermsg.length() > 1)
697 return ermsg.toString();
705 public int getServerStatusFor(String url)
707 if (validServiceUrls != null && validServiceUrls.contains(url))
711 if (urlsWithoutServices != null && urlsWithoutServices.contains(url))
713 return STATUS_NO_SERVICES;
715 if (invalidServiceUrls != null && invalidServiceUrls.contains(url))
717 return STATUS_INVALID;
719 return STATUS_UNKNOWN;
723 * Set a URL to try before any others. For use with command-line parameter to
724 * configure a local Jabaws installation without the need to add to property
728 * @throws MalformedURLException
730 public void setPreferredUrl(String value) throws MalformedURLException
732 if (value != null && value.trim().length() > 0)
735 preferredUrl = value;