DCAE-D be initial commit
[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         public CommonLocator() {
32                 addSearchPath(
33                         Paths.get(".").toAbsolutePath().normalize().toUri());
34         }
35         
36         public CommonLocator(String... theSearchPaths) {
37                 for (String path: theSearchPaths) {
38                         addSearchPath(path);
39                 }
40         }
41
42         public boolean addSearchPath(URI theURI) {
43
44                 if (!theURI.isAbsolute()) {
45                         errLogger.log(LogLevel.WARN, this.getClass().getName(), "Search paths must be absolute uris: {}", theURI);
46                         return false;
47                 }
48
49                 return searchPaths.add(theURI);
50         }
51
52         public boolean addSearchPath(String thePath) {
53                 URI suri = null; 
54                 try {
55                         suri = new URI(thePath);
56                 }
57                 catch(URISyntaxException urisx) {
58                         errLogger.log(LogLevel.WARN, this.getClass().getName(), "Invalid search path: {} {}", thePath, urisx);
59                         return false;
60                 }
61
62                 return addSearchPath(suri);
63         }
64
65         public Iterable<URI> searchPaths() {
66                 return Iterables.unmodifiableIterable(this.searchPaths);
67         }
68
69         /**
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.
73          */
74         public Target resolve(String theName) {
75                 URI puri = null;
76                 InputStream     pis = null;
77                 
78                 //try classpath
79                 URL purl = getClass().getClassLoader().getResource(theName);
80                 if (purl != null) {
81                         try {
82                                 return new Target(theName, purl.toURI());
83                         }
84                         catch (URISyntaxException urisx) {
85                                 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "The file {} wasn't found {}", theName, urisx);
86                         }
87                 }
88
89                 //try absolute
90                 try {
91                         puri = new URI(theName);
92                         if (puri.isAbsolute()) {
93                                 try {
94                                         pis = puri.toURL().openStream();
95                                 }
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);
98                                         return null;
99                                 }
100                         }
101                 }
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 ..
105                 }
106
107                 //try relative to the search paths
108                 for (URI suri: searchPaths) {
109                         try {
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());
114                         }
115                         catch (Exception x) {
116                                 debugLogger.log(LogLevel.ERROR, this.getClass().getName(), "TargetResolver failed attempting {} {}", puri, x);
117                                 continue;
118                         }
119                         finally {
120                                 if (pis!= null) {
121                                         try {
122                                                 pis.close();
123                                         }
124                                         catch (IOException iox) {
125                                         }
126                                 }
127                         }
128                 }
129
130                 return null;
131         }
132
133         public String toString() {
134                 return "CommonLocator(" + this.searchPaths + ")";
135         }
136
137         
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());
143         }
144 }