diff options
author | vempo <vitaliy.emporopulo@amdocs.com> | 2018-10-31 18:39:44 +0200 |
---|---|---|
committer | Oren Kleks <orenkle@amdocs.com> | 2018-11-01 11:39:00 +0000 |
commit | 1777d1336dc7eaf3aa6c8e89fb5462c7700128d2 (patch) | |
tree | 0bb41862e2da36eb299944280aa33d17fd241848 /common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java | |
parent | 789391414249ef81d869a20dc250ae1d70d5cc3e (diff) |
Fixed Sonar violations in configuration
Simplified code, added unit tests.
Change-Id: I0a35daf8a2d4cfd0979e30363c9fad30e7c0570b
Issue-ID: SDC-1867
Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java')
-rw-r--r-- | common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java | 146 |
1 files changed, 99 insertions, 47 deletions
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java index 752f202d3a..459c8b476b 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java @@ -17,77 +17,129 @@ package org.onap.config; import java.io.File; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Collection; +import java.util.Arrays; import java.util.Collections; import java.util.HashSet; +import java.util.List; +import java.util.Objects; import java.util.Set; -import java.util.function.Predicate; +import java.util.function.Function; public class NonConfigResource { - private static final Set<URL> urls = Collections.synchronizedSet(new HashSet<>()); - private static final Set<File> files = Collections.synchronizedSet(new HashSet<>()); + static final String NODE_CONFIG_LOCATION = "node.config.location"; + static final String CONFIG_LOCATION = "config.location"; - public static void add(URL url) { + private final List<Function<String, Path>> lookupFunctions = + Arrays.asList(this::getFromFile, this::findInFiles, this::getForNode, this::getGlobal, this::findInUrls); + + private final Set<URL> urls = Collections.synchronizedSet(new HashSet<>()); + private final Set<File> files = Collections.synchronizedSet(new HashSet<>()); + + private final Function<String, String> propertyGetter; + + NonConfigResource(Function<String, String> propertyGetter) { + this.propertyGetter = propertyGetter; + } + + public NonConfigResource() { + this(System::getProperty); + } + + public void add(URL url) { urls.add(url); } - public static Path locate(String resource) { + public void add(File file) { + files.add(file); + } + + public Path locate(String resource) { + + if (resource == null) { + return null; + } + try { - if (resource != null) { - File file = new File(resource); - if (file.exists()) { - return Paths.get(resource); - } - for (File availableFile : files) { - if (availableFile.getAbsolutePath().endsWith(resource) && availableFile.exists()) { - return Paths.get(availableFile.getAbsolutePath()); - } - } - if (System.getProperty("node.config.location") != null) { - Path path = locate(new File(System.getProperty("node.config.location")), resource); - if (path != null) { - return path; - } - } - if (System.getProperty("config.location") != null) { - Path path = locate(new File(System.getProperty("config.location")), resource); - if (path != null) { - return path; - } - } - for (URL url : urls) { - if (url.getFile().endsWith(resource)) { - return Paths.get(url.toURI()); - } - } - } + + return lookupFunctions.stream() + .map(f -> f.apply(resource)) + .filter(Objects::nonNull) + .findFirst().orElse(null); + } catch (Exception exception) { exception.printStackTrace(); + return null; } + } + + private Path locate(File root, String resource) { + + if (!root.exists()) { + return null; + } + + return ConfigurationUtils.getAllFiles(root, true, false) + .stream() + .filter(f -> !ConfigurationUtils.isConfig(f)) + .peek(this::add).filter(f -> f.getAbsolutePath().endsWith(resource)) + .findFirst() + .map(file -> Paths.get(file.getAbsolutePath())).orElse(null); + } + + private Path getFromFile(String resource) { + 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)); + } + } + return null; } - private static Path locate(File root, String resource) { - if (root.exists()) { - Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false); - Predicate<File> f1 = ConfigurationUtils::isConfig; - for (File file : filesystemResources) { - if (!f1.test(file)) { - add(file); - if (file.getAbsolutePath().endsWith(resource)) { - return Paths.get(file.getAbsolutePath()); - } - } + private Path findInFiles(String resource) { + + for (File availableFile : files) { + + String absolutePath = availableFile.getAbsolutePath(); + if (absolutePath.endsWith(resource) && availableFile.exists()) { + return Paths.get(absolutePath); } } + return null; } - public static void add(File file) { - files.add(file); + private Path getForNode(String resource) { + return getFromProperty(NODE_CONFIG_LOCATION, resource); + } + + private Path getGlobal(String resource) { + return getFromProperty(CONFIG_LOCATION, resource); + } + + private Path getFromProperty(String property, String resource) { + String value = propertyGetter.apply(property); + return (value == null) ? null : locate(new File(value), resource); + } + + private static URI toUri(URL url) { + + try { + return url.toURI(); + } catch (URISyntaxException e) { + throw new IllegalStateException("Unexpected URL syntax: " + url.toString(), e); + } } } |