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