1 package org.onap.config;
3 import io.vavr.Function1;
4 import io.vavr.collection.HashSet;
5 import io.vavr.collection.Set;
6 import io.vavr.control.Option;
9 import java.net.URISyntaxException;
11 import java.nio.file.Path;
12 import java.nio.file.Paths;
14 import static io.vavr.API.Option;
17 * The type Non config resource.
19 public class NonConfigResource {
21 private Function1<String, String> propertyGetter;
22 private Set<URL> urls;
23 private Set<Path> files;
25 private NonConfigResource(Function1<String, String> propertyGetter) {
26 this.propertyGetter = propertyGetter;
27 this.files = HashSet.empty();
28 this.urls = HashSet.empty();
31 public static NonConfigResource create(Function1<String, String> propertyGetter) {
32 return new NonConfigResource(propertyGetter);
40 public void add(URL url) {
47 * @param file the file
49 public void add(File file) {
50 files = files.add(file.toPath());
56 * @param resource the resource
59 public Path locate(String resource) {
62 if (resource != null) {
63 toReturn = tryToLocateResource(resource).getOrNull();
65 } catch (Exception exception) {
66 exception.printStackTrace();
71 private Option<Path> tryToLocateResource(String resource) throws URISyntaxException {
72 return new File(resource).exists() ? Option(Paths.get(resource)) : getPathForResourceAmongFiles(resource)
73 .orElse(getPathForResourceBasedOnProperty(resource, "node.config.location"))
74 .orElse(getPathForResourceBasedOnProperty(resource, "config.location"))
75 .orElse(getPathForResourceAmongUrls(resource));
78 private Option<Path> getPathForResourceBasedOnProperty(String resource, String configPropertyKey) {
79 return Option(propertyGetter.apply(configPropertyKey))
80 .flatMap(el -> locate(new File(el), resource));
83 private Option<Path> getPathForResourceAmongFiles(String resource) {
84 return files.map(Path::toAbsolutePath)
85 .filter(path -> path.toFile().exists() && path.endsWith(resource))
89 private Option<Path> getPathForResourceAmongUrls(String resource) throws URISyntaxException {
90 return urls.filter(url -> url.getFile().endsWith(resource))
92 .flatMap(url -> Option(Paths.get(url.getPath())));
95 private Option<Path> locate(File root, String resource) {
96 return root.exists() ? Option.ofOptional(ConfigurationUtils.getAllFiles(root, true, false)
98 .filter(file -> !ConfigurationUtils.isConfig(file))
100 .filter(file -> file.getAbsolutePath().endsWith(resource))
101 .map(file -> Paths.get(file.getAbsolutePath()))
102 .findAny()) : Option.none();