2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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.io.Closeable;
26 import java.net.ConnectException;
28 import java.util.HashSet;
29 import java.util.Hashtable;
30 import java.util.StringTokenizer;
31 import java.util.Vector;
33 import javax.swing.JMenu;
34 import javax.swing.JMenuItem;
35 import javax.swing.event.MenuEvent;
36 import javax.swing.event.MenuListener;
38 import org.apache.log4j.Level;
40 import jalview.bin.Cache;
41 import jalview.datamodel.AlignmentView;
42 import jalview.gui.AlignFrame;
43 import jalview.gui.Desktop;
44 import jalview.ws.WSMenuEntryProviderI;
45 import jalview.ws.params.ParamDatastoreI;
46 import compbio.data.msa.MsaWS;
47 import compbio.metadata.Option;
48 import compbio.metadata.Preset;
49 import compbio.metadata.PresetManager;
50 import compbio.metadata.RunnerConfig;
51 import compbio.ws.client.Jws2Client;
52 import compbio.ws.client.Services;
55 * discoverer for jws2 services. Follows the lightweight service discoverer
56 * pattern (archetyped by EnfinEnvision2OneWay)
61 public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
63 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
67 * change listeners are notified of "services" property changes
70 * to be added that consumes new services Hashtable object.
72 public void addPropertyChangeListener(
73 java.beans.PropertyChangeListener listener)
75 changeSupport.addPropertyChangeListener(listener);
84 public void removePropertyChangeListener(
85 java.beans.PropertyChangeListener listener)
87 changeSupport.removePropertyChangeListener(listener);
90 boolean running = false;
92 Thread oldthread = null;
96 if (running && oldthread != null && oldthread.isAlive())
101 oldthread = Thread.currentThread();
104 Class foo = getClass().getClassLoader().loadClass(
105 "compbio.ws.client.Jws2Client");
106 } catch (ClassNotFoundException e)
109 .println("Not enabling Jalview Webservices version 2: client jar is not available."
110 + "\nPlease check that your webstart JNLP file is up to date!");
114 if (services != null)
116 services.removeAllElements();
118 for (String jwsservers : getServiceUrls())
122 if (Jws2Client.validURL(jwsservers))
125 for (Services srv : Services.values())
127 MsaWS service = null;
130 service = Jws2Client.connect(jwsservers, srv);
131 } catch (Exception e)
133 System.err.println("Jws2 Discoverer: Problem on "
134 + jwsservers + " with service " + srv + ":\n"
136 if (!(e instanceof javax.xml.ws.WebServiceException))
144 addService(jwsservers, srv, service);
151 Cache.log.info("Ignoring invalid Jws2 service url " + jwsservers);
153 } catch (Exception e)
156 Cache.log.warn("Exception when discovering Jws2 services.", e);
159 Cache.log.error("Exception when discovering Jws2 services.", e);
164 changeSupport.firePropertyChange("services", new Vector(), services);
168 * record this service endpoint so we can use it
174 private void addService(String jwsservers, Services srv, MsaWS service2)
176 if (services == null)
178 services = new Vector<Jws2Instance>();
180 System.out.println("Discovered service: " + jwsservers + " "
182 Jws2Instance service = new Jws2Instance(jwsservers, srv.toString(), service2);
184 services.add(service);
185 // retrieve the presets and parameter set and cache now
186 service.getParamStore().getPresets();
187 service.hasParameters();
190 public class Jws2Instance
192 public String hosturl;
194 public String serviceType;
196 public MsaWS service;
198 public Jws2Instance(String hosturl, String serviceType, MsaWS service)
201 this.hosturl = hosturl;
202 this.serviceType = serviceType;
203 this.service = service;
206 PresetManager presets = null;
208 public JabaParamStore paramStore = null;
211 * non thread safe - gets the presets for this service (blocks whilst it
212 * calls the service to get the preset set)
214 * @return service presets or null if exceptions were raised.
216 public PresetManager getPresets()
222 presets = service.getPresets();
223 } catch (Exception ex)
226 .println("Exception when retrieving presets for service "
227 + serviceType + " at " + hosturl);
233 public String getHost()
237 * try { URL serviceurl = new URL(hosturl); if (serviceurl.getPort()!=80)
238 * { return serviceurl.getHost()+":"+serviceurl.getPort(); } return
239 * serviceurl.getHost(); } catch (Exception e) {
240 * System.err.println("Failed to parse service URL '" + hosturl +
241 * "' as a valid URL!"); } return null;
246 * @return short description of what the service will do
248 public String getActionText()
250 return "Align with " + serviceType;
254 * non-thread safe - blocks whilst accessing service to get complete set of
255 * available options and parameters
259 public RunnerConfig getRunnerConfig()
261 return service.getRunnerOptions();
265 protected void finalize() throws Throwable
271 Closeable svc = (Closeable) service;
274 } catch (Exception e)
282 public ParamDatastoreI getParamStore()
284 if (paramStore == null)
288 paramStore = new JabaParamStore(this,
289 (Desktop.instance != null ? Desktop
290 .getUserParameterStore() : null));
291 } catch (Exception ex)
299 public String getUri()
301 // this is only valid for Jaba 1.0 - this formula might have to change!
302 return hosturl+(hosturl.lastIndexOf("/")==(hosturl.length()-1) ? "/" : "") +serviceType;
304 private boolean hasParams=false,lookedForParams=false;
306 public boolean hasParameters()
308 if (!lookedForParams)
310 lookedForParams=true;
313 hasParams = (getRunnerConfig().getArguments().size() > 0);
314 } catch (Exception e)
324 * holds list of services.
326 protected Vector<Jws2Instance> services;
329 * find or add a submenu with the given title in the given menu
333 * @return the new or existing submenu
335 private JMenu findOrCreateMenu(JMenu menu, String submenu)
337 JMenu submenuinstance = null;
338 for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
340 if (menu.getMenuComponent(i) instanceof JMenu
341 && ((JMenu) menu.getMenuComponent(i)).getText().equals(
344 submenuinstance = (JMenu) menu.getMenuComponent(i);
347 if (submenuinstance == null)
349 submenuinstance = new JMenu(submenu);
350 menu.add(submenuinstance);
352 return submenuinstance;
356 public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
358 // dynamically regenerate service list.
359 final JMenu jws2al = new JMenu("JABAWS Alignment");
360 jws2al.addMenuListener(new MenuListener()
362 // TODO: future: add menu listener to parent menu - so submenus are
363 // populated *before* they are selected.
365 public void menuSelected(MenuEvent e)
367 populateWSMenuEntry(jws2al, alignFrame);
371 public void menuDeselected(MenuEvent e)
373 // TODO Auto-generated method stub
378 public void menuCanceled(MenuEvent e)
380 // TODO Auto-generated method stub
389 private void populateWSMenuEntry(JMenu jws2al, final AlignFrame alignFrame)
391 if (running || services == null || services.size() == 0)
395 boolean byhost = Cache.getDefault("WSMENU_BYHOST", true), bytype = Cache
396 .getDefault("WSMENU_BYTYPE", true);
398 * eventually, JWS2 services will appear under the same align/etc submenus.
399 * for moment we keep them separate.
402 MsaWSClient msacl = new MsaWSClient();
403 Vector hostLabels = new Vector();
405 String lasthost=null;
406 for (final Jws2Instance service : services)
409 String host = service.getHost();
410 String type = service.serviceType;
413 atpoint = findOrCreateMenu(atpoint, host);
414 if (atpoint.getToolTipText() == null)
416 atpoint.setToolTipText("Services at " + host);
421 atpoint = findOrCreateMenu(atpoint, type);
422 if (atpoint.getToolTipText() == null)
424 atpoint.setToolTipText(service.getActionText());
427 if (!byhost && !hostLabels.contains(host + service.serviceType+service.getActionText()))
428 //!hostLabels.contains(host + (bytype ? service.serviceType+service.getActionText() : "")))
430 // add a marker indicating where this service is hosted
431 // relies on services from the same host being listed in a contiguous
434 atpoint.addSeparator();
435 if (lasthost==null || !lasthost.equals(host))
437 atpoint.add(hitm = new JMenuItem(host));
438 hitm.setForeground(Color.blue);
441 hostLabels.addElement(host + service.serviceType+service.getActionText() );
442 // hostLabels.addElement(host + (bytype ? service.serviceType+service.getActionText() : ""));
444 msacl.attachWSMenuEntry(atpoint, service, alignFrame);
446 * JMenuItem sitem = new JMenuItem(service.serviceType);
447 * sitem.setToolTipText("Hosted at " + service.hosturl);
448 * sitem.addActionListener(new ActionListener() {
450 * @Override public void actionPerformed(ActionEvent e) { AlignmentView
451 * msa = alignFrame.gatherSequencesForAlignment(); MsaWSClient client =
452 * new MsaWSClient(service, "JWS2 Alignment of " + alignFrame.getTitle(),
453 * msa, false, true, alignFrame.getViewport().getAlignment().getDataset(),
459 public static void main(String[] args)
461 Thread runner = new Thread(getDiscoverer());
462 getDiscoverer().addPropertyChangeListener(new PropertyChangeListener()
465 public void propertyChange(PropertyChangeEvent evt)
467 System.out.println("Changesupport: There are now "
468 + getDiscoverer().services.size() + " services");
472 while (runner.isAlive())
477 } catch (InterruptedException e)
484 private static Jws2Discoverer discoverer;
486 public static Jws2Discoverer getDiscoverer()
488 if (discoverer == null)
490 discoverer = new Jws2Discoverer();
495 public boolean hasServices()
497 // TODO Auto-generated method stub
498 return !running && services != null && services.size() > 0;
501 public boolean isRunning()
507 * the jalview .properties entry for JWS2 URLS
509 final static String JWS2HOSTURLS = "JWS2HOSTURLS";
511 public static void setServiceUrls(Vector<String> urls)
515 StringBuffer urlbuffer = new StringBuffer();
517 for (String url : urls)
519 urlbuffer.append(sep);
520 urlbuffer.append(url);
523 Cache.setProperty(JWS2HOSTURLS, urlbuffer.toString());
527 Cache.removeProperty(JWS2HOSTURLS);
531 public static Vector<String> getServiceUrls()
533 String surls = Cache.getDefault(JWS2HOSTURLS,
534 "http://www.compbio.dundee.ac.uk/jabaws");
535 Vector<String> urls = new Vector<String>();
538 StringTokenizer st = new StringTokenizer(surls, ",");
539 while (st.hasMoreElements())
544 java.net.URL u = new java.net.URL(url = st.nextToken());
545 if (!urls.contains(url))
551 jalview.bin.Cache.log.info("Ignoring duplicate url in "
552 + JWS2HOSTURLS + " list");
554 } catch (Exception ex)
556 jalview.bin.Cache.log
557 .warn("Problem whilst trying to make a URL from '"
558 + ((url != null) ? url : "<null>") + "'");
559 jalview.bin.Cache.log
560 .warn("This was probably due to a malformed comma separated list"
563 + " entry of $(HOME)/.jalview_properties)");
564 jalview.bin.Cache.log.debug("Exception was ", ex);
567 } catch (Exception ex)
569 jalview.bin.Cache.log.warn(
570 "Error parsing comma separated list of urls in "
571 + JWS2HOSTURLS + " preference.", ex);
573 if (urls.size() >= 0)
580 public Vector<Jws2Instance> getServices()
582 return (services == null) ? new Vector<Jws2Instance>()
583 : new Vector<Jws2Instance>(services);
587 * test the given URL with the JabaWS test code
592 public static boolean testServiceUrl(URL foo)
596 compbio.ws.client.WSTester.main(new String[]
597 { "-h=" + foo.toString() });
598 } catch (Exception e)
601 } catch (OutOfMemoryError e)