459c8b476b82164a0356d311015667eeea1677cb
[sdc.git] /
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.URI;
21 import java.net.URISyntaxException;
22 import java.net.URL;
23 import java.nio.file.Path;
24 import java.nio.file.Paths;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Objects;
30 import java.util.Set;
31 import java.util.function.Function;
32
33 public class NonConfigResource {
34
35     static final String NODE_CONFIG_LOCATION = "node.config.location";
36     static final String CONFIG_LOCATION = "config.location";
37
38     private final List<Function<String, Path>> lookupFunctions =
39             Arrays.asList(this::getFromFile, this::findInFiles, this::getForNode, this::getGlobal, this::findInUrls);
40
41     private final Set<URL> urls = Collections.synchronizedSet(new HashSet<>());
42     private final Set<File> files = Collections.synchronizedSet(new HashSet<>());
43
44     private final Function<String, String> propertyGetter;
45
46     NonConfigResource(Function<String, String> propertyGetter) {
47         this.propertyGetter = propertyGetter;
48     }
49
50     public NonConfigResource() {
51         this(System::getProperty);
52     }
53
54     public void add(URL url) {
55         urls.add(url);
56     }
57
58     public void add(File file) {
59         files.add(file);
60     }
61
62     public Path locate(String resource) {
63
64         if (resource == null) {
65             return null;
66         }
67
68         try {
69
70             return lookupFunctions.stream()
71                            .map(f -> f.apply(resource))
72                            .filter(Objects::nonNull)
73                            .findFirst().orElse(null);
74
75         } catch (Exception exception) {
76             exception.printStackTrace();
77             return null;
78         }
79     }
80
81     private Path locate(File root, String resource) {
82
83         if (!root.exists()) {
84             return null;
85         }
86
87         return ConfigurationUtils.getAllFiles(root, true, false)
88                        .stream()
89                        .filter(f -> !ConfigurationUtils.isConfig(f))
90                        .peek(this::add).filter(f -> f.getAbsolutePath().endsWith(resource))
91                        .findFirst()
92                        .map(file -> Paths.get(file.getAbsolutePath())).orElse(null);
93     }
94
95     private Path getFromFile(String resource) {
96         return new File(resource).exists() ? Paths.get(resource) : null;
97     }
98
99     private Path findInUrls(String resource) {
100
101         for (URL url : urls) {
102
103             if (url.getFile().endsWith(resource)) {
104                 return Paths.get(toUri(url));
105             }
106         }
107
108         return null;
109     }
110
111     private Path findInFiles(String resource) {
112
113         for (File availableFile : files) {
114
115             String absolutePath = availableFile.getAbsolutePath();
116             if (absolutePath.endsWith(resource) && availableFile.exists()) {
117                 return Paths.get(absolutePath);
118             }
119         }
120
121         return null;
122     }
123
124     private Path getForNode(String resource) {
125         return getFromProperty(NODE_CONFIG_LOCATION, resource);
126     }
127
128     private Path getGlobal(String resource) {
129         return getFromProperty(CONFIG_LOCATION, resource);
130     }
131
132     private Path getFromProperty(String property, String resource) {
133         String value = propertyGetter.apply(property);
134         return (value == null) ? null : locate(new File(value), resource);
135     }
136
137     private static URI toUri(URL url) {
138
139         try {
140             return url.toURI();
141         } catch (URISyntaxException e) {
142             throw new IllegalStateException("Unexpected URL syntax: " + url.toString(), e);
143         }
144     }
145 }