added SeqSearch service interface. Documented and refactored web service client ...
[jalview.git] / src / jalview / ws / Discoverer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.ws;
20
21 /**
22  * <p>Title: </p>
23  *
24  * <p>Description: </p>
25  *
26  * <p>Copyright: Copyright (c) 2004</p>
27  *
28  * <p>Company: Dundee University</p>
29  *
30  * @author not attributable
31  * @version 1.0
32  */
33 import java.util.*;
34
35 import javax.swing.*;
36
37 import ext.vamsas.*;
38
39 public class Discoverer
40     extends Thread implements Runnable
41 {
42   ext.vamsas.IRegistry registry; // the root registry service.
43   private java.beans.PropertyChangeSupport changeSupport = new java.beans.
44       PropertyChangeSupport(this);
45
46   /**
47    * change listeners are notified of "services" property changes
48    *
49    * @param listener to be added that consumes new services Hashtable object.
50    */
51   public void addPropertyChangeListener(
52       java.beans.PropertyChangeListener listener)
53   {
54     changeSupport.addPropertyChangeListener(listener);
55   }
56
57   /**
58    *
59    *
60    * @param listener to be removed
61    */
62   public void removePropertyChangeListener(
63       java.beans.PropertyChangeListener listener)
64   {
65     changeSupport.removePropertyChangeListener(listener);
66   }
67
68   /**
69    * Property change listener firing routine
70    *
71    * @param prop services
72    * @param oldvalue old services hash
73    * @param newvalue new services hash
74    */
75   public void firePropertyChange(String prop, Object oldvalue, Object newvalue)
76   {
77     changeSupport.firePropertyChange(prop, oldvalue, newvalue);
78   }
79
80   /**
81    * Initializes the server field with a valid service implementation.
82    *
83    * @return true if service was located.
84    */
85   private IRegistry locateWebService(java.net.URL WsURL)
86   {
87     IRegistryServiceLocator loc = new IRegistryServiceLocator(); // Default
88     IRegistry server = null;
89     try
90     {
91       server = loc.getRegistryService(WsURL);
92       ( (RegistryServiceSoapBindingStub) server).setTimeout(60000); // One minute timeout
93     }
94     catch (Exception ex)
95     {
96       jalview.bin.Cache.log.error(
97           "Serious!  Service location failed\nfor URL :" + WsURL +
98           "\n", ex);
99
100       return null;
101     }
102
103     loc.getEngine().setOption("axis", "1");
104
105     return server;
106   }
107
108   static private java.net.URL RootServiceURL = null;
109   static public Vector ServiceURLList = null;
110   static private boolean reallyDiscoverServices = true;
111
112   public static java.util.Hashtable services = null; // vectors of services stored by abstractServiceType string
113   public static java.util.Vector serviceList = null; // flat list of services
114   static private Vector getDiscoveryURLS()
115   {
116     Vector urls = new Vector();
117     String RootServiceURLs = jalview.bin.Cache.getDefault("DISCOVERY_URLS",
118         "http://www.compbio.dundee.ac.uk/JalviewWS/services/ServiceRegistry");
119
120     try
121     {
122       StringTokenizer st = new StringTokenizer(RootServiceURLs, ",");
123       while (st.hasMoreElements())
124       {
125         String url = null;
126         try
127         {
128           java.net.URL u = new java.net.URL(url = st.nextToken());
129           if (!urls.contains(u))
130           {
131             urls.add(u);
132           }
133           else
134           {
135             jalview.bin.Cache.log.info(
136                 "Ignoring duplicate url in DISCOVERY_URLS list");
137           }
138         }
139         catch (Exception ex)
140         {
141           jalview.bin.Cache.log.warn(
142               "Problem whilst trying to make a URL from '" +
143               ( (url != null) ? url : "<null>") + "'");
144           jalview.bin.Cache.log.warn(
145               "This was probably due to a malformed comma separated list"
146               + " in the DISCOVERY_URLS entry of $(HOME)/.jalview_properties)");
147           jalview.bin.Cache.log.debug("Exception was ", ex);
148         }
149       }
150     }
151     catch (Exception ex)
152     {
153       jalview.bin.Cache.log.warn(
154           "Error parsing comma separated list of urls in DISCOVERY_URLS.", ex);
155     }
156     if (urls.size() > 0)
157     {
158       return urls;
159     }
160     return null;
161   }
162
163   /**
164    * fetch new services or reset to hardwired defaults depending on preferences.
165    */
166   static public void doDiscovery()
167   {
168     jalview.bin.Cache.log.debug("(Re)-Initialising the discovery URL list.");
169     try
170     {
171       reallyDiscoverServices = jalview.bin.Cache.getDefault("DISCOVERY_START", false);
172       if (reallyDiscoverServices)
173       {
174         ServiceURLList = getDiscoveryURLS();
175       }
176       else
177       {
178         jalview.bin.Cache.log.debug("Setting default services");
179         services = new Hashtable();
180         // Muscle, Clustal and JPred.
181         ServiceHandle[] defServices =
182             {
183             new ServiceHandle(
184                 "MsaWS",
185                 "Edgar, Robert C. (2004), MUSCLE: multiple sequence alignment " +
186                 "with high accuracy and high throughput, Nucleic Acids Research 32(5), 1792-97.",
187                 "http://www.compbio.dundee.ac.uk/JalviewWS/services/MuscleWS",
188                 "Muscle Multiple Protein Sequence Alignment"
189             ),
190             new ServiceHandle(
191                 "MsaWS",
192                 "Katoh, K., K. Kuma, K., Toh, H.,  and Miyata, T. (2005) " +
193                 "\"MAFFT version 5: improvement in accuracy of multiple sequence alignment.\"" +
194                 " Nucleic Acids Research, 33 511-518",
195                 "http://www.compbio.dundee.ac.uk/JalviewWS/services/MafftWS",
196                 "MAFFT Multiple Sequence Alignment"),
197             new ServiceHandle(
198                 "MsaWS",
199                 "Thompson, J.D., Higgins, D.G. and Gibson, T.J. (1994) CLUSTAL W: improving the sensitivity of progressive multiple" +
200                 " sequence alignment through sequence weighting, position specific gap penalties and weight matrix choice." +
201                 " Nucleic Acids Research, 22 4673-4680",
202                 "http://www.compbio.dundee.ac.uk/JalviewWS/services/ClustalWS",
203                 "ClustalW Multiple Sequence Alignment"),
204             new ServiceHandle(
205                 "SecStrPred",
206                 "Cuff J. A and Barton G.J (2000) Application of " +
207                 "multiple sequence alignment profiles to improve protein secondary structure prediction, " +
208                 "Proteins 40:502-511",
209                 "http://www.compbio.dundee.ac.uk/JalviewWS/services/jpred",
210                 "JNet Secondary Structure Prediction"
211             )};
212         services = new Hashtable();
213         serviceList = new Vector();
214         buildServiceLists(defServices, serviceList, services);
215       }
216
217     }
218     catch (Exception e)
219     {
220       System.err.println(
221           "jalview.rootRegistry is not a proper url!\nWas set to " +
222           RootServiceURL + "\n" + e);
223     }
224
225   }
226
227   // TODO: JBPNote : make this discover more services based on list of
228   // discovery service urls, break cyclic references to the same url and
229   // duplicate service entries (same endpoint *and* same interface)
230   private ServiceHandle[] getServices(java.net.URL location)
231   {
232     ServiceHandles shs = null;
233     try
234     {
235       jalview.bin.Cache.log.debug("Discovering services using " + location);
236       shs = locateWebService(location).getServices();
237     }
238     catch (org.apache.axis.AxisFault f)
239     {
240       // JBPNote - should do this a better way!
241       if (f.getFaultReason().indexOf("(407)") > -1)
242       {
243         if (jalview.gui.Desktop.desktop != null)
244         {
245           JOptionPane.showMessageDialog(jalview.gui.Desktop.desktop, "Please set up your proxy settings in the 'Connections' tab of the Preferences window",
246                                         "Proxy Authorization Failed",
247                                         JOptionPane.WARNING_MESSAGE);
248         }
249       }
250       else
251       {
252         jalview.bin.Cache.log.warn("No Discovery service at " +
253                                    location);
254         jalview.bin.Cache.log.debug("Axis Fault", f);
255       }
256     }
257     catch (Exception e)
258     {
259       jalview.bin.Cache.log.warn("No Discovery service at " +
260                                  location);
261       jalview.bin.Cache.log.debug("Discovery Service General Exception", e);
262     }
263     if ( (shs != null) && shs.getServices().length > 0)
264     {
265       return shs.getServices();
266     }
267     return null;
268   }
269
270   /**
271    * Adds a list of services to the service catalog and categorised catalog
272    * returns true if ServiceURLList was modified with a new DiscoveryService URL
273    * @param sh ServiceHandle[]
274    * @param cat Vector
275    * @param sscat Hashtable
276    * @return boolean
277    */
278   static private boolean buildServiceLists(ServiceHandle[] sh, Vector cat,
279                                            Hashtable sscat)
280   {
281     boolean seenNewDiscovery = false;
282     for (int i = 0, j = sh.length; i < j; i++)
283     {
284       if (!cat.contains(sh[i]))
285       {
286         jalview.bin.Cache.log.debug("A " + sh[i].getAbstractName() +
287                                     " service called " +
288                                     sh[i].getName() + " exists at " +
289                                     sh[i].getEndpointURL() + "\n");
290         if (!sscat.containsKey(sh[i].getAbstractName()))
291         {
292           sscat.put(sh[i].getAbstractName(), cat = new Vector());
293         }
294         else
295         {
296           cat = (Vector) sscat.get(sh[i].getAbstractName());
297         }
298         cat.add(sh[i]);
299         if (sh[i].getAbstractName().equals("Registry"))
300         {
301           for (int s = 0, sUrls = ServiceURLList.size(); s < sUrls; s++)
302           {
303             java.net.URL disc_serv = null;
304             try
305             {
306               disc_serv = new java.net.URL(sh[i].getEndpointURL());
307               if (!ServiceURLList.contains(disc_serv))
308               {
309                 jalview.bin.Cache.log.debug(
310                     "Adding new discovery service at " + disc_serv);
311                 ServiceURLList.add(disc_serv);
312                 seenNewDiscovery = true;
313               }
314             }
315             catch (Exception e)
316             {
317               jalview.bin.Cache.log.debug(
318                   "Ignoring bad discovery service URL " + sh[i].getEndpointURL(),
319                   e);
320             }
321           }
322         }
323       }
324     }
325     return seenNewDiscovery;
326   }
327
328   public void discoverServices()
329   {
330     Hashtable sscat = new Hashtable();
331     Vector cat = new Vector();
332     ServiceHandle sh[] = null;
333     int s_url = 0;
334     if (ServiceURLList == null)
335     {
336       jalview.bin.Cache.log.debug(
337           "No service endpoints to use for service discovery.");
338       return;
339     }
340     while (s_url < ServiceURLList.size())
341     {
342       if ( (sh = getServices( (java.net.URL) ServiceURLList.get(s_url))) != null)
343       {
344
345         buildServiceLists(sh, cat, sscat);
346       }
347       else
348       {
349         jalview.bin.Cache.log.warn(
350             "No services at "
351             + ( (java.net.URL) ServiceURLList.get(s_url))
352             + " - check DISCOVERY_URLS property in .jalview_properties");
353       }
354       s_url++;
355     }
356     // TODO: decide on correct semantics for services list - PropertyChange
357     // provides a way of passing the new object around
358     // so no need to access original discovery thread.
359     // Curent decision is to change properties then notify listeners with old and new values.
360     Hashtable oldServices = services;
361     //Vector oldServicelist = serviceList;
362     services = sscat;
363     serviceList = cat;
364     firePropertyChange("services", oldServices, services);
365   }
366
367   /**
368    * creates a new thread to call discoverServices()
369    */
370   public void run()
371   {
372     final Discoverer discoverer = this;
373     Thread discoverThread = new Thread()
374     {
375       public void run()
376       {
377         discoverer.doDiscovery();
378         discoverer.discoverServices();
379       }
380     };
381     discoverThread.start();
382   }
383   /**
384    * binding service abstract name to handler class
385    */
386   private static Hashtable serviceClientBindings;
387   public static WSClient getServiceClient(ServiceHandle sh)
388   {
389     if (serviceClientBindings==null)
390     {
391       // get a list from Config or create below
392       serviceClientBindings = new Hashtable();
393       serviceClientBindings.put("MsaWS",new MsaWSClient());
394       serviceClientBindings.put("SecStrPred",new JPredClient());
395       serviceClientBindings.put("SeqSearch",new SeqSearchWSClient());
396     }
397     WSClient instance = (WSClient) serviceClientBindings.get(sh.getAbstractName());
398     if (instance==null)
399     {
400       System.err.println("WARNING - POSSIBLE IMPLEMENTATION ERROR - cannot find WSClient implementation for "+sh.getAbstractName());
401     } else
402     {
403       instance.serviceHandle = sh;
404     }
405     return instance;
406   }
407   /**
408    * notes on discovery service
409    * 1. need to allow multiple discovery source urls.
410    * 2. user interface to add/control list of urls in preferences
411    * notes on wsclient discovery
412    * 1. need a classpath property with list of additional plugin directories
413    * 2. optional config to cite specific bindings between class name and Abstract service name.
414    * 3. precedence for automatic discovery by using getAbstractName for WSClient - user added plugins override default plugins ?
415    * notes on wsclient gui
416    * code for gui attachment now moved to wsclient implementation. Needs more abstraction but approach seems to work.
417    * is it possible to 'generalise' the data retrieval calls further ? current methods are very specific (gatherForMSA or gatherForSeqOrMsaSecStrPred), new methods for conservation (group or alignment), treecalc (aligned profile), seqannot (sequences selected from dataset, annotation back to dataset).
418    *  
419    */
420 }