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