KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > helpers > Loader


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.helpers;
18
19 import java.net.URL JavaDoc;
20 import java.lang.IllegalAccessException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23
24 //import java.awt.Image;
25
//import java.awt.Toolkit;
26

27 /**
28    Load resources (or images) from various sources.
29  
30   @author Ceki Gülcü
31  */

32
33 public class Loader {
34
35   static final String JavaDoc TSTR = "Caught Exception while in Loader.getResource. This may be innocuous.";
36
37   // We conservatively assume that we are running under Java 1.x
38
static private boolean java1 = true;
39   
40   static private boolean ignoreTCL = false;
41   
42   static {
43     String JavaDoc prop = OptionConverter.getSystemProperty("java.version", null);
44     
45     if(prop != null) {
46       int i = prop.indexOf('.');
47       if(i != -1) {
48     if(prop.charAt(i+1) != '1')
49       java1 = false;
50       }
51     }
52     String JavaDoc ignoreTCLProp = OptionConverter.getSystemProperty("log4j.ignoreTCL", null);
53     if(ignoreTCLProp != null) {
54       ignoreTCL = OptionConverter.toBoolean(ignoreTCLProp, true);
55     }
56   }
57
58   /**
59      This method will search for <code>resource</code> in different
60      places. The rearch order is as follows:
61
62      <ol>
63
64      <p><li>Search for <code>resource</code> using the thread context
65      class loader under Java2. If that fails, search for
66      <code>resource</code> using the class loader that loaded this
67      class (<code>Loader</code>). Under JDK 1.1, only the the class
68      loader that loaded this class (<code>Loader</code>) is used.
69
70      <p><li>Try one last time with
71      <code>ClassLoader.getSystemResource(resource)</code>, that is is
72      using the system class loader in JDK 1.2 and virtual machine's
73      built-in class loader in JDK 1.1.
74
75      </ol>
76   */

77   static public URL JavaDoc getResource(String JavaDoc resource) {
78     ClassLoader JavaDoc classLoader = null;
79     URL JavaDoc url = null;
80     
81     try {
82     if(!java1) {
83       classLoader = getTCL();
84       if(classLoader != null) {
85         LogLog.debug("Trying to find ["+resource+"] using context classloader "
86              +classLoader+".");
87         url = classLoader.getResource(resource);
88         if(url != null) {
89           return url;
90         }
91       }
92     }
93     
94     // We could not find resource. Ler us now try with the
95
// classloader that loaded this class.
96
classLoader = Loader.class.getClassLoader();
97     if(classLoader != null) {
98       LogLog.debug("Trying to find ["+resource+"] using "+classLoader
99                +" class loader.");
100       url = classLoader.getResource(resource);
101       if(url != null) {
102         return url;
103       }
104     }
105     } catch(Throwable JavaDoc t) {
106     LogLog.warn(TSTR, t);
107     }
108     
109     // Last ditch attempt: get the resource from the class path. It
110
// may be the case that clazz was loaded by the Extentsion class
111
// loader which the parent of the system class loader. Hence the
112
// code below.
113
LogLog.debug("Trying to find ["+resource+
114            "] using ClassLoader.getSystemResource().");
115     return ClassLoader.getSystemResource(resource);
116   }
117   
118   /**
119      Are we running under JDK 1.x?
120   */

121   public
122   static
123   boolean isJava1() {
124     return java1;
125   }
126   
127   /**
128     * Get the Thread Context Loader which is a JDK 1.2 feature. If we
129     * are running under JDK 1.1 or anything else goes wrong the method
130     * returns <code>null<code>.
131     *
132     * */

133   private static ClassLoader JavaDoc getTCL() throws IllegalAccessException JavaDoc,
134     InvocationTargetException JavaDoc {
135
136     // Are we running on a JDK 1.2 or later system?
137
Method JavaDoc method = null;
138     try {
139       method = Thread JavaDoc.class.getMethod("getContextClassLoader", null);
140     } catch (NoSuchMethodException JavaDoc e) {
141       // We are running on JDK 1.1
142
return null;
143     }
144     
145     return (ClassLoader JavaDoc) method.invoke(Thread.currentThread(), null);
146   }
147
148
149   
150   /**
151    * If running under JDK 1.2 load the specified class using the
152    * <code>Thread</code> <code>contextClassLoader</code> if that
153    * fails try Class.forname. Under JDK 1.1 only Class.forName is
154    * used.
155    *
156    */

157   static public Class JavaDoc loadClass (String JavaDoc clazz) throws ClassNotFoundException JavaDoc {
158     // Just call Class.forName(clazz) if we are running under JDK 1.1
159
// or if we are instructed to ignore the TCL.
160
if(java1 || ignoreTCL) {
161       return Class.forName(clazz);
162     } else {
163       try {
164     return getTCL().loadClass(clazz);
165       } catch(Throwable JavaDoc e) {
166     // we reached here because tcl was null or because of a
167
// security exception, or because clazz could not be loaded...
168
// In any case we now try one more time
169
return Class.forName(clazz);
170       }
171     }
172   }
173 }
174
Popular Tags