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;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 import java.net.MalformedURLException;
25 import java.net.URISyntaxException;
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;
35 import java.util.function.Function;
37 public class NonConfigResource {
38 private static final Logger LOGGER = LoggerFactory.getLogger(NonConfigResource.class);
40 static final String NODE_CONFIG_LOCATION = "node.config.location";
41 static final String CONFIG_LOCATION = "config.location";
43 private final List<Function<String, Path>> lookupFunctions =
44 Arrays.asList(this::getFromFile, this::findInFiles, this::getForNode, this::getGlobal, this::findInUris);
46 private final Set<URI> uris = Collections.synchronizedSet(new HashSet<>());
47 private final Set<File> files = Collections.synchronizedSet(new HashSet<>());
49 private final Function<String, String> propertyGetter;
51 NonConfigResource(Function<String, String> propertyGetter) {
52 this.propertyGetter = propertyGetter;
55 public NonConfigResource() {
56 this(System::getProperty);
59 public void add(URL url) {
63 public void add(File file) {
67 public Path locate(String resource) {
69 if (resource == null) {
75 return lookupFunctions.stream()
76 .map(f -> f.apply(resource))
77 .filter(Objects::nonNull)
78 .findFirst().orElse(null);
80 } catch (Exception exception) {
81 LOGGER.error("Failed to locate resource '{}'.", resource, exception);
86 private Path locate(File root, String resource) {
92 return ConfigurationUtils.getAllFiles(root, true, false)
94 .filter(f -> !ConfigurationUtils.isConfig(f))
95 .peek(this::add).filter(f -> f.getAbsolutePath().endsWith(resource))
97 .map(file -> Paths.get(file.getAbsolutePath())).orElse(null);
100 private Path getFromFile(String resource) {
101 return new File(resource).exists() ? Paths.get(resource) : null;
104 private Path findInUris(String resource) {
105 for (URI uri : uris) {
106 if (toUrl(uri).getFile().endsWith(resource)) {
107 return Paths.get(uri);
113 private Path findInFiles(String resource) {
115 for (File availableFile : files) {
117 String absolutePath = availableFile.getAbsolutePath();
118 if (absolutePath.endsWith(resource) && availableFile.exists()) {
119 return Paths.get(absolutePath);
126 private Path getForNode(String resource) {
127 return getFromProperty(NODE_CONFIG_LOCATION, resource);
130 private Path getGlobal(String resource) {
131 return getFromProperty(CONFIG_LOCATION, resource);
134 private Path getFromProperty(String property, String resource) {
135 String value = propertyGetter.apply(property);
136 return (value == null) ? null : locate(new File(value), resource);
139 private static URI toUri(URL url) {
143 } catch (URISyntaxException e) {
144 throw new IllegalStateException("Unexpected URL syntax: " + url, e);
148 private static URL toUrl(URI uri) {
151 } catch (MalformedURLException e) {
152 throw new IllegalStateException("Unexpected URI syntax: " + uri, e);