Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 14:43:25 GMT
  2. Package jalview.ws.jws2

File PreferredServiceRegistry.java

 

Coverage histogram

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

Code metrics

56
123
10
1
355
278
42
0.34
12.3
10
4.2

Classes

Class Line # Actions
PreferredServiceRegistry 23 123 42
0.5820105758.2%
 

Contributing tests

This file is covered by 141 tests. .

Source view

1    package jalview.ws.jws2;
2   
3    import jalview.bin.Cache;
4    import jalview.gui.AlignFrame;
5    import jalview.gui.Desktop;
6    import jalview.gui.JvSwingUtils;
7    import jalview.util.MessageManager;
8    import jalview.ws.api.ServiceWithParameters;
9   
10    import java.awt.Color;
11    import java.awt.event.ActionEvent;
12    import java.awt.event.ActionListener;
13    import java.util.ArrayList;
14    import java.util.Arrays;
15    import java.util.HashMap;
16    import java.util.HashSet;
17    import java.util.Hashtable;
18    import java.util.List;
19    import java.util.Map;
20    import javax.swing.JMenu;
21    import javax.swing.JMenuItem;
22   
 
23    public class PreferredServiceRegistry
24    {
25   
26    private static PreferredServiceRegistry us = new PreferredServiceRegistry();
27   
 
28  2691 toggle public static PreferredServiceRegistry getRegistry()
29    {
30  2691 if (us == null)
31    {
32  0 us = new PreferredServiceRegistry();
33    }
34  2691 return us;
35    }
36   
37    List<ServiceWithParameters> ourServices = new ArrayList<>();
38   
39    /**
40    * forget about any known services
41    */
 
42  0 toggle public void clearServices()
43    {
44  0 ourServices.clear();
45    }
46   
 
47  2691 toggle public void populateWSMenuEntry(List<ServiceWithParameters> services,
48    PreferredServiceChangeListener changeListener, JMenu menu,
49    final AlignFrame alignFrame, String typeFilter)
50    {
51    /**
52    * eventually, JWS2 services will appear under the same align/etc submenus.
53    * for moment we keep them separate.
54    */
55  2691 ourServices.addAll(services);
56  2691 JMenu atpoint;
57   
58  2691 List<ServiceWithParameters> oneshotServices = new ArrayList<>();
59  2691 List<ServiceWithParameters> interactiveServices = new ArrayList<>();
60  2691 Map<String, ServiceWithParameters> preferredHosts = new HashMap<>();
61  2691 Map<String, List<ServiceWithParameters>> alternates = new HashMap<>();
62   
63  2691 for (var service : services)
64    {
65  18168 if (service.isInteractiveUpdate())
66  1884 interactiveServices.add(service);
67    else
68  16284 oneshotServices.add(service);
69    }
70  2691 for (var service : interactiveServices)
71    {
72  1884 if (!preferredHosts.containsKey(service.getName()))
73    {
74  1883 var preferred = getPreferredServiceFor(alignFrame, service.getName());
75  1883 preferredHosts.put(service.getName(), (preferred != null) ? preferred : service);
76    }
77  1884 var ph = alternates.getOrDefault(service.getName(), new ArrayList<>());
78  1884 if (!preferredHosts.containsValue(service))
79    {
80  0 ph.add(service);
81  0 alternates.putIfAbsent(service.getName(), ph);
82    }
83    }
84   
85    // create GUI element for classic services
86  2691 addEnumeratedServices(menu, alignFrame, oneshotServices);
87    // and the instantaneous services
88  2691 for (final ServiceWithParameters service : preferredHosts.values())
89    {
90  1883 atpoint = JvSwingUtils.findOrCreateMenu(menu,
91    service.getServiceType());
92  1883 if (atpoint.getItemCount() > 1)
93    {
94    // previous service of this type already present
95  0 atpoint.addSeparator();
96    }
97  1883 JMenuItem hitm;
98  1883 atpoint.add(hitm = new JMenuItem(service.getHostURL()));
99  1883 hitm.setForeground(Color.blue);
100  1883 hitm.addActionListener(e -> Desktop.showUrl(service.getHostURL()));
101  1883 hitm.setToolTipText(JvSwingUtils.wrapTooltip(false,
102    MessageManager.getString("label.open_jabaws_web_page")));
103   
104  1883 service.attachWSMenuEntry(atpoint, alignFrame);
105  1883 if (alternates.containsKey(service.getName()))
106    {
107  0 atpoint.add(hitm = new JMenu(
108    MessageManager.getString("label.switch_server")));
109  0 hitm.setToolTipText(JvSwingUtils.wrapTooltip(false,
110    MessageManager.getString("label.choose_jabaws_server")));
111  0 for (final ServiceWithParameters sv : alternates
112    .get(service.getName()))
113    {
114  0 JMenuItem itm;
115  0 hitm.add(itm = new JMenuItem(sv.getHostURL()));
116  0 itm.setForeground(Color.blue);
117  0 itm.addActionListener(e -> {
118  0 setPreferredServiceFor(alignFrame, sv.getName(), sv.getServiceType(), sv);
119  0 changeListener.preferredServiceChanged(sv);
120    });
121    }
122    }
123    }
124    }
125   
126    /**
127    * add services using the Java 2.5/2.6/2.7 system which optionally creates
128    * submenus to index by host and service program type
129    */
 
130  2691 toggle private void addEnumeratedServices(final JMenu jws2al,
131    final AlignFrame alignFrame,
132    List<ServiceWithParameters> enumerableServices)
133    {
134  2691 boolean byhost = Cache.getDefault("WSMENU_BYHOST", false),
135    bytype = Cache.getDefault("WSMENU_BYTYPE", false);
136    /**
137    * eventually, JWS2 services will appear under the same align/etc submenus.
138    * for moment we keep them separate.
139    */
140  2691 JMenu atpoint;
141   
142  2691 List<String> hostLabels = new ArrayList<>();
143  2691 Hashtable<String, String> lasthostFor = new Hashtable<>();
144  2691 Hashtable<String, ArrayList<ServiceWithParameters>> hosts = new Hashtable<>();
145  2691 ArrayList<String> hostlist = new ArrayList<>();
146  2691 for (ServiceWithParameters service : enumerableServices)
147    {
148  16284 ArrayList<ServiceWithParameters> hostservices = hosts
149    .get(service.getHostURL());
150  16284 if (hostservices == null)
151    {
152  2691 hosts.put(service.getHostURL(), hostservices = new ArrayList<>());
153  2691 hostlist.add(service.getHostURL());
154    }
155  16284 hostservices.add(service);
156    }
157    // now add hosts in order of the given array
158  2691 for (String host : hostlist)
159    {
160  2691 ServiceWithParameters orderedsvcs[] = hosts.get(host)
161    .toArray(new ServiceWithParameters[1]);
162  2691 String sortbytype[] = new String[orderedsvcs.length];
163  18975 for (int i = 0; i < sortbytype.length; i++)
164    {
165  16284 sortbytype[i] = orderedsvcs[i].getName();
166    }
167  2691 jalview.util.QuickSort.sort(sortbytype, orderedsvcs);
168  2691 for (final ServiceWithParameters service : orderedsvcs)
169    {
170  16284 atpoint = JvSwingUtils.findOrCreateMenu(jws2al,
171    service.getAction());
172  16284 String type = service.getName();
173  16284 if (byhost)
174    {
175  0 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, host);
176  0 if (atpoint.getToolTipText() == null)
177    {
178  0 atpoint.setToolTipText(MessageManager
179    .formatMessage("label.services_at", new String[]
180    { host }));
181    }
182    }
183  16284 if (bytype)
184    {
185  0 atpoint = JvSwingUtils.findOrCreateMenu(atpoint, type);
186  0 if (atpoint.getToolTipText() == null)
187    {
188  0 atpoint.setToolTipText(service.getActionText());
189    }
190    }
191  16284 if (!byhost && !hostLabels.contains(
192    host + service.getName() + service.getActionText()))
193    // !hostLabels.contains(host + (bytype ?
194    // service.serviceType+service.getActionText() : "")))
195    {
196    // add a marker indicating where this service is hosted
197    // relies on services from the same host being listed in a
198    // contiguous
199    // group
200  15320 JMenuItem hitm;
201  15320 if (hostLabels.contains(host))
202    {
203  12629 atpoint.addSeparator();
204    }
205    else
206    {
207  2691 hostLabels.add(host);
208    }
209  15320 if (lasthostFor.get(service.getAction()) == null
210    || !lasthostFor.get(service.getAction()).equals(host))
211    {
212  3618 atpoint.add(hitm = new JMenuItem(host));
213  3618 hitm.setForeground(Color.blue);
214  3618 hitm.addActionListener(new ActionListener()
215    {
216   
 
217  0 toggle @Override
218    public void actionPerformed(ActionEvent e)
219    {
220  0 Desktop.showUrl(service.getHostURL());
221    }
222    });
223  3618 hitm.setToolTipText(
224    JvSwingUtils.wrapTooltip(true, MessageManager
225    .getString("label.open_jabaws_web_page")));
226  3618 lasthostFor.put(service.getAction(), host);
227    }
228  15320 hostLabels
229    .add(host + service.getName() + service.getActionText());
230    }
231   
232  16284 service.attachWSMenuEntry(atpoint, alignFrame);
233    }
234    }
235    }
236   
237    /**
238    * pick the user's preferred service based on a set of URLs (jaba server
239    * locations) and service URIs (specifying version and service interface
240    * class)
241    *
242    * @param serviceURL
243    * @return null or best match for given uri/ls.
244    */
 
245  0 toggle public ServiceWithParameters getPreferredServiceFor(String[] serviceURLs)
246    {
247  0 HashSet<String> urls = new HashSet<>();
248  0 urls.addAll(Arrays.asList(serviceURLs));
249  0 ServiceWithParameters match = null;
250   
251  0 if (ourServices != null)
252    {
253  0 for (ServiceWithParameters svc : ourServices)
254    {
255    // TODO getNameURI Should return a versioned URI for the service, but
256    // doesn't as of 2.11
257  0 if (urls.contains(svc.getNameURI()))
258    {
259  0 if (match == null)
260    {
261    // for moment we always pick service from server ordered first in
262    // user's preferences
263  0 match = svc;
264    }
265  0 if (urls.contains(svc.getUri()))
266    {
267    // stop and return - we've matched type URI and URI for service
268    // endpoint
269  0 return svc;
270    }
271    }
272    }
273    }
274  0 return match;
275    }
276   
277    Map<String, Map<String, String>> preferredServiceMap = new HashMap<>();
278   
279    /**
280    * get current preferred endpoint of the given Jabaws service, or global
281    * default
282    *
283    * @param af
284    * null or a specific alignFrame
285    * @param serviceName
286    * ServiceWithParameters.getName() for service
287    * @return null if no service of this type is available, the preferred service
288    * for the serviceType and af if specified and if defined.
289    */
 
290  1883 toggle public ServiceWithParameters getPreferredServiceFor(AlignFrame af,
291    String serviceName)
292    {
293  1883 String serviceurl = null;
294  1883 synchronized (preferredServiceMap)
295    {
296  1883 String afid = (af == null) ? "" : af.getViewport().getSequenceSetId();
297  1883 Map<String, String> prefmap = preferredServiceMap.get(afid);
298  1883 if (afid.length() > 0 && prefmap == null)
299    {
300    // recover global setting, if any
301  1883 prefmap = preferredServiceMap.get("");
302    }
303  1883 if (prefmap != null)
304    {
305  0 serviceurl = prefmap.get(serviceName);
306    }
307   
308    }
309  1883 ServiceWithParameters response = null;
310  1883 for (ServiceWithParameters svc : ourServices)
311    {
312  1138978 if (svc.getName().equals(serviceName))
313    {
314  1883 if (serviceurl == null || serviceurl.equals(svc.getHostURL()))
315    {
316  1883 response = svc;
317  1883 break;
318    }
319    }
320    }
321  1883 return response;
322    }
323   
 
324  0 toggle public void setPreferredServiceFor(AlignFrame af, String serviceName,
325    String serviceAction, ServiceWithParameters selectedServer)
326    {
327    // TODO: pull out and generalise for the selectedServer's attributes
328  0 String afid = (af == null) ? "" : af.getViewport().getSequenceSetId();
329  0 if (preferredServiceMap == null)
330    {
331  0 preferredServiceMap = new HashMap<>();
332    }
333  0 Map<String, String> prefmap = preferredServiceMap.get(afid);
334  0 if (prefmap == null)
335    {
336  0 prefmap = new HashMap<>();
337  0 preferredServiceMap.put(afid, prefmap);
338    }
339  0 prefmap.put(serviceName, selectedServer.getHostURL());
340  0 prefmap.put(serviceAction, selectedServer.getHostURL());
341    }
342   
 
343  0 toggle public void setPreferredServiceFor(String serviceType,
344    String serviceAction, ServiceWithParameters selectedServer)
345    {
346  0 setPreferredServiceFor(null, serviceType, serviceAction,
347    selectedServer);
348    }
349   
 
350  0 toggle public boolean contains(ServiceWithParameters service)
351    {
352  0 return ourServices.contains(service);
353    }
354   
355    }