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