1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.util;
18
19 import java.io.IOException;
20 import java.lang.reflect.InvocationTargetException;
21 import java.net.URL;
22 import java.security.AccessController;
23 import java.security.PrivilegedAction;
24 import java.util.Collection;
25 import java.util.Enumeration;
26 import java.util.LinkedHashSet;
27 import java.util.Objects;
28
29
30
31
32
33
34
35
36
37 public final class LoaderUtil {
38
39
40
41
42
43
44 public static final String IGNORE_TCCL_PROPERTY = "log4j.ignoreTCL";
45
46 private static final SecurityManager SECURITY_MANAGER = System.getSecurityManager();
47
48
49
50 private static Boolean ignoreTCCL;
51
52 private static final boolean GET_CLASS_LOADER_DISABLED;
53
54 private static final PrivilegedAction<ClassLoader> TCCL_GETTER = new ThreadContextClassLoaderGetter();
55
56 static {
57 if (SECURITY_MANAGER != null) {
58 boolean getClassLoaderDisabled;
59 try {
60 SECURITY_MANAGER.checkPermission(new RuntimePermission("getClassLoader"));
61 getClassLoaderDisabled = false;
62 } catch (final SecurityException ignored) {
63 getClassLoaderDisabled = true;
64 }
65 GET_CLASS_LOADER_DISABLED = getClassLoaderDisabled;
66 } else {
67 GET_CLASS_LOADER_DISABLED = false;
68 }
69 }
70
71 private LoaderUtil() {
72 }
73
74
75
76
77
78
79
80
81
82 public static ClassLoader getThreadContextClassLoader() {
83 if (GET_CLASS_LOADER_DISABLED) {
84
85
86 return LoaderUtil.class.getClassLoader();
87 }
88 return SECURITY_MANAGER == null ? TCCL_GETTER.run() : AccessController.doPrivileged(TCCL_GETTER);
89 }
90
91
92
93
94 private static class ThreadContextClassLoaderGetter implements PrivilegedAction<ClassLoader> {
95 @Override
96 public ClassLoader run() {
97 final ClassLoader cl = Thread.currentThread().getContextClassLoader();
98 if (cl != null) {
99 return cl;
100 }
101 final ClassLoader ccl = LoaderUtil.class.getClassLoader();
102 return ccl == null && !GET_CLASS_LOADER_DISABLED ? ClassLoader.getSystemClassLoader() : ccl;
103 }
104 }
105
106 public static ClassLoader[] getClassLoaders() {
107 final Collection<ClassLoader> classLoaders = new LinkedHashSet<>();
108 final ClassLoader tcl = getThreadContextClassLoader();
109 if (tcl != null) {
110 classLoaders.add(tcl);
111 }
112 accumulateClassLoaders(LoaderUtil.class.getClassLoader(), classLoaders);
113 accumulateClassLoaders(tcl == null ? null : tcl.getParent(), classLoaders);
114 final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
115 if (systemClassLoader != null) {
116 classLoaders.add(systemClassLoader);
117 }
118 return classLoaders.toArray(new ClassLoader[classLoaders.size()]);
119 }
120
121
122
123
124
125 private static void accumulateClassLoaders(ClassLoader loader, Collection<ClassLoader> loaders) {
126
127 if (loader != null && loaders.add(loader)) {
128 accumulateClassLoaders(loader.getParent(), loaders);
129 }
130 }
131
132
133
134
135
136
137
138
139 public static boolean isClassAvailable(final String className) {
140 try {
141 final Class<?> clazz = loadClass(className);
142 return clazz != null;
143 } catch (final ClassNotFoundException | LinkageError e) {
144 return false;
145 } catch (final Throwable e) {
146 LowLevelLogUtil.logException("Unknown error checking for existence of class: " + className, e);
147 return false;
148 }
149 }
150
151
152
153
154
155
156
157
158
159
160 public static Class<?> loadClass(final String className) throws ClassNotFoundException {
161 if (isIgnoreTccl()) {
162 return Class.forName(className);
163 }
164 try {
165 ClassLoader tccl = getThreadContextClassLoader();
166 if (tccl != null) {
167 return tccl.loadClass(className);
168 }
169 } catch (final Throwable ignored) {
170 }
171 return Class.forName(className);
172 }
173
174
175
176
177
178
179
180
181
182
183
184 public static <T> T newInstanceOf(final Class<T> clazz)
185 throws InstantiationException, IllegalAccessException, InvocationTargetException {
186 try {
187 return clazz.getConstructor().newInstance();
188 } catch (final NoSuchMethodException ignored) {
189
190 return clazz.newInstance();
191 }
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206 @SuppressWarnings("unchecked")
207 public static <T> T newInstanceOf(final String className) throws ClassNotFoundException, IllegalAccessException,
208 InstantiationException, NoSuchMethodException, InvocationTargetException {
209 return newInstanceOf((Class<T>) loadClass(className));
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227 public static <T> T newCheckedInstanceOf(final String className, final Class<T> clazz)
228 throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException,
229 IllegalAccessException {
230 return clazz.cast(newInstanceOf(className));
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 public static <T> T newCheckedInstanceOfProperty(final String propertyName, final Class<T> clazz)
249 throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException,
250 IllegalAccessException {
251 final String className = PropertiesUtil.getProperties().getStringProperty(propertyName);
252 if (className == null) {
253 return null;
254 }
255 return newCheckedInstanceOf(className, clazz);
256 }
257
258 private static boolean isIgnoreTccl() {
259
260 if (ignoreTCCL == null) {
261 final String ignoreTccl = PropertiesUtil.getProperties().getStringProperty(IGNORE_TCCL_PROPERTY, null);
262 ignoreTCCL = ignoreTccl != null && !"false".equalsIgnoreCase(ignoreTccl.trim());
263 }
264 return ignoreTCCL;
265 }
266
267
268
269
270
271
272
273
274 public static Collection<URL> findResources(final String resource) {
275 final Collection<UrlResource> urlResources = findUrlResources(resource);
276 final Collection<URL> resources = new LinkedHashSet<>(urlResources.size());
277 for (final UrlResource urlResource : urlResources) {
278 resources.add(urlResource.getUrl());
279 }
280 return resources;
281 }
282
283 static Collection<UrlResource> findUrlResources(final String resource) {
284
285 final ClassLoader[] candidates = {
286 getThreadContextClassLoader(),
287 LoaderUtil.class.getClassLoader(),
288 GET_CLASS_LOADER_DISABLED ? null : ClassLoader.getSystemClassLoader()};
289
290 final Collection<UrlResource> resources = new LinkedHashSet<>();
291 for (final ClassLoader cl : candidates) {
292 if (cl != null) {
293 try {
294 final Enumeration<URL> resourceEnum = cl.getResources(resource);
295 while (resourceEnum.hasMoreElements()) {
296 resources.add(new UrlResource(cl, resourceEnum.nextElement()));
297 }
298 } catch (final IOException e) {
299 LowLevelLogUtil.logException(e);
300 }
301 }
302 }
303 return resources;
304 }
305
306
307
308
309 static class UrlResource {
310 private final ClassLoader classLoader;
311 private final URL url;
312
313 UrlResource(final ClassLoader classLoader, final URL url) {
314 this.classLoader = classLoader;
315 this.url = url;
316 }
317
318 public ClassLoader getClassLoader() {
319 return classLoader;
320 }
321
322 public URL getUrl() {
323 return url;
324 }
325
326 @Override
327 public boolean equals(final Object o) {
328 if (this == o) {
329 return true;
330 }
331 if (o == null || getClass() != o.getClass()) {
332 return false;
333 }
334
335 final UrlResource that = (UrlResource) o;
336
337 if (classLoader != null ? !classLoader.equals(that.classLoader) : that.classLoader != null) {
338 return false;
339 }
340 if (url != null ? !url.equals(that.url) : that.url != null) {
341 return false;
342 }
343
344 return true;
345 }
346
347 @Override
348 public int hashCode() {
349 return Objects.hashCode(classLoader) + Objects.hashCode(url);
350 }
351 }
352 }