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.net.ConnectException;
10 import java.util.HashSet;
11 import java.util.Hashtable;
12 import java.util.StringTokenizer;
13 import java.util.Vector;
15 import javax.swing.JMenu;
16 import javax.swing.JMenuItem;
18 import org.apache.log4j.Level;
20 import jalview.bin.Cache;
21 import jalview.datamodel.AlignmentView;
22 import jalview.gui.AlignFrame;
23 import jalview.ws.WSMenuEntryProviderI;
24 import compbio.data.msa.MsaWS;
25 import compbio.metadata.Option;
26 import compbio.metadata.Preset;
27 import compbio.metadata.PresetManager;
28 import compbio.metadata.RunnerConfig;
29 import compbio.ws.client.Jws2Base;
30 import compbio.ws.client.Jws2Base.Services;
33 * discoverer for jws2 services. Follows the lightweight service discoverer
34 * pattern (archetyped by EnfinEnvision2OneWay)
39 public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
41 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
45 * change listeners are notified of "services" property changes
48 * to be added that consumes new services Hashtable object.
50 public void addPropertyChangeListener(
51 java.beans.PropertyChangeListener listener)
53 changeSupport.addPropertyChangeListener(listener);
62 public void removePropertyChangeListener(
63 java.beans.PropertyChangeListener listener)
65 changeSupport.removePropertyChangeListener(listener);
68 boolean running = false;
70 Thread oldthread = null;
75 if (running && oldthread != null && oldthread.isAlive())
80 oldthread = Thread.currentThread();
83 Class foo = getClass().getClassLoader().loadClass(
84 "compbio.ws.client.Jws2Base");
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 (Jws2Base.validURL(jwsservers))
104 for (Services srv : Jws2Base.Services.values())
106 MsaWS service = null;
109 service = Jws2Base.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()
210 URL serviceurl = new URL(hosturl);
211 if (serviceurl.getPort()!=80)
213 return serviceurl.getHost()+":"+serviceurl.getPort();
215 return serviceurl.getHost();
216 } catch (Exception e)
218 System.err.println("Failed to parse service URL '" + hosturl
219 + "' as a valid URL!");
224 * @return short description of what the service will do
226 public String getActionText() {
232 * non-thread safe - blocks whilst accessing service to get complete set of available options and parameters
235 public RunnerConfig getRunnerConfig()
237 return service.getRunnerOptions();
242 * holds list of services.
244 protected Vector<Jws2Instance> services;
247 * find or add a submenu with the given title in the given menu
250 * @return the new or existing submenu
252 private JMenu findOrCreateMenu(JMenu menu, String submenu)
254 JMenu submenuinstance = null;
255 for (int i=0,iSize=menu.getMenuComponentCount(); i<iSize; i++)
257 if (menu.getMenuComponent(i) instanceof JMenu &&
258 ((JMenu)menu.getMenuComponent(i)).getText().equals(submenu))
260 submenuinstance = (JMenu) menu.getMenuComponent(i);
263 if (submenuinstance==null)
265 submenuinstance = new JMenu(submenu);
266 menu.add(submenuinstance);
268 return submenuinstance;
272 public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
274 if (running || services == null || services.size() == 0)
278 boolean byhost = Cache.getDefault("WSMENU_BYHOST", true), bytype = Cache
279 .getDefault("WSMENU_BYTYPE", true);
281 * eventually, JWS2 services will appear under the same align/etc submenus.
282 * for moment we keep them separate.
284 JMenu atpoint, jws2al = new JMenu("JABA Alignment");
285 MsaWSClient msacl = new MsaWSClient();
286 Vector hostLabels=new Vector();
287 for (final Jws2Instance service : services)
290 String host = service.getHost();
291 String type = service.serviceType;
294 atpoint = findOrCreateMenu(atpoint, host);
295 if (atpoint.getToolTipText()==null) {
296 atpoint.setToolTipText("Services at "+host);
301 atpoint = findOrCreateMenu(atpoint,type);
302 if (atpoint.getToolTipText()==null) {
303 atpoint.setToolTipText(service.getActionText());
306 if (!byhost && !hostLabels.contains(host+service.getActionText()))
308 // add a marker indicating where this service is hosted
309 // relies on services from the same host being listed in a contiguous group
311 atpoint.addSeparator();
312 atpoint.add(hitm=new JMenuItem(host));
313 hitm.setForeground(Color.blue);
314 hostLabels.addElement(host);
316 msacl.attachWSMenuEntry(atpoint, service, alignFrame);
318 * JMenuItem sitem = new JMenuItem(service.serviceType);
319 * sitem.setToolTipText("Hosted at " + service.hosturl);
320 * sitem.addActionListener(new ActionListener() {
322 * @Override public void actionPerformed(ActionEvent e) { AlignmentView
323 * msa = alignFrame.gatherSequencesForAlignment(); MsaWSClient client =
324 * new MsaWSClient(service, "JWS2 Alignment of " + alignFrame.getTitle(),
325 * msa, false, true, alignFrame.getViewport().getAlignment().getDataset(),
329 if (services.size() > 0)
336 public static void main(String[] args)
338 Thread runner = new Thread(getDiscoverer());
339 getDiscoverer().addPropertyChangeListener(new PropertyChangeListener()
343 public void propertyChange(PropertyChangeEvent evt)
345 System.out.println("Changesupport: There are now "
346 + getDiscoverer().services.size() + " services");
350 while (runner.isAlive())
355 } catch (InterruptedException e)
362 private static Jws2Discoverer discoverer;
364 public static Jws2Discoverer getDiscoverer()
366 if (discoverer == null)
368 discoverer = new Jws2Discoverer();
373 public boolean hasServices()
375 // TODO Auto-generated method stub
376 return !running && services != null && services.size() > 0;
379 public boolean isRunning()
385 * the jalview .properties entry for JWS2 URLS
387 final static String JWS2HOSTURLS = "JWS2HOSTURLS";
389 public static void setServiceUrls(Vector<String> urls)
393 StringBuffer urlbuffer = new StringBuffer();
395 for (String url : urls)
397 urlbuffer.append(sep);
398 urlbuffer.append(url);
401 Cache.setProperty(JWS2HOSTURLS, urlbuffer.toString());
405 Cache.removeProperty(JWS2HOSTURLS);
409 public static Vector<String> getServiceUrls()
411 String surls = Cache.getDefault(JWS2HOSTURLS,
412 "http://www.compbio.dundee.ac.uk/jaba");
413 Vector<String> urls = new Vector<String>();
416 StringTokenizer st = new StringTokenizer(surls, ",");
417 while (st.hasMoreElements())
422 java.net.URL u = new java.net.URL(url = st.nextToken());
423 if (!urls.contains(url))
429 jalview.bin.Cache.log.info("Ignoring duplicate url in "
430 + JWS2HOSTURLS + " list");
432 } catch (Exception ex)
434 jalview.bin.Cache.log
435 .warn("Problem whilst trying to make a URL from '"
436 + ((url != null) ? url : "<null>") + "'");
437 jalview.bin.Cache.log
438 .warn("This was probably due to a malformed comma separated list"
441 + " entry of $(HOME)/.jalview_properties)");
442 jalview.bin.Cache.log.debug("Exception was ", ex);
445 } catch (Exception ex)
447 jalview.bin.Cache.log.warn(
448 "Error parsing comma separated list of urls in "
449 + JWS2HOSTURLS + " preference.", ex);
451 if (urls.size() >= 0)
458 public Vector<Jws2Instance> getServices()
460 return (services==null) ? new Vector<Jws2Instance>(): new Vector<Jws2Instance>(services);