Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.ws.jws2

File Jws2Discoverer.java

 

Coverage histogram

../../../img/srcFileCovDistChart6.png
33% of files have more coverage

Code metrics

148
320
37
1
1,066
788
141
0.44
8.65
37
3.81

Classes

Class Line # Actions
Jws2Discoverer 63 320 141
0.5584158355.8%
 

Contributing tests

This file is covered by 157 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.jws2;
22   
23    import jalview.bin.Cache;
24    import jalview.gui.AlignFrame;
25    import jalview.gui.Desktop;
26    import jalview.gui.JvSwingUtils;
27    import jalview.util.MessageManager;
28    import jalview.ws.WSMenuEntryProviderI;
29    import jalview.ws.jws2.jabaws2.Jws2Instance;
30    import jalview.ws.params.ParamDatastoreI;
31   
32    import java.awt.Color;
33    import java.awt.event.ActionEvent;
34    import java.awt.event.ActionListener;
35    import java.beans.PropertyChangeEvent;
36    import java.beans.PropertyChangeListener;
37    import java.beans.PropertyChangeSupport;
38    import java.net.MalformedURLException;
39    import java.net.URL;
40    import java.util.ArrayList;
41    import java.util.Arrays;
42    import java.util.HashMap;
43    import java.util.HashSet;
44    import java.util.Hashtable;
45    import java.util.List;
46    import java.util.Map;
47    import java.util.Set;
48    import java.util.StringTokenizer;
49    import java.util.Vector;
50   
51    import javax.swing.JMenu;
52    import javax.swing.JMenuItem;
53   
54    import compbio.ws.client.Services;
55   
56    /**
57    * discoverer for jws2 services. Follows the lightweight service discoverer
58    * pattern (archetyped by EnfinEnvision2OneWay)
59    *
60    * @author JimP
61    *
62    */
 
63    public class Jws2Discoverer implements Runnable, WSMenuEntryProviderI
64    {
65    public static final String COMPBIO_JABAWS = "http://www.compbio.dundee.ac.uk/jabaws";
66   
67    /*
68    * the .jalview_properties entry for JWS2 URLS
69    */
70    private final static String JWS2HOSTURLS = "JWS2HOSTURLS";
71   
72    /*
73    * Singleton instance
74    */
75    private static Jws2Discoverer discoverer;
76   
77    /*
78    * Override for testing only
79    */
80    private static List<String> testUrls = null;
81   
82    // preferred url has precedence over others
83    private String preferredUrl;
84   
85    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
86    this);
87   
88    private Vector<String> invalidServiceUrls = null;
89   
90    private Vector<String> urlsWithoutServices = null;
91   
92    private Vector<String> validServiceUrls = null;
93   
94    private volatile boolean running = false;
95   
96    private volatile boolean aborted = false;
97   
98    private Thread oldthread = null;
99   
100    /**
101    * holds list of services.
102    */
103    protected Vector<Jws2Instance> services;
104   
105    /**
106    * Private constructor enforces use of singleton via getDiscoverer()
107    */
 
108  3 toggle private Jws2Discoverer()
109    {
110    }
111   
112    /**
113    * change listeners are notified of "services" property changes
114    *
115    * @param listener
116    * to be added that consumes new services Hashtable object.
117    */
 
118  18 toggle public void addPropertyChangeListener(
119    java.beans.PropertyChangeListener listener)
120    {
121  18 changeSupport.addPropertyChangeListener(listener);
122    }
123   
124    /**
125    *
126    *
127    * @param listener
128    * to be removed
129    */
 
130  0 toggle public void removePropertyChangeListener(
131    java.beans.PropertyChangeListener listener)
132    {
133  0 changeSupport.removePropertyChangeListener(listener);
134    }
135   
136    /**
137    * @return the aborted
138    */
 
139  145 toggle public boolean isAborted()
140    {
141  145 return aborted;
142    }
143   
144    /**
145    * @param aborted
146    * the aborted to set
147    */
 
148  8 toggle public void setAborted(boolean aborted)
149    {
150  8 this.aborted = aborted;
151    }
152   
 
153  18 toggle @Override
154    public void run()
155    {
156   
157  18 if (running && oldthread != null && oldthread.isAlive())
158    {
159  8 if (!aborted)
160    {
161  0 return;
162    }
163  17 while (running)
164    {
165  9 try
166    {
167  9 Cache.log.debug(
168    "Waiting around for old discovery thread to finish.");
169    // wait around until old discoverer dies
170  9 Thread.sleep(100);
171    } catch (Exception e)
172    {
173    }
174    }
175  8 aborted = false;
176  8 Cache.log.debug("Old discovery thread has finished.");
177    }
178  18 running = true;
179   
180    // first set up exclusion list if needed
181  18 final Set<String> ignoredServices = new HashSet<>();
182  18 for (String ignored : Cache
183    .getDefault("IGNORED_JABAWS_SERVICETYPES", "").split("\\|"))
184    {
185  18 ignoredServices.add(ignored);
186    }
187   
188  18 changeSupport.firePropertyChange("services", services,
189    new Vector<Jws2Instance>());
190  18 oldthread = Thread.currentThread();
191  18 try
192    {
193  18 Class foo = getClass().getClassLoader()
194    .loadClass("compbio.ws.client.Jws2Client");
195    } catch (ClassNotFoundException e)
196    {
197  0 System.err.println(
198    "Not enabling JABA Webservices : client jar is not available."
199    + "\nPlease check that your webstart JNLP file is up to date!");
200  0 running = false;
201  0 return;
202    }
203    // reinitialise records of good and bad service URLs
204  18 if (services != null)
205    {
206  16 services.removeAllElements();
207    }
208  18 if (urlsWithoutServices != null)
209    {
210  0 urlsWithoutServices.removeAllElements();
211    }
212  18 if (invalidServiceUrls != null)
213    {
214  0 invalidServiceUrls.removeAllElements();
215    }
216  18 if (validServiceUrls != null)
217    {
218  16 validServiceUrls.removeAllElements();
219    }
220  18 ArrayList<String> svctypes = new ArrayList<>();
221   
222  18 List<JabaWsServerQuery> qrys = new ArrayList<>();
223  18 for (final String jwsserver : getServiceUrls())
224    {
225  18 JabaWsServerQuery squery = new JabaWsServerQuery(this, jwsserver);
226  18 if (svctypes.size() == 0)
227    {
228    // TODO: remove this ugly hack to get Canonical JABA service ordering
229    // for all possible services
230  18 for (Services sv : squery.JABAWS2SERVERS)
231    {
232  198 if (!ignoredServices.contains(sv.toString()))
233    {
234  198 svctypes.add(sv.toString());
235    }
236    }
237   
238    }
239  18 qrys.add(squery);
240  18 new Thread(squery).start();
241    }
242  18 boolean finished = true;
243  18 do
244    {
245  108 finished = true;
246  108 try
247    {
248  108 Thread.sleep(100);
249    } catch (Exception e)
250    {
251    }
252  108 for (JabaWsServerQuery squery : qrys)
253    {
254  108 if (squery.isRunning())
255    {
256  98 finished = false;
257    }
258    }
259  108 if (aborted)
260    {
261  8 Cache.log.debug(
262    "Aborting " + qrys.size() + " JABAWS discovery threads.");
263  8 for (JabaWsServerQuery squery : qrys)
264    {
265  8 squery.setQuit(true);
266    }
267    }
268  108 } while (!aborted && !finished);
269  18 if (!aborted)
270    {
271    // resort services according to order found in jabaws service list
272    // also ensure services for each host are ordered in same way.
273   
274  10 if (services != null && services.size() > 0)
275    {
276  10 Jws2Instance[] svcs = new Jws2Instance[services.size()];
277  10 int[] spos = new int[services.size()];
278  10 int ipos = 0;
279  10 List<String> svcUrls = getServiceUrls();
280  10 for (Jws2Instance svc : services)
281    {
282  140 svcs[ipos] = svc;
283  140 spos[ipos++] = 1000 * svcUrls.indexOf(svc.getHost()) + 1
284    + svctypes.indexOf(svc.serviceType);
285    }
286  10 jalview.util.QuickSort.sort(spos, svcs);
287  10 services = new Vector<>();
288  10 for (Jws2Instance svc : svcs)
289    {
290  140 if (!ignoredServices.contains(svc.serviceType))
291    {
292  140 services.add(svc);
293    }
294    }
295    }
296    }
297  18 oldthread = null;
298  18 running = false;
299  18 changeSupport.firePropertyChange("services", new Vector<Jws2Instance>(),
300    services);
301    }
302   
303    /**
304    * record this service endpoint so we can use it
305    *
306    * @param jwsservers
307    * @param srv
308    * @param service2
309    */
 
310  191 toggle synchronized void addService(String jwsservers, Jws2Instance service)
311    {
312  191 if (services == null)
313    {
314  2 services = new Vector<>();
315    }
316  191 System.out.println(
317    "Discovered service: " + jwsservers + " " + service.toString());
318    // Jws2Instance service = new Jws2Instance(jwsservers, srv.toString(),
319    // service2);
320   
321  191 services.add(service);
322    // retrieve the presets and parameter set and cache now
323  191 ParamDatastoreI pds = service.getParamStore();
324  191 if (pds != null)
325    {
326  191 pds.getPresets();
327    }
328  191 service.hasParameters();
329  191 if (validServiceUrls == null)
330    {
331  2 validServiceUrls = new Vector<>();
332    }
333  191 validServiceUrls.add(jwsservers);
334    }
335   
336    /**
337    * attach all available web services to the appropriate submenu in the given
338    * JMenu
339    */
 
340  625 toggle @Override
341    public void attachWSMenuEntry(JMenu wsmenu, final AlignFrame alignFrame)
342    {
343    // dynamically regenerate service list.
344  625 populateWSMenuEntry(wsmenu, alignFrame, null);
345    }
346   
 
347  8079 toggle private boolean isRecalculable(String action)
348    {
349  8079 return (action != null && action.equalsIgnoreCase("conservation"));
350    }
351   
 
352  625 toggle private void populateWSMenuEntry(JMenu jws2al,
353    final AlignFrame alignFrame, String typeFilter)
354    {
355  625 if (running || services == null || services.size() == 0)
356    {
357  0 return;
358    }
359   
360    /**
361    * eventually, JWS2 services will appear under the same align/etc submenus.
362    * for moment we keep them separate.
363    */
364  625 JMenu atpoint;
365  625 List<Jws2Instance> enumerableServices = new ArrayList<>();
366    // jws2al.removeAll();
367  625 Map<String, Jws2Instance> preferredHosts = new HashMap<>();
368  625 Map<String, List<Jws2Instance>> alternates = new HashMap<>();
369  625 for (Jws2Instance service : services.toArray(new Jws2Instance[0]))
370    {
371  8079 if (!isRecalculable(service.action))
372    {
373    // add 'one shot' services to be displayed using the classic menu
374    // structure
375  7504 enumerableServices.add(service);
376    }
377    else
378    {
379  575 if (!preferredHosts.containsKey(service.serviceType))
380    {
381  575 Jws2Instance preferredInstance = getPreferredServiceFor(
382    alignFrame, service.serviceType);
383  575 if (preferredInstance != null)
384    {
385  575 preferredHosts.put(service.serviceType, preferredInstance);
386    }
387    else
388    {
389  0 preferredHosts.put(service.serviceType, service);
390    }
391    }
392  575 List<Jws2Instance> ph = alternates.get(service.serviceType);
393  575 if (preferredHosts.get(service.serviceType) != service)
394    {
395  0 if (ph == null)
396    {
397  0 ph = new ArrayList<>();
398    }
399  0 ph.add(service);
400  0 alternates.put(service.serviceType, ph);
401    }
402    }
403   
404    }
405   
406    // create GUI element for classic services
407  625 addEnumeratedServices(jws2al, alignFrame, enumerableServices);
408    // and the instantaneous services
409  621 for (final Jws2Instance service : preferredHosts.values())
410    {
411  571 atpoint = JvSwingUtils.findOrCreateMenu(jws2al, service.action);
412  571 JMenuItem hitm;
413  571 if (atpoint.getItemCount() > 1)
414    {
415    // previous service of this type already present
416  0 atpoint.addSeparator();
417    }
418  571 atpoint.add(hitm = new JMenuItem(service.getHost()));
419  571 hitm.setForeground(Color.blue);
420  571 hitm.addActionListener(new ActionListener()
421    {
422   
 
423  0 toggle @Override
424    public void actionPerformed(ActionEvent e)
425    {
426  0 Desktop.showUrl(service.getHost());
427    }
428    });
429  571 hitm.setToolTipText(JvSwingUtils.wrapTooltip(false,
430    MessageManager.getString("label.open_jabaws_web_page")));
431   
432  571 service.attachWSMenuEntry(atpoint, alignFrame);
433  571 if (alternates.containsKey(service.serviceType))
434    {
435  0 atpoint.add(hitm = new JMenu(
436    MessageManager.getString("label.switch_server")));
437  0 hitm.setToolTipText(JvSwingUtils.wrapTooltip(false,
438    MessageManager.getString("label.choose_jabaws_server")));
439  0 for (final Jws2Instance sv : alternates.get(service.serviceType))
440    {
441  0 JMenuItem itm;
442  0 hitm.add(itm = new JMenuItem(sv.getHost()));
443  0 itm.setForeground(Color.blue);
444  0 itm.addActionListener(new ActionListener()
445    {
446   
 
447  0 toggle @Override
448    public void actionPerformed(ActionEvent arg0)
449    {
450  0 new Thread(new Runnable()
451    {
 
452  0 toggle @Override
453    public void run()
454    {
455  0 setPreferredServiceFor(alignFrame, sv.serviceType,
456    sv.action, sv);
457  0 changeSupport.firePropertyChange("services",
458    new Vector<Jws2Instance>(), services);
459    }
460    }).start();
461   
462    }
463    });
464    }
465    }
466    }
467    }
468   
469    /**
470    * add services using the Java 2.5/2.6/2.7 system which optionally creates
471    * submenus to index by host and service program type
472    */
 
473  625 toggle private void addEnumeratedServices(final JMenu jws2al,
474    final AlignFrame alignFrame,
475    List<Jws2Instance> enumerableServices)
476    {
477  625 boolean byhost = Cache.getDefault("WSMENU_BYHOST", false),
478    bytype = Cache.getDefault("WSMENU_BYTYPE", false);
479    /**
480    * eventually, JWS2 services will appear under the same align/etc submenus.
481    * for moment we keep them separate.
482    */
483  625 JMenu atpoint;
484   
485  625 List<String> hostLabels = new ArrayList<>();
486  625 Hashtable<String, String> lasthostFor = new Hashtable<>();
487  625 Hashtable<String, ArrayList<Jws2Instance>> hosts = new Hashtable<>();
488  625 ArrayList<String> hostlist = new ArrayList<>();
489  625 for (Jws2Instance service : enumerableServices)
490    {
491  7504 ArrayList<Jws2Instance> hostservices = hosts.get(service.getHost());
492  7504 if (hostservices == null)
493    {
494  625 hosts.put(service.getHost(),
495    hostservices = new ArrayList<>());
496  625 hostlist.add(service.getHost());
497    }
498  7504 hostservices.add(service);
499    }
500    // now add hosts in order of the given array
501  625 for (String host : hostlist)
502    {
503  625 Jws2Instance orderedsvcs[] = hosts.get(host)
504    .toArray(new Jws2Instance[1]);
505  625 String sortbytype[] = new String[orderedsvcs.length];
506  8129 for (int i = 0; i < sortbytype.length; i++)
507    {
508  7504 sortbytype[i] = orderedsvcs[i].serviceType;
509    }
510  625 jalview.util.QuickSort.sort(sortbytype, orderedsvcs);
511  625 for (final Jws2Instance service : orderedsvcs)
512    {
513  7460 atpoint = JvSwingUtils.findOrCreateMenu(jws2al, service.action);
514  7460 String type = service.serviceType;
515  7460 if (byhost)
516    {
517  0 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, host);
518  0 if (atpoint.getToolTipText() == null)
519    {
520  0 atpoint.setToolTipText(MessageManager
521    .formatMessage("label.services_at", new String[]
522    { host }));
523    }
524    }
525  7460 if (bytype)
526    {
527  0 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, type);
528  0 if (atpoint.getToolTipText() == null)
529    {
530  0 atpoint.setToolTipText(service.getActionText());
531    }
532    }
533  7460 if (!byhost && !hostLabels.contains(
534    host + service.serviceType + service.getActionText()))
535    // !hostLabels.contains(host + (bytype ?
536    // service.serviceType+service.getActionText() : "")))
537    {
538    // add a marker indicating where this service is hosted
539    // relies on services from the same host being listed in a
540    // contiguous
541    // group
542  7457 JMenuItem hitm;
543  7457 if (hostLabels.contains(host))
544    {
545  6832 atpoint.addSeparator();
546    }
547    else
548    {
549  625 hostLabels.add(host);
550    }
551  7457 if (lasthostFor.get(service.action) == null
552    || !lasthostFor.get(service.action).equals(host))
553    {
554  1707 atpoint.add(hitm = new JMenuItem(host));
555  1707 hitm.setForeground(Color.blue);
556  1707 hitm.addActionListener(new ActionListener()
557    {
558   
 
559  0 toggle @Override
560    public void actionPerformed(ActionEvent e)
561    {
562  0 Desktop.showUrl(service.getHost());
563    }
564    });
565  1707 hitm.setToolTipText(
566    JvSwingUtils.wrapTooltip(true, MessageManager
567    .getString("label.open_jabaws_web_page")));
568  1707 lasthostFor.put(service.action, host);
569    }
570  7457 hostLabels.add(
571    host + service.serviceType + service.getActionText());
572    }
573   
574  7460 service.attachWSMenuEntry(atpoint, alignFrame);
575    }
576    }
577    }
578   
579    /**
580    *
581    * @param args
582    * @j2sIgnore
583    */
 
584  0 toggle public static void main(String[] args)
585    {
586  0 if (args.length > 0)
587    {
588  0 testUrls = new ArrayList<>();
589  0 for (String url : args)
590    {
591  0 testUrls.add(url);
592    }
593    }
594  0 Thread runner = getDiscoverer()
595    .startDiscoverer(new PropertyChangeListener()
596    {
597   
 
598  0 toggle @Override
599    public void propertyChange(PropertyChangeEvent evt)
600    {
601  0 if (getDiscoverer().services != null)
602    {
603  0 System.out.println("Changesupport: There are now "
604    + getDiscoverer().services.size() + " services");
605  0 int i = 1;
606  0 for (Jws2Instance instance : getDiscoverer().services)
607    {
608  0 System.out.println("Service " + i++ + " "
609    + instance.getClass() + "@" + instance.getHost()
610    + ": " + instance.getActionText());
611    }
612   
613    }
614    }
615    });
616  0 while (runner.isAlive())
617    {
618  0 try
619    {
620  0 Thread.sleep(50);
621    } catch (InterruptedException e)
622    {
623    }
624    }
625  0 try
626    {
627  0 Thread.sleep(50);
628    } catch (InterruptedException x)
629    {
630    }
631    }
632   
633    /**
634    * Returns the singleton instance of this class.
635    *
636    * @return
637    */
 
638  2068 toggle public static Jws2Discoverer getDiscoverer()
639    {
640  2068 if (discoverer == null)
641    {
642  3 discoverer = new Jws2Discoverer();
643    }
644  2068 return discoverer;
645    }
646   
 
647  1750 toggle public boolean hasServices()
648    {
649  1750 return !running && services != null && services.size() > 0;
650    }
651   
 
652  2063 toggle public boolean isRunning()
653    {
654  2063 return running;
655    }
656   
 
657  0 toggle public void setServiceUrls(List<String> wsUrls)
658    {
659  0 if (wsUrls != null && !wsUrls.isEmpty())
660    {
661  0 StringBuilder urls = new StringBuilder(128);
662  0 String sep = "";
663  0 for (String url : wsUrls)
664    {
665  0 urls.append(sep);
666  0 urls.append(url);
667  0 sep = ",";
668    }
669  0 Cache.setProperty(JWS2HOSTURLS, urls.toString());
670    }
671    else
672    {
673  0 Cache.removeProperty(JWS2HOSTURLS);
674    }
675    }
676   
677    /**
678    * Returns web service URLs, in the order in which they should be tried (or an
679    * empty list).
680    *
681    * @return
682    */
 
683  28 toggle public List<String> getServiceUrls()
684    {
685  28 if (testUrls != null)
686    {
687    // return test urls, if there are any, instead of touching cache
688  0 return testUrls;
689    }
690  28 List<String> urls = new ArrayList<>();
691   
692  28 if (this.preferredUrl != null)
693    {
694  0 urls.add(preferredUrl);
695    }
696   
697  28 String surls = Cache.getDefault(JWS2HOSTURLS, COMPBIO_JABAWS);
698  28 try
699    {
700  28 StringTokenizer st = new StringTokenizer(surls, ",");
701  56 while (st.hasMoreElements())
702    {
703  28 String url = null;
704  28 try
705    {
706  28 url = st.nextToken();
707  28 new URL(url);
708  28 if (!urls.contains(url))
709    {
710  28 urls.add(url);
711    }
712    else
713    {
714  0 Cache.log.warn("Ignoring duplicate url " + url + " in "
715    + JWS2HOSTURLS + " list");
716    }
717    } catch (MalformedURLException ex)
718    {
719  0 Cache.log.warn("Problem whilst trying to make a URL from '"
720  0 + ((url != null) ? url : "<null>") + "'");
721  0 Cache.log.warn(
722    "This was probably due to a malformed comma separated list"
723    + " in the " + JWS2HOSTURLS
724    + " entry of $(HOME)/.jalview_properties)");
725  0 Cache.log.debug("Exception was ", ex);
726    }
727    }
728    } catch (Exception ex)
729    {
730  0 Cache.log.warn("Error parsing comma separated list of urls in "
731    + JWS2HOSTURLS + " preference.", ex);
732    }
733  28 return urls;
734    }
735   
 
736  621 toggle public Vector<Jws2Instance> getServices()
737    {
738  621 return (services == null) ? new Vector<>()
739    : new Vector<>(services);
740    }
741   
742    /**
743    * test the given URL with the JabaWS test code
744    *
745    * @param foo
746    * @return
747    */
 
748  0 toggle public static boolean testServiceUrl(URL foo)
749    {
750  0 try
751    {
752  0 compbio.ws.client.WSTester
753    .main(new String[]
754    { "-h=" + foo.toString() });
755    } catch (Exception e)
756    {
757  0 e.printStackTrace();
758  0 return false;
759    } catch (OutOfMemoryError e)
760    {
761  0 e.printStackTrace();
762  0 return false;
763    } catch (Error e)
764    {
765  0 e.printStackTrace();
766  0 return false;
767    }
768   
769  0 return true;
770    }
771   
 
772  0 toggle public boolean restart()
773    {
774  0 synchronized (this)
775    {
776  0 if (running)
777    {
778  0 aborted = true;
779    }
780    else
781    {
782  0 running = true;
783    }
784  0 return aborted;
785    }
786    }
787   
788    /**
789    * Start a fresh discovery thread and notify the given object when we're
790    * finished. Any known existing threads will be killed before this one is
791    * started.
792    *
793    * @param changeSupport2
794    * @return new thread
795    */
 
796  18 toggle public Thread startDiscoverer(PropertyChangeListener changeSupport2)
797    {
798    /* if (restart())
799    {
800    return;
801    }
802    else
803    {
804    Thread thr = new Thread(this);
805    thr.start();
806    }
807    */
808  18 if (isRunning())
809    {
810  8 setAborted(true);
811    }
812  18 addPropertyChangeListener(changeSupport2);
813  18 Thread thr = new Thread(this);
814  18 thr.start();
815  18 return thr;
816    }
817   
818    /**
819    * @return the invalidServiceUrls
820    */
 
821  74 toggle public Vector<String> getInvalidServiceUrls()
822    {
823  74 return invalidServiceUrls;
824    }
825   
826    /**
827    * @return the urlsWithoutServices
828    */
 
829  74 toggle public Vector<String> getUrlsWithoutServices()
830    {
831  74 return urlsWithoutServices;
832    }
833   
834    /**
835    * add an 'empty' JABA server to the list. Only servers not already in the
836    * 'bad URL' list will be added to this list.
837    *
838    * @param jwsservers
839    */
 
840  0 toggle public synchronized void addUrlwithnoservices(String jwsservers)
841    {
842  0 if (urlsWithoutServices == null)
843    {
844  0 urlsWithoutServices = new Vector<>();
845    }
846   
847  0 if ((invalidServiceUrls == null
848    || !invalidServiceUrls.contains(jwsservers))
849    && !urlsWithoutServices.contains(jwsservers))
850    {
851  0 urlsWithoutServices.add(jwsservers);
852    }
853    }
854   
855    /**
856    * add a bad URL to the list
857    *
858    * @param jwsservers
859    */
 
860  0 toggle public synchronized void addInvalidServiceUrl(String jwsservers)
861    {
862  0 if (invalidServiceUrls == null)
863    {
864  0 invalidServiceUrls = new Vector<>();
865    }
866  0 if (!invalidServiceUrls.contains(jwsservers))
867    {
868  0 invalidServiceUrls.add(jwsservers);
869    }
870    }
871   
872    /**
873    *
874    * @return a human readable report of any problems with the service URLs used
875    * for discovery
876    */
 
877  299 toggle public String getErrorMessages()
878    {
879  299 if (!isRunning() && !isAborted())
880    {
881  74 StringBuffer ermsg = new StringBuffer();
882  74 boolean list = false;
883  74 if (getInvalidServiceUrls() != null
884    && getInvalidServiceUrls().size() > 0)
885    {
886  0 ermsg.append(MessageManager.getString("warn.urls_not_contacted")
887    + ": \n");
888  0 for (String svcurl : getInvalidServiceUrls())
889    {
890  0 if (list)
891    {
892  0 ermsg.append(", ");
893    }
894  0 list = true;
895  0 ermsg.append(svcurl);
896    }
897  0 ermsg.append("\n\n");
898    }
899  74 list = false;
900  74 if (getUrlsWithoutServices() != null
901    && getUrlsWithoutServices().size() > 0)
902    {
903  0 ermsg.append(
904    MessageManager.getString("warn.urls_no_jaba") + ": \n");
905  0 for (String svcurl : getUrlsWithoutServices())
906    {
907  0 if (list)
908    {
909  0 ermsg.append(", ");
910    }
911  0 list = true;
912  0 ermsg.append(svcurl);
913    }
914  0 ermsg.append("\n");
915    }
916  74 if (ermsg.length() > 1)
917    {
918  0 return ermsg.toString();
919    }
920   
921    }
922  299 return null;
923    }
924   
 
925  0 toggle public int getServerStatusFor(String url)
926    {
927  0 if (validServiceUrls != null && validServiceUrls.contains(url))
928    {
929  0 return 1;
930    }
931  0 if (urlsWithoutServices != null && urlsWithoutServices.contains(url))
932    {
933  0 return 0;
934    }
935  0 if (invalidServiceUrls != null && invalidServiceUrls.contains(url))
936    {
937  0 return -1;
938    }
939  0 return -2;
940    }
941   
942    /**
943    * pick the user's preferred service based on a set of URLs (jaba server
944    * locations) and service URIs (specifying version and service interface
945    * class)
946    *
947    * @param serviceURL
948    * @return null or best match for given uri/ls.
949    */
 
950  0 toggle public Jws2Instance getPreferredServiceFor(String[] serviceURLs)
951    {
952  0 HashSet<String> urls = new HashSet<>();
953  0 urls.addAll(Arrays.asList(serviceURLs));
954  0 Jws2Instance match = null;
955  0 if (services != null)
956    {
957  0 for (Jws2Instance svc : services)
958    {
959  0 if (urls.contains(svc.getServiceTypeURI()))
960    {
961  0 if (match == null)
962    {
963    // for moment we always pick service from server ordered first in
964    // user's preferences
965  0 match = svc;
966    }
967  0 if (urls.contains(svc.getUri()))
968    {
969    // stop and return - we've matched type URI and URI for service
970    // endpoint
971  0 return svc;
972    }
973    }
974    }
975    }
976  0 return match;
977    }
978   
979    Map<String, Map<String, String>> preferredServiceMap = new HashMap<>();
980   
981    /**
982    * get current preferred service of the given type, or global default
983    *
984    * @param af
985    * null or a specific alignFrame
986    * @param serviceType
987    * Jws2Instance.serviceType for service
988    * @return null if no service of this type is available, the preferred service
989    * for the serviceType and af if specified and if defined.
990    */
 
991  575 toggle public Jws2Instance getPreferredServiceFor(AlignFrame af,
992    String serviceType)
993    {
994  575 String serviceurl = null;
995  575 synchronized (preferredServiceMap)
996    {
997  575 String afid = (af == null) ? "" : af.getViewport().getSequenceSetId();
998  575 Map<String, String> prefmap = preferredServiceMap.get(afid);
999  575 if (afid.length() > 0 && prefmap == null)
1000    {
1001    // recover global setting, if any
1002  575 prefmap = preferredServiceMap.get("");
1003    }
1004  575 if (prefmap != null)
1005    {
1006  0 serviceurl = prefmap.get(serviceType);
1007    }
1008   
1009    }
1010  575 Jws2Instance response = null;
1011  575 for (Jws2Instance svc : services)
1012    {
1013  5178 if (svc.serviceType.equals(serviceType))
1014    {
1015  575 if (serviceurl == null || serviceurl.equals(svc.getHost()))
1016    {
1017  575 response = svc;
1018  575 break;
1019    }
1020    }
1021    }
1022  575 return response;
1023    }
1024   
 
1025  0 toggle public void setPreferredServiceFor(AlignFrame af, String serviceType,
1026    String serviceAction, Jws2Instance selectedServer)
1027    {
1028  0 String afid = (af == null) ? "" : af.getViewport().getSequenceSetId();
1029  0 if (preferredServiceMap == null)
1030    {
1031  0 preferredServiceMap = new HashMap<>();
1032    }
1033  0 Map<String, String> prefmap = preferredServiceMap.get(afid);
1034  0 if (prefmap == null)
1035    {
1036  0 prefmap = new HashMap<>();
1037  0 preferredServiceMap.put(afid, prefmap);
1038    }
1039  0 prefmap.put(serviceType, selectedServer.getHost());
1040  0 prefmap.put(serviceAction, selectedServer.getHost());
1041    }
1042   
 
1043  0 toggle public void setPreferredServiceFor(String serviceType,
1044    String serviceAction, Jws2Instance selectedServer)
1045    {
1046  0 setPreferredServiceFor(null, serviceType, serviceAction,
1047    selectedServer);
1048    }
1049   
1050    /**
1051    * Set a URL to try before any others. For use with command-line parameter to
1052    * configure a local Jabaws installation without the need to add to property
1053    * files.
1054    *
1055    * @param value
1056    * @throws MalformedURLException
1057    */
 
1058  1 toggle public void setPreferredUrl(String value) throws MalformedURLException
1059    {
1060  1 if (value != null && value.trim().length() > 0)
1061    {
1062  1 new URL(value);
1063  1 preferredUrl = value;
1064    }
1065    }
1066    }