Upgrade dt-be-main
[sdc/dcae-d/dt-be-main.git] / dcaedt_validator / checker / src / main / java / org / onap / sdc / dcae / checker / CommonLocator.java
1 package org.onap.sdc.dcae.checker;
2
3 import java.io.InputStream;
4 import java.io.IOException;
5
6 import java.net.URL;
7 import java.net.URI;
8 import java.net.URISyntaxException;
9
10 import java.nio.file.Paths;
11
12 import java.util.Set;
13 import java.util.LinkedHashSet;
14
15 import com.google.common.collect.Iterables;
16 import org.onap.sdc.common.onaplog.OnapLoggerDebug;
17 import org.onap.sdc.common.onaplog.OnapLoggerError;
18 import org.onap.sdc.common.onaplog.Enums.LogLevel;
19
20
21 public class CommonLocator implements TargetLocator {
22
23     private static OnapLoggerError errLogger = OnapLoggerError.getInstance();
24     private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
25
26     private Set<URI> searchPaths = new LinkedHashSet<>();
27
28     /* will create a locator with 2 default search paths: the file directory
29      * from where the app was and the jar from which this checker (actually this
30      * class) was loaded */
31     CommonLocator() {
32         addSearchPath(
33             Paths.get(".").toAbsolutePath().normalize().toUri());
34     }
35
36     public boolean addSearchPath(URI theURI) {
37
38         if (!theURI.isAbsolute()) {
39             errLogger.log(LogLevel.WARN, this.getClass().getName(), "Search paths must be absolute uris: {}", theURI);
40             return false;
41         }
42
43         return searchPaths.add(theURI);
44     }
45
46     public boolean addSearchPath(String thePath) {
47         URI suri;
48         try {
49             suri = new URI(thePath);
50         }
51         catch(URISyntaxException urisx) {
52             errLogger.log(LogLevel.WARN, this.getClass().getName(), "Invalid search path: {} {}", thePath, urisx);
53             return false;
54         }
55
56         return addSearchPath(suri);
57     }
58
59     public Iterable<URI> searchPaths() {
60         return Iterables.unmodifiableIterable(this.searchPaths);
61     }
62
63     /**
64      * Takes the given path and first URI resolves it and then attempts to open
65      * it (a way of verifying its existence) against each search path and stops
66      * at the first succesful test.
67      */
68     public Target resolve(String theName) {
69         URI puri = null;
70         InputStream     pis = null;
71
72         //try classpath
73         URL purl = getClass().getClassLoader().getResource(theName);
74         if (purl != null) {
75             try {
76                 return new Target(theName, purl.toURI());
77             }
78             catch (URISyntaxException urisx) {
79                 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "The file {} wasn't found {}", theName, urisx);
80             }
81         }
82
83         //try absolute
84         try {
85             puri = new URI(theName);
86             if (puri.isAbsolute()) {
87                 pis = getPathInputStream(puri,theName);
88                 if (pis == null){
89                     return null;
90                 }
91             }
92         }
93         catch(URISyntaxException urisx) {
94             debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "TargetResolver failed attempting {} {}", theName, urisx);
95             //keep it silent but what are the chances ..
96         }
97
98         //try relative to the search paths
99         for (URI suri: searchPaths) {
100             try {
101                 puri = suri.resolve(theName);
102                 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "TargetResolver trying {}", puri);
103                 pis = puri.toURL().openStream();
104                 return new Target(theName, puri.normalize());
105             }
106             catch (Exception x) {
107                 debugLogger.log(LogLevel.ERROR, this.getClass().getName(), "TargetResolver failed attempting {} {}", puri, x);
108             }
109             finally {
110                 if (pis!= null) {
111                     try {
112                         pis.close();
113                     }
114                     catch (IOException iox) {
115                         debugLogger.log(LogLevel.ERROR, this.getClass().getName(),"Error closing input stream {}", iox);
116                     }
117                 }
118             }
119         }
120
121         return null;
122     }
123
124     private InputStream getPathInputStream(URI puri, String theName){
125         InputStream res = null;
126         try (InputStream pis = puri.toURL().openStream()){
127             res = pis;
128         }
129         catch (IOException iox) {
130             errLogger.log(LogLevel.WARN, this.getClass().getName(), "The path {} is an absolute uri but it cannot be opened {}", theName, iox);
131         }
132         return res;
133     }
134
135
136     public String toString() {
137         return "CommonLocator(" + this.searchPaths + ")";
138     }
139
140
141     public static void main(String[] theArgs) {
142         TargetLocator tl = new CommonLocator();
143         tl.addSearchPath(java.nio.file.Paths.get("").toUri());
144         tl.addSearchPath("file:///");
145         debugLogger.log(LogLevel.DEBUG, CommonLocator.class.getName(), tl.resolve(theArgs[0]).toString());
146     }
147 }