KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > util > ClassLoaderUtils


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

20 package org.apache.cactus.internal.util;
21
22 import java.util.Locale JavaDoc;
23 import java.util.MissingResourceException JavaDoc;
24 import java.util.PropertyResourceBundle JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26
27 /**
28  * Utiliy methods related to class loading in a webapp environment.
29  *
30  * @version $Id: ClassLoaderUtils.java,v 1.1 2004/05/22 11:34:48 vmassol Exp $
31  */

32 public class ClassLoaderUtils
33 {
34     /**
35      * Try loading a class first by using the context class loader or by using
36      * the classloader of the referrer class if the context classloader failed
37      * to load the class.
38      *
39      * @param theClassName the name of the test class
40      * @param theReferrer the class will be loaded using the classloader which
41      * has loaded this referrer class
42      * @return the class object the test class to call
43      * @exception ClassNotFoundException if the class cannot be loaded through
44      * either classloader
45      */

46     public static Class JavaDoc loadClass(String JavaDoc theClassName, Class JavaDoc theReferrer)
47         throws ClassNotFoundException JavaDoc
48     {
49         // Get the class to call and build an instance of it.
50
Class JavaDoc clazz = null;
51
52         try
53         {
54             // try loading from webapp classloader first
55
clazz = loadClassFromWebappClassLoader(theClassName, theReferrer);
56         }
57         catch (Throwable JavaDoc internalException)
58         {
59             // Then try first from Context class loader so that we can put the
60
// Cactus jar as an external library.
61
clazz = loadClassFromContextClassLoader(theClassName);
62         }
63
64         return clazz;
65     }
66
67     /**
68      * Try loading class using the Context class loader.
69      *
70      * @param theClassName the class to load
71      * @return the <code>Class</code> object for the class to load
72      * @exception ClassNotFoundException if the class cannot be loaded through
73      * this class loader
74      */

75     public static Class JavaDoc loadClassFromContextClassLoader(String JavaDoc theClassName)
76         throws ClassNotFoundException JavaDoc
77     {
78         return Class.forName(theClassName, true,
79             Thread.currentThread().getContextClassLoader());
80     }
81
82     /**
83      * Try loading class using the Webapp class loader.
84      *
85      * @param theClassName the class to load
86      * @param theReferrer the class will be loaded using the classloader which
87      * has loaded this referrer class
88      * @return the <code>Class</code> object for the class to load
89      * @exception ClassNotFoundException if the class cannot be loaded through
90      * this class loader
91      */

92     public static Class JavaDoc loadClassFromWebappClassLoader(String JavaDoc theClassName,
93         Class JavaDoc theReferrer) throws ClassNotFoundException JavaDoc
94     {
95         return Class.forName(theClassName, true, theReferrer.getClassLoader());
96     }
97
98     /**
99      * Try loading a resource bundle from either the context class loader or
100      * the
101      *
102      * @param theName the resource bundle name
103      * @param theReferrer the resource bundle will be loaded using the
104      * classloader which has loaded this referrer class
105      * @return the loaded resource bundle
106      */

107     public static ResourceBundle JavaDoc loadPropertyResourceBundle(String JavaDoc theName,
108         Class JavaDoc theReferrer)
109     {
110         ResourceBundle JavaDoc bundle;
111
112         try
113         {
114             // Try to load from the referrer class loader first
115

116             // Some JDK implementation will return "null" when calling
117
// getClassLoader(), signalling that the classloader is the
118
// bootstrap class loader. However, getBundle() does not support
119
// passing null for the class loader, hence the following test.
120
if (theReferrer.getClassLoader() == null)
121             {
122                 bundle = PropertyResourceBundle.getBundle(theName,
123                     Locale.getDefault());
124             }
125             else
126             {
127                 bundle = PropertyResourceBundle.getBundle(theName,
128                     Locale.getDefault(), theReferrer.getClassLoader());
129             }
130         }
131         catch (MissingResourceException JavaDoc e)
132         {
133             // Then, try to load from context classloader
134
bundle = PropertyResourceBundle.getBundle(theName,
135                 Locale.getDefault(),
136                 Thread.currentThread().getContextClassLoader());
137         }
138
139         return bundle;
140     }
141 }
142
Popular Tags