1 package jalview.ws.jws2;
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.beans.PropertyChangeEvent;
6 import java.beans.PropertyChangeListener;
7 import java.net.ConnectException;
8 import java.util.HashSet;
9 import java.util.StringTokenizer;
10 import java.util.Vector;
12 import javax.swing.JMenu;
13 import javax.swing.JMenuItem;
15 import org.apache.log4j.Level;
17 import jalview.bin.Cache;
18 import jalview.datamodel.AlignmentView;
19 import jalview.gui.AlignFrame;
20 import jalview.ws.WSMenuEntryProviderI;
21 import compbio.data.msa.MsaWS;
22 import compbio.metadata.Preset;
23 import compbio.metadata.PresetManager;
24 import compbio.ws.client.Jws2Base;
25 import compbio.ws.client.Jws2Base.Services;
28 * discoverer for jws2 services. Follows the lightweight service discoverer
29 * pattern (archetyped by EnfinEnvision2OneWay)
34 public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
36 private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
40 * change listeners are notified of "services" property changes
43 * to be added that consumes new services Hashtable object.
45 public void addPropertyChangeListener(
46 java.beans.PropertyChangeListener listener)
48 changeSupport.addPropertyChangeListener(listener);
57 public void removePropertyChangeListener(
58 java.beans.PropertyChangeListener listener)
60 changeSupport.removePropertyChangeListener(listener);
63 boolean running = false;
64 Thread oldthread=null;
68 if (running && oldthread!=null && oldthread.isAlive())
73 oldthread = Thread.currentThread();
76 Class foo = getClass().getClassLoader().loadClass(
77 "compbio.ws.client.Jws2Base");
78 } catch (ClassNotFoundException e)
81 .println("Not enabling Jalview Webservices version 2: client jar is not available."
82 + "\nPlease check that your webstart JNLP file is up to date!");
88 services.removeAllElements();
90 for (String jwsservers : getServiceUrls())
94 if (Jws2Base.validURL(jwsservers))
97 for (Services srv : Jws2Base.Services.values())
102 service = Jws2Base.connect(jwsservers, srv);
103 } catch (Exception e)
105 System.err.println("Jws2 Discoverer: Problem on "
106 + jwsservers + " with service " + srv + ":\n"
108 if (!(e instanceof javax.xml.ws.WebServiceException))
116 addService(jwsservers, srv, service);
123 Cache.log.info("Ignoring invalid Jws2 service url " + jwsservers);
125 } catch (Exception e)
128 Cache.log.warn("Exception when discovering Jws2 services.", e);
131 Cache.log.error("Exception when discovering Jws2 services.", e);
136 changeSupport.firePropertyChange("services", new Vector(), services);
140 * record this service endpoint so we can use it
146 private void addService(String jwsservers, Services srv, MsaWS service2)
148 if (services == null)
150 services = new Vector<Jws2Instance>();
152 System.out.println("Discovered service: " + jwsservers + " "
154 services.add(new Jws2Instance(jwsservers, "Align with "
155 + srv.toString(), service2));
158 public class Jws2Instance
166 public Jws2Instance(String hosturl, String serviceType, MsaWS service)
169 this.hosturl = hosturl;
170 this.serviceType = serviceType;
171 this.service = service;
174 PresetManager presets = null;
177 * non thread safe - gets the presets for this service (blocks whilst it
178 * calls the service to get the preset set)
180 * @return service presets or null if exceptions were raised.
182 public PresetManager getPresets()
188 presets = service.getPresets();
189 } catch (Exception ex)
192 .println("Exception when retrieving presets for service "
193 + serviceType + " at " + hosturl);
201 * holds list of services.
203 Vector<Jws2Instance> services;
206 public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
208 if (running || services == null || services.size() == 0)
213 * eventually, JWS2 services will appear under the same align/etc submenus.
214 * for moment we keep them separate.
216 JMenu jws2 = new JMenu("JWS2 Alignment");
217 MsaWSClient msacl = new MsaWSClient();
218 for (final Jws2Instance service : services)
220 msacl.attachWSMenuEntry(jws2, service, alignFrame);
222 * JMenuItem sitem = new JMenuItem(service.serviceType);
223 * sitem.setToolTipText("Hosted at " + service.hosturl);
224 * sitem.addActionListener(new ActionListener() {
226 * @Override public void actionPerformed(ActionEvent e) { AlignmentView
227 * msa = alignFrame.gatherSequencesForAlignment(); MsaWSClient client =
228 * new MsaWSClient(service, "JWS2 Alignment of " + alignFrame.getTitle(),
229 * msa, false, true, alignFrame.getViewport().getAlignment().getDataset(),
233 if (services.size() > 0)
240 public static void main(String[] args)
242 Thread runner = new Thread(getDiscoverer());
243 getDiscoverer().addPropertyChangeListener(new PropertyChangeListener()
247 public void propertyChange(PropertyChangeEvent evt)
249 System.out.println("Changesupport: There are now "
250 + getDiscoverer().services.size() + " services");
254 while (runner.isAlive())
259 } catch (InterruptedException e)
266 private static Jws2Discoverer discoverer;
268 public static Jws2Discoverer getDiscoverer()
270 if (discoverer == null)
272 discoverer = new Jws2Discoverer();
277 public boolean hasServices()
279 // TODO Auto-generated method stub
280 return !running && services != null && services.size() > 0;
283 public boolean isRunning()
289 * the jalview .properties entry for JWS2 URLS
291 final static String JWS2HOSTURLS = "JWS2HOSTURLS";
293 public static void setServiceUrls(Vector<String> urls)
297 StringBuffer urlbuffer = new StringBuffer();
299 for (String url : urls)
301 urlbuffer.append(sep);
302 urlbuffer.append(url);
305 Cache.setProperty(JWS2HOSTURLS, urlbuffer.toString());
309 Cache.removeProperty(JWS2HOSTURLS);
313 public static Vector<String> getServiceUrls()
315 String surls = Cache.getDefault(JWS2HOSTURLS,
316 "http://webservices.compbio.dundee.ac.uk:8084/jws2");
317 Vector<String> urls = new Vector<String>();
320 StringTokenizer st = new StringTokenizer(surls, ",");
321 while (st.hasMoreElements())
326 java.net.URL u = new java.net.URL(url = st.nextToken());
327 if (!urls.contains(url))
333 jalview.bin.Cache.log.info("Ignoring duplicate url in "
334 + JWS2HOSTURLS + " list");
336 } catch (Exception ex)
338 jalview.bin.Cache.log
339 .warn("Problem whilst trying to make a URL from '"
340 + ((url != null) ? url : "<null>") + "'");
341 jalview.bin.Cache.log
342 .warn("This was probably due to a malformed comma separated list"
345 + " entry of $(HOME)/.jalview_properties)");
346 jalview.bin.Cache.log.debug("Exception was ", ex);
349 } catch (Exception ex)
351 jalview.bin.Cache.log.warn(
352 "Error parsing comma separated list of urls in "
353 + JWS2HOSTURLS + " preference.", ex);
355 if (urls.size() >= 0)