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.util.jarInputStreamProvider;
48 import jalview.ws.WSMenuEntryProviderI;
49 import jalview.ws.jws2.jabaws2.Jws2Instance;
50 import compbio.data.msa.JABAService;
51 import compbio.metadata.Option;
52 import compbio.metadata.Preset;
53 import compbio.ws.client.Jws2Client;
54 import compbio.ws.client.Services;
57 * discoverer for jws2 services. Follows the lightweight service discoverer
58 * pattern (archetyped by EnfinEnvision2OneWay)
63 public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
65 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
69 * change listeners are notified of "services" property changes
72 * to be added that consumes new services Hashtable object.
74 public void addPropertyChangeListener(
75 java.beans.PropertyChangeListener listener)
77 changeSupport.addPropertyChangeListener(listener);
86 public void removePropertyChangeListener(
87 java.beans.PropertyChangeListener listener)
89 changeSupport.removePropertyChangeListener(listener);
92 boolean running = false, aborted = false;
97 public boolean isAborted()
106 public void setAborted(boolean aborted)
108 this.aborted = aborted;
111 Thread oldthread = null;
115 if (running && oldthread != null && oldthread.isAlive())
126 .debug("Waiting around for old discovery thread to finish.");
127 // wait around until old discoverer dies
129 } catch (Exception e)
133 Cache.log.debug("Old discovery thread has finished.");
136 changeSupport.firePropertyChange("services", services, new Vector());
137 oldthread = Thread.currentThread();
140 Class foo = getClass().getClassLoader().loadClass(
141 "compbio.ws.client.Jws2Client");
142 } catch (ClassNotFoundException e)
145 .println("Not enabling JABA Webservices : client jar is not available."
146 + "\nPlease check that your webstart JNLP file is up to date!");
150 // reinitialise records of good and bad service URLs
151 if (services != null)
153 services.removeAllElements();
155 if (urlsWithoutServices != null)
157 urlsWithoutServices.removeAllElements();
159 if (invalidServiceUrls != null)
161 invalidServiceUrls.removeAllElements();
163 ArrayList<String> svctypes=new ArrayList<String>();
165 List<JabaWsServerQuery> qrys = new ArrayList<JabaWsServerQuery>();
166 for (final String jwsservers : getServiceUrls())
168 JabaWsServerQuery squery = new JabaWsServerQuery(this, jwsservers);
169 if (svctypes.size()==0)
171 // TODO: remove this ugly hack to get Canonical JABA service ordering for all possible services
172 for (Services sv:squery.JABAWS2SERVERS)
174 svctypes.add(sv.toString());
179 new Thread(squery).start();
181 boolean finished = true;
188 } catch (Exception e)
192 for (JabaWsServerQuery squery : qrys)
194 finished = finished && !squery.isRunning();
198 Cache.log.debug("Aborting " + qrys.size()
199 + " JABAWS discovery threads.");
200 for (JabaWsServerQuery squery : qrys)
202 squery.setQuit(true);
205 } while (!aborted && !finished);
208 // resort services according to order found in jabaws service list
209 // also ensure servics for each host are ordered in same way.
211 if (services!=null && services.size()>0)
213 Jws2Instance[] svcs=new Jws2Instance[services.size()];
214 int[] spos=new int[services.size()];
216 Vector svcUrls = getServiceUrls();
217 for (Jws2Instance svc:services)
220 spos[ipos++]=1000*svcUrls.indexOf(svc.getHost()) + 1+svctypes.indexOf(svc.serviceType);
222 jalview.util.QuickSort.sort(spos, svcs);
223 services=new Vector<Jws2Instance>();
224 for (Jws2Instance svc:svcs) {
231 changeSupport.firePropertyChange("services", new Vector(), services);
235 * record this service endpoint so we can use it
241 synchronized void addService(String jwsservers, Jws2Instance service)
243 if (services == null)
245 services = new Vector<Jws2Instance>();
247 System.out.println("Discovered service: " + jwsservers + " "
248 + service.toString());
249 // Jws2Instance service = new Jws2Instance(jwsservers, srv.toString(),
252 services.add(service);
253 // retrieve the presets and parameter set and cache now
254 service.getParamStore().getPresets();
255 service.hasParameters();
259 * holds list of services.
261 protected Vector<Jws2Instance> services;
263 * attach all available web services to the appropriate submenu in the given JMenu
265 public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
267 // dynamically regenerate service list.
268 populateWSMenuEntry(wsmenu, alignFrame, null);
271 private void populateWSMenuEntry(JMenu jws2al, final AlignFrame alignFrame, String typeFilter)
273 if (running || services == null || services.size() == 0)
277 boolean byhost = Cache.getDefault("WSMENU_BYHOST", false), bytype = Cache
278 .getDefault("WSMENU_BYTYPE", false);
280 * eventually, JWS2 services will appear under the same align/etc submenus.
281 * for moment we keep them separate.
284 MsaWSClient msacl = new MsaWSClient();
285 Vector hostLabels = new Vector();
287 Hashtable<String,String> lasthostFor = new Hashtable<String,String>();
288 Hashtable<String, ArrayList<Jws2Instance>> hosts = new Hashtable<String, ArrayList<Jws2Instance>>();
289 ArrayList<String> hostlist=new ArrayList<String>();
290 for (Jws2Instance service : services)
292 ArrayList<Jws2Instance> hostservices = hosts.get(service.getHost());
293 if (hostservices == null)
295 hosts.put(service.getHost(),
296 hostservices = new ArrayList<Jws2Instance>());
297 hostlist.add(service.getHost());
299 hostservices.add(service);
301 // now add hosts in order of the given array
302 for (String host : hostlist)
304 Jws2Instance orderedsvcs[] = hosts.get(host).toArray(
305 new Jws2Instance[1]);
306 String sortbytype[] = new String[orderedsvcs.length];
307 for (int i = 0; i < sortbytype.length; i++)
309 sortbytype[i] = orderedsvcs[i].serviceType;
311 jalview.util.QuickSort.sort(sortbytype, orderedsvcs);
312 for (final Jws2Instance service : orderedsvcs)
314 atpoint = JvSwingUtils.findOrCreateMenu(jws2al,service.action);
315 String type = service.serviceType;
318 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, host);
319 if (atpoint.getToolTipText() == null)
321 atpoint.setToolTipText("Services at " + host);
326 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, type);
327 if (atpoint.getToolTipText() == null)
329 atpoint.setToolTipText(service.getActionText());
333 && !hostLabels.contains(host + service.serviceType
334 + service.getActionText()))
335 // !hostLabels.contains(host + (bytype ?
336 // service.serviceType+service.getActionText() : "")))
338 // add a marker indicating where this service is hosted
339 // relies on services from the same host being listed in a
343 atpoint.addSeparator();
344 if (lasthostFor.get(service.action) == null || !lasthostFor.get(service.action).equals(host))
346 atpoint.add(hitm = new JMenuItem(host));
347 hitm.setForeground(Color.blue);
348 hitm.addActionListener(new ActionListener()
352 public void actionPerformed(ActionEvent e)
354 Desktop.showUrl(service.getHost());
357 hitm.setToolTipText(JvSwingUtils
358 .wrapTooltip("Opens the JABAWS server's homepage in web browser"));
359 lasthostFor.put(service.action,host);
361 hostLabels.addElement(host + service.serviceType
362 + service.getActionText());
363 // hostLabels.addElement(host + (bytype ?
364 // service.serviceType+service.getActionText() : ""));
367 service.attachWSMenuEntry(atpoint, alignFrame);
369 * JMenuItem sitem = new JMenuItem(service.serviceType);
370 * sitem.setToolTipText("Hosted at " + service.hosturl);
371 * sitem.addActionListener(new ActionListener() {
373 * @Override public void actionPerformed(ActionEvent e) { AlignmentView
374 * msa = alignFrame.gatherSequencesForAlignment(); MsaWSClient client =
375 * new MsaWSClient(service, "JWS2 Alignment of " +
376 * alignFrame.getTitle(), msa, false, true,
377 * alignFrame.getViewport().getAlignment().getDataset(), alignFrame); }
385 public static void main(String[] args)
389 testUrls = new Vector<String>();
390 for (String url:args)
395 Thread runner = getDiscoverer().startDiscoverer(
396 new PropertyChangeListener()
399 public void propertyChange(PropertyChangeEvent evt)
401 if (getDiscoverer().services != null)
403 System.out.println("Changesupport: There are now "
404 + getDiscoverer().services.size() + " services");
406 for (Jws2Instance instance:getDiscoverer().services)
408 System.out.println("Service "+i+++" "+instance.getClass()+"@"+instance.getHost()+": "+instance.getActionText());
414 while (runner.isAlive())
419 } catch (InterruptedException e)
426 } catch (InterruptedException x) {}
429 private static Jws2Discoverer discoverer;
431 public static Jws2Discoverer getDiscoverer()
433 if (discoverer == null)
435 discoverer = new Jws2Discoverer();
440 public boolean hasServices()
442 // TODO Auto-generated method stub
443 return !running && services != null && services.size() > 0;
446 public boolean isRunning()
452 * the jalview .properties entry for JWS2 URLS
454 final static String JWS2HOSTURLS = "JWS2HOSTURLS";
456 public static void setServiceUrls(Vector<String> urls)
460 StringBuffer urlbuffer = new StringBuffer();
462 for (String url : urls)
464 urlbuffer.append(sep);
465 urlbuffer.append(url);
468 Cache.setProperty(JWS2HOSTURLS, urlbuffer.toString());
472 Cache.removeProperty(JWS2HOSTURLS);
476 private static Vector<String> testUrls=null;
477 public static Vector<String> getServiceUrls()
481 // return test urls, if there are any, instead of touching cache
484 String surls = Cache.getDefault(JWS2HOSTURLS,
485 "http://www.compbio.dundee.ac.uk/jabaws");
486 Vector<String> urls = new Vector<String>();
489 StringTokenizer st = new StringTokenizer(surls, ",");
490 while (st.hasMoreElements())
495 java.net.URL u = new java.net.URL(url = st.nextToken());
496 if (!urls.contains(url))
502 jalview.bin.Cache.log.info("Ignoring duplicate url in "
503 + JWS2HOSTURLS + " list");
505 } catch (Exception ex)
507 jalview.bin.Cache.log
508 .warn("Problem whilst trying to make a URL from '"
509 + ((url != null) ? url : "<null>") + "'");
510 jalview.bin.Cache.log
511 .warn("This was probably due to a malformed comma separated list"
514 + " entry of $(HOME)/.jalview_properties)");
515 jalview.bin.Cache.log.debug("Exception was ", ex);
518 } catch (Exception ex)
520 jalview.bin.Cache.log.warn(
521 "Error parsing comma separated list of urls in "
522 + JWS2HOSTURLS + " preference.", ex);
524 if (urls.size() >= 0)
531 public Vector<Jws2Instance> getServices()
533 return (services == null) ? new Vector<Jws2Instance>()
534 : new Vector<Jws2Instance>(services);
538 * test the given URL with the JabaWS test code
543 public static boolean testServiceUrl(URL foo)
547 compbio.ws.client.WSTester.main(new String[]
548 { "-h=" + foo.toString() });
549 } catch (Exception e)
553 } catch (OutOfMemoryError e)
567 * Start a fresh discovery thread and notify the given object when we're
568 * finished. Any known existing threads will be killed before this one is
571 * @param changeSupport2
574 public Thread startDiscoverer(PropertyChangeListener changeSupport2)
580 addPropertyChangeListener(changeSupport2);
581 Thread thr = new Thread(this);
586 Vector<String> invalidServiceUrls = null, urlsWithoutServices = null;
589 * @return the invalidServiceUrls
591 public Vector<String> getInvalidServiceUrls()
593 return invalidServiceUrls;
597 * @return the urlsWithoutServices
599 public Vector<String> getUrlsWithoutServices()
601 return urlsWithoutServices;
605 * add an 'empty' JABA server to the list. Only servers not already in the
606 * 'bad URL' list will be added to this list.
610 public synchronized void addUrlwithnoservices(String jwsservers)
612 if (urlsWithoutServices == null)
614 urlsWithoutServices = new Vector<String>();
617 if ((invalidServiceUrls == null || !invalidServiceUrls
618 .contains(jwsservers))
619 && !urlsWithoutServices.contains(jwsservers))
621 urlsWithoutServices.add(jwsservers);
626 * add a bad URL to the list
630 public synchronized void addInvalidServiceUrl(String jwsservers)
632 if (invalidServiceUrls == null)
634 invalidServiceUrls = new Vector<String>();
636 if (!invalidServiceUrls.contains(jwsservers))
638 invalidServiceUrls.add(jwsservers);
644 * @return a human readable report of any problems with the service URLs used
647 public String getErrorMessages()
649 if (!isRunning() && !isAborted())
651 StringBuffer ermsg = new StringBuffer();
652 boolean list = false;
653 if (getInvalidServiceUrls() != null
654 && getInvalidServiceUrls().size() > 0)
656 ermsg.append("URLs that could not be contacted: \n");
657 for (String svcurl : getInvalidServiceUrls())
664 ermsg.append(svcurl);
666 ermsg.append("\n\n");
669 if (getUrlsWithoutServices() != null
670 && getUrlsWithoutServices().size() > 0)
672 ermsg.append("URLs without any JABA Services : \n");
673 for (String svcurl : getUrlsWithoutServices())
680 ermsg.append(svcurl);
684 if (ermsg.length() > 1)
686 return ermsg.toString();