001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.jelly.util;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: /**
023: * A class to centralize the class loader management code.
024: */
025: public class ClassLoaderUtils {
026:
027: /** log for debug etc output */
028: private static final Log log = LogFactory
029: .getLog(ClassLoaderUtils.class);
030:
031: /**
032: * Return the class loader to be used for instantiating application objects
033: * when required. This is determined based upon the following rules:
034: * <ul>
035: * <li>The specified class loader, if any</li>
036: * <li>The thread context class loader, if it exists and <code>useContextClassLoader</code> is true</li>
037: * <li>The class loader used to load the calling class.
038: * <li>The System class loader.
039: * </ul>
040: */
041: public static ClassLoader getClassLoader(
042: ClassLoader specifiedLoader, boolean useContextClassLoader,
043: Class callingClass) {
044: if (specifiedLoader != null) {
045: return specifiedLoader;
046: }
047: if (useContextClassLoader) {
048: ClassLoader classLoader = Thread.currentThread()
049: .getContextClassLoader();
050: if (classLoader != null) {
051: return classLoader;
052: }
053: }
054: return getClassLoader(callingClass);
055: }
056:
057: /**
058: * Return the class loader to be used for instantiating application objects
059: * when a context class loader is not specified. This is determined based upon the following rules:
060: * <ul>
061: * <li>The specified class loader, if any</li>
062: * <li>The class loader used to load the calling class.
063: * <li>The System class loader.
064: * </ul>
065: */
066: public static ClassLoader getClassLoader(
067: ClassLoader specifiedLoader, Class callingClass) {
068: if (specifiedLoader != null) {
069: return specifiedLoader;
070: }
071: return getClassLoader(callingClass);
072: }
073:
074: /**
075: * Get the loader for the given class.
076: * @param clazz the class to retrieve the loader for
077: * @return the class loader that loaded the provided class
078: */
079: public static ClassLoader getClassLoader(Class clazz) {
080: ClassLoader callersLoader = clazz.getClassLoader();
081: if (callersLoader == null) {
082: callersLoader = ClassLoader.getSystemClassLoader();
083: }
084: return callersLoader;
085: }
086:
087: /**
088: * Loads the given class using the current Thread's context class loader first
089: * otherwise use the class loader which loaded this class.
090: */
091: public static Class loadClass(String className, Class callingClass)
092: throws ClassNotFoundException {
093: ClassLoader loader = Thread.currentThread()
094: .getContextClassLoader();
095: if (loader == null) {
096: return getClassLoader(callingClass).loadClass(className);
097: } else {
098: return loader.loadClass(className);
099: }
100: }
101:
102: /**
103: * Loads the given class using:
104: * <ol>
105: * <li>the specified classloader,</li>
106: * <li>the current Thread's context class loader first, if asked</li>
107: * <li>otherwise use the class loader which loaded this class.</li>
108: * </ol>
109: */
110: public static Class loadClass(String className,
111: ClassLoader specifiedLoader, boolean useContextLoader,
112: Class callingClass) throws ClassNotFoundException {
113: Class clazz = null;
114: if (specifiedLoader != null) {
115: try {
116: clazz = specifiedLoader.loadClass(className);
117: } catch (ClassNotFoundException e) {
118: log.debug("couldn't find class in specified loader", e);
119: }
120: }
121: if (clazz == null && useContextLoader) {
122: ClassLoader contextLoader = Thread.currentThread()
123: .getContextClassLoader();
124: if (contextLoader != null) {
125: try {
126: clazz = contextLoader.loadClass(className);
127: } catch (ClassNotFoundException e) {
128: log.debug(
129: "couldn't find class in specified loader",
130: e);
131: }
132: }
133: }
134: if (clazz == null) {
135: ClassLoader loader = getClassLoader(callingClass);
136: clazz = loader.loadClass(className);
137: }
138: return clazz;
139: }
140: }
|