KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > util > ClassLoaderUtil


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.util;
6
7
8 /**
9  * <p>This code is borrowed directly from OSCore, but is duplicated
10  * here to avoid having to add a dependency on the entire OSCore jar.</p>
11  *
12  * <p>If much more code from OSCore is needed then it might be wiser to
13  * bite the bullet and add a dependency.</p>
14  */

15 public class ClassLoaderUtil {
16     /**
17      * Load a class with a given name.
18      *
19      * It will try to load the class in the following order:
20      * <ul>
21      * <li>From Thread.currentThread().getContextClassLoader()
22      * <li>Using the basic Class.forName()
23      * <li>From ClassLoaderUtil.class.getClassLoader()
24      * <li>From the callingClass.getClassLoader()
25      * </ul>
26      *
27      * @param className The name of the class to load
28      * @param callingClass The Class object of the calling object
29      * @throws ClassNotFoundException If the class cannot be found anywhere.
30      */

31     public static Class JavaDoc loadClass(String JavaDoc className, Class JavaDoc callingClass) throws ClassNotFoundException JavaDoc {
32         try {
33             return Thread.currentThread().getContextClassLoader().loadClass(className);
34         } catch (ClassNotFoundException JavaDoc e) {
35             try {
36                 return Class.forName(className);
37             } catch (ClassNotFoundException JavaDoc ex) {
38                 try {
39                     return ClassLoaderUtil.class.getClassLoader().loadClass(className);
40                 } catch (ClassNotFoundException JavaDoc exc) {
41                     return callingClass.getClassLoader().loadClass(className);
42                 }
43             }
44         }
45     }
46 }
47
Popular Tags