1 package org.onap.sdc.dcae.checker;
3 import java.io.InputStream;
4 import java.io.IOException;
8 import java.net.URISyntaxException;
10 import java.nio.file.Paths;
13 import java.util.LinkedHashSet;
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;
21 public class CommonLocator implements TargetLocator {
23 private static OnapLoggerError errLogger = OnapLoggerError.getInstance();
24 private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
26 private Set<URI> searchPaths = new LinkedHashSet<>();
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 */
33 Paths.get(".").toAbsolutePath().normalize().toUri());
36 public boolean addSearchPath(URI theURI) {
38 if (!theURI.isAbsolute()) {
39 errLogger.log(LogLevel.WARN, this.getClass().getName(), "Search paths must be absolute uris: {}", theURI);
43 return searchPaths.add(theURI);
46 public boolean addSearchPath(String thePath) {
49 suri = new URI(thePath);
51 catch(URISyntaxException urisx) {
52 errLogger.log(LogLevel.WARN, this.getClass().getName(), "Invalid search path: {} {}", thePath, urisx);
56 return addSearchPath(suri);
59 public Iterable<URI> searchPaths() {
60 return Iterables.unmodifiableIterable(this.searchPaths);
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.
68 public Target resolve(String theName) {
70 InputStream pis = null;
73 URL purl = getClass().getClassLoader().getResource(theName);
76 return new Target(theName, purl.toURI());
78 catch (URISyntaxException urisx) {
79 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "The file {} wasn't found {}", theName, urisx);
85 puri = new URI(theName);
86 if (puri.isAbsolute()) {
87 pis = getPathInputStream(puri,theName);
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 ..
98 //try relative to the search paths
99 for (URI suri: searchPaths) {
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());
106 catch (Exception x) {
107 debugLogger.log(LogLevel.ERROR, this.getClass().getName(), "TargetResolver failed attempting {} {}", puri, x);
114 catch (IOException iox) {
115 debugLogger.log(LogLevel.ERROR, this.getClass().getName(),"Error closing input stream {}", iox);
124 private InputStream getPathInputStream(URI puri, String theName){
125 InputStream res = null;
126 try (InputStream pis = puri.toURL().openStream()){
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);
136 public String toString() {
137 return "CommonLocator(" + this.searchPaths + ")";
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());