ActivitySpec - Correcting logger messages
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-core / src / main / java / org / onap / config / NonConfigResource.java
1 package org.onap.config;
2
3 import java.io.File;
4 import java.net.URL;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
7 import java.util.Collection;
8 import java.util.HashSet;
9 import java.util.Set;
10 import java.util.function.Predicate;
11
12 /**
13  * The type Non config resource.
14  */
15 public class NonConfigResource {
16
17   private static Set<URL> urls = new HashSet<>();
18   private static Set<File> files = new HashSet<>();
19
20   /**
21    * Add.
22    *
23    * @param url the url
24    */
25   public static void add(URL url) {
26     urls.add(url);
27   }
28
29   /**
30    * Add.
31    *
32    * @param file the file
33    */
34   public static void add(File file) {
35     files.add(file);
36   }
37
38   /**
39    * Locate path.
40    *
41    * @param resource the resource
42    * @return the path
43    */
44   public static Path locate(String resource) {
45     try {
46       if (resource != null) {
47         File file = new File(resource);
48         if (file.exists()) {
49           return Paths.get(resource);
50         }
51         for (File availableFile : files) {
52           if (availableFile.getAbsolutePath().endsWith(resource) && availableFile.exists()) {
53             return Paths.get(availableFile.getAbsolutePath());
54           }
55         }
56         if (System.getProperty("node.config.location") != null) {
57           Path path = locate(new File(System.getProperty("node.config.location")), resource);
58           if (path != null) {
59             return path;
60           }
61         }
62         if (System.getProperty("config.location") != null) {
63           Path path = locate(new File(System.getProperty("config.location")), resource);
64           if (path != null) {
65             return path;
66           }
67         }
68         for (URL url : urls) {
69           if (url.getFile().endsWith(resource)) {
70             return Paths.get(url.toURI());
71           }
72         }
73       }
74     } catch (Exception exception) {
75       exception.printStackTrace();
76     }
77     return null;
78   }
79
80   private static Path locate(File root, String resource) {
81     if (root.exists()) {
82       Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false);
83       Predicate<File> f1 = ConfigurationUtils::isConfig;
84       for (File file : filesystemResources) {
85         if (!f1.test(file)) {
86           add(file);
87           if (file.getAbsolutePath().endsWith(resource)) {
88             return Paths.get(file.getAbsolutePath());
89           }
90         }
91       }
92     }
93     return null;
94   }
95 }