2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.ws.jws2;
20 import java.awt.Color;
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.beans.PropertyChangeEvent;
24 import java.beans.PropertyChangeListener;
25 import java.net.ConnectException;
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.Hashtable;
30 import java.util.List;
31 import java.util.StringTokenizer;
32 import java.util.Vector;
34 import javax.swing.JMenu;
35 import javax.swing.JMenuItem;
36 import javax.swing.event.MenuEvent;
37 import javax.swing.event.MenuListener;
39 import org.apache.log4j.Level;
41 import jalview.bin.Cache;
42 import jalview.datamodel.AlignmentView;
43 import jalview.gui.AlignFrame;
44 import jalview.gui.Desktop;
45 import jalview.gui.JalviewChangeSupport;
46 import jalview.gui.JvSwingUtils;
47 import jalview.ws.WSMenuEntryProviderI;
48 import jalview.ws.jws2.jabaws2.Jws2Instance;
49 import compbio.data.msa.JABAService;
50 import compbio.metadata.Option;
51 import compbio.metadata.Preset;
52 import compbio.ws.client.Jws2Client;
53 import compbio.ws.client.Services;
56 * discoverer for jws2 services. Follows the lightweight service discoverer
57 * pattern (archetyped by EnfinEnvision2OneWay)
62 public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
64 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
68 * change listeners are notified of "services" property changes
71 * to be added that consumes new services Hashtable object.
73 public void addPropertyChangeListener(
74 java.beans.PropertyChangeListener listener)
76 changeSupport.addPropertyChangeListener(listener);
85 public void removePropertyChangeListener(
86 java.beans.PropertyChangeListener listener)
88 changeSupport.removePropertyChangeListener(listener);
91 boolean running = false, aborted = false;
96 public boolean isAborted()
105 public void setAborted(boolean aborted)
107 this.aborted = aborted;
110 Thread oldthread = null;
114 if (running && oldthread != null && oldthread.isAlive())
125 .debug("Waiting around for old discovery thread to finish.");
126 // wait around until old discoverer dies
128 } catch (Exception e)
132 Cache.log.debug("Old discovery thread has finished.");
135 oldthread = Thread.currentThread();
138 Class foo = getClass().getClassLoader().loadClass(
139 "compbio.ws.client.Jws2Client");
140 } catch (ClassNotFoundException e)
143 .println("Not enabling JABA Webservices : client jar is not available."
144 + "\nPlease check that your webstart JNLP file is up to date!");
148 // reinitialise records of good and bad service URLs
149 if (services != null)
151 services.removeAllElements();
153 if (urlsWithoutServices != null)
155 urlsWithoutServices.removeAllElements();
157 if (invalidServiceUrls != null)
159 invalidServiceUrls.removeAllElements();
162 List<JabaWsServerQuery> qrys = new ArrayList<JabaWsServerQuery>();
163 for (final String jwsservers : getServiceUrls())
165 JabaWsServerQuery squery = new JabaWsServerQuery(this, jwsservers);
167 new Thread(squery).start();
169 boolean finished = false;
175 } catch (Exception e)
179 for (JabaWsServerQuery squery : qrys)
181 finished |= !squery.isRunning();
185 Cache.log.debug("Aborting " + qrys.size()
186 + " JABAWS discovery threads.");
187 for (JabaWsServerQuery squery : qrys)
189 squery.setQuit(true);
192 } while (!aborted && !finished);
197 changeSupport.firePropertyChange("services", new Vector(), services);
202 * record this service endpoint so we can use it
208 synchronized void addService(String jwsservers, Jws2Instance service)
210 if (services == null)
212 services = new Vector<Jws2Instance>();
214 System.out.println("Discovered service: " + jwsservers + " "
215 + service.toString());
216 // Jws2Instance service = new Jws2Instance(jwsservers, srv.toString(),
219 services.add(service);
220 // retrieve the presets and parameter set and cache now
221 service.getParamStore().getPresets();
222 service.hasParameters();
226 * holds list of services.
228 protected Vector<Jws2Instance> services;
230 * attach all available web services to the appropriate submenu in the given JMenu
232 public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
234 // dynamically regenerate service list.
235 populateWSMenuEntry(wsmenu, alignFrame, null);
238 private void populateWSMenuEntry(JMenu jws2al, final AlignFrame alignFrame, String typeFilter)
240 if (running || services == null || services.size() == 0)
244 boolean byhost = Cache.getDefault("WSMENU_BYHOST", false), bytype = Cache
245 .getDefault("WSMENU_BYTYPE", false);
247 * eventually, JWS2 services will appear under the same align/etc submenus.
248 * for moment we keep them separate.
251 MsaWSClient msacl = new MsaWSClient();
252 Vector hostLabels = new Vector();
254 String lasthost = null;
255 Hashtable<String, ArrayList<Jws2Instance>> hosts = new Hashtable<String, ArrayList<Jws2Instance>>();
257 for (Jws2Instance service : services)
259 ArrayList<Jws2Instance> hostservices = hosts.get(service.getHost());
260 if (hostservices == null)
262 hosts.put(service.getHost(),
263 hostservices = new ArrayList<Jws2Instance>());
265 hostservices.add(service);
267 sorton = hosts.keySet().toArray(new String[1]);
268 String hostlist[] = sorton.clone();
269 jalview.util.QuickSort.sort(sorton, hostlist);
270 for (String host : hostlist)
272 Jws2Instance orderedsvcs[] = hosts.get(host).toArray(
273 new Jws2Instance[1]);
274 String sortbytype[] = new String[orderedsvcs.length];
275 for (int i = 0; i < sortbytype.length; i++)
277 sortbytype[i] = orderedsvcs[i].serviceType;
279 jalview.util.QuickSort.sort(sortbytype, orderedsvcs);
280 for (final Jws2Instance service : orderedsvcs)
282 atpoint = JvSwingUtils.findOrCreateMenu(jws2al,service.action);
283 String type = service.serviceType;
286 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, host);
287 if (atpoint.getToolTipText() == null)
289 atpoint.setToolTipText("Services at " + host);
294 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, type);
295 if (atpoint.getToolTipText() == null)
297 atpoint.setToolTipText(service.getActionText());
301 && !hostLabels.contains(host + service.serviceType
302 + service.getActionText()))
303 // !hostLabels.contains(host + (bytype ?
304 // service.serviceType+service.getActionText() : "")))
306 // add a marker indicating where this service is hosted
307 // relies on services from the same host being listed in a
311 atpoint.addSeparator();
312 if (lasthost == null || !lasthost.equals(host))
314 atpoint.add(hitm = new JMenuItem(host));
315 hitm.setForeground(Color.blue);
316 hitm.addActionListener(new ActionListener()
320 public void actionPerformed(ActionEvent e)
322 Desktop.showUrl(service.getHost());
325 hitm.setToolTipText(JvSwingUtils
326 .wrapTooltip("Opens the JABAWS server's homepage in web browser"));
329 hostLabels.addElement(host + service.serviceType
330 + service.getActionText());
331 // hostLabels.addElement(host + (bytype ?
332 // service.serviceType+service.getActionText() : ""));
335 service.attachWSMenuEntry(atpoint, alignFrame);
337 * JMenuItem sitem = new JMenuItem(service.serviceType);
338 * sitem.setToolTipText("Hosted at " + service.hosturl);
339 * sitem.addActionListener(new ActionListener() {
341 * @Override public void actionPerformed(ActionEvent e) { AlignmentView
342 * msa = alignFrame.gatherSequencesForAlignment(); MsaWSClient client =
343 * new MsaWSClient(service, "JWS2 Alignment of " +
344 * alignFrame.getTitle(), msa, false, true,
345 * alignFrame.getViewport().getAlignment().getDataset(), alignFrame); }
353 public static void main(String[] args)
357 testUrls = new Vector<String>();
358 for (String url:args)
363 Thread runner = getDiscoverer().startDiscoverer(
364 new PropertyChangeListener()
367 public void propertyChange(PropertyChangeEvent evt)
369 if (getDiscoverer().services != null)
371 System.out.println("Changesupport: There are now "
372 + getDiscoverer().services.size() + " services");
377 while (runner.isAlive())
382 } catch (InterruptedException e)
389 private static Jws2Discoverer discoverer;
391 public static Jws2Discoverer getDiscoverer()
393 if (discoverer == null)
395 discoverer = new Jws2Discoverer();
400 public boolean hasServices()
402 // TODO Auto-generated method stub
403 return !running && services != null && services.size() > 0;
406 public boolean isRunning()
412 * the jalview .properties entry for JWS2 URLS
414 final static String JWS2HOSTURLS = "JWS2HOSTURLS";
416 public static void setServiceUrls(Vector<String> urls)
420 StringBuffer urlbuffer = new StringBuffer();
422 for (String url : urls)
424 urlbuffer.append(sep);
425 urlbuffer.append(url);
428 Cache.setProperty(JWS2HOSTURLS, urlbuffer.toString());
432 Cache.removeProperty(JWS2HOSTURLS);
436 private static Vector<String> testUrls=null;
437 public static Vector<String> getServiceUrls()
441 // return test urls, if there are any, instead of touching cache
444 String surls = Cache.getDefault(JWS2HOSTURLS,
445 "http://www.compbio.dundee.ac.uk/jabaws");
446 Vector<String> urls = new Vector<String>();
449 StringTokenizer st = new StringTokenizer(surls, ",");
450 while (st.hasMoreElements())
455 java.net.URL u = new java.net.URL(url = st.nextToken());
456 if (!urls.contains(url))
462 jalview.bin.Cache.log.info("Ignoring duplicate url in "
463 + JWS2HOSTURLS + " list");
465 } catch (Exception ex)
467 jalview.bin.Cache.log
468 .warn("Problem whilst trying to make a URL from '"
469 + ((url != null) ? url : "<null>") + "'");
470 jalview.bin.Cache.log
471 .warn("This was probably due to a malformed comma separated list"
474 + " entry of $(HOME)/.jalview_properties)");
475 jalview.bin.Cache.log.debug("Exception was ", ex);
478 } catch (Exception ex)
480 jalview.bin.Cache.log.warn(
481 "Error parsing comma separated list of urls in "
482 + JWS2HOSTURLS + " preference.", ex);
484 if (urls.size() >= 0)
491 public Vector<Jws2Instance> getServices()
493 return (services == null) ? new Vector<Jws2Instance>()
494 : new Vector<Jws2Instance>(services);
498 * test the given URL with the JabaWS test code
503 public static boolean testServiceUrl(URL foo)
507 compbio.ws.client.WSTester.main(new String[]
508 { "-h=" + foo.toString() });
509 } catch (Exception e)
513 } catch (OutOfMemoryError e)
527 * Start a fresh discovery thread and notify the given object when we're
528 * finished. Any known existing threads will be killed before this one is
531 * @param changeSupport2
534 public Thread startDiscoverer(PropertyChangeListener changeSupport2)
540 addPropertyChangeListener(changeSupport2);
541 Thread thr = new Thread(this);
546 Vector<String> invalidServiceUrls = null, urlsWithoutServices = null;
549 * @return the invalidServiceUrls
551 public Vector<String> getInvalidServiceUrls()
553 return invalidServiceUrls;
557 * @return the urlsWithoutServices
559 public Vector<String> getUrlsWithoutServices()
561 return urlsWithoutServices;
565 * add an 'empty' JABA server to the list. Only servers not already in the
566 * 'bad URL' list will be added to this list.
570 public synchronized void addUrlwithnoservices(String jwsservers)
572 if (urlsWithoutServices == null)
574 urlsWithoutServices = new Vector<String>();
577 if ((invalidServiceUrls == null || !invalidServiceUrls
578 .contains(jwsservers))
579 && !urlsWithoutServices.contains(jwsservers))
581 urlsWithoutServices.add(jwsservers);
586 * add a bad URL to the list
590 public synchronized void addInvalidServiceUrl(String jwsservers)
592 if (invalidServiceUrls == null)
594 invalidServiceUrls = new Vector<String>();
596 if (!invalidServiceUrls.contains(jwsservers))
598 invalidServiceUrls.add(jwsservers);
604 * @return a human readable report of any problems with the service URLs used
607 public String getErrorMessages()
609 if (!isRunning() && !isAborted())
611 StringBuffer ermsg = new StringBuffer();
612 boolean list = false;
613 if (getInvalidServiceUrls() != null
614 && getInvalidServiceUrls().size() > 0)
616 ermsg.append("URLs that could not be contacted: \n");
617 for (String svcurl : getInvalidServiceUrls())
624 ermsg.append(svcurl);
626 ermsg.append("\n\n");
629 if (getUrlsWithoutServices() != null
630 && getUrlsWithoutServices().size() > 0)
632 ermsg.append("URLs without any JABA Services : \n");
633 for (String svcurl : getUrlsWithoutServices())
640 ermsg.append(svcurl);
644 if (ermsg.length() > 1)
646 return ermsg.toString();