+ /**
+ * Start a fresh discovery thread and notify the given object when we're
+ * finished. Any known existing threads will be killed before this one is
+ * started.
+ *
+ * @param changeSupport2
+ * @return new thread
+ */
+ public Thread startDiscoverer(PropertyChangeListener changeSupport2)
+ {
+ if (isRunning())
+ {
+ setAborted(true);
+ }
+ addPropertyChangeListener(changeSupport2);
+ Thread thr = new Thread(this);
+ thr.start();
+ return thr;
+ }
+
+ /**
+ * @return the invalidServiceUrls
+ */
+ public Vector<String> getInvalidServiceUrls()
+ {
+ return invalidServiceUrls;
+ }
+
+ /**
+ * @return the urlsWithoutServices
+ */
+ public Vector<String> getUrlsWithoutServices()
+ {
+ return urlsWithoutServices;
+ }
+
+ /**
+ * add an 'empty' JABA server to the list. Only servers not already in the
+ * 'bad URL' list will be added to this list.
+ *
+ * @param jwsservers
+ */
+ public synchronized void addUrlwithnoservices(String jwsservers)
+ {
+ if (urlsWithoutServices == null)
+ {
+ urlsWithoutServices = new Vector<String>();
+ }
+
+ if ((invalidServiceUrls == null || !invalidServiceUrls
+ .contains(jwsservers))
+ && !urlsWithoutServices.contains(jwsservers))
+ {
+ urlsWithoutServices.add(jwsservers);
+ }
+ }
+
+ /**
+ * add a bad URL to the list
+ *
+ * @param jwsservers
+ */
+ public synchronized void addInvalidServiceUrl(String jwsservers)
+ {
+ if (invalidServiceUrls == null)
+ {
+ invalidServiceUrls = new Vector<String>();
+ }
+ if (!invalidServiceUrls.contains(jwsservers))
+ {
+ invalidServiceUrls.add(jwsservers);
+ }
+ }
+
+ /**
+ *
+ * @return a human readable report of any problems with the service URLs used
+ * for discovery
+ */
+ public String getErrorMessages()
+ {
+ if (!isRunning() && !isAborted())
+ {
+ StringBuffer ermsg = new StringBuffer();
+ boolean list = false;
+ if (getInvalidServiceUrls() != null
+ && getInvalidServiceUrls().size() > 0)
+ {
+ ermsg.append(MessageManager.getString("warn.urls_not_contacted")
+ + ": \n");
+ for (String svcurl : getInvalidServiceUrls())
+ {
+ if (list)
+ {
+ ermsg.append(", ");
+ }
+ list = true;
+ ermsg.append(svcurl);
+ }
+ ermsg.append("\n\n");
+ }
+ list = false;
+ if (getUrlsWithoutServices() != null
+ && getUrlsWithoutServices().size() > 0)
+ {
+ ermsg.append(MessageManager.getString("warn.urls_no_jaba") + ": \n");
+ for (String svcurl : getUrlsWithoutServices())
+ {
+ if (list)
+ {
+ ermsg.append(", ");
+ }
+ list = true;
+ ermsg.append(svcurl);
+ }
+ ermsg.append("\n");
+ }
+ if (ermsg.length() > 1)
+ {
+ return ermsg.toString();
+ }
+
+ }
+ return null;
+ }
+
+ public int getServerStatusFor(String url)
+ {
+ if (validServiceUrls != null && validServiceUrls.contains(url))
+ {
+ return 1;
+ }
+ if (urlsWithoutServices != null && urlsWithoutServices.contains(url))
+ {
+ return 0;
+ }
+ if (invalidServiceUrls != null && invalidServiceUrls.contains(url))
+ {
+ return -1;
+ }
+ return -2;
+ }
+
+ /**
+ * pick the user's preferred service based on a set of URLs (jaba server
+ * locations) and service URIs (specifying version and service interface
+ * class)
+ *
+ * @param serviceURL
+ * @return null or best match for given uri/ls.
+ */
+ public Jws2Instance getPreferredServiceFor(String[] serviceURLs)
+ {
+ HashSet<String> urls = new HashSet<String>();
+ urls.addAll(Arrays.asList(serviceURLs));
+ Jws2Instance match = null;
+ if (services != null)
+ {
+ for (Jws2Instance svc : services)
+ {
+ if (urls.contains(svc.getServiceTypeURI()))
+ {
+ if (match == null)
+ {
+ // for moment we always pick service from server ordered first in
+ // user's preferences
+ match = svc;
+ }
+ if (urls.contains(svc.getUri()))
+ {
+ // stop and return - we've matched type URI and URI for service
+ // endpoint
+ return svc;
+ }
+ }
+ }
+ }
+ return match;
+ }
+
+ Map<String, Map<String, String>> preferredServiceMap = new HashMap<String, Map<String, String>>();;
+
+ /**
+ * get current preferred service of the given type, or global default
+ *
+ * @param af
+ * null or a specific alignFrame
+ * @param serviceType
+ * Jws2Instance.serviceType for service
+ * @return null if no service of this type is available, the preferred service
+ * for the serviceType and af if specified and if defined.
+ */
+ public Jws2Instance getPreferredServiceFor(AlignFrame af,
+ String serviceType)
+ {
+ String serviceurl = null;
+ synchronized (preferredServiceMap)
+ {
+ String afid = (af == null) ? "" : af.getViewport().getSequenceSetId();
+ Map<String, String> prefmap = preferredServiceMap.get(afid);
+ if (afid.length() > 0 && prefmap == null)
+ {
+ // recover global setting, if any
+ prefmap = preferredServiceMap.get("");
+ }
+ if (prefmap != null)
+ {
+ serviceurl = prefmap.get(serviceType);
+ }
+
+ }
+ Jws2Instance response = null;
+ for (Jws2Instance svc : services)
+ {
+ if (svc.serviceType.equals(serviceType))
+ {
+ if (serviceurl == null || serviceurl.equals(svc.getHost()))
+ {
+ response = svc;
+ break;
+ }
+ }
+ }
+ return response;
+ }
+
+ public void setPreferredServiceFor(AlignFrame af, String serviceType,
+ String serviceAction, Jws2Instance selectedServer)
+ {
+ String afid = (af == null) ? "" : af.getViewport().getSequenceSetId();
+ if (preferredServiceMap == null)
+ {
+ preferredServiceMap = new HashMap<String, Map<String, String>>();
+ }
+ Map<String, String> prefmap = preferredServiceMap.get(afid);
+ if (prefmap == null)
+ {
+ prefmap = new HashMap<String, String>();
+ preferredServiceMap.put(afid, prefmap);
+ }
+ prefmap.put(serviceType, selectedServer.getHost());
+ prefmap.put(serviceAction, selectedServer.getHost());
+ }
+
+ public void setPreferredServiceFor(String serviceType,
+ String serviceAction, Jws2Instance selectedServer)
+ {
+ setPreferredServiceFor(null, serviceType, serviceAction, selectedServer);
+ }
+
+ /**
+ * Set a URL to try before any others. For use with command-line parameter to
+ * configure a local Jabaws installation without the need to add to property
+ * files.
+ *
+ * @param value
+ * @throws MalformedURLException
+ */
+ public void setPreferredUrl(String value) throws MalformedURLException
+ {
+ if (value != null && value.trim().length() > 0)
+ {
+ new URL(value);
+ preferredUrl = value;
+ }
+ }