KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > util > Loader


1 /*
2  * Copyright 2001-2004 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 package org.apache.juddi.util;
17
18 import java.io.InputStream JavaDoc;
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * The idea for most of this code was taken from the Apache
28  * (Jakarta) Log4j project: http://jakarta.apache.org/log4j
29  *
30  * - Steve
31  *
32  * @author Ceki Gülcü
33  * @author anou_mana;
34 */

35 public class Loader
36 {
37   // private reference to the jUDDI logger
38
private static Log log = LogFactory.getLog(Loader.class);
39
40   /**
41    * @param resource
42    * @return InputStream to the named resource
43    */

44   public static InputStream JavaDoc getResourceAsStream(String JavaDoc resource)
45   {
46     ClassLoader JavaDoc classLoader = null;
47     InputStream JavaDoc stream = null;
48
49     // Get the Thread Context Class Loader which is
50
// only available in JDK 1.2 and later.
51
try
52     {
53       classLoader = getContextClassLoader();
54       if (classLoader != null)
55       {
56         log.debug("Trying to find ["+resource+"] using context classloader "+classLoader+".");
57         stream = classLoader.getResourceAsStream(resource);
58         if(stream != null) {
59           return stream;
60         }
61       }
62
63       // We could not find resource. Ler us now try
64
// with the class loader that loaded this class.
65
classLoader = Loader.class.getClassLoader();
66       if (classLoader != null)
67       {
68         log.debug("Trying to find ["+resource+"] using "+classLoader+" class loader.");
69         stream = classLoader.getResourceAsStream(resource);
70         if(stream != null) {
71           return stream;
72         }
73       }
74     }
75     catch(Throwable JavaDoc th) {
76       log.warn("Exception thrown from Loader.getResource(\""+resource+"\").",th);
77     }
78
79     // Last ditch attempt: get the resource from the class path. It
80
// may be the case that class was loaded by the Extentsion class
81
// loader which the parent of the system class loader.
82
log.debug("Trying to find ["+resource+"] using ClassLoader.getSystemResource().");
83
84     return ClassLoader.getSystemResourceAsStream(resource);
85   }
86
87   /**
88    * @param resource name
89    * @return URL to the named resource
90    */

91   public static URL JavaDoc getResource(String JavaDoc resource)
92   {
93     ClassLoader JavaDoc classLoader = null;
94     URL JavaDoc url = null;
95
96     // Get the Thread Context Class Loader which is
97
// only available in JDK 1.2 and later.
98
try
99     {
100       classLoader = getContextClassLoader();
101       if (classLoader != null)
102       {
103         log.debug("Trying to find ["+resource+"] using context classloader "+classLoader+".");
104         url = classLoader.getResource(resource);
105         if(url != null) {
106           return url;
107         }
108       }
109
110       // We could not find resource. Ler us now try
111
// with the class loader that loaded this class.
112
classLoader = Loader.class.getClassLoader();
113       if (classLoader != null)
114       {
115         log.debug("Trying to find ["+resource+"] using "+classLoader+" class loader.");
116         url = classLoader.getResource(resource);
117         if(url != null) {
118           return url;
119         }
120       }
121     }
122     catch(Throwable JavaDoc th) {
123       log.warn("Exception thrown from Loader.getResource(\""+resource+"\").",th);
124     }
125
126     // Last ditch attempt: get the resource from the class path. It
127
// may be the case that class was loaded by the Extentsion class
128
// loader which the parent of the system class loader.
129
log.debug("Trying to find ["+resource+"] using ClassLoader.getSystemResource().");
130
131     return ClassLoader.getSystemResource(resource);
132   }
133
134   /**
135    * Get the Thread Context Class Loader which is only available
136    * in JDK 1.2 and later.
137    * @return null if running under a JDK that's earlier than 1.2
138    **/

139   private static ClassLoader JavaDoc getContextClassLoader()
140     throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
141   {
142     Method JavaDoc method = null;
143     try {
144       method = Thread JavaDoc.class.getMethod("getContextClassLoader",null);
145     }
146     catch (NoSuchMethodException JavaDoc e) {
147       return null; // Using JDK 1.1 or earlier
148
}
149
150     return (ClassLoader JavaDoc)method.invoke(Thread.currentThread(),null);
151   }
152
153   /**
154    *
155    * @param name
156    * @return The class object for the name given
157    * @throws ClassNotFoundException
158    * @throws NoClassDefFoundError
159    */

160   public static Class JavaDoc getClassForName(String JavaDoc name)
161     throws ClassNotFoundException JavaDoc, NoClassDefFoundError JavaDoc
162   {
163     Class JavaDoc clazz = null;
164
165     try
166     {
167       log.info("Using the Context ClassLoader");
168       ClassLoader JavaDoc ccl = Thread.currentThread().getContextClassLoader();
169       clazz = Class.forName(name, true, ccl);
170     }
171     catch (Exception JavaDoc e)
172     {
173       log.warn("Failed to load the class " + name + " with context class loader " + e);
174     }
175
176     if (null == clazz)
177     {
178       ClassLoader JavaDoc scl = ClassLoader.getSystemClassLoader();
179
180       try
181       {
182         log.info("Using the System ClassLoader");
183         clazz = Class.forName(name, true, scl);
184       }
185       catch (Exception JavaDoc e)
186       {
187         log.warn("Failed to load the class " + name + " with system class loader " + e);
188       }
189     }
190
191     return clazz;
192   }
193 }
Popular Tags