diff options
author | 2018-07-09 13:42:31 +0200 | |
---|---|---|
committer | 2018-08-06 10:09:52 +0000 | |
commit | baf7f0a965d0ffebd5308d44758bfa9ba96c0c76 (patch) | |
tree | bc07c24e50a53a744484b15592fafda013159686 /common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config | |
parent | c8661bd2bab88a77a29167c782a76cdf729648ed (diff) |
Fixes of sonar violations sdc and refactor
Issue-ID: SDC-1484
Change-Id: I3cf17454c533d3419c97af63cc6b5412976726fb
Signed-off-by: Sandra Koblosz <sandra.koblosz@nokia.com>
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config')
3 files changed, 90 insertions, 79 deletions
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java index db64f7f7fc..9782fc9969 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java @@ -670,7 +670,7 @@ public class ConfigurationUtils { Configuration dbConfig = ConfigurationRepository.lookup() .getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE); BasicConfigurationBuilder<AgglomerateConfiguration> builder = - new BasicConfigurationBuilder<AgglomerateConfiguration>(AgglomerateConfiguration.class); + new BasicConfigurationBuilder<>(AgglomerateConfiguration.class); builder.configure( new Parameters().database() .setDataSource(ConfigurationDataSource.lookup()) 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 830cdfef5a..a63b455a16 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 @@ -1,95 +1,104 @@ package org.onap.config; +import io.vavr.Function1; +import io.vavr.collection.HashSet; +import io.vavr.collection.Set; +import io.vavr.control.Option; + import java.io.File; +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.HashSet; -import java.util.Set; -import java.util.function.Predicate; + +import static io.vavr.API.Option; /** * The type Non config resource. */ public class NonConfigResource { - private static Set<URL> urls = new HashSet<>(); - private static Set<File> files = new HashSet<>(); + private Function1<String, String> propertyGetter; + private Set<URL> urls; + private Set<Path> files; - /** - * Add. - * - * @param url the url - */ - public static void add(URL url) { - urls.add(url); - } + private NonConfigResource(Function1<String, String> propertyGetter) { + this.propertyGetter = propertyGetter; + this.files = HashSet.empty(); + this.urls = HashSet.empty(); + } - /** - * Add. - * - * @param file the file - */ - public static void add(File file) { - files.add(file); - } + public static NonConfigResource create(Function1<String, String> propertyGetter) { + return new NonConfigResource(propertyGetter); + } - /** - * Locate path. - * - * @param resource the resource - * @return the path - */ - public static Path locate(String resource) { - 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()); - } - } - } - } catch (Exception exception) { - exception.printStackTrace(); + /** + * Add. + * + * @param url the url + */ + public void add(URL url) { + urls = urls.add(url); + } + + /** + * Add. + * + * @param file the file + */ + public void add(File file) { + files = files.add(file.toPath()); } - 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()); - } + /** + * Locate path. + * + * @param resource the resource + * @return the path + */ + public Path locate(String resource) { + Path toReturn = null; + try { + if (resource != null) { + toReturn = tryToLocateResource(resource).getOrNull(); + } + } catch (Exception exception) { + exception.printStackTrace(); } - } + return toReturn; + } + + private Option<Path> tryToLocateResource(String resource) throws URISyntaxException { + return new File(resource).exists() ? Option(Paths.get(resource)) : getPathForResourceAmongFiles(resource) + .orElse(getPathForResourceBasedOnProperty(resource, "node.config.location")) + .orElse(getPathForResourceBasedOnProperty(resource, "config.location")) + .orElse(getPathForResourceAmongUrls(resource)); + } + + private Option<Path> getPathForResourceBasedOnProperty(String resource, String configPropertyKey) { + return Option(propertyGetter.apply(configPropertyKey)) + .flatMap(el -> locate(new File(el), resource)); + } + + private Option<Path> getPathForResourceAmongFiles(String resource) { + return files.map(Path::toAbsolutePath) + .filter(path -> path.toFile().exists() && path.endsWith(resource)) + .headOption(); + } + + private Option<Path> getPathForResourceAmongUrls(String resource) throws URISyntaxException { + return urls.filter(url -> url.getFile().endsWith(resource)) + .headOption() + .flatMap(url -> Option(Paths.get(url.getPath()))); + } + + private Option<Path> locate(File root, String resource) { + return root.exists() ? Option.ofOptional(ConfigurationUtils.getAllFiles(root, true, false) + .stream() + .filter(file -> !ConfigurationUtils.isConfig(file)) + .peek(this::add) + .filter(file -> file.getAbsolutePath().endsWith(resource)) + .map(file -> Paths.get(file.getAbsolutePath())) + .findAny()) : Option.none(); } - return null; - } } diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java index 00725b1165..56390ea54a 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationImpl.java @@ -1,5 +1,6 @@ package org.onap.config.impl; + import org.apache.commons.configuration2.Configuration; import org.apache.commons.configuration2.DatabaseConfiguration; import org.onap.config.ConfigurationUtils; @@ -39,6 +40,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { ; }; private static boolean instantiated = false; + private static NonConfigResource nonConfigResource = NonConfigResource.create(propertyName -> System.getProperties().getProperty(propertyName)); /** * The Change notifier. */ @@ -66,7 +68,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } moduleConfig.addConfig(url); } else { - NonConfigResource.add(url); + nonConfigResource.add(url); } } String configLocation = System.getProperty("config.location"); @@ -84,7 +86,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { } moduleConfig.addConfig(file); } else { - NonConfigResource.add(file); + nonConfigResource.add(file); } } } @@ -453,7 +455,7 @@ public class ConfigurationImpl implements org.onap.config.api.Configuration { if (String.class.equals(clazz)) { if (obj.toString().startsWith("@") && ConfigurationUtils.isExternalLookup(processingHint)) { String contents = ConfigurationUtils - .getFileContents(NonConfigResource.locate(obj.toString().substring(1).trim())); + .getFileContents(nonConfigResource.locate(obj.toString().substring(1).trim())); if (contents == null) { contents = ConfigurationUtils.getFileContents(obj.toString().substring(1).trim()); } |