Fix Agent and CM Issues
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / v2_0 / AbsAAFLocator.java
index e43250a..a6b7da8 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -66,20 +66,20 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
 
 
     public AbsAAFLocator(Access access, String name, final long refreshMin) throws LocatorException {
-       RegistrationPropHolder rph;
-               try {
-                       rph = new RegistrationPropHolder(access, 0);
-               } catch (UnknownHostException | CadiException e1) {
-                       throw new LocatorException(e1);
-               }
+        RegistrationPropHolder rph;
         try {
-               aaf_locator_host = rph.replacements(getClass().getSimpleName(),"https://"+Config.AAF_LOCATE_URL_TAG,null,null);
-               if(aaf_locator_host.endsWith("/locate")) {
+            rph = new RegistrationPropHolder(access, 0);
+        } catch (UnknownHostException | CadiException e1) {
+            throw new LocatorException(e1);
+        }
+        URI aaf_locator_uri;
+        try {
+            aaf_locator_host = rph.replacements(getClass().getSimpleName(),"https://"+Config.AAF_LOCATE_URL_TAG,null,null);
+            if(aaf_locator_host.endsWith("/locate")) {
                 aaf_locator_uri = new URI(aaf_locator_host);
-               } else {
-                       aaf_locator_uri = new URI(aaf_locator_host+"/locate");
-               }
-               
+            } else {
+                aaf_locator_uri = new URI(aaf_locator_host+"/locate");
+            }
             access.printf(Level.INFO, "AbsAAFLocator AAF URI is %s",aaf_locator_uri);
         } catch (URISyntaxException e) {
             throw new LocatorException(e);
@@ -110,7 +110,6 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
             this.name = split[0];
             this.version = (split.length > 1) ? split[1] : access.getProperty(Config.AAF_API_VERSION,Config.AAF_DEFAULT_API_VERSION);
         }
-        
     }
 
     /**
@@ -118,13 +117,13 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
      * @param preload
      */
     public static void setCreator(LocatorCreator lc) {
-        locatorCreator = lc; 
+        locatorCreator = lc;
     }
-        
+
     public static Locator<URI> create(final String name, final String version) throws LocatorException {
-       if(locatorCreator==null) {
-               throw new LocatorException("LocatorCreator is not set");
-       }
+        if(locatorCreator==null) {
+            throw new LocatorException("LocatorCreator is not set");
+        }
         return locatorCreator.create(name, version);
     }
 
@@ -138,14 +137,14 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
         if (path.length>1 && "locate".equals(path[1])) {
            return path[2];
         } else if(path.length>1) {
-               return path[1];
+             return path[1];
         } else {
             return locatorURI.toString();
         }
     }
-    
+
     /**
-     * Setting "self" excludes this service from the list.  Critical for contacting peers. 
+     * Setting "self" excludes this service from the list.  Critical for contacting peers.
      */
     public void setSelf(final String hostname, final int port) {
         myhostname=hostname;
@@ -162,10 +161,10 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
     protected final synchronized void replace(List<EP> list) {
         epList = list;
     }
-    
+
     /**
      * Call _refresh as needed during calls, but actual refresh will not occur if there
-     * are existing entities or if it has been called in the last 10 (settable) seconds.  
+     * are existing entities or if it has been called in the last 10 (settable) seconds.
      * Timed Refreshes happen by Scheduled Thread
      */
     private final boolean _refresh() {
@@ -221,7 +220,7 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
     public void invalidate(Item item) throws LocatorException {
         if (item!=null) {
             if (item instanceof AAFLItem) {
-                AAFLItem ali =(AAFLItem)item; 
+                AAFLItem ali =(AAFLItem)item;
                 EP ep = ali.ep;
                 synchronized(epList) {
                     epList.remove(ep);
@@ -237,7 +236,10 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
     @Override
     public Item best() throws LocatorException {
         if (!hasItems()) {
-            throw new LocatorException("No Entries found for '" + aaf_locator_uri.toString() + '/' + name + ':' + version + '\'');
+            throw new LocatorException(String.format("No Entries found for '%s/%s:%s'",
+                    (aaf_locator_uri==null?(aaf_locator_host+"/locate"):aaf_locator_uri.toString()),
+                    name,
+                    version));
         }
         List<EP> lep = new ArrayList<>();
         EP first = null;
@@ -272,7 +274,7 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
                 } else {
                     return new AAFLItem(iter,lep.get(i));
                 }
-            
+
         }
     }
 
@@ -289,7 +291,7 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
         private final Object[] epa;
         private final List<EP> epList;
         private int idx;
-        
+
         public EPIterator(Object[] epa, List<EP> epList) {
             this.epa = epa;
             this.epList = epList;
@@ -330,7 +332,7 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
             }
         }
     }
-    
+
     @Override
     public Item first()  {
         Iterator<EP> iter = getIterator();
@@ -373,13 +375,13 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
             private Iterator<EP> iter;
             private URI uri;
             private EP ep;
-    
+
             public AAFLItem(Iterator<EP> iter, EP ep) {
                 this.iter = iter;
                 this.ep = ep;
                 uri = ep.uri;
             }
-            
+
             private static EP next(Iterator<EP> iter) {
                 EP ep=null;
                 while (iter.hasNext() && (ep==null || !ep.valid)) {
@@ -387,7 +389,7 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
                 }
                 return ep;
             }
-            
+
             public String toString() {
                 return ep==null?"Locator Item Invalid":ep.toString();
             }
@@ -397,7 +399,7 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
         private URI uri;
         private final double distance;
         private boolean valid;
-        
+
         public EP(final Endpoint ep, double latitude, double longitude) throws URISyntaxException {
             uri = new URI(ep.getProtocol(),null,ep.getHostname(),ep.getPort(),null,null,null);
             distance = GreatCircle.calc(latitude, longitude, ep.getLatitude(), ep.getLongitude());
@@ -418,13 +420,13 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
                 return 0;
             }
         }
-        
+
         @Override
         public String toString() {
             return distance + ": " + uri + (valid?" valid":" invalidate");
         }
     }
-    
+
     /* (non-Javadoc)
      * @see org.onap.aaf.cadi.Locator#destroy()
      */
@@ -432,7 +434,7 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
     public void destroy() {
         // Nothing to do
     }
-    
+
     @Override
     public String toString() {
         return "AAFLocator for " + name + " on " + getURI();
@@ -471,8 +473,8 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
     }
 
     protected void clear() {
-       epList.clear();
-       earliest=0L;
+        epList.clear();
+        earliest=0L;
     }