2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.config;
21 import java.net.URISyntaxException;
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;
31 import java.util.function.Function;
33 public class NonConfigResource {
35 static final String NODE_CONFIG_LOCATION = "node.config.location";
36 static final String CONFIG_LOCATION = "config.location";
38 private final List<Function<String, Path>> lookupFunctions =
39 Arrays.asList(this::getFromFile, this::findInFiles, this::getForNode, this::getGlobal, this::findInUrls);
41 private final Set<URL> urls = Collections.synchronizedSet(new HashSet<>());
42 private final Set<File> files = Collections.synchronizedSet(new HashSet<>());
44 private final Function<String, String> propertyGetter;
46 NonConfigResource(Function<String, String> propertyGetter) {
47 this.propertyGetter = propertyGetter;
50 public NonConfigResource() {
51 this(System::getProperty);
54 public void add(URL url) {
58 public void add(File file) {
62 public Path locate(String resource) {
64 if (resource == null) {
70 return lookupFunctions.stream()
71 .map(f -> f.apply(resource))
72 .filter(Objects::nonNull)
73 .findFirst().orElse(null);
75 } catch (Exception exception) {
76 exception.printStackTrace();
81 private Path locate(File root, String resource) {
87 return ConfigurationUtils.getAllFiles(root, true, false)
89 .filter(f -> !ConfigurationUtils.isConfig(f))
90 .peek(this::add).filter(f -> f.getAbsolutePath().endsWith(resource))
92 .map(file -> Paths.get(file.getAbsolutePath())).orElse(null);
95 private Path getFromFile(String resource) {
96 return new File(resource).exists() ? Paths.get(resource) : null;
99 private Path findInUrls(String resource) {
101 for (URL url : urls) {
103 if (url.getFile().endsWith(resource)) {
104 return Paths.get(toUri(url));
111 private Path findInFiles(String resource) {
113 for (File availableFile : files) {
115 String absolutePath = availableFile.getAbsolutePath();
116 if (absolutePath.endsWith(resource) && availableFile.exists()) {
117 return Paths.get(absolutePath);
124 private Path getForNode(String resource) {
125 return getFromProperty(NODE_CONFIG_LOCATION, resource);
128 private Path getGlobal(String resource) {
129 return getFromProperty(CONFIG_LOCATION, resource);
132 private Path getFromProperty(String property, String resource) {
133 String value = propertyGetter.apply(property);
134 return (value == null) ? null : locate(new File(value), resource);
137 private static URI toUri(URL url) {
141 } catch (URISyntaxException e) {
142 throw new IllegalStateException("Unexpected URL syntax: " + url.toString(), e);