Handled not thread-safe fields in configuration
[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.Collections;
25 import java.util.HashSet;
26 import java.util.Set;
27 import java.util.function.Predicate;
28
29 public class NonConfigResource {
30
31     private static final Set<URL> urls = Collections.synchronizedSet(new HashSet<>());
32     private static final Set<File> files = Collections.synchronizedSet(new HashSet<>());
33
34     public static void add(URL url) {
35         urls.add(url);
36     }
37
38     public static Path locate(String resource) {
39         try {
40             if (resource != null) {
41                 File file = new File(resource);
42                 if (file.exists()) {
43                     return Paths.get(resource);
44                 }
45                 for (File availableFile : files) {
46                     if (availableFile.getAbsolutePath().endsWith(resource) && availableFile.exists()) {
47                         return Paths.get(availableFile.getAbsolutePath());
48                     }
49                 }
50                 if (System.getProperty("node.config.location") != null) {
51                     Path path = locate(new File(System.getProperty("node.config.location")), resource);
52                     if (path != null) {
53                         return path;
54                     }
55                 }
56                 if (System.getProperty("config.location") != null) {
57                     Path path = locate(new File(System.getProperty("config.location")), resource);
58                     if (path != null) {
59                         return path;
60                     }
61                 }
62                 for (URL url : urls) {
63                     if (url.getFile().endsWith(resource)) {
64                         return Paths.get(url.toURI());
65                     }
66                 }
67             }
68         } catch (Exception exception) {
69             exception.printStackTrace();
70         }
71         return null;
72     }
73
74     private static Path locate(File root, String resource) {
75         if (root.exists()) {
76             Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false);
77             Predicate<File> f1 = ConfigurationUtils::isConfig;
78             for (File file : filesystemResources) {
79                 if (!f1.test(file)) {
80                     add(file);
81                     if (file.getAbsolutePath().endsWith(resource)) {
82                         return Paths.get(file.getAbsolutePath());
83                     }
84                 }
85             }
86         }
87         return null;
88     }
89
90     public static void add(File file) {
91         files.add(file);
92     }
93 }