summaryrefslogtreecommitdiffstats
path: root/dcaedt_validator/checker/src/main/java/org/onap/sdc/dcae/checker/CommonLocator.java
blob: acc0a4a49e74fffc6f0cd2360d2c56fb10680519 (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
package org.onap.sdc.dcae.checker;

import java.io.InputStream;
import java.io.IOException;

import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;

import java.nio.file.Paths;

import java.util.Set;
import java.util.LinkedHashSet;

import com.google.common.collect.Iterables;
import org.onap.sdc.common.onaplog.OnapLoggerDebug;
import org.onap.sdc.common.onaplog.OnapLoggerError;
import org.onap.sdc.common.onaplog.Enums.LogLevel;


public class CommonLocator implements TargetLocator {

    private static OnapLoggerError errLogger = OnapLoggerError.getInstance();
    private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();

    private Set<URI> searchPaths = new LinkedHashSet<>();

    /* will create a locator with 2 default search paths: the file directory
     * from where the app was and the jar from which this checker (actually this
     * class) was loaded */
    CommonLocator() {
        addSearchPath(
            Paths.get(".").toAbsolutePath().normalize().toUri());
    }

    public boolean addSearchPath(URI theURI) {

        if (!theURI.isAbsolute()) {
            errLogger.log(LogLevel.WARN, this.getClass().getName(), "Search paths must be absolute uris: {}", theURI);
            return false;
        }

        return searchPaths.add(theURI);
    }

    public boolean addSearchPath(String thePath) {
        URI suri;
        try {
            suri = new URI(thePath);
        }
        catch(URISyntaxException urisx) {
            errLogger.log(LogLevel.WARN, this.getClass().getName(), "Invalid search path: {} {}", thePath, urisx);
            return false;
        }

        return addSearchPath(suri);
    }

    public Iterable<URI> searchPaths() {
        return Iterables.unmodifiableIterable(this.searchPaths);
    }

    /**
     * Takes the given path and first URI resolves it and then attempts to open
     * it (a way of verifying its existence) against each search path and stops
     * at the first succesful test.
     */
    public Target resolve(String theName) {
        URI puri = null;
        InputStream	pis = null;

        //try classpath
        URL purl = getClass().getClassLoader().getResource(theName);
        if (purl != null) {
            try {
                return new Target(theName, purl.toURI());
            }
            catch (URISyntaxException urisx) {
                errLogger.log(LogLevel.ERROR, this.getClass().getName(), "The file {} wasn't found {}", theName, urisx);
            }
        }

        //try absolute
        try {
            puri = new URI(theName);
            if (puri.isAbsolute()) {
                pis = getPathInputStream(puri,theName);
                if (pis == null){
                    return null;
                }
            }
        }
        catch(URISyntaxException urisx) {
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "TargetResolver failed attempting {} {}", theName, urisx);
            //keep it silent but what are the chances ..
        }

        //try relative to the search paths
        for (URI suri: searchPaths) {
            try {
                puri = suri.resolve(theName);
                debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "TargetResolver trying {}", puri);
                pis = puri.toURL().openStream();
                return new Target(theName, puri.normalize());
            }
            catch (Exception x) {
                debugLogger.log(LogLevel.ERROR, this.getClass().getName(), "TargetResolver failed attempting {} {}", puri, x);
            }
            finally {
                if (pis!= null) {
                    try {
                        pis.close();
                    }
                    catch (IOException iox) {
                        debugLogger.log(LogLevel.ERROR, this.getClass().getName(),"Error closing input stream {}", iox);
                    }
                }
            }
        }

        return null;
    }

    private InputStream getPathInputStream(URI puri, String theName){
        InputStream res = null;
        try (InputStream pis = puri.toURL().openStream()){
            res = pis;
        }
        catch (IOException iox) {
            errLogger.log(LogLevel.WARN, this.getClass().getName(), "The path {} is an absolute uri but it cannot be opened {}", theName, iox);
        }
        return res;
    }


    public String toString() {
        return "CommonLocator(" + this.searchPaths + ")";
    }


    public static void main(String[] theArgs) {
        TargetLocator tl = new CommonLocator();
        tl.addSearchPath(java.nio.file.Paths.get("").toUri());
        tl.addSearchPath("file:///");
        debugLogger.log(LogLevel.DEBUG, CommonLocator.class.getName(), tl.resolve(theArgs[0]).toString());
    }
}