View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jelly.util;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  /***
23   * A class to centralize the class loader management code.
24   */
25  public class ClassLoaderUtils {
26  
27      /*** log for debug etc output */
28      private static final Log log = LogFactory.getLog(ClassLoaderUtils.class);
29      
30      /***
31       * Return the class loader to be used for instantiating application objects
32       * when required.  This is determined based upon the following rules:
33       * <ul>
34       * <li>The specified class loader, if any</li>
35       * <li>The thread context class loader, if it exists and <code>useContextClassLoader</code> is true</li>
36       * <li>The class loader used to load the calling class.
37       * <li>The System class loader.
38       * </ul>
39       */
40      public static ClassLoader getClassLoader(ClassLoader specifiedLoader, boolean useContextClassLoader, Class callingClass) {
41          if (specifiedLoader != null) {
42              return specifiedLoader;
43          }
44          if (useContextClassLoader) {
45              ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
46              if (classLoader != null) {
47                  return classLoader;
48              }
49          }
50          return getClassLoader(callingClass);
51      }
52  
53      /***
54       * Return the class loader to be used for instantiating application objects
55       * when a context class loader is not specified.  This is determined based upon the following rules:
56       * <ul>
57       * <li>The specified class loader, if any</li>
58       * <li>The class loader used to load the calling class.
59       * <li>The System class loader.
60       * </ul>
61       */
62      public static ClassLoader getClassLoader(ClassLoader specifiedLoader, Class callingClass) {
63          if (specifiedLoader != null) {
64              return specifiedLoader;
65          }
66          return getClassLoader(callingClass);
67      }
68  
69      /***
70       * Get the loader for the given class. 
71       * @param clazz the class to retrieve the loader for
72       * @return the class loader that loaded the provided class
73       */
74      public static ClassLoader getClassLoader(Class clazz) {
75          ClassLoader callersLoader = clazz.getClassLoader();
76          if (callersLoader == null) {
77              callersLoader = ClassLoader.getSystemClassLoader();
78          }
79          return callersLoader;
80      }
81  
82      /***
83       * Loads the given class using the current Thread's context class loader first
84       * otherwise use the class loader which loaded this class.
85       */
86      public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException {
87          ClassLoader loader = Thread.currentThread().getContextClassLoader();
88          if (loader == null) {
89              return getClassLoader(callingClass).loadClass(className);
90          } else {
91              return loader.loadClass(className);
92          }
93      }
94  
95      /***
96       * Loads the given class using:
97       * <ol>
98       * <li>the specified classloader,</li>
99       * <li>the current Thread's context class loader first, if asked</li>
100      * <li>otherwise use the class loader which loaded this class.</li>
101      * </ol>
102      */
103     public static Class loadClass(String className, ClassLoader specifiedLoader, boolean useContextLoader, Class callingClass) throws ClassNotFoundException {
104         Class clazz = null;
105         if (specifiedLoader != null) {
106             try {
107                 clazz = specifiedLoader.loadClass(className);
108             } catch (ClassNotFoundException e) {
109                 log.debug("couldn't find class in specified loader", e);
110             }
111         }
112         if (clazz == null && useContextLoader) {
113             ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
114             if (contextLoader != null) {
115                 try {
116                     clazz = contextLoader.loadClass(className);
117                 } catch (ClassNotFoundException e) {
118                     log.debug("couldn't find class in specified loader", e);
119                 }
120             }
121         }
122         if (clazz == null) {
123             ClassLoader loader = getClassLoader(callingClass);
124             clazz = loader.loadClass(className);
125         }
126         return clazz;
127     }
128 }