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;
19 import org.apache.log4j.Level;
21 import jalview.bin.Cache;
22 import jalview.datamodel.AlignmentView;
23 import jalview.gui.AlignFrame;
24 import jalview.ws.WSMenuEntryProviderI;
25 import compbio.data.msa.MsaWS;
26 import compbio.metadata.Option;
27 import compbio.metadata.Preset;
28 import compbio.metadata.PresetManager;
29 import compbio.metadata.RunnerConfig;
30 import compbio.ws.client.Jws2Client;
31 import compbio.ws.client.Services;
34 * discoverer for jws2 services. Follows the lightweight service discoverer
35 * pattern (archetyped by EnfinEnvision2OneWay)
40 public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
42 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
46 * change listeners are notified of "services" property changes
49 * to be added that consumes new services Hashtable object.
51 public void addPropertyChangeListener(
52 java.beans.PropertyChangeListener listener)
54 changeSupport.addPropertyChangeListener(listener);
63 public void removePropertyChangeListener(
64 java.beans.PropertyChangeListener listener)
66 changeSupport.removePropertyChangeListener(listener);
69 boolean running = false;
71 Thread oldthread = null;
75 if (running && oldthread != null && oldthread.isAlive())
80 oldthread = Thread.currentThread();
83 Class foo = getClass().getClassLoader().loadClass(
84 "compbio.ws.client.Jws2Client");
85 } catch (ClassNotFoundException e)
88 .println("Not enabling Jalview Webservices version 2: client jar is not available."
89 + "\nPlease check that your webstart JNLP file is up to date!");
95 services.removeAllElements();
97 for (String jwsservers : getServiceUrls())
101 if (Jws2Client.validURL(jwsservers))
104 for (Services srv : Services.values())
106 MsaWS service = null;
109 service = Jws2Client.connect(jwsservers, srv);
110 } catch (Exception e)
112 System.err.println("Jws2 Discoverer: Problem on "
113 + jwsservers + " with service " + srv + ":\n"
115 if (!(e instanceof javax.xml.ws.WebServiceException))
123 addService(jwsservers, srv, service);
130 Cache.log.info("Ignoring invalid Jws2 service url " + jwsservers);
132 } catch (Exception e)
135 Cache.log.warn("Exception when discovering Jws2 services.", e);
138 Cache.log.error("Exception when discovering Jws2 services.", e);
143 changeSupport.firePropertyChange("services", new Vector(), services);
147 * record this service endpoint so we can use it
153 private void addService(String jwsservers, Services srv, MsaWS service2)
155 if (services == null)
157 services = new Vector<Jws2Instance>();
159 System.out.println("Discovered service: " + jwsservers + " "
161 services.add(new Jws2Instance(jwsservers, srv.toString(), service2));
164 public class Jws2Instance
166 public String hosturl;
168 public String serviceType;
170 public MsaWS service;
172 public Jws2Instance(String hosturl, String serviceType, MsaWS service)
175 this.hosturl = hosturl;
176 this.serviceType = serviceType;
177 this.service = service;
180 PresetManager presets = null;
183 * non thread safe - gets the presets for this service (blocks whilst it
184 * calls the service to get the preset set)
186 * @return service presets or null if exceptions were raised.
188 public PresetManager getPresets()
194 presets = service.getPresets();
195 } catch (Exception ex)
198 .println("Exception when retrieving presets for service "
199 + serviceType + " at " + hosturl);
205 public String getHost()
209 * try { URL serviceurl = new URL(hosturl); if (serviceurl.getPort()!=80)
210 * { return serviceurl.getHost()+":"+serviceurl.getPort(); } return
211 * serviceurl.getHost(); } catch (Exception e) {
212 * System.err.println("Failed to parse service URL '" + hosturl +
213 * "' as a valid URL!"); } return null;
218 * @return short description of what the service will do
220 public String getActionText()
222 return "Align with " + serviceType;
226 * non-thread safe - blocks whilst accessing service to get complete set of
227 * available options and parameters
231 public RunnerConfig getRunnerConfig()
233 return service.getRunnerOptions();
237 protected void finalize() throws Throwable
243 Closeable svc = (Closeable) service;
246 } catch (Exception e)
256 * holds list of services.
258 protected Vector<Jws2Instance> services;
261 * find or add a submenu with the given title in the given menu
265 * @return the new or existing submenu
267 private JMenu findOrCreateMenu(JMenu menu, String submenu)
269 JMenu submenuinstance = null;
270 for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
272 if (menu.getMenuComponent(i) instanceof JMenu
273 && ((JMenu) menu.getMenuComponent(i)).getText().equals(
276 submenuinstance = (JMenu) menu.getMenuComponent(i);
279 if (submenuinstance == null)
281 submenuinstance = new JMenu(submenu);
282 menu.add(submenuinstance);
284 return submenuinstance;
288 public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
290 if (running || services == null || services.size() == 0)
294 boolean byhost = Cache.getDefault("WSMENU_BYHOST", true), bytype = Cache
295 .getDefault("WSMENU_BYTYPE", true);
297 * eventually, JWS2 services will appear under the same align/etc submenus.
298 * for moment we keep them separate.
300 JMenu atpoint, jws2al = new JMenu("JABA Alignment");
301 MsaWSClient msacl = new MsaWSClient();
302 Vector hostLabels = new Vector();
303 for (final Jws2Instance service : services)
306 String host = service.getHost();
307 String type = service.serviceType;
310 atpoint = findOrCreateMenu(atpoint, host);
311 if (atpoint.getToolTipText() == null)
313 atpoint.setToolTipText("Services at " + host);
318 atpoint = findOrCreateMenu(atpoint, type);
319 if (atpoint.getToolTipText() == null)
321 atpoint.setToolTipText(service.getActionText());
324 if (!byhost && !hostLabels.contains(host + service.getActionText()))
326 // add a marker indicating where this service is hosted
327 // relies on services from the same host being listed in a contiguous
330 atpoint.addSeparator();
331 atpoint.add(hitm = new JMenuItem(host));
332 hitm.setForeground(Color.blue);
333 hostLabels.addElement(host);
335 msacl.attachWSMenuEntry(atpoint, service, alignFrame);
337 * JMenuItem sitem = new JMenuItem(service.serviceType);
338 * sitem.setToolTipText("Hosted at " + service.hosturl);
339 * sitem.addActionListener(new ActionListener() {
341 * @Override public void actionPerformed(ActionEvent e) { AlignmentView
342 * msa = alignFrame.gatherSequencesForAlignment(); MsaWSClient client =
343 * new MsaWSClient(service, "JWS2 Alignment of " + alignFrame.getTitle(),
344 * msa, false, true, alignFrame.getViewport().getAlignment().getDataset(),
348 if (services.size() > 0)
355 public static void main(String[] args)
357 Thread runner = new Thread(getDiscoverer());
358 getDiscoverer().addPropertyChangeListener(new PropertyChangeListener()
361 public void propertyChange(PropertyChangeEvent evt)
363 System.out.println("Changesupport: There are now "
364 + getDiscoverer().services.size() + " services");
368 while (runner.isAlive())
373 } catch (InterruptedException e)
380 private static Jws2Discoverer discoverer;
382 public static Jws2Discoverer getDiscoverer()
384 if (discoverer == null)
386 discoverer = new Jws2Discoverer();
391 public boolean hasServices()
393 // TODO Auto-generated method stub
394 return !running && services != null && services.size() > 0;
397 public boolean isRunning()
403 * the jalview .properties entry for JWS2 URLS
405 final static String JWS2HOSTURLS = "JWS2HOSTURLS";
407 public static void setServiceUrls(Vector<String> urls)
411 StringBuffer urlbuffer = new StringBuffer();
413 for (String url : urls)
415 urlbuffer.append(sep);
416 urlbuffer.append(url);
419 Cache.setProperty(JWS2HOSTURLS, urlbuffer.toString());
423 Cache.removeProperty(JWS2HOSTURLS);
427 public static Vector<String> getServiceUrls()
429 String surls = Cache.getDefault(JWS2HOSTURLS,
430 "http://www.compbio.dundee.ac.uk/jabaws");
431 Vector<String> urls = new Vector<String>();
434 StringTokenizer st = new StringTokenizer(surls, ",");
435 while (st.hasMoreElements())
440 java.net.URL u = new java.net.URL(url = st.nextToken());
441 if (!urls.contains(url))
447 jalview.bin.Cache.log.info("Ignoring duplicate url in "
448 + JWS2HOSTURLS + " list");
450 } catch (Exception ex)
452 jalview.bin.Cache.log
453 .warn("Problem whilst trying to make a URL from '"
454 + ((url != null) ? url : "<null>") + "'");
455 jalview.bin.Cache.log
456 .warn("This was probably due to a malformed comma separated list"
459 + " entry of $(HOME)/.jalview_properties)");
460 jalview.bin.Cache.log.debug("Exception was ", ex);
463 } catch (Exception ex)
465 jalview.bin.Cache.log.warn(
466 "Error parsing comma separated list of urls in "
467 + JWS2HOSTURLS + " preference.", ex);
469 if (urls.size() >= 0)
476 public Vector<Jws2Instance> getServices()
478 return (services == null) ? new Vector<Jws2Instance>()
479 : new Vector<Jws2Instance>(services);
483 * test the given URL with the JabaWS test code
487 public static boolean testServiceUrl(URL foo)
490 compbio.ws.client.WSTester.main(new String[] { "-h="+foo.toString()});
491 } catch (Exception e)
495 catch (OutOfMemoryError e)