summaryrefslogtreecommitdiffstats
path: root/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/NonConfigResource.java
blob: 065e7f42b4b59b0a215c5099940fe7774dd2737b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.config;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;

public class NonConfigResource {

    static final String NODE_CONFIG_LOCATION = "node.config.location";
    static final String CONFIG_LOCATION = "config.location";

    private final List<Function<String, Path>> lookupFunctions =
            Arrays.asList(this::getFromFile, this::findInFiles, this::getForNode, this::getGlobal, this::findInUris);

    private final Set<URI> uris = Collections.synchronizedSet(new HashSet<>());
    private final Set<File> files = Collections.synchronizedSet(new HashSet<>());

    private final Function<String, String> propertyGetter;

    NonConfigResource(Function<String, String> propertyGetter) {
        this.propertyGetter = propertyGetter;
    }

    public NonConfigResource() {
        this(System::getProperty);
    }

    public void add(URL url) {
        uris.add(toUri(url));
    }

    public void add(File file) {
        files.add(file);
    }

    public Path locate(String resource) {

        if (resource == null) {
            return null;
        }

        try {

            return lookupFunctions.stream()
                           .map(f -> f.apply(resource))
                           .filter(Objects::nonNull)
                           .findFirst().orElse(null);

        } catch (Exception exception) {
            exception.printStackTrace();
            return null;
        }
    }

    private Path locate(File root, String resource) {

        if (!root.exists()) {
            return null;
        }

        return ConfigurationUtils.getAllFiles(root, true, false)
                       .stream()
                       .filter(f -> !ConfigurationUtils.isConfig(f))
                       .peek(this::add).filter(f -> f.getAbsolutePath().endsWith(resource))
                       .findFirst()
                       .map(file -> Paths.get(file.getAbsolutePath())).orElse(null);
    }

    private Path getFromFile(String resource) {
        return new File(resource).exists() ? Paths.get(resource) : null;
    }

    private Path findInUris(String resource) {
        for (URI uri : uris) {
            if (toUrl(uri).getFile().endsWith(resource)) {
                return Paths.get(uri);
            }
        }
        return null;
    }

    private Path findInFiles(String resource) {

        for (File availableFile : files) {

            String absolutePath = availableFile.getAbsolutePath();
            if (absolutePath.endsWith(resource) && availableFile.exists()) {
                return Paths.get(absolutePath);
            }
        }

        return null;
    }

    private Path getForNode(String resource) {
        return getFromProperty(NODE_CONFIG_LOCATION, resource);
    }

    private Path getGlobal(String resource) {
        return getFromProperty(CONFIG_LOCATION, resource);
    }

    private Path getFromProperty(String property, String resource) {
        String value = propertyGetter.apply(property);
        return (value == null) ? null : locate(new File(value), resource);
    }

    private static URI toUri(URL url) {

        try {
            return url.toURI();
        } catch (URISyntaxException e) {
            throw new IllegalStateException("Unexpected URL syntax: " + url, e);
        }
    }

    private static URL toUrl(URI uri) {
        try {
            return uri.toURL();
        } catch (MalformedURLException e) {
            throw new IllegalStateException("Unexpected URI syntax: " + uri, e);
        }
    }
}