1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.helpers;
18
19 import org.apache.logging.log4j.Logger;
20 import org.apache.logging.log4j.status.StatusLogger;
21 import org.apache.logging.log4j.util.PropertiesUtil;
22
23 import java.io.InputStream;
24 import java.io.InterruptedIOException;
25 import java.lang.reflect.InvocationTargetException;
26 import java.net.URL;
27
28
29
30
31 public final class Loader {
32
33 private static final String TSTR = "Caught Exception while in Loader.getResource. This may be innocuous.";
34
35 private static boolean ignoreTCL = false;
36
37 private static final Logger LOGGER = StatusLogger.getLogger();
38
39 static {
40 final String ignoreTCLProp = PropertiesUtil.getProperties().getStringProperty("log4j.ignoreTCL", null);
41 if (ignoreTCLProp != null) {
42 ignoreTCL = OptionConverter.toBoolean(ignoreTCLProp, true);
43 }
44 }
45
46 private Loader() {
47 }
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public static URL getResource(final String resource, final ClassLoader defaultLoader) {
72 try {
73 ClassLoader classLoader = getTCL();
74 if (classLoader != null) {
75 LOGGER.trace("Trying to find [" + resource + "] using context classloader "
76 + classLoader + '.');
77 final URL url = classLoader.getResource(resource);
78 if (url != null) {
79 return url;
80 }
81 }
82
83
84 classLoader = Loader.class.getClassLoader();
85 if (classLoader != null) {
86 LOGGER.trace("Trying to find [" + resource + "] using " + classLoader + " class loader.");
87 final URL url = classLoader.getResource(resource);
88 if (url != null) {
89 return url;
90 }
91 }
92
93 if (defaultLoader != null) {
94 LOGGER.trace("Trying to find [" + resource + "] using " + defaultLoader + " class loader.");
95 final URL url = defaultLoader.getResource(resource);
96 if (url != null) {
97 return url;
98 }
99 }
100 } catch (final IllegalAccessException t) {
101 LOGGER.warn(TSTR, t);
102 } catch (final InvocationTargetException t) {
103 if (t.getTargetException() instanceof InterruptedException
104 || t.getTargetException() instanceof InterruptedIOException) {
105 Thread.currentThread().interrupt();
106 }
107 LOGGER.warn(TSTR, t);
108 } catch (final Throwable t) {
109
110
111
112 LOGGER.warn(TSTR, t);
113 }
114
115
116
117
118
119 LOGGER.trace("Trying to find [" + resource + "] using ClassLoader.getSystemResource().");
120 return ClassLoader.getSystemResource(resource);
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 public static InputStream getResourceAsStream(final String resource, final ClassLoader defaultLoader) {
146 ClassLoader classLoader;
147 InputStream is;
148
149 try {
150 classLoader = getTCL();
151 if (classLoader != null) {
152 LOGGER.trace("Trying to find [" + resource + "] using context classloader " + classLoader + '.');
153 is = classLoader.getResourceAsStream(resource);
154 if (is != null) {
155 return is;
156 }
157 }
158
159
160 classLoader = Loader.class.getClassLoader();
161 if (classLoader != null) {
162 LOGGER.trace("Trying to find [" + resource + "] using " + classLoader + " class loader.");
163 is = classLoader.getResourceAsStream(resource);
164 if (is != null) {
165 return is;
166 }
167 }
168
169
170 if (defaultLoader != null) {
171 LOGGER.trace("Trying to find [" + resource + "] using " + defaultLoader + " class loader.");
172 is = defaultLoader.getResourceAsStream(resource);
173 if (is != null) {
174 return is;
175 }
176 }
177 } catch (final IllegalAccessException t) {
178 LOGGER.warn(TSTR, t);
179 } catch (final InvocationTargetException t) {
180 if (t.getTargetException() instanceof InterruptedException
181 || t.getTargetException() instanceof InterruptedIOException) {
182 Thread.currentThread().interrupt();
183 }
184 LOGGER.warn(TSTR, t);
185 } catch (final Throwable t) {
186
187
188
189 LOGGER.warn(TSTR, t);
190 }
191
192
193
194
195
196 LOGGER.trace("Trying to find [" + resource + "] using ClassLoader.getSystemResource().");
197 return ClassLoader.getSystemResourceAsStream(resource);
198 }
199
200
201
202
203
204
205
206 public static Class<?> loadClass(final String className) throws ClassNotFoundException {
207
208 if (ignoreTCL) {
209 return Class.forName(className);
210 } else {
211 try {
212 return getTCL().loadClass(className);
213 } catch (final Throwable e) {
214 return Class.forName(className);
215 }
216 }
217 }
218
219 public static ClassLoader getClassLoader(final Class<?> class1, final Class<?> class2) {
220
221 ClassLoader loader1 = null;
222 try {
223 loader1 = getTCL();
224 } catch (final Exception ex) {
225 LOGGER.warn("Caught exception locating thread ClassLoader {}", ex.getMessage());
226 }
227 final ClassLoader loader2 = class1 == null ? null : class1.getClassLoader();
228 final ClassLoader loader3 = class2 == null ? null : class2.getClass().getClassLoader();
229
230 if (isChild(loader1, loader2)) {
231 return isChild(loader1, loader3) ? loader1 : loader3;
232 } else {
233 return isChild(loader2, loader3) ? loader2 : loader3;
234 }
235 }
236
237 private static boolean isChild(final ClassLoader loader1, final ClassLoader loader2) {
238 if (loader1 != null && loader2 != null) {
239 ClassLoader parent = loader1.getParent();
240 while (parent != null && parent != loader2) {
241 parent = parent.getParent();
242 }
243 return parent != null;
244 }
245 return loader1 != null;
246 }
247
248
249
250
251
252 public static ClassLoader getClassLoader() {
253
254 return getClassLoader(Loader.class, null);
255 }
256
257 private static ClassLoader getTCL() throws IllegalAccessException, InvocationTargetException {
258 ClassLoader cl;
259 if (System.getSecurityManager() == null) {
260 cl = Thread.currentThread().getContextClassLoader();
261 } else {
262 cl = java.security.AccessController.doPrivileged(
263 new java.security.PrivilegedAction<ClassLoader>() {
264 public ClassLoader run() {
265 return Thread.currentThread().getContextClassLoader();
266 }
267 }
268 );
269 }
270
271 return cl;
272 }
273 }