KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > utils > ClassUtils


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.utils;
56
57 import java.io.InputStream JavaDoc;
58 import java.security.AccessController JavaDoc;
59 import java.security.PrivilegedAction JavaDoc;
60
61 /**
62  * Utility methods for Class Loading.
63  *
64  * @author Davanum Srinvas (dims@yahoo.com)
65  */

66 public final class ClassUtils
67 {
68    // [TDI] static caching of classloaders won't work in JBoss
69
//private static Hashtable classloaders = new Hashtable();
70

71    /**
72     * Set the ClassLoader associated with the given className.
73     *
74     * @param className the name of a class
75     */

76    public static void setClassLoader(String JavaDoc className, ClassLoader JavaDoc loader)
77    {
78       // [TDI] static caching of classloaders won't work in JBoss
79
// if (className != null && loader != null)
80
// classloaders.put(className, loader);
81
}
82
83    /**
84     * Obtain the ClassLoader (if any) associated with the given
85     * className.
86     *
87     * @param className the name of a class
88     * @return class loader
89     */

90    public static ClassLoader JavaDoc getClassLoader(String JavaDoc className)
91    {
92       // [TDI] static caching of classloaders won't work in JBoss
93
//if (className == null) return null;
94
//return (ClassLoader)classloaders.get(className);
95
return null;
96    }
97
98    /**
99     * Deregister the ClassLoader for a given className.
100     *
101     * @param className the name of a class
102     */

103    public static void removeClassLoader(String JavaDoc className)
104    {
105       // [TDI] static caching of classloaders won't work in JBoss
106
//classloaders.remove(className);
107
}
108
109
110    /**
111     * Use this method instead of Class.forName
112     *
113     * @param className Class name
114     * @return java class
115     * @throws ClassNotFoundException if the class is not found
116     */

117    public static Class JavaDoc forName(String JavaDoc className)
118            throws ClassNotFoundException JavaDoc
119    {
120       return loadClass(className);
121    }
122
123    /** Use this method instead of Class.forName (String className, boolean init, ClassLoader loader)
124     */

125    public static Class JavaDoc forName(String JavaDoc _className, boolean init, ClassLoader JavaDoc _loader)
126            throws ClassNotFoundException JavaDoc
127    {
128
129       // Create final vars for doPrivileged block
130
final String JavaDoc className = _className;
131       final ClassLoader JavaDoc loader = _loader;
132       try
133       {
134          // Get the class within a doPrivleged block
135
Object JavaDoc ret =
136                  AccessController.doPrivileged(new PrivilegedAction JavaDoc()
137                  {
138                     public Object JavaDoc run()
139                     {
140                        try
141                        {
142                           return Class.forName(className, true, loader);
143                        }
144                        catch (Throwable JavaDoc e)
145                        {
146                           return e;
147                        }
148                     }
149                  });
150          // If the class was located, return it. Otherwise throw exception
151
if (ret instanceof Class JavaDoc)
152          {
153             return (Class JavaDoc)ret;
154          }
155          else if (ret instanceof ClassNotFoundException JavaDoc)
156          {
157             throw (ClassNotFoundException JavaDoc)ret;
158          }
159          else
160          {
161             throw new ClassNotFoundException JavaDoc(_className);
162          }
163       }
164       catch (ClassNotFoundException JavaDoc cnfe)
165       {
166          return loadClass(className);
167       }
168    }
169
170    /** Loads the class from the context class loader and then falls back to Class.forName
171     */

172    private static Class JavaDoc loadClass(String JavaDoc _className)
173            throws ClassNotFoundException JavaDoc
174    {
175       // Create final vars for doPrivileged block
176
final String JavaDoc className = _className;
177
178       // Get the class within a doPrivleged block
179
Object JavaDoc ret =
180               AccessController.doPrivileged(new PrivilegedAction JavaDoc()
181               {
182                  public Object JavaDoc run()
183                  {
184                     try
185                     {
186                        // Check if the class is a registered class then
187
// use the classloader for that class.
188
ClassLoader JavaDoc classLoader = getClassLoader(className);
189                        return Class.forName(className, true, classLoader);
190                     }
191                     catch (ClassNotFoundException JavaDoc cnfe)
192                     {
193                     }
194
195                     try
196                     {
197                        // Try the context class loader
198
ClassLoader JavaDoc classLoader =
199                                Thread.currentThread().getContextClassLoader();
200                        return Class.forName(className, true, classLoader);
201                     }
202                     catch (ClassNotFoundException JavaDoc cnfe2)
203                     {
204                        try
205                        {
206                           // Try the classloader that loaded this class.
207
ClassLoader JavaDoc classLoader =
208                                   ClassUtils.class.getClassLoader();
209                           return Class.forName(className, true, classLoader);
210                        }
211                        catch (ClassNotFoundException JavaDoc cnfe3)
212                        {
213                           // Try the default class loader.
214
try
215                           {
216                              return Class.forName(className);
217                           }
218                           catch (Throwable JavaDoc e)
219                           {
220                              // Still not found, return exception
221
return e;
222                           }
223                        }
224                     }
225                  }
226               });
227
228       // If the class was located, return it. Otherwise throw exception
229
if (ret instanceof Class JavaDoc)
230       {
231          return (Class JavaDoc)ret;
232       }
233       else if (ret instanceof ClassNotFoundException JavaDoc)
234       {
235          throw (ClassNotFoundException JavaDoc)ret;
236       }
237       else
238       {
239          throw new ClassNotFoundException JavaDoc(_className);
240       }
241    }
242
243    public static InputStream JavaDoc getResourceAsStream(Class JavaDoc clazz, String JavaDoc resource)
244    {
245       InputStream JavaDoc myInputStream = null;
246
247       if (clazz.getClassLoader() != null)
248       {
249          // Try the class loader that loaded this class.
250
myInputStream = clazz.getClassLoader().getResourceAsStream(resource);
251       }
252       else
253       {
254          // Try the system class loader.
255
myInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(resource);
256       }
257       if (myInputStream == null)
258       {
259          // if not found in classpath fall back to default
260
myInputStream = clazz.getResourceAsStream(resource);
261       }
262       return myInputStream;
263    }
264 }
265
Popular Tags