001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.internal.util;
021:
022: import java.util.Locale;
023: import java.util.MissingResourceException;
024: import java.util.PropertyResourceBundle;
025: import java.util.ResourceBundle;
026:
027: /**
028: * Utiliy methods related to class loading in a webapp environment.
029: *
030: * @version $Id: ClassLoaderUtils.java 238991 2004-05-22 11:34:50Z vmassol $
031: */
032: public class ClassLoaderUtils {
033: /**
034: * Try loading a class first by using the context class loader or by using
035: * the classloader of the referrer class if the context classloader failed
036: * to load the class.
037: *
038: * @param theClassName the name of the test class
039: * @param theReferrer the class will be loaded using the classloader which
040: * has loaded this referrer class
041: * @return the class object the test class to call
042: * @exception ClassNotFoundException if the class cannot be loaded through
043: * either classloader
044: */
045: public static Class loadClass(String theClassName, Class theReferrer)
046: throws ClassNotFoundException {
047: // Get the class to call and build an instance of it.
048: Class clazz = null;
049:
050: try {
051: // try loading from webapp classloader first
052: clazz = loadClassFromWebappClassLoader(theClassName,
053: theReferrer);
054: } catch (Throwable internalException) {
055: // Then try first from Context class loader so that we can put the
056: // Cactus jar as an external library.
057: clazz = loadClassFromContextClassLoader(theClassName);
058: }
059:
060: return clazz;
061: }
062:
063: /**
064: * Try loading class using the Context class loader.
065: *
066: * @param theClassName the class to load
067: * @return the <code>Class</code> object for the class to load
068: * @exception ClassNotFoundException if the class cannot be loaded through
069: * this class loader
070: */
071: public static Class loadClassFromContextClassLoader(
072: String theClassName) throws ClassNotFoundException {
073: return Class.forName(theClassName, true, Thread.currentThread()
074: .getContextClassLoader());
075: }
076:
077: /**
078: * Try loading class using the Webapp class loader.
079: *
080: * @param theClassName the class to load
081: * @param theReferrer the class will be loaded using the classloader which
082: * has loaded this referrer class
083: * @return the <code>Class</code> object for the class to load
084: * @exception ClassNotFoundException if the class cannot be loaded through
085: * this class loader
086: */
087: public static Class loadClassFromWebappClassLoader(
088: String theClassName, Class theReferrer)
089: throws ClassNotFoundException {
090: return Class.forName(theClassName, true, theReferrer
091: .getClassLoader());
092: }
093:
094: /**
095: * Try loading a resource bundle from either the context class loader or
096: * the
097: *
098: * @param theName the resource bundle name
099: * @param theReferrer the resource bundle will be loaded using the
100: * classloader which has loaded this referrer class
101: * @return the loaded resource bundle
102: */
103: public static ResourceBundle loadPropertyResourceBundle(
104: String theName, Class theReferrer) {
105: ResourceBundle bundle;
106:
107: try {
108: // Try to load from the referrer class loader first
109:
110: // Some JDK implementation will return "null" when calling
111: // getClassLoader(), signalling that the classloader is the
112: // bootstrap class loader. However, getBundle() does not support
113: // passing null for the class loader, hence the following test.
114: if (theReferrer.getClassLoader() == null) {
115: bundle = PropertyResourceBundle.getBundle(theName,
116: Locale.getDefault());
117: } else {
118: bundle = PropertyResourceBundle.getBundle(theName,
119: Locale.getDefault(), theReferrer
120: .getClassLoader());
121: }
122: } catch (MissingResourceException e) {
123: // Then, try to load from context classloader
124: bundle = PropertyResourceBundle.getBundle(theName, Locale
125: .getDefault(), Thread.currentThread()
126: .getContextClassLoader());
127: }
128:
129: return bundle;
130: }
131: }
|