2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.log4j.helpers;
21 import java.lang.reflect.Method;
22 import java.lang.reflect.InvocationTargetException;
23 import java.io.InterruptedIOException;
27 Load resources (or images) from various sources.
29 @author Ceki Gülcü
34 static final String TSTR = "Caught Exception while in Loader.getResource. This may be innocuous.";
36 // We conservatively assume that we are running under Java 1.x
37 static private boolean java1 = true;
39 static private boolean ignoreTCL = false;
42 String prop = OptionConverter.getSystemProperty("java.version", null);
45 int i = prop.indexOf('.');
47 if(prop.charAt(i+1) != '1') {
52 String ignoreTCLProp = OptionConverter.getSystemProperty("log4j.ignoreTCL", null);
53 if(ignoreTCLProp != null) {
54 ignoreTCL = OptionConverter.toBoolean(ignoreTCLProp, true);
59 * Get a resource by delegating to getResource(String).
60 * @param resource resource name
61 * @param clazz class, ignored.
62 * @return URL to resource or null.
63 * @deprecated as of 1.2.
65 public static URL getResource(String resource, Class clazz) {
66 return getResource(resource);
70 This method will search for <code>resource</code> in different
71 places. The search order is as follows:
75 <p><li>Search for <code>resource</code> using the thread context
76 class loader under Java2. If that fails, search for
77 <code>resource</code> using the class loader that loaded this
78 class (<code>Loader</code>). Under JDK 1.1, only the the class
79 loader that loaded this class (<code>Loader</code>) is used.
81 <p><li>Try one last time with
82 <code>ClassLoader.getSystemResource(resource)</code>, that is is
83 using the system class loader in JDK 1.2 and virtual machine's
84 built-in class loader in JDK 1.1.
88 static public URL getResource(String resource) {
89 ClassLoader classLoader = null;
93 if(!java1 && !ignoreTCL) {
94 classLoader = getTCL();
95 if(classLoader != null) {
96 LogLog.debug("Trying to find ["+resource+"] using context classloader "
98 url = classLoader.getResource(resource);
105 // We could not find resource. Ler us now try with the
106 // classloader that loaded this class.
107 classLoader = Loader.class.getClassLoader();
108 if(classLoader != null) {
109 LogLog.debug("Trying to find ["+resource+"] using "+classLoader
111 url = classLoader.getResource(resource);
116 } catch(IllegalAccessException t) {
117 LogLog.warn(TSTR, t);
118 } catch(InvocationTargetException t) {
119 if (t.getTargetException() instanceof InterruptedException
120 || t.getTargetException() instanceof InterruptedIOException) {
121 Thread.currentThread().interrupt();
123 LogLog.warn(TSTR, t);
124 } catch(Throwable t) {
126 // can't be InterruptedException or InterruptedIOException
127 // since not declared, must be error or RuntimeError.
128 LogLog.warn(TSTR, t);
131 // Last ditch attempt: get the resource from the class path. It
132 // may be the case that clazz was loaded by the Extentsion class
133 // loader which the parent of the system class loader. Hence the
135 LogLog.debug("Trying to find ["+resource+
136 "] using ClassLoader.getSystemResource().");
137 return ClassLoader.getSystemResource(resource);
141 Are we running under JDK 1.x?
150 * Get the Thread Context Loader which is a JDK 1.2 feature. If we
151 * are running under JDK 1.1 or anything else goes wrong the method
152 * returns <code>null<code>.
155 private static ClassLoader getTCL() throws IllegalAccessException,
156 InvocationTargetException {
158 // Are we running on a JDK 1.2 or later system?
159 Method method = null;
161 method = Thread.class.getMethod("getContextClassLoader", null);
162 } catch (NoSuchMethodException e) {
163 // We are running on JDK 1.1
167 return (ClassLoader) method.invoke(Thread.currentThread(), null);
173 * If running under JDK 1.2 load the specified class using the
174 * <code>Thread</code> <code>contextClassLoader</code> if that
175 * fails try Class.forname. Under JDK 1.1 only Class.forName is
179 static public Class loadClass (String clazz) throws ClassNotFoundException {
180 // Just call Class.forName(clazz) if we are running under JDK 1.1
181 // or if we are instructed to ignore the TCL.
182 if(java1 || ignoreTCL) {
183 return Class.forName(clazz);
186 return getTCL().loadClass(clazz);
188 // we reached here because tcl was null or because of a
189 // security exception, or because clazz could not be loaded...
190 // In any case we now try one more time
191 catch(InvocationTargetException e) {
192 if (e.getTargetException() instanceof InterruptedException
193 || e.getTargetException() instanceof InterruptedIOException) {
194 Thread.currentThread().interrupt();
196 } catch(Throwable t) {
199 return Class.forName(clazz);