Fixing sonar issues 02/98602/3
authorDmitry Puzikov <d.puzikov2@partner.samsung.com>
Tue, 19 Nov 2019 14:13:23 +0000 (15:13 +0100)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Thu, 21 Nov 2019 15:36:52 +0000 (15:36 +0000)
Replaced URL with URI to avoid
trigger DNS.

Change-Id: If85f1f2780c46e0966f3707af51571f4a7aa2f15
Issue-ID: SDC-2660
Signed-off-by: Dmitry Puzikov <d.puzikov2@partner.samsung.com>
common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java

index 459c8b4..065e7f4 100644 (file)
@@ -17,6 +17,7 @@
 package org.onap.config;
 
 import java.io.File;
+import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -36,9 +37,9 @@ public class NonConfigResource {
     static final String CONFIG_LOCATION = "config.location";
 
     private final List<Function<String, Path>> lookupFunctions =
-            Arrays.asList(this::getFromFile, this::findInFiles, this::getForNode, this::getGlobal, this::findInUrls);
+            Arrays.asList(this::getFromFile, this::findInFiles, this::getForNode, this::getGlobal, this::findInUris);
 
-    private final Set<URL> urls = Collections.synchronizedSet(new HashSet<>());
+    private final Set<URI> uris = Collections.synchronizedSet(new HashSet<>());
     private final Set<File> files = Collections.synchronizedSet(new HashSet<>());
 
     private final Function<String, String> propertyGetter;
@@ -52,7 +53,7 @@ public class NonConfigResource {
     }
 
     public void add(URL url) {
-        urls.add(url);
+        uris.add(toUri(url));
     }
 
     public void add(File file) {
@@ -96,15 +97,12 @@ public class NonConfigResource {
         return new File(resource).exists() ? Paths.get(resource) : null;
     }
 
-    private Path findInUrls(String resource) {
-
-        for (URL url : urls) {
-
-            if (url.getFile().endsWith(resource)) {
-                return Paths.get(toUri(url));
+    private Path findInUris(String resource) {
+        for (URI uri : uris) {
+            if (toUrl(uri).getFile().endsWith(resource)) {
+                return Paths.get(uri);
             }
         }
-
         return null;
     }
 
@@ -139,7 +137,15 @@ public class NonConfigResource {
         try {
             return url.toURI();
         } catch (URISyntaxException e) {
-            throw new IllegalStateException("Unexpected URL syntax: " + url.toString(), e);
+            throw new IllegalStateException("Unexpected URL syntax: " + url, e);
+        }
+    }
+
+    private static URL toUrl(URI uri) {
+        try {
+            return uri.toURL();
+        } catch (MalformedURLException e) {
+            throw new IllegalStateException("Unexpected URI syntax: " + uri, e);
         }
     }
 }