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 jalview.bin.Cache;
21 import jalview.gui.AlignFrame;
22 import jalview.gui.Desktop;
23 import jalview.gui.JvSwingUtils;
24 import jalview.ws.WSMenuEntryProviderI;
25 import jalview.ws.jws2.jabaws2.Jws2Instance;
27 import java.awt.Color;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.beans.PropertyChangeEvent;
31 import java.beans.PropertyChangeListener;
33 import java.util.ArrayList;
34 import java.util.Hashtable;
35 import java.util.List;
36 import java.util.StringTokenizer;
37 import java.util.Vector;
39 import javax.swing.JMenu;
40 import javax.swing.JMenuItem;
42 import compbio.ws.client.Services;
45 * discoverer for jws2 services. Follows the lightweight service discoverer
46 * pattern (archetyped by EnfinEnvision2OneWay)
51 public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
53 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
57 * change listeners are notified of "services" property changes
60 * to be added that consumes new services Hashtable object.
62 public void addPropertyChangeListener(
63 java.beans.PropertyChangeListener listener)
65 changeSupport.addPropertyChangeListener(listener);
74 public void removePropertyChangeListener(
75 java.beans.PropertyChangeListener listener)
77 changeSupport.removePropertyChangeListener(listener);
80 boolean running = false, aborted = false;
85 public boolean isAborted()
94 public void setAborted(boolean aborted)
96 this.aborted = aborted;
99 Thread oldthread = null;
103 if (running && oldthread != null && oldthread.isAlive())
114 .debug("Waiting around for old discovery thread to finish.");
115 // wait around until old discoverer dies
117 } catch (Exception e)
121 Cache.log.debug("Old discovery thread has finished.");
124 changeSupport.firePropertyChange("services", services, new Vector());
125 oldthread = Thread.currentThread();
128 Class foo = getClass().getClassLoader().loadClass(
129 "compbio.ws.client.Jws2Client");
130 } catch (ClassNotFoundException e)
133 .println("Not enabling JABA Webservices : client jar is not available."
134 + "\nPlease check that your webstart JNLP file is up to date!");
138 // reinitialise records of good and bad service URLs
139 if (services != null)
141 services.removeAllElements();
143 if (urlsWithoutServices != null)
145 urlsWithoutServices.removeAllElements();
147 if (invalidServiceUrls != null)
149 invalidServiceUrls.removeAllElements();
151 if (validServiceUrls != null)
153 validServiceUrls.removeAllElements();
155 ArrayList<String> svctypes=new ArrayList<String>();
157 List<JabaWsServerQuery> qrys = new ArrayList<JabaWsServerQuery>();
158 for (final String jwsservers : getServiceUrls())
160 JabaWsServerQuery squery = new JabaWsServerQuery(this, jwsservers);
161 if (svctypes.size()==0)
163 // TODO: remove this ugly hack to get Canonical JABA service ordering for all possible services
164 for (Services sv:squery.JABAWS2SERVERS)
166 svctypes.add(sv.toString());
171 new Thread(squery).start();
173 boolean finished = true;
180 } catch (Exception e)
184 for (JabaWsServerQuery squery : qrys)
186 finished = finished && !squery.isRunning();
190 Cache.log.debug("Aborting " + qrys.size()
191 + " JABAWS discovery threads.");
192 for (JabaWsServerQuery squery : qrys)
194 squery.setQuit(true);
197 } while (!aborted && !finished);
200 // resort services according to order found in jabaws service list
201 // also ensure servics for each host are ordered in same way.
203 if (services!=null && services.size()>0)
205 Jws2Instance[] svcs=new Jws2Instance[services.size()];
206 int[] spos=new int[services.size()];
208 Vector svcUrls = getServiceUrls();
209 for (Jws2Instance svc:services)
212 spos[ipos++]=1000*svcUrls.indexOf(svc.getHost()) + 1+svctypes.indexOf(svc.serviceType);
214 jalview.util.QuickSort.sort(spos, svcs);
215 services=new Vector<Jws2Instance>();
216 for (Jws2Instance svc:svcs) {
223 changeSupport.firePropertyChange("services", new Vector(), services);
227 * record this service endpoint so we can use it
233 synchronized void addService(String jwsservers, Jws2Instance service)
235 if (services == null)
237 services = new Vector<Jws2Instance>();
239 System.out.println("Discovered service: " + jwsservers + " "
240 + service.toString());
241 // Jws2Instance service = new Jws2Instance(jwsservers, srv.toString(),
244 services.add(service);
245 // retrieve the presets and parameter set and cache now
246 service.getParamStore().getPresets();
247 service.hasParameters();
248 if (validServiceUrls==null)
250 validServiceUrls=new Vector();
252 validServiceUrls.add(jwsservers);
256 * holds list of services.
258 protected Vector<Jws2Instance> services;
260 * attach all available web services to the appropriate submenu in the given JMenu
262 public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
264 // dynamically regenerate service list.
265 populateWSMenuEntry(wsmenu, alignFrame, null);
268 private void populateWSMenuEntry(JMenu jws2al, final AlignFrame alignFrame, String typeFilter)
270 if (running || services == null || services.size() == 0)
274 boolean byhost = Cache.getDefault("WSMENU_BYHOST", false), bytype = Cache
275 .getDefault("WSMENU_BYTYPE", false);
277 * eventually, JWS2 services will appear under the same align/etc submenus.
278 * for moment we keep them separate.
281 MsaWSClient msacl = new MsaWSClient();
282 Vector hostLabels = new Vector();
284 Hashtable<String,String> lasthostFor = new Hashtable<String,String>();
285 Hashtable<String, ArrayList<Jws2Instance>> hosts = new Hashtable<String, ArrayList<Jws2Instance>>();
286 ArrayList<String> hostlist=new ArrayList<String>();
287 for (Jws2Instance service : services)
289 ArrayList<Jws2Instance> hostservices = hosts.get(service.getHost());
290 if (hostservices == null)
292 hosts.put(service.getHost(),
293 hostservices = new ArrayList<Jws2Instance>());
294 hostlist.add(service.getHost());
296 hostservices.add(service);
298 // now add hosts in order of the given array
299 for (String host : hostlist)
301 Jws2Instance orderedsvcs[] = hosts.get(host).toArray(
302 new Jws2Instance[1]);
303 String sortbytype[] = new String[orderedsvcs.length];
304 for (int i = 0; i < sortbytype.length; i++)
306 sortbytype[i] = orderedsvcs[i].serviceType;
308 jalview.util.QuickSort.sort(sortbytype, orderedsvcs);
309 for (final Jws2Instance service : orderedsvcs)
311 atpoint = JvSwingUtils.findOrCreateMenu(jws2al,service.action);
312 String type = service.serviceType;
315 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, host);
316 if (atpoint.getToolTipText() == null)
318 atpoint.setToolTipText("Services at " + host);
323 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, type);
324 if (atpoint.getToolTipText() == null)
326 atpoint.setToolTipText(service.getActionText());
330 && !hostLabels.contains(host + service.serviceType
331 + service.getActionText()))
332 // !hostLabels.contains(host + (bytype ?
333 // service.serviceType+service.getActionText() : "")))
335 // add a marker indicating where this service is hosted
336 // relies on services from the same host being listed in a
340 atpoint.addSeparator();
341 if (lasthostFor.get(service.action) == null || !lasthostFor.get(service.action).equals(host))
343 atpoint.add(hitm = new JMenuItem(host));
344 hitm.setForeground(Color.blue);
345 hitm.addActionListener(new ActionListener()
349 public void actionPerformed(ActionEvent e)
351 Desktop.showUrl(service.getHost());
354 hitm.setToolTipText(JvSwingUtils
355 .wrapTooltip("Opens the JABAWS server's homepage in web browser"));
356 lasthostFor.put(service.action,host);
358 hostLabels.addElement(host + service.serviceType
359 + service.getActionText());
360 // hostLabels.addElement(host + (bytype ?
361 // service.serviceType+service.getActionText() : ""));
364 service.attachWSMenuEntry(atpoint, alignFrame);
366 * JMenuItem sitem = new JMenuItem(service.serviceType);
367 * sitem.setToolTipText("Hosted at " + service.hosturl);
368 * sitem.addActionListener(new ActionListener() {
370 * @Override public void actionPerformed(ActionEvent e) { AlignmentView
371 * msa = alignFrame.gatherSequencesForAlignment(); MsaWSClient client =
372 * new MsaWSClient(service, "JWS2 Alignment of " +
373 * alignFrame.getTitle(), msa, false, true,
374 * alignFrame.getViewport().getAlignment().getDataset(), alignFrame); }
382 public static void main(String[] args)
386 testUrls = new Vector<String>();
387 for (String url:args)
392 Thread runner = getDiscoverer().startDiscoverer(
393 new PropertyChangeListener()
396 public void propertyChange(PropertyChangeEvent evt)
398 if (getDiscoverer().services != null)
400 System.out.println("Changesupport: There are now "
401 + getDiscoverer().services.size() + " services");
403 for (Jws2Instance instance:getDiscoverer().services)
405 System.out.println("Service "+i+++" "+instance.getClass()+"@"+instance.getHost()+": "+instance.getActionText());
411 while (runner.isAlive())
416 } catch (InterruptedException e)
423 } catch (InterruptedException x) {}
426 private static Jws2Discoverer discoverer;
428 public static Jws2Discoverer getDiscoverer()
430 if (discoverer == null)
432 discoverer = new Jws2Discoverer();
437 public boolean hasServices()
439 // TODO Auto-generated method stub
440 return !running && services != null && services.size() > 0;
443 public boolean isRunning()
449 * the jalview .properties entry for JWS2 URLS
451 final static String JWS2HOSTURLS = "JWS2HOSTURLS";
453 public static void setServiceUrls(Vector<String> urls)
457 StringBuffer urlbuffer = new StringBuffer();
459 for (String url : urls)
461 urlbuffer.append(sep);
462 urlbuffer.append(url);
465 Cache.setProperty(JWS2HOSTURLS, urlbuffer.toString());
469 Cache.removeProperty(JWS2HOSTURLS);
473 private static Vector<String> testUrls=null;
474 public static Vector<String> getServiceUrls()
478 // return test urls, if there are any, instead of touching cache
481 String surls = Cache.getDefault(JWS2HOSTURLS,
482 "http://www.compbio.dundee.ac.uk/jabaws");
483 Vector<String> urls = new Vector<String>();
486 StringTokenizer st = new StringTokenizer(surls, ",");
487 while (st.hasMoreElements())
492 java.net.URL u = new java.net.URL(url = st.nextToken());
493 if (!urls.contains(url))
499 jalview.bin.Cache.log.info("Ignoring duplicate url in "
500 + JWS2HOSTURLS + " list");
502 } catch (Exception ex)
504 jalview.bin.Cache.log
505 .warn("Problem whilst trying to make a URL from '"
506 + ((url != null) ? url : "<null>") + "'");
507 jalview.bin.Cache.log
508 .warn("This was probably due to a malformed comma separated list"
511 + " entry of $(HOME)/.jalview_properties)");
512 jalview.bin.Cache.log.debug("Exception was ", ex);
515 } catch (Exception ex)
517 jalview.bin.Cache.log.warn(
518 "Error parsing comma separated list of urls in "
519 + JWS2HOSTURLS + " preference.", ex);
521 if (urls.size() >= 0)
528 public Vector<Jws2Instance> getServices()
530 return (services == null) ? new Vector<Jws2Instance>()
531 : new Vector<Jws2Instance>(services);
535 * test the given URL with the JabaWS test code
540 public static boolean testServiceUrl(URL foo)
544 compbio.ws.client.WSTester.main(new String[]
545 { "-h=" + foo.toString() });
546 } catch (Exception e)
550 } catch (OutOfMemoryError e)
564 * Start a fresh discovery thread and notify the given object when we're
565 * finished. Any known existing threads will be killed before this one is
568 * @param changeSupport2
571 public Thread startDiscoverer(PropertyChangeListener changeSupport2)
577 addPropertyChangeListener(changeSupport2);
578 Thread thr = new Thread(this);
583 Vector<String> invalidServiceUrls = null, urlsWithoutServices = null, validServiceUrls=null;
586 * @return the invalidServiceUrls
588 public Vector<String> getInvalidServiceUrls()
590 return invalidServiceUrls;
594 * @return the urlsWithoutServices
596 public Vector<String> getUrlsWithoutServices()
598 return urlsWithoutServices;
602 * add an 'empty' JABA server to the list. Only servers not already in the
603 * 'bad URL' list will be added to this list.
607 public synchronized void addUrlwithnoservices(String jwsservers)
609 if (urlsWithoutServices == null)
611 urlsWithoutServices = new Vector<String>();
614 if ((invalidServiceUrls == null || !invalidServiceUrls
615 .contains(jwsservers))
616 && !urlsWithoutServices.contains(jwsservers))
618 urlsWithoutServices.add(jwsservers);
623 * add a bad URL to the list
627 public synchronized void addInvalidServiceUrl(String jwsservers)
629 if (invalidServiceUrls == null)
631 invalidServiceUrls = new Vector<String>();
633 if (!invalidServiceUrls.contains(jwsservers))
635 invalidServiceUrls.add(jwsservers);
641 * @return a human readable report of any problems with the service URLs used
644 public String getErrorMessages()
646 if (!isRunning() && !isAborted())
648 StringBuffer ermsg = new StringBuffer();
649 boolean list = false;
650 if (getInvalidServiceUrls() != null
651 && getInvalidServiceUrls().size() > 0)
653 ermsg.append("URLs that could not be contacted: \n");
654 for (String svcurl : getInvalidServiceUrls())
661 ermsg.append(svcurl);
663 ermsg.append("\n\n");
666 if (getUrlsWithoutServices() != null
667 && getUrlsWithoutServices().size() > 0)
669 ermsg.append("URLs without any JABA Services : \n");
670 for (String svcurl : getUrlsWithoutServices())
677 ermsg.append(svcurl);
681 if (ermsg.length() > 1)
683 return ermsg.toString();
690 public int getServerStatusFor(String url)
692 if (validServiceUrls!=null && validServiceUrls.contains(url))
696 if (urlsWithoutServices!=null && urlsWithoutServices.contains(url))
698 if (invalidServiceUrls!=null && invalidServiceUrls.contains(url))