1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.helpers;
19
20 import java.net.URL;
21 import java.lang.reflect.Method;
22 import java.lang.reflect.InvocationTargetException;
23 import java.io.InterruptedIOException;
24
25
26
27
28
29
30
31
32 public class Loader {
33
34 static final String TSTR = "Caught Exception while in Loader.getResource. This may be innocuous.";
35
36
37 static private boolean java1 = true;
38
39 static private boolean ignoreTCL = false;
40
41 static {
42 String prop = OptionConverter.getSystemProperty("java.version", null);
43
44 if(prop != null) {
45 int i = prop.indexOf('.');
46 if(i != -1) {
47 if(prop.charAt(i+1) != '1')
48 java1 = false;
49 }
50 }
51 String ignoreTCLProp = OptionConverter.getSystemProperty("log4j.ignoreTCL", null);
52 if(ignoreTCLProp != null) {
53 ignoreTCL = OptionConverter.toBoolean(ignoreTCLProp, true);
54 }
55 }
56
57
58
59
60
61
62
63
64 public static URL getResource(String resource, Class clazz) {
65 return getResource(resource);
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 static public URL getResource(String resource) {
88 ClassLoader classLoader = null;
89 URL url = null;
90
91 try {
92 if(!java1 && !ignoreTCL) {
93 classLoader = getTCL();
94 if(classLoader != null) {
95 LogLog.debug("Trying to find ["+resource+"] using context classloader "
96 +classLoader+".");
97 url = classLoader.getResource(resource);
98 if(url != null) {
99 return url;
100 }
101 }
102 }
103
104
105
106 classLoader = Loader.class.getClassLoader();
107 if(classLoader != null) {
108 LogLog.debug("Trying to find ["+resource+"] using "+classLoader
109 +" class loader.");
110 url = classLoader.getResource(resource);
111 if(url != null) {
112 return url;
113 }
114 }
115 } catch(IllegalAccessException t) {
116 LogLog.warn(TSTR, t);
117 } catch(InvocationTargetException t) {
118 if (t.getTargetException() instanceof InterruptedException
119 || t.getTargetException() instanceof InterruptedIOException) {
120 Thread.currentThread().interrupt();
121 }
122 LogLog.warn(TSTR, t);
123 } catch(Throwable t) {
124
125
126
127 LogLog.warn(TSTR, t);
128 }
129
130
131
132
133
134 LogLog.debug("Trying to find ["+resource+
135 "] using ClassLoader.getSystemResource().");
136 return ClassLoader.getSystemResource(resource);
137 }
138
139
140
141
142 public
143 static
144 boolean isJava1() {
145 return java1;
146 }
147
148
149
150
151
152
153
154 private static ClassLoader getTCL() throws IllegalAccessException,
155 InvocationTargetException {
156
157
158 Method method = null;
159 try {
160 method = Thread.class.getMethod("getContextClassLoader", null);
161 } catch (NoSuchMethodException e) {
162
163 return null;
164 }
165
166 return (ClassLoader) method.invoke(Thread.currentThread(), null);
167 }
168
169
170
171
172
173
174
175
176
177
178 static public Class loadClass (String clazz) throws ClassNotFoundException {
179
180
181 if(java1 || ignoreTCL) {
182 return Class.forName(clazz);
183 } else {
184 try {
185 return getTCL().loadClass(clazz);
186 }
187
188
189
190 catch(InvocationTargetException e) {
191 if (e.getTargetException() instanceof InterruptedException
192 || e.getTargetException() instanceof InterruptedIOException) {
193 Thread.currentThread().interrupt();
194 }
195 } catch(Throwable t) {
196 }
197 }
198 return Class.forName(clazz);
199 }
200 }