1 package jalview.ws.jws2;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.beans.PropertyChangeEvent;
7 import java.beans.PropertyChangeListener;
8 import java.io.Closeable;
9 import java.net.ConnectException;
11 import java.util.HashSet;
12 import java.util.Hashtable;
13 import java.util.StringTokenizer;
14 import java.util.Vector;
16 import javax.swing.JMenu;
17 import javax.swing.JMenuItem;
18 import javax.swing.event.MenuEvent;
19 import javax.swing.event.MenuListener;
21 import org.apache.log4j.Level;
23 import jalview.bin.Cache;
24 import jalview.datamodel.AlignmentView;
25 import jalview.gui.AlignFrame;
26 import jalview.ws.WSMenuEntryProviderI;
27 import jalview.ws.params.ParamDatastoreI;
28 import compbio.data.msa.MsaWS;
29 import compbio.metadata.Option;
30 import compbio.metadata.Preset;
31 import compbio.metadata.PresetManager;
32 import compbio.metadata.RunnerConfig;
33 import compbio.ws.client.Jws2Client;
34 import compbio.ws.client.Services;
37 * discoverer for jws2 services. Follows the lightweight service discoverer
38 * pattern (archetyped by EnfinEnvision2OneWay)
43 public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
45 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
49 * change listeners are notified of "services" property changes
52 * to be added that consumes new services Hashtable object.
54 public void addPropertyChangeListener(
55 java.beans.PropertyChangeListener listener)
57 changeSupport.addPropertyChangeListener(listener);
66 public void removePropertyChangeListener(
67 java.beans.PropertyChangeListener listener)
69 changeSupport.removePropertyChangeListener(listener);
72 boolean running = false;
74 Thread oldthread = null;
78 if (running && oldthread != null && oldthread.isAlive())
83 oldthread = Thread.currentThread();
86 Class foo = getClass().getClassLoader().loadClass(
87 "compbio.ws.client.Jws2Client");
88 } catch (ClassNotFoundException e)
91 .println("Not enabling Jalview Webservices version 2: client jar is not available."
92 + "\nPlease check that your webstart JNLP file is up to date!");
98 services.removeAllElements();
100 for (String jwsservers : getServiceUrls())
104 if (Jws2Client.validURL(jwsservers))
107 for (Services srv : Services.values())
109 MsaWS service = null;
112 service = Jws2Client.connect(jwsservers, srv);
113 } catch (Exception e)
115 System.err.println("Jws2 Discoverer: Problem on "
116 + jwsservers + " with service " + srv + ":\n"
118 if (!(e instanceof javax.xml.ws.WebServiceException))
126 addService(jwsservers, srv, service);
133 Cache.log.info("Ignoring invalid Jws2 service url " + jwsservers);
135 } catch (Exception e)
138 Cache.log.warn("Exception when discovering Jws2 services.", e);
141 Cache.log.error("Exception when discovering Jws2 services.", e);
146 changeSupport.firePropertyChange("services", new Vector(), services);
150 * record this service endpoint so we can use it
156 private void addService(String jwsservers, Services srv, MsaWS service2)
158 if (services == null)
160 services = new Vector<Jws2Instance>();
162 System.out.println("Discovered service: " + jwsservers + " "
164 services.add(new Jws2Instance(jwsservers, srv.toString(), service2));
167 public class Jws2Instance
169 public String hosturl;
171 public String serviceType;
173 public MsaWS service;
175 public Jws2Instance(String hosturl, String serviceType, MsaWS service)
178 this.hosturl = hosturl;
179 this.serviceType = serviceType;
180 this.service = service;
183 PresetManager presets = null;
185 public JabaParamStore paramStore=null;
188 * non thread safe - gets the presets for this service (blocks whilst it
189 * calls the service to get the preset set)
191 * @return service presets or null if exceptions were raised.
193 public PresetManager getPresets()
199 presets = service.getPresets();
200 } catch (Exception ex)
203 .println("Exception when retrieving presets for service "
204 + serviceType + " at " + hosturl);
210 public String getHost()
214 * try { URL serviceurl = new URL(hosturl); if (serviceurl.getPort()!=80)
215 * { return serviceurl.getHost()+":"+serviceurl.getPort(); } return
216 * serviceurl.getHost(); } catch (Exception e) {
217 * System.err.println("Failed to parse service URL '" + hosturl +
218 * "' as a valid URL!"); } return null;
223 * @return short description of what the service will do
225 public String getActionText()
227 return "Align with " + serviceType;
231 * non-thread safe - blocks whilst accessing service to get complete set of
232 * available options and parameters
236 public RunnerConfig getRunnerConfig()
238 return service.getRunnerOptions();
242 protected void finalize() throws Throwable
248 Closeable svc = (Closeable) service;
251 } catch (Exception e)
259 public ParamDatastoreI getParamStore()
261 if (paramStore == null)
264 paramStore = new JabaParamStore(this);
265 } catch (Exception ex)
274 * holds list of services.
276 protected Vector<Jws2Instance> services;
279 * find or add a submenu with the given title in the given menu
283 * @return the new or existing submenu
285 private JMenu findOrCreateMenu(JMenu menu, String submenu)
287 JMenu submenuinstance = null;
288 for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
290 if (menu.getMenuComponent(i) instanceof JMenu
291 && ((JMenu) menu.getMenuComponent(i)).getText().equals(
294 submenuinstance = (JMenu) menu.getMenuComponent(i);
297 if (submenuinstance == null)
299 submenuinstance = new JMenu(submenu);
300 menu.add(submenuinstance);
302 return submenuinstance;
306 public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
308 // dynamically regenerate service list.
309 final JMenu jws2al = new JMenu("JABA Alignment");
310 jws2al.addMenuListener(new MenuListener() {
311 // TODO: future: add menu listener to parent menu - so submenus are populated *before* they are selected.
313 public void menuSelected(MenuEvent e)
315 populateWSMenuEntry(jws2al, alignFrame);
319 public void menuDeselected(MenuEvent e)
321 // TODO Auto-generated method stub
326 public void menuCanceled(MenuEvent e)
328 // TODO Auto-generated method stub
335 private void populateWSMenuEntry(JMenu jws2al, final AlignFrame alignFrame)
337 if (running || services == null || services.size() == 0)
341 boolean byhost = Cache.getDefault("WSMENU_BYHOST", true), bytype = Cache
342 .getDefault("WSMENU_BYTYPE", true);
344 * eventually, JWS2 services will appear under the same align/etc submenus.
345 * for moment we keep them separate.
348 MsaWSClient msacl = new MsaWSClient();
349 Vector hostLabels = new Vector();
351 for (final Jws2Instance service : services)
354 String host = service.getHost();
355 String type = service.serviceType;
358 atpoint = findOrCreateMenu(atpoint, host);
359 if (atpoint.getToolTipText() == null)
361 atpoint.setToolTipText("Services at " + host);
366 atpoint = findOrCreateMenu(atpoint, type);
367 if (atpoint.getToolTipText() == null)
369 atpoint.setToolTipText(service.getActionText());
372 if (!byhost && !hostLabels.contains(host + service.getActionText()))
374 // add a marker indicating where this service is hosted
375 // relies on services from the same host being listed in a contiguous
378 atpoint.addSeparator();
379 atpoint.add(hitm = new JMenuItem(host));
380 hitm.setForeground(Color.blue);
381 hostLabels.addElement(host);
383 msacl.attachWSMenuEntry(atpoint, service, alignFrame);
385 * JMenuItem sitem = new JMenuItem(service.serviceType);
386 * sitem.setToolTipText("Hosted at " + service.hosturl);
387 * sitem.addActionListener(new ActionListener() {
389 * @Override public void actionPerformed(ActionEvent e) { AlignmentView
390 * msa = alignFrame.gatherSequencesForAlignment(); MsaWSClient client =
391 * new MsaWSClient(service, "JWS2 Alignment of " + alignFrame.getTitle(),
392 * msa, false, true, alignFrame.getViewport().getAlignment().getDataset(),
398 public static void main(String[] args)
400 Thread runner = new Thread(getDiscoverer());
401 getDiscoverer().addPropertyChangeListener(new PropertyChangeListener()
404 public void propertyChange(PropertyChangeEvent evt)
406 System.out.println("Changesupport: There are now "
407 + getDiscoverer().services.size() + " services");
411 while (runner.isAlive())
416 } catch (InterruptedException e)
423 private static Jws2Discoverer discoverer;
425 public static Jws2Discoverer getDiscoverer()
427 if (discoverer == null)
429 discoverer = new Jws2Discoverer();
434 public boolean hasServices()
436 // TODO Auto-generated method stub
437 return !running && services != null && services.size() > 0;
440 public boolean isRunning()
446 * the jalview .properties entry for JWS2 URLS
448 final static String JWS2HOSTURLS = "JWS2HOSTURLS";
450 public static void setServiceUrls(Vector<String> urls)
454 StringBuffer urlbuffer = new StringBuffer();
456 for (String url : urls)
458 urlbuffer.append(sep);
459 urlbuffer.append(url);
462 Cache.setProperty(JWS2HOSTURLS, urlbuffer.toString());
466 Cache.removeProperty(JWS2HOSTURLS);
470 public static Vector<String> getServiceUrls()
472 String surls = Cache.getDefault(JWS2HOSTURLS,
473 "http://www.compbio.dundee.ac.uk/jabaws");
474 Vector<String> urls = new Vector<String>();
477 StringTokenizer st = new StringTokenizer(surls, ",");
478 while (st.hasMoreElements())
483 java.net.URL u = new java.net.URL(url = st.nextToken());
484 if (!urls.contains(url))
490 jalview.bin.Cache.log.info("Ignoring duplicate url in "
491 + JWS2HOSTURLS + " list");
493 } catch (Exception ex)
495 jalview.bin.Cache.log
496 .warn("Problem whilst trying to make a URL from '"
497 + ((url != null) ? url : "<null>") + "'");
498 jalview.bin.Cache.log
499 .warn("This was probably due to a malformed comma separated list"
502 + " entry of $(HOME)/.jalview_properties)");
503 jalview.bin.Cache.log.debug("Exception was ", ex);
506 } catch (Exception ex)
508 jalview.bin.Cache.log.warn(
509 "Error parsing comma separated list of urls in "
510 + JWS2HOSTURLS + " preference.", ex);
512 if (urls.size() >= 0)
519 public Vector<Jws2Instance> getServices()
521 return (services == null) ? new Vector<Jws2Instance>()
522 : new Vector<Jws2Instance>(services);
526 * test the given URL with the JabaWS test code
530 public static boolean testServiceUrl(URL foo)
533 compbio.ws.client.WSTester.main(new String[] { "-h="+foo.toString()});
534 } catch (Exception e)
538 catch (OutOfMemoryError e)