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