Remove redundancy in Eclipse
[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.StringTokenizer;
37
38 public class Discoverer
39     extends Thread implements Runnable
40 {
41   ext.vamsas.IRegistry registry; // the root registry service.
42   private java.beans.PropertyChangeSupport changeSupport = new java.beans.
43       PropertyChangeSupport(this);
44
45   /**
46    * change listeners are notified of "services" property changes
47    *
48    * @param listener to be added that consumes new services Hashtable object.
49    */
50   public void addPropertyChangeListener(
51       java.beans.PropertyChangeListener listener)
52   {
53     changeSupport.addPropertyChangeListener(listener);
54   }
55
56   /**
57    *
58    *
59    * @param listener to be removed
60    */
61   public void removePropertyChangeListener(
62       java.beans.PropertyChangeListener listener)
63   {
64     changeSupport.removePropertyChangeListener(listener);
65   }
66
67   /**
68    * Property change listener firing routine
69    *
70    * @param prop services
71    * @param oldvalue old services hash
72    * @param newvalue new services hash
73    */
74   public void firePropertyChange(String prop, Object oldvalue, Object newvalue)
75   {
76     changeSupport.firePropertyChange(prop, oldvalue, newvalue);
77   }
78
79   /**
80    * Initializes the server field with a valid service implementation.
81    *
82    * @return true if service was located.
83    */
84   private IRegistry locateWebService(java.net.URL WsURL)
85   {
86     IRegistryServiceLocator loc = new IRegistryServiceLocator(); // Default
87     IRegistry server = null;
88     try
89     {
90       server = loc.getRegistryService(WsURL);
91       ( (RegistryServiceSoapBindingStub) server).setTimeout(60000); // One minute timeout
92     }
93     catch (Exception ex)
94     {
95       jalview.bin.Cache.log.error(
96           "Serious!  Service location failed\nfor URL :" + WsURL +
97           "\n", ex);
98
99       return null;
100     }
101
102     loc.getEngine().setOption("axis", "1");
103
104     return server;
105   }
106
107   static private java.net.URL RootServiceURL = null;
108   static public Vector ServiceURLList = null;
109   static private boolean reallyDiscoverServices = true;
110
111   public static java.util.Hashtable services = null; // vectors of services stored by abstractServiceType string
112   public static java.util.Vector serviceList = null; // flat list of services
113   static private Vector getDiscoveryURLS() {
114     Vector urls = new Vector();
115     String RootServiceURLs = jalview.bin.Cache.getDefault("DISCOVERY_URLS",
116         "http://www.compbio.dundee.ac.uk/JalviewWS/services/ServiceRegistry");
117
118     try{
119       StringTokenizer st = new StringTokenizer(RootServiceURLs, ",");
120       while (st.hasMoreElements())
121       {
122         String url = null;
123         try
124         {
125           java.net.URL u = new java.net.URL(url = st.nextToken());
126           if (!urls.contains(u))
127             urls.add(u);
128           else
129             jalview.bin.Cache.log.info("Ignoring duplicate url in DISCOVERY_URLS list");
130         }
131         catch (Exception ex)
132         {
133           jalview.bin.Cache.log.warn(
134               "Problem whilst trying to make a URL from '" +
135               ( (url != null) ? url : "<null>")+"'");
136           jalview.bin.Cache.log.warn("This was probably due to a malformed comma separated list"
137                                        +" in the DISCOVERY_URLS entry of $(HOME)/.jalview_properties)");
138           jalview.bin.Cache.log.debug("Exception was ",ex);
139         }
140       }
141     }catch(Exception ex)
142     {jalview.bin.Cache.log.warn("Error parsing comma separated list of urls in DISCOVERY_URLS.",ex);}
143     if (urls.size()>0)
144       return urls;
145     return null;
146   }
147   static
148   {
149
150     try
151     {
152       reallyDiscoverServices = jalview.bin.Cache.getDefault("DISCOVERY_START", false);
153       if (reallyDiscoverServices)
154       {
155         ServiceURLList = getDiscoveryURLS();
156       }
157       else
158       {
159         jalview.bin.Cache.log.debug("Setting default services");
160         services = new Hashtable();
161         // Muscle, Clustal and JPred.
162         ServiceHandle[] defServices = {
163             new ServiceHandle(
164                 "MsaWS",
165                 "Edgar, Robert C. (2004), MUSCLE: multiple sequence alignment " +
166                 "with high accuracy and high throughput, Nucleic Acids Research 32(5), 1792-97.",
167                 "http://www.compbio.dundee.ac.uk/JalviewWS/services/MuscleWS",
168                 "Muscle Multiple Protein Sequence Alignment"
169 ),
170             new ServiceHandle(
171                 "MsaWS",
172                 "Thompson, J.D., Higgins, D.G. and Gibson, T.J. (1994) CLUSTAL W: improving the sensitivity of progressive multiple" +
173                 " sequence alignment through sequence weighting, position specific gap penalties and weight matrix choice." +
174                 " Nucleic Acids Research, 22 4673-4680",
175                 "http://www.compbio.dundee.ac.uk/JalviewWS/services/ClustalWS",
176                 "ClustalW Multiple Sequence Alignment"),
177             new ServiceHandle(
178                 "SecStrPred",
179                 "Cuff J. A and Barton G.J (1999) Application of enhanced " +
180                 "multiple sequence alignment profiles to improve protein secondary structure prediction, " +
181                 "Proteins 40:502-511",
182                 "http://www.compbio.dundee.ac.uk/JalviewWS/services/jpred","JNet Secondary Structure Prediction"
183                 )};
184         services = new Hashtable();
185         serviceList = new Vector();
186         buildServiceLists(defServices, serviceList, services);
187       }
188
189     }
190     catch (Exception e)
191     {
192       System.err.println(
193           "jalview.rootRegistry is not a proper url!\nWas set to " +
194           RootServiceURL + "\n" + e);
195     }
196
197   }
198   // TODO: JBPNote : make this discover more services based on list of
199   // discovery service urls, break cyclic references to the same url and
200   // duplicate service entries (same endpoint *and* same interface)
201   private ServiceHandle[] getServices(java.net.URL location)
202   {
203     ServiceHandles shs = null;
204     try
205     {
206       jalview.bin.Cache.log.debug("Discovering services using " + location);
207       shs = locateWebService(location).getServices();
208     }
209     catch (Exception e)
210     {
211       jalview.bin.Cache.log.debug("No Discovery service at " +
212                          location);
213       jalview.bin.Cache.log.debug(e);
214
215     }
216     if ( (shs != null) && shs.getServices().length > 0)
217     {
218       return shs.getServices();
219     }
220     return null;
221   }
222
223   /**
224    * Adds a list of services to the service catalog and categorised catalog
225    * returns true if ServiceURLList was modified with a new DiscoveryService URL
226    * @param sh ServiceHandle[]
227    * @param cat Vector
228    * @param sscat Hashtable
229    * @return boolean
230    */
231   static private boolean buildServiceLists(ServiceHandle[] sh, Vector cat,
232                                     Hashtable sscat)
233   {
234     boolean seenNewDiscovery = false;
235     for (int i = 0, j = sh.length; i < j; i++)
236     {
237       if (!cat.contains(sh[i]))
238       {
239         jalview.bin.Cache.log.debug("A " + sh[i].getAbstractName() +
240                                       " service called " +
241                                       sh[i].getName() + " exists at " +
242                                       sh[i].getEndpointURL() + "\n");
243         if (!sscat.containsKey(sh[i].getAbstractName()))
244         {
245           sscat.put(sh[i].getAbstractName(), cat = new Vector());
246         }
247         else
248         {
249           cat = (Vector) sscat.get(sh[i].getAbstractName());
250         }
251         cat.add(sh[i]);
252         if (sh[i].getAbstractName().equals("Registry"))
253         {
254           for (int s = 0, sUrls = ServiceURLList.size(); s < sUrls; s++)
255           {
256             java.net.URL disc_serv = null;
257             try
258             {
259               disc_serv = new java.net.URL(sh[i].getEndpointURL());
260               if (!ServiceURLList.contains(disc_serv))
261               {
262                 jalview.bin.Cache.log.debug(
263                     "Adding new discovery service at " + disc_serv);
264                 ServiceURLList.add(disc_serv);
265                 seenNewDiscovery = true;
266               }
267             }
268             catch (Exception e)
269             {
270               jalview.bin.Cache.log.debug(
271                   "Ignoring bad discovery service URL " + sh[i].getEndpointURL(),
272                   e);
273             }
274           }
275         }
276       }
277     }
278     return seenNewDiscovery;
279   }
280
281   public void discoverServices()
282   {
283     Hashtable sscat = new Hashtable();
284     Vector cat = new Vector();
285     ServiceHandle sh[] = null;
286     int s_url = 0;
287     if (ServiceURLList==null)
288     {
289       jalview.bin.Cache.log.debug("No service endpoints to use for service discovery.");
290       return;
291     }
292     while (s_url < ServiceURLList.size())
293     {
294       if ( (sh = getServices( (java.net.URL) ServiceURLList.get(s_url))) != null)
295       {
296
297         buildServiceLists(sh, cat, sscat);
298       } else {
299         jalview.bin.Cache.log.warn(
300             "No services at "
301             +((java.net.URL) ServiceURLList.get(s_url))
302             +" - check DISCOVERY_URLS property in .jalview_properties");
303       }
304       s_url++;
305     }
306     // TODO: decide on correct semantics for services list - PropertyChange
307     // provides a way of passing the new object around
308     // so no need to access original discovery thread.
309     // Curent decision is to change properties then notify listeners with old and new values.
310     Hashtable oldServices = services;
311     //Vector oldServicelist = serviceList;
312     services = sscat;
313     serviceList = cat;
314     firePropertyChange("services", oldServices, services);
315   }
316
317   public void run()
318   {
319     discoverServices();
320   }
321 }