Code formatting of configuration framework
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-core / src / main / java / org / onap / config / NonConfigResource.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.config;
18
19 import java.io.File;
20 import java.net.URL;
21 import java.nio.file.Path;
22 import java.nio.file.Paths;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.Set;
26 import java.util.function.Predicate;
27
28 public class NonConfigResource {
29
30     private static final Set<URL> urls = new HashSet<>();
31     private static final Set<File> files = new HashSet<>();
32
33     public static void add(URL url) {
34         urls.add(url);
35     }
36
37     public static Path locate(String resource) {
38         try {
39             if (resource != null) {
40                 File file = new File(resource);
41                 if (file.exists()) {
42                     return Paths.get(resource);
43                 }
44                 for (File availableFile : files) {
45                     if (availableFile.getAbsolutePath().endsWith(resource) && availableFile.exists()) {
46                         return Paths.get(availableFile.getAbsolutePath());
47                     }
48                 }
49                 if (System.getProperty("node.config.location") != null) {
50                     Path path = locate(new File(System.getProperty("node.config.location")), resource);
51                     if (path != null) {
52                         return path;
53                     }
54                 }
55                 if (System.getProperty("config.location") != null) {
56                     Path path = locate(new File(System.getProperty("config.location")), resource);
57                     if (path != null) {
58                         return path;
59                     }
60                 }
61                 for (URL url : urls) {
62                     if (url.getFile().endsWith(resource)) {
63                         return Paths.get(url.toURI());
64                     }
65                 }
66             }
67         } catch (Exception exception) {
68             exception.printStackTrace();
69         }
70         return null;
71     }
72
73     private static Path locate(File root, String resource) {
74         if (root.exists()) {
75             Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false);
76             Predicate<File> f1 = ConfigurationUtils::isConfig;
77             for (File file : filesystemResources) {
78                 if (!f1.test(file)) {
79                     add(file);
80                     if (file.getAbsolutePath().endsWith(resource)) {
81                         return Paths.get(file.getAbsolutePath());
82                     }
83                 }
84             }
85         }
86         return null;
87     }
88
89     public static void add(File file) {
90         files.add(file);
91     }
92 }