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
22 import java.io.InputStream;
23 import java.io.InterruptedIOException;
24 import java.lang.reflect.InvocationTargetException;
25 import java.net.URL;
26
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 String ignoreTCLProp = OptionConverter.getSystemProperty("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(String resource, ClassLoader defaultLoader) {
72 ClassLoader classLoader;
73 URL url;
74
75 try {
76 classLoader = getTCL();
77 if (classLoader != null) {
78 LOGGER.trace("Trying to find [" + resource + "] using context classloader "
79 + classLoader + ".");
80 url = classLoader.getResource(resource);
81 if (url != null) {
82 return url;
83 }
84 }
85
86
87 classLoader = Loader.class.getClassLoader();
88 if (classLoader != null) {
89 LOGGER.trace("Trying to find [" + resource + "] using " + classLoader + " class loader.");
90 url = classLoader.getResource(resource);
91 if (url != null) {
92 return url;
93 }
94 }
95
96 if (defaultLoader != null) {
97 LOGGER.trace("Trying to find [" + resource + "] using " + defaultLoader + " class loader.");
98 url = defaultLoader.getResource(resource);
99 if (url != null) {
100 return url;
101 }
102 }
103 } catch (IllegalAccessException t) {
104 LOGGER.warn(TSTR, t);
105 } catch (InvocationTargetException t) {
106 if (t.getTargetException() instanceof InterruptedException
107 || t.getTargetException() instanceof InterruptedIOException) {
108 Thread.currentThread().interrupt();
109 }
110 LOGGER.warn(TSTR, t);
111 } catch (Throwable t) {
112
113
114
115 LOGGER.warn(TSTR, t);
116 }
117
118
119
120
121
122 LOGGER.trace("Trying to find [" + resource + "] using ClassLoader.getSystemResource().");
123 return ClassLoader.getSystemResource(resource);
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 public static InputStream getResourceAsStream(String resource, ClassLoader defaultLoader) {
149 ClassLoader classLoader;
150 InputStream is;
151
152 try {
153 classLoader = getTCL();
154 if (classLoader != null) {
155 LOGGER.trace("Trying to find [" + resource + "] using context classloader " + classLoader + ".");
156 is = classLoader.getResourceAsStream(resource);
157 if (is != null) {
158 return is;
159 }
160 }
161
162
163 classLoader = Loader.class.getClassLoader();
164 if (classLoader != null) {
165 LOGGER.trace("Trying to find [" + resource + "] using " + classLoader + " class loader.");
166 is = classLoader.getResourceAsStream(resource);
167 if (is != null) {
168 return is;
169 }
170 }
171
172
173 if (defaultLoader != null) {
174 LOGGER.trace("Trying to find [" + resource + "] using " + defaultLoader + " class loader.");
175 is = defaultLoader.getResourceAsStream(resource);
176 if (is != null) {
177 return is;
178 }
179 }
180 } catch (IllegalAccessException t) {
181 LOGGER.warn(TSTR, t);
182 } catch (InvocationTargetException t) {
183 if (t.getTargetException() instanceof InterruptedException
184 || t.getTargetException() instanceof InterruptedIOException) {
185 Thread.currentThread().interrupt();
186 }
187 LOGGER.warn(TSTR, t);
188 } catch (Throwable t) {
189
190
191
192 LOGGER.warn(TSTR, t);
193 }
194
195
196
197
198
199 LOGGER.trace("Trying to find [" + resource + "] using ClassLoader.getSystemResource().");
200 return ClassLoader.getSystemResourceAsStream(resource);
201 }
202
203
204
205
206
207
208
209 public static Class loadClass(String clazz) throws ClassNotFoundException {
210
211 if (ignoreTCL) {
212 return Class.forName(clazz);
213 } else {
214 try {
215 return getTCL().loadClass(clazz);
216 } catch (Throwable e) {
217 return Class.forName(clazz);
218 }
219 }
220 }
221
222
223
224
225
226 public static ClassLoader getClassLoader() {
227 ClassLoader cl = null;
228
229 try {
230 cl = getTCL();
231 } catch (Exception ex) {
232
233 }
234 if (cl == null) {
235 cl = Loader.getClassLoader();
236 }
237 return cl;
238 }
239
240 private static ClassLoader getTCL() throws IllegalAccessException, InvocationTargetException {
241 ClassLoader cl;
242 if (System.getSecurityManager() == null) {
243 cl = Thread.currentThread().getContextClassLoader();
244 } else {
245 cl = (ClassLoader) java.security.AccessController.doPrivileged(
246 new java.security.PrivilegedAction() {
247 public Object run() {
248 return Thread.currentThread().getContextClassLoader();
249 }
250 }
251 );
252 }
253
254 return cl;
255 }
256 }