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, Services srv,
211 if (services == null)
213 services = new Vector<Jws2Instance>();
215 System.out.println("Discovered service: " + jwsservers + " "
217 Jws2Instance service = new Jws2Instance(jwsservers, srv.toString(),
220 services.add(service);
221 // retrieve the presets and parameter set and cache now
222 service.getParamStore().getPresets();
223 service.hasParameters();
227 * holds list of services.
229 protected Vector<Jws2Instance> services;
231 public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
233 // dynamically regenerate service list.
234 final JMenu jws2al = wsmenu; // new JMenu("JABAWS Alignment");
235 jws2al.addMenuListener(new MenuListener()
237 // TODO: future: add menu listener to parent menu - so submenus are
238 // populated *before* they are selected.
240 public void menuSelected(MenuEvent e)
242 populateWSMenuEntry(jws2al, alignFrame);
246 public void menuDeselected(MenuEvent e)
248 // TODO Auto-generated method stub
253 public void menuCanceled(MenuEvent e)
255 // TODO Auto-generated method stub
264 private void populateWSMenuEntry(JMenu jws2al, final AlignFrame alignFrame)
266 if (running || services == null || services.size() == 0)
270 boolean byhost = Cache.getDefault("WSMENU_BYHOST", false), bytype = Cache
271 .getDefault("WSMENU_BYTYPE", false);
273 * eventually, JWS2 services will appear under the same align/etc submenus.
274 * for moment we keep them separate.
277 MsaWSClient msacl = new MsaWSClient();
278 Vector hostLabels = new Vector();
280 String lasthost = null;
281 Hashtable<String, ArrayList<Jws2Instance>> hosts = new Hashtable<String, ArrayList<Jws2Instance>>();
283 for (Jws2Instance service : services)
285 ArrayList<Jws2Instance> hostservices = hosts.get(service.getHost());
286 if (hostservices == null)
288 hosts.put(service.getHost(),
289 hostservices = new ArrayList<Jws2Instance>());
291 hostservices.add(service);
293 sorton = hosts.keySet().toArray(new String[1]);
294 String hostlist[] = sorton.clone();
295 jalview.util.QuickSort.sort(sorton, hostlist);
296 for (String host : hostlist)
298 Jws2Instance orderedsvcs[] = hosts.get(host).toArray(
299 new Jws2Instance[1]);
300 String sortbytype[] = new String[orderedsvcs.length];
301 for (int i = 0; i < sortbytype.length; i++)
303 sortbytype[i] = orderedsvcs[i].serviceType;
305 jalview.util.QuickSort.sort(sortbytype, orderedsvcs);
306 for (final Jws2Instance service : orderedsvcs)
309 String type = service.serviceType;
312 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, host);
313 if (atpoint.getToolTipText() == null)
315 atpoint.setToolTipText("Services at " + host);
320 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, type);
321 if (atpoint.getToolTipText() == null)
323 atpoint.setToolTipText(service.getActionText());
327 && !hostLabels.contains(host + service.serviceType
328 + service.getActionText()))
329 // !hostLabels.contains(host + (bytype ?
330 // service.serviceType+service.getActionText() : "")))
332 // add a marker indicating where this service is hosted
333 // relies on services from the same host being listed in a
337 atpoint.addSeparator();
338 if (lasthost == null || !lasthost.equals(host))
340 atpoint.add(hitm = new JMenuItem(host));
341 hitm.setForeground(Color.blue);
342 hitm.addActionListener(new ActionListener()
346 public void actionPerformed(ActionEvent e)
348 Desktop.showUrl(service.getHost());
351 hitm.setToolTipText(JvSwingUtils
352 .wrapTooltip("Opens the JABAWS server's homepage in web browser"));
355 hostLabels.addElement(host + service.serviceType
356 + service.getActionText());
357 // hostLabels.addElement(host + (bytype ?
358 // service.serviceType+service.getActionText() : ""));
360 msacl.attachWSMenuEntry(atpoint, service, alignFrame);
362 * JMenuItem sitem = new JMenuItem(service.serviceType);
363 * sitem.setToolTipText("Hosted at " + service.hosturl);
364 * sitem.addActionListener(new ActionListener() {
366 * @Override public void actionPerformed(ActionEvent e) { AlignmentView
367 * msa = alignFrame.gatherSequencesForAlignment(); MsaWSClient client =
368 * new MsaWSClient(service, "JWS2 Alignment of " +
369 * alignFrame.getTitle(), msa, false, true,
370 * alignFrame.getViewport().getAlignment().getDataset(), alignFrame); }
378 public static void main(String[] args)
380 Thread runner = getDiscoverer().startDiscoverer(
381 new PropertyChangeListener()
384 public void propertyChange(PropertyChangeEvent evt)
386 if (getDiscoverer().services != null)
388 System.out.println("Changesupport: There are now "
389 + getDiscoverer().services.size() + " services");
394 while (runner.isAlive())
399 } catch (InterruptedException e)
406 private static Jws2Discoverer discoverer;
408 public static Jws2Discoverer getDiscoverer()
410 if (discoverer == null)
412 discoverer = new Jws2Discoverer();
417 public boolean hasServices()
419 // TODO Auto-generated method stub
420 return !running && services != null && services.size() > 0;
423 public boolean isRunning()
429 * the jalview .properties entry for JWS2 URLS
431 final static String JWS2HOSTURLS = "JWS2HOSTURLS";
433 public static void setServiceUrls(Vector<String> urls)
437 StringBuffer urlbuffer = new StringBuffer();
439 for (String url : urls)
441 urlbuffer.append(sep);
442 urlbuffer.append(url);
445 Cache.setProperty(JWS2HOSTURLS, urlbuffer.toString());
449 Cache.removeProperty(JWS2HOSTURLS);
453 public static Vector<String> getServiceUrls()
455 String surls = Cache.getDefault(JWS2HOSTURLS,
456 "http://www.compbio.dundee.ac.uk/jabaws");
457 Vector<String> urls = new Vector<String>();
460 StringTokenizer st = new StringTokenizer(surls, ",");
461 while (st.hasMoreElements())
466 java.net.URL u = new java.net.URL(url = st.nextToken());
467 if (!urls.contains(url))
473 jalview.bin.Cache.log.info("Ignoring duplicate url in "
474 + JWS2HOSTURLS + " list");
476 } catch (Exception ex)
478 jalview.bin.Cache.log
479 .warn("Problem whilst trying to make a URL from '"
480 + ((url != null) ? url : "<null>") + "'");
481 jalview.bin.Cache.log
482 .warn("This was probably due to a malformed comma separated list"
485 + " entry of $(HOME)/.jalview_properties)");
486 jalview.bin.Cache.log.debug("Exception was ", ex);
489 } catch (Exception ex)
491 jalview.bin.Cache.log.warn(
492 "Error parsing comma separated list of urls in "
493 + JWS2HOSTURLS + " preference.", ex);
495 if (urls.size() >= 0)
502 public Vector<Jws2Instance> getServices()
504 return (services == null) ? new Vector<Jws2Instance>()
505 : new Vector<Jws2Instance>(services);
509 * test the given URL with the JabaWS test code
514 public static boolean testServiceUrl(URL foo)
518 compbio.ws.client.WSTester.main(new String[]
519 { "-h=" + foo.toString() });
520 } catch (Exception e)
524 } catch (OutOfMemoryError e)
538 * Start a fresh discovery thread and notify the given object when we're
539 * finished. Any known existing threads will be killed before this one is
542 * @param changeSupport2
545 public Thread startDiscoverer(PropertyChangeListener changeSupport2)
551 addPropertyChangeListener(changeSupport2);
552 Thread thr = new Thread(this);
557 Vector<String> invalidServiceUrls = null, urlsWithoutServices = null;
560 * @return the invalidServiceUrls
562 public Vector<String> getInvalidServiceUrls()
564 return invalidServiceUrls;
568 * @return the urlsWithoutServices
570 public Vector<String> getUrlsWithoutServices()
572 return urlsWithoutServices;
576 * add an 'empty' JABA server to the list. Only servers not already in the
577 * 'bad URL' list will be added to this list.
581 public synchronized void addUrlwithnoservices(String jwsservers)
583 if (urlsWithoutServices == null)
585 urlsWithoutServices = new Vector<String>();
588 if ((invalidServiceUrls == null || !invalidServiceUrls
589 .contains(jwsservers))
590 && !urlsWithoutServices.contains(jwsservers))
592 urlsWithoutServices.add(jwsservers);
597 * add a bad URL to the list
601 public synchronized void addInvalidServiceUrl(String jwsservers)
603 if (invalidServiceUrls == null)
605 invalidServiceUrls = new Vector<String>();
607 if (!invalidServiceUrls.contains(jwsservers))
609 invalidServiceUrls.add(jwsservers);
615 * @return a human readable report of any problems with the service URLs used
618 public String getErrorMessages()
620 if (!isRunning() && !isAborted())
622 StringBuffer ermsg = new StringBuffer();
623 boolean list = false;
624 if (getInvalidServiceUrls() != null
625 && getInvalidServiceUrls().size() > 0)
627 ermsg.append("URLs that could not be contacted: \n");
628 for (String svcurl : getInvalidServiceUrls())
635 ermsg.append(svcurl);
637 ermsg.append("\n\n");
640 if (getUrlsWithoutServices() != null
641 && getUrlsWithoutServices().size() > 0)
643 ermsg.append("URLs without any JABA Services : \n");
644 for (String svcurl : getUrlsWithoutServices())
651 ermsg.append(svcurl);
655 if (ermsg.length() > 1)
657 return ermsg.toString();