- /* (non-Javadoc)
- * @see org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator#getURI()
- */
- @Override
- protected URI getURI() {
- return client.getURI();
- }
-
- protected HClient createClient(SecuritySetter<HttpURLConnection> ss, URI uri, int connectTimeout) throws LocatorException {
- return new HClient(ss, uri, connectTimeout);
- }
+ private URI locatorFail(URI uri) throws LocatorException, URISyntaxException {
+ locatorLocator.invalidate(locatorItem);
+ locatorItem = locatorLocator.best();
+ URI newURI = locatorLocator.get(locatorItem);
+ return new URI(uri.getScheme(),
+ uri.getUserInfo(),
+ newURI.getHost(),
+ newURI.getPort(),
+ uri.getPath(),
+ uri.getQuery(),
+ uri.getFragment());
+ }
+
+ protected final int maxIters() {
+ return locatorLocator.size();
+ }
+
+
+ @Override
+ public boolean refresh() {
+ try {
+ int max = locatorLocator.size();
+ for(int i=0;i<max;) {
+ ++i;
+ try {
+ lclient.setMethod("GET");
+ lclient.send();
+ break;
+ } catch (APIException connectE) {
+ Throwable ce = connectE.getCause();
+ if(ce!=null && ce instanceof java.net.ConnectException && i< maxIters()) {
+ try {
+ URI old = client.getURI();
+ lclient.setURI(locatorFail(old));
+ access.printf(Level.INFO, "AAF Locator changed from %s to %s",old, lclient.getURI());
+ continue;
+ } catch (LocatorException e) {
+ throw connectE;
+ }
+ }
+ // last one, just throw
+ throw connectE;
+ }
+ }
+ Future<Endpoints> fr = lclient.futureRead(epsDF, TYPE.JSON);
+ if (fr.get(lclient.timeout())) {
+ List<EP> epl = new LinkedList<>();
+ for (Endpoint endpoint : fr.value.getEndpoint()) {
+ epl.add(new EP(endpoint,latitude,longitude));
+ }
+
+ Collections.sort(epl);
+ replace(epl);
+ return true;
+ } else {
+ env.error().printf("Error reading location information from %s: %d %s\n",client.getURI().toString(),fr.code(),fr.body());
+ }
+ } catch (CadiException | URISyntaxException | APIException e) {
+ env.error().log(e,"Error connecting " + client.getURI() + " for location.");
+ }
+ return false;
+ }
+
+ /* (non-Javadoc)
+ * @see org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator#getURI()
+ */
+ @Override
+ protected URI getURI() {
+ return client.getURI();
+ }
+
+ protected HClient createClient(SecuritySetter<HttpURLConnection> ss, URI uri, int connectTimeout) throws LocatorException {
+ return new HClient(ss, uri, connectTimeout);
+ }
+