Clover icon

Coverage Report

  1. Project Clover database Wed Dec 3 2025 17:03:17 GMT
  2. Package jalview.ws.jws1

File Discoverer.java

 

Coverage histogram

../../../img/srcFileCovDistChart4.png
49% of files have more coverage

Code metrics

38
110
17
1
460
324
44
0.4
6.47
17
2.59

Classes

Class Line # Actions
Discoverer 41 110 44
0.406060640.6%
 

Contributing tests

This file is covered by 285 tests. .

Source view

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