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 */
31 public CommonLocator() {
33 Paths.get(".").toAbsolutePath().normalize().toUri());
36 public CommonLocator(String... theSearchPaths) {
37 for (String path: theSearchPaths) {
42 public boolean addSearchPath(URI theURI) {
44 if (!theURI.isAbsolute()) {
45 errLogger.log(LogLevel.WARN, this.getClass().getName(), "Search paths must be absolute uris: {}", theURI);
49 return searchPaths.add(theURI);
52 public boolean addSearchPath(String thePath) {
55 suri = new URI(thePath);
57 catch(URISyntaxException urisx) {
58 errLogger.log(LogLevel.WARN, this.getClass().getName(), "Invalid search path: {} {}", thePath, urisx);
62 return addSearchPath(suri);
65 public Iterable<URI> searchPaths() {
66 return Iterables.unmodifiableIterable(this.searchPaths);
70 * Takes the given path and first URI resolves it and then attempts to open
71 * it (a way of verifying its existence) against each search path and stops
72 * at the first succesful test.
74 public Target resolve(String theName) {
76 InputStream pis = null;
79 URL purl = getClass().getClassLoader().getResource(theName);
82 return new Target(theName, purl.toURI());
84 catch (URISyntaxException urisx) {
85 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "The file {} wasn't found {}", theName, urisx);
91 puri = new URI(theName);
92 if (puri.isAbsolute()) {
94 pis = puri.toURL().openStream();
96 catch (IOException iox) {
97 errLogger.log(LogLevel.WARN, this.getClass().getName(), "The path {} is an absolute uri but it cannot be opened {}", theName, iox);
102 catch(URISyntaxException urisx) {
103 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "TargetResolver failed attempting {} {}", puri, urisx);
104 //keep it silent but what are the chances ..
107 //try relative to the search paths
108 for (URI suri: searchPaths) {
110 puri = suri.resolve(theName);
111 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "TargetResolver trying {}", puri);
112 pis = puri.toURL().openStream();
113 return new Target(theName, puri.normalize());
115 catch (Exception x) {
116 debugLogger.log(LogLevel.ERROR, this.getClass().getName(), "TargetResolver failed attempting {} {}", puri, x);
124 catch (IOException iox) {
133 public String toString() {
134 return "CommonLocator(" + this.searchPaths + ")";
138 public static void main(String[] theArgs) {
139 TargetLocator tl = new CommonLocator();
140 tl.addSearchPath(java.nio.file.Paths.get("").toUri());
141 tl.addSearchPath("file:///");
142 debugLogger.log(LogLevel.DEBUG, CommonLocator.class.getName(), tl.resolve(theArgs[0]).toString());