b30a05fcb91e48e34c85e30008522d959c140a99
[jalview.git] / src / jalview / ws / Discoverer.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 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 ext.vamsas.*;
34 import java.util.Vector;
35 import java.util.Hashtable;
36 import java.util.ArrayList;
37 import java.util.StringTokenizer;
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     Vector urls = new Vector();
116     String RootServiceURLs = jalview.bin.Cache.getDefault("DISCOVERY_URLS",
117         "http://www.compbio.dundee.ac.uk/JalviewWS/services/ServiceRegistry");
118
119     try{
120       StringTokenizer st = new StringTokenizer(RootServiceURLs, ",");
121       while (st.hasMoreElements())
122       {
123         String url = null;
124         try
125         {
126           java.net.URL u = new java.net.URL(url = st.nextToken());
127           if (!urls.contains(u))
128             urls.add(u);
129           else
130             jalview.bin.Cache.log.info("Ignoring duplicate url in DISCOVERY_URLS list");
131         }
132         catch (Exception ex)
133         {
134           jalview.bin.Cache.log.warn(
135               "Problem whilst trying to make a URL from '" +
136               ( (url != null) ? url : "<null>")+"'");
137           jalview.bin.Cache.log.warn("This was probably due to a malformed comma separated list"
138                                        +" in the DISCOVERY_URLS entry of $(HOME)/.jalview_properties)");
139           jalview.bin.Cache.log.debug("Exception was ",ex);
140         }
141       }
142     }catch(Exception ex)
143     {jalview.bin.Cache.log.warn("Error parsing comma separated list of urls in DISCOVERY_URLS.",ex);}
144     if (urls.size()>0)
145       return urls;
146     return null;
147   }
148   static
149   {
150
151     try
152     {
153       reallyDiscoverServices = jalview.bin.Cache.getDefault("DISCOVERY_START", false);
154       if (reallyDiscoverServices)
155       {
156         ServiceURLList = getDiscoveryURLS();
157       }
158       else
159       {
160         jalview.bin.Cache.log.debug("Setting default services");
161         services = new Hashtable();
162         // Muscle, Clustal and JPred.
163         ServiceHandle[] defServices = {
164             new ServiceHandle(
165                 "MsaWS",
166                 "Edgar, Robert C. (2004), MUSCLE: multiple sequence alignment " +
167                 "with high accuracy and high throughput, Nucleic Acids Research 32(5), 1792-97.",
168                 "http://www.compbio.dundee.ac.uk/JalviewWS/services/MuscleWS",
169                 "Muscle Multiple Protein Sequence Alignment"
170 ),
171             new ServiceHandle(
172                 "MsaWS",
173                 "Thompson, J.D., Higgins, D.G. and Gibson, T.J. (1994) CLUSTAL W: improving the sensitivity of progressive multiple" +
174                 " sequence alignment through sequence weighting, position specific gap penalties and weight matrix choice." +
175                 " Nucleic Acids Research, 22 4673-4680",
176                 "http://www.compbio.dundee.ac.uk/JalviewWS/services/ClustalWS",
177                 "ClustalW Multiple Sequence Alignment"),
178             new ServiceHandle(
179                 "SecStrPred",
180                 "Cuff J. A and Barton G.J (1999) Application of enhanced " +
181                 "multiple sequence alignment profiles to improve protein secondary structure prediction, " +
182                 "Proteins 40:502-511",
183                 "http://www.compbio.dundee.ac.uk/JalviewWS/services/jpred","JNet Secondary Structure Prediction"
184                 )};
185         services = new Hashtable();
186         serviceList = new Vector();
187         buildServiceLists(defServices, serviceList, services);
188       }
189
190     }
191     catch (Exception e)
192     {
193       System.err.println(
194           "jalview.rootRegistry is not a proper url!\nWas set to " +
195           RootServiceURL + "\n" + e);
196     }
197
198   }
199   // TODO: JBPNote : make this discover more services based on list of
200   // discovery service urls, break cyclic references to the same url and
201   // duplicate service entries (same endpoint *and* same interface)
202   private ServiceHandle[] getServices(java.net.URL location)
203   {
204     ServiceHandles shs = null;
205     try
206     {
207       jalview.bin.Cache.log.debug("Discovering services using " + location);
208       shs = locateWebService(location).getServices();
209     }
210     catch (Exception e)
211     {
212       jalview.bin.Cache.log.debug("No Discovery service at " +
213                          location);
214       jalview.bin.Cache.log.debug(e);
215
216     }
217     if ( (shs != null) && shs.getServices().length > 0)
218     {
219       return shs.getServices();
220     }
221     return null;
222   }
223
224   /**
225    * Adds a list of services to the service catalog and categorised catalog
226    * returns true if ServiceURLList was modified with a new DiscoveryService URL
227    * @param sh ServiceHandle[]
228    * @param cat Vector
229    * @param sscat Hashtable
230    * @return boolean
231    */
232   static private boolean buildServiceLists(ServiceHandle[] sh, Vector cat,
233                                     Hashtable sscat)
234   {
235     boolean seenNewDiscovery = false;
236     for (int i = 0, j = sh.length; i < j; i++)
237     {
238       if (!cat.contains(sh[i]))
239       {
240         jalview.bin.Cache.log.debug("A " + sh[i].getAbstractName() +
241                                       " service called " +
242                                       sh[i].getName() + " exists at " +
243                                       sh[i].getEndpointURL() + "\n");
244         if (!sscat.containsKey(sh[i].getAbstractName()))
245         {
246           sscat.put(sh[i].getAbstractName(), cat = new Vector());
247         }
248         else
249         {
250           cat = (Vector) sscat.get(sh[i].getAbstractName());
251         }
252         cat.add(sh[i]);
253         if (sh[i].getAbstractName().equals("Registry"))
254         {
255           for (int s = 0, sUrls = ServiceURLList.size(); s < sUrls; s++)
256           {
257             java.net.URL disc_serv = null;
258             try
259             {
260               disc_serv = new java.net.URL(sh[i].getEndpointURL());
261               if (!ServiceURLList.contains(disc_serv))
262               {
263                 jalview.bin.Cache.log.debug(
264                     "Adding new discovery service at " + disc_serv);
265                 ServiceURLList.add(disc_serv);
266                 seenNewDiscovery = true;
267               }
268             }
269             catch (Exception e)
270             {
271               jalview.bin.Cache.log.debug(
272                   "Ignoring bad discovery service URL " + sh[i].getEndpointURL(),
273                   e);
274             }
275           }
276         }
277       }
278     }
279     return seenNewDiscovery;
280   }
281
282   public void discoverServices()
283   {
284     Hashtable sscat = new Hashtable();
285     Vector cat = new Vector();
286     ServiceHandle sh[] = null;
287     int s_url = 0;
288     if (ServiceURLList==null)
289     {
290       jalview.bin.Cache.log.debug("No service endpoints to use for service discovery.");
291       return;
292     }
293     while (s_url < ServiceURLList.size())
294     {
295       if ( (sh = getServices( (java.net.URL) ServiceURLList.get(s_url))) != null)
296       {
297
298         buildServiceLists(sh, cat, sscat);
299       } else {
300         jalview.bin.Cache.log.warn(
301             "No services at "
302             +((java.net.URL) ServiceURLList.get(s_url))
303             +" - check DISCOVERY_URLS property in .jalview_properties");
304       }
305       s_url++;
306     }
307     // TODO: decide on correct semantics for services list - PropertyChange
308     // provides a way of passing the new object around
309     // so no need to access original discovery thread.
310     // Curent decision is to change properties then notify listeners with old and new values.
311     Hashtable oldServices = services;
312     Vector oldServicelist = serviceList;
313     services = sscat;
314     serviceList = cat;
315     firePropertyChange("services", oldServices, services);
316   }
317
318   public void run()
319   {
320     discoverServices();
321   }
322 }