1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.wss4j.common.util;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.lang.reflect.InvocationTargetException;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29 import java.security.AccessController;
30 import java.security.PrivilegedAction;
31
32 import org.apache.wss4j.common.ext.WSSecurityException;
33
34
35
36
37
38 public final class Loader {
39 private static final org.slf4j.Logger LOG =
40 org.slf4j.LoggerFactory.getLogger(Loader.class);
41
42 private Loader() {
43
44 }
45
46 public static InputStream loadInputStream(ClassLoader loader, String resource)
47 throws WSSecurityException, IOException {
48 InputStream is = null;
49 if (resource != null) {
50 URL url = null;
51
52 try {
53 url = new URL(resource);
54 } catch (MalformedURLException ex) {
55
56 }
57
58 if (url == null) {
59 url = Loader.getResource(loader, resource);
60 }
61 if (url != null) {
62 is = url.openStream();
63 }
64
65
66
67
68 if (is == null) {
69 try {
70 is = Files.newInputStream(Paths.get(resource));
71 } catch (Exception e) {
72 LOG.debug(e.getMessage(), e);
73 throw new WSSecurityException(
74 WSSecurityException.ErrorCode.FAILURE, e, "resourceNotFound", new Object[] {resource}
75 );
76 }
77 }
78 }
79 return is;
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public static URL getResource(String resource) {
99 URL url = null;
100 try {
101 ClassLoader classLoader = getTCL();
102 if (classLoader != null) {
103 if (LOG.isDebugEnabled()) {
104 LOG.debug("Trying to find [" + resource + "] using " + classLoader + " class loader.");
105 }
106 url = classLoader.getResource(resource);
107 if (url == null && resource.startsWith("/")) {
108
109 url = classLoader.getResource(resource.substring(1));
110 }
111 if (url != null) {
112 return url;
113 }
114 }
115 } catch (Exception e) {
116 LOG.warn("Caught Exception while in Loader.getResource. This may be innocuous.", e);
117 }
118
119 ClassLoader cluClassloader = Loader.class.getClassLoader();
120 if (cluClassloader == null) {
121 cluClassloader = ClassLoader.getSystemClassLoader();
122 }
123 url = cluClassloader.getResource(resource);
124 if (url == null && resource.startsWith("/")) {
125
126 url = cluClassloader.getResource(resource.substring(1));
127 }
128 if (url != null) {
129 return url;
130 }
131
132
133
134
135
136 LOG.debug("Trying to find [{}] using ClassLoader.getSystemResource().", resource);
137 return ClassLoader.getSystemResource(resource);
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public static URL getResource(ClassLoader loader, String resource) {
159 URL url = null;
160 try {
161 if (loader != null) {
162 if (LOG.isDebugEnabled()) {
163 LOG.debug("Trying to find [" + resource + "] using " + loader + " class loader.");
164 }
165 url = loader.getResource(resource);
166 if (url == null && resource.startsWith("/")) {
167
168 url = loader.getResource(resource.substring(1));
169 }
170 if (url != null) {
171 return url;
172 }
173 }
174 } catch (Exception e) {
175 LOG.warn("Caught Exception while in Loader.getResource. This may be innocuous.", e);
176 }
177 return getResource(resource);
178 }
179
180
181
182
183
184
185
186 public static InputStream getResourceAsStream(String resourceName) {
187 URL url = getResource(resourceName);
188
189 try {
190 return (url != null) ? url.openStream() : null;
191 } catch (IOException e) {
192 LOG.debug(e.getMessage(), e);
193 return null;
194 }
195 }
196
197
198
199
200
201
202
203
204
205 public static ClassLoader getTCL() throws IllegalAccessException, InvocationTargetException {
206 return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
207 public ClassLoader run() {
208 return Thread.currentThread().getContextClassLoader();
209 }
210 });
211 }
212
213
214
215
216
217
218
219 public static ClassLoader getClassLoader(final Class<?> clazz) {
220 return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
221 public ClassLoader run() {
222 return clazz.getClassLoader();
223 }
224 });
225 }
226
227
228
229
230
231
232
233
234
235
236 public static Class<?> loadClass(ClassLoader loader, String clazz) throws ClassNotFoundException {
237 try {
238 if (loader != null) {
239 Class<?> c = loader.loadClass(clazz);
240 if (c != null) {
241 return c;
242 }
243 }
244 } catch (Exception e) {
245 LOG.warn(e.getMessage(), e);
246 }
247 return loadClass(clazz, true);
248 }
249
250
251
252
253
254
255
256
257
258
259
260 public static <T> Class<? extends T> loadClass(ClassLoader loader,
261 String clazz,
262 Class<T> type) throws ClassNotFoundException {
263 try {
264 if (loader != null) {
265 Class<?> c = loader.loadClass(clazz);
266 if (c != null) {
267 return c.asSubclass(type);
268 }
269 }
270 } catch (Exception e) {
271 LOG.warn(e.getMessage(), e);
272 }
273 return loadClass(clazz, true, type);
274 }
275
276
277
278
279
280
281
282
283
284
285
286 public static Class<?> loadClass(String clazz) throws ClassNotFoundException {
287 return loadClass(clazz, true);
288 }
289
290
291
292
293
294
295
296
297
298
299
300
301 public static <T> Class<? extends T> loadClass(String clazz, Class<T> type)
302 throws ClassNotFoundException {
303 return loadClass(clazz, true, type);
304 }
305
306 public static <T> Class<? extends T> loadClass(String clazz,
307 boolean warn,
308 Class<T> type) throws ClassNotFoundException {
309 return loadClass(clazz, warn).asSubclass(type);
310 }
311
312 public static Class<?> loadClass(String clazz, boolean warn) throws ClassNotFoundException {
313 try {
314 ClassLoader tcl = getTCL();
315
316 if (tcl != null) {
317 Class<?> c = tcl.loadClass(clazz);
318 if (c != null) {
319 return c;
320 }
321 }
322 } catch (Exception e) {
323 if (warn) {
324 LOG.warn(e.getMessage(), e);
325 } else {
326 LOG.debug(e.getMessage(), e);
327 }
328 }
329
330 return loadClass2(clazz, null);
331 }
332
333 private static Class<?> loadClass2(String className, Class<?> callingClass)
334 throws ClassNotFoundException {
335 try {
336 return Class.forName(className);
337 } catch (ClassNotFoundException ex) {
338 try {
339 if (Loader.class.getClassLoader() != null) {
340 return Loader.class.getClassLoader().loadClass(className);
341 }
342 } catch (ClassNotFoundException exc) {
343 if (callingClass != null && callingClass.getClassLoader() != null) {
344 return callingClass.getClassLoader().loadClass(className);
345 }
346 }
347 throw ex;
348 }
349 }
350 }