1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.internal.util;
22
23 import java.util.Locale;
24 import java.util.MissingResourceException;
25 import java.util.PropertyResourceBundle;
26 import java.util.ResourceBundle;
27
28 /**
29 * Utiliy methods related to class loading in a webapp environment.
30 *
31 * @version $Id: ClassLoaderUtils.java 238991 2004-05-22 11:34:50Z vmassol $
32 */
33 public class ClassLoaderUtils
34 {
35 /**
36 * Try loading a class first by using the context class loader or by using
37 * the classloader of the referrer class if the context classloader failed
38 * to load the class.
39 *
40 * @param theClassName the name of the test class
41 * @param theReferrer the class will be loaded using the classloader which
42 * has loaded this referrer class
43 * @return the class object the test class to call
44 * @exception ClassNotFoundException if the class cannot be loaded through
45 * either classloader
46 */
47 public static Class loadClass(String theClassName, Class theReferrer)
48 throws ClassNotFoundException
49 {
50 // Get the class to call and build an instance of it.
51 Class clazz = null;
52
53 try
54 {
55 // try loading from webapp classloader first
56 clazz = loadClassFromWebappClassLoader(theClassName, theReferrer);
57 }
58 catch (Throwable internalException)
59 {
60 // Then try first from Context class loader so that we can put the
61 // Cactus jar as an external library.
62 clazz = loadClassFromContextClassLoader(theClassName);
63 }
64
65 return clazz;
66 }
67
68 /**
69 * Try loading class using the Context class loader.
70 *
71 * @param theClassName the class to load
72 * @return the <code>Class</code> object for the class to load
73 * @exception ClassNotFoundException if the class cannot be loaded through
74 * this class loader
75 */
76 public static Class loadClassFromContextClassLoader(String theClassName)
77 throws ClassNotFoundException
78 {
79 return Class.forName(theClassName, true,
80 Thread.currentThread().getContextClassLoader());
81 }
82
83 /**
84 * Try loading class using the Webapp class loader.
85 *
86 * @param theClassName the class to load
87 * @param theReferrer the class will be loaded using the classloader which
88 * has loaded this referrer class
89 * @return the <code>Class</code> object for the class to load
90 * @exception ClassNotFoundException if the class cannot be loaded through
91 * this class loader
92 */
93 public static Class loadClassFromWebappClassLoader(String theClassName,
94 Class theReferrer) throws ClassNotFoundException
95 {
96 return Class.forName(theClassName, true, theReferrer.getClassLoader());
97 }
98
99 /**
100 * Try loading a resource bundle from either the context class loader or
101 * the.
102 *
103 * @param theName the resource bundle name
104 * @param theReferrer the resource bundle will be loaded using the
105 * classloader which has loaded this referrer class
106 * @return the loaded resource bundle
107 */
108 public static ResourceBundle loadPropertyResourceBundle(String theName,
109 Class theReferrer)
110 {
111 ResourceBundle bundle;
112
113 try
114 {
115 // Try to load from the referrer class loader first
116
117 // Some JDK implementation will return "null" when calling
118 // getClassLoader(), signalling that the classloader is the
119 // bootstrap class loader. However, getBundle() does not support
120 // passing null for the class loader, hence the following test.
121 if (theReferrer.getClassLoader() == null)
122 {
123 bundle = PropertyResourceBundle.getBundle(theName,
124 Locale.getDefault());
125 }
126 else
127 {
128 bundle = PropertyResourceBundle.getBundle(theName,
129 Locale.getDefault(), theReferrer.getClassLoader());
130 }
131 }
132 catch (MissingResourceException e)
133 {
134 // Then, try to load from context classloader
135 bundle = PropertyResourceBundle.getBundle(theName,
136 Locale.getDefault(),
137 Thread.currentThread().getContextClassLoader());
138 }
139
140 return bundle;
141 }
142 }