01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.oscache.util;
06:
07: /**
08: * <p>This code is borrowed directly from OSCore, but is duplicated
09: * here to avoid having to add a dependency on the entire OSCore jar.</p>
10: *
11: * <p>If much more code from OSCore is needed then it might be wiser to
12: * bite the bullet and add a dependency.</p>
13: */
14: public class ClassLoaderUtil {
15:
16: private ClassLoaderUtil() {
17: }
18:
19: /**
20: * Load a class with a given name.
21: *
22: * It will try to load the class in the following order:
23: * <ul>
24: * <li>From Thread.currentThread().getContextClassLoader()
25: * <li>Using the basic Class.forName()
26: * <li>From ClassLoaderUtil.class.getClassLoader()
27: * <li>From the callingClass.getClassLoader()
28: * </ul>
29: *
30: * @param className The name of the class to load
31: * @param callingClass The Class object of the calling object
32: * @throws ClassNotFoundException If the class cannot be found anywhere.
33: */
34: public static Class loadClass(String className, Class callingClass)
35: throws ClassNotFoundException {
36: try {
37: return Thread.currentThread().getContextClassLoader()
38: .loadClass(className);
39: } catch (ClassNotFoundException e) {
40: try {
41: return Class.forName(className);
42: } catch (ClassNotFoundException ex) {
43: try {
44: return ClassLoaderUtil.class.getClassLoader()
45: .loadClass(className);
46: } catch (ClassNotFoundException exc) {
47: return callingClass.getClassLoader().loadClass(
48: className);
49: }
50: }
51: }
52: }
53: }
|