1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.util;
18
19 import java.io.InputStream;
20 import java.lang.reflect.InvocationTargetException;
21 import java.net.URL;
22
23 import org.apache.logging.log4j.Logger;
24 import org.apache.logging.log4j.status.StatusLogger;
25 import org.apache.logging.log4j.util.LoaderUtil;
26 import org.apache.logging.log4j.util.PropertiesUtil;
27
28
29
30
31 public final class Loader {
32
33 private static final Logger LOGGER = StatusLogger.getLogger();
34
35 private static final String TSTR = "Caught Exception while in Loader.getResource. This may be innocuous.";
36
37 private Loader() {
38 }
39
40
41
42
43
44 public static ClassLoader getClassLoader() {
45 return getClassLoader(Loader.class, null);
46 }
47
48
49
50
51
52
53
54
55 public static ClassLoader getThreadContextClassLoader() {
56 return LoaderUtil.getThreadContextClassLoader();
57 }
58
59
60 public static ClassLoader getClassLoader(final Class<?> class1, final Class<?> class2) {
61 final ClassLoader threadContextClassLoader = getThreadContextClassLoader();
62 final ClassLoader loader1 = class1 == null ? null : class1.getClassLoader();
63 final ClassLoader loader2 = class2 == null ? null : class2.getClassLoader();
64
65 if (isChild(threadContextClassLoader, loader1)) {
66 return isChild(threadContextClassLoader, loader2) ? threadContextClassLoader : loader2;
67 }
68 return isChild(loader1, loader2) ? loader1 : loader2;
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public static URL getResource(final String resource, final ClassLoader defaultLoader) {
92 try {
93 ClassLoader classLoader = getThreadContextClassLoader();
94 if (classLoader != null) {
95 LOGGER.trace("Trying to find [{}] using context class loader {}.", resource, classLoader);
96 final URL url = classLoader.getResource(resource);
97 if (url != null) {
98 return url;
99 }
100 }
101
102
103 classLoader = Loader.class.getClassLoader();
104 if (classLoader != null) {
105 LOGGER.trace("Trying to find [{}] using {} class loader.", resource, classLoader);
106 final URL url = classLoader.getResource(resource);
107 if (url != null) {
108 return url;
109 }
110 }
111
112 if (defaultLoader != null) {
113 LOGGER.trace("Trying to find [{}] using {} class loader.", resource, defaultLoader);
114 final URL url = defaultLoader.getResource(resource);
115 if (url != null) {
116 return url;
117 }
118 }
119 } catch (final Throwable t) {
120
121
122
123 LOGGER.warn(TSTR, t);
124 }
125
126
127
128
129
130 LOGGER.trace("Trying to find [{}] using ClassLoader.getSystemResource().", resource);
131 return ClassLoader.getSystemResource(resource);
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public static InputStream getResourceAsStream(final String resource, final ClassLoader defaultLoader) {
154 try {
155 ClassLoader classLoader = getThreadContextClassLoader();
156 InputStream is;
157 if (classLoader != null) {
158 LOGGER.trace("Trying to find [{}] using context class loader {}.", resource, classLoader);
159 is = classLoader.getResourceAsStream(resource);
160 if (is != null) {
161 return is;
162 }
163 }
164
165
166 classLoader = Loader.class.getClassLoader();
167 if (classLoader != null) {
168 LOGGER.trace("Trying to find [{}] using {} class loader.", resource, classLoader);
169 is = classLoader.getResourceAsStream(resource);
170 if (is != null) {
171 return is;
172 }
173 }
174
175
176 if (defaultLoader != null) {
177 LOGGER.trace("Trying to find [{}] using {} class loader.", resource, defaultLoader);
178 is = defaultLoader.getResourceAsStream(resource);
179 if (is != null) {
180 return is;
181 }
182 }
183 } catch (final Throwable t) {
184
185
186
187 LOGGER.warn(TSTR, t);
188 }
189
190
191
192
193
194 LOGGER.trace("Trying to find [{}] using ClassLoader.getSystemResource().", resource);
195 return ClassLoader.getSystemResourceAsStream(resource);
196 }
197
198
199
200
201
202
203
204
205
206 private static boolean isChild(final ClassLoader loader1, final ClassLoader loader2) {
207 if (loader1 != null && loader2 != null) {
208 ClassLoader parent = loader1.getParent();
209 while (parent != null && parent != loader2) {
210 parent = parent.getParent();
211 }
212
213 return parent != null;
214 }
215 return loader1 != null;
216 }
217
218
219
220
221
222
223
224
225
226 public static Class<?> initializeClass(final String className, final ClassLoader loader)
227 throws ClassNotFoundException {
228 return Class.forName(className, true, loader);
229 }
230
231
232
233
234
235
236
237
238
239 public static Class<?> loadClass(final String className, final ClassLoader loader)
240 throws ClassNotFoundException {
241 return loader != null ? loader.loadClass(className) : null;
242 }
243
244
245
246
247
248
249
250
251
252 public static Class<?> loadSystemClass(final String className) throws ClassNotFoundException {
253 try {
254 return Class.forName(className, true, ClassLoader.getSystemClassLoader());
255 } catch (final Throwable t) {
256 LOGGER.trace("Couldn't use SystemClassLoader. Trying Class.forName({}).", className, t);
257 return Class.forName(className);
258 }
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272 @SuppressWarnings("unchecked")
273 public static <T> T newInstanceOf(final String className)
274 throws ClassNotFoundException,
275 IllegalAccessException,
276 InstantiationException,
277 NoSuchMethodException,
278 InvocationTargetException {
279 final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
280 try {
281 Thread.currentThread().setContextClassLoader(getClassLoader());
282 return LoaderUtil.newInstanceOf(className);
283 } finally {
284 Thread.currentThread().setContextClassLoader(contextClassLoader);
285 }
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 public static <T> T newCheckedInstanceOf(final String className, final Class<T> clazz)
303 throws ClassNotFoundException,
304 NoSuchMethodException,
305 IllegalAccessException,
306 InvocationTargetException,
307 InstantiationException {
308 final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
309 try {
310 Thread.currentThread().setContextClassLoader(getClassLoader());
311 return LoaderUtil.newCheckedInstanceOf(className, clazz);
312 } finally {
313 Thread.currentThread().setContextClassLoader(contextClassLoader);
314 }
315 }
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331 public static <T> T newCheckedInstanceOfProperty(final String propertyName, final Class<T> clazz)
332 throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException,
333 IllegalAccessException {
334 final String className = PropertiesUtil.getProperties().getStringProperty(propertyName);
335 final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
336 try {
337 Thread.currentThread().setContextClassLoader(getClassLoader());
338 return LoaderUtil.newCheckedInstanceOfProperty(propertyName, clazz);
339 } finally {
340 Thread.currentThread().setContextClassLoader(contextClassLoader);
341 }
342 }
343
344
345
346
347
348
349
350 public static boolean isClassAvailable(final String className) {
351 final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
352 try {
353 Thread.currentThread().setContextClassLoader(getClassLoader());
354 return LoaderUtil.isClassAvailable(className);
355 } finally {
356 Thread.currentThread().setContextClassLoader(contextClassLoader);
357 }
358 }
359
360 public static boolean isJansiAvailable() {
361 return isClassAvailable("org.fusesource.jansi.AnsiRenderer");
362 }
363
364
365
366
367
368
369
370
371
372 public static Class<?> loadClass(final String className) throws ClassNotFoundException {
373
374 final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
375 try {
376 Thread.currentThread().setContextClassLoader(getClassLoader());
377 return LoaderUtil.loadClass(className);
378 } finally {
379 Thread.currentThread().setContextClassLoader(contextClassLoader);
380 }
381 }
382 }