KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > util > ClassLoaderUtils


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

5 package com.opensymphony.webwork.util;
6
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.net.URL JavaDoc;
10
11
12 /**
13  * This class is extremely useful for loading resources and classes in a fault tolerant manner
14  * that works across different applications servers.
15  * <p/>
16  * It has come out of many months of frustrating use of multiple application servers at Atlassian,
17  * please don't change things unless you're sure they're not going to break in one server or another!
18  *
19  * @author $Author: jcarreira $
20  * @version $Revision: 1.7 $
21  */

22 public class ClassLoaderUtils {
23     //~ Methods ////////////////////////////////////////////////////////////////
24

25     /**
26      * Load a given resource.
27      * <p/>
28      * This method will try to load the resource using the following methods (in order):
29      * <ul>
30      * <li>From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()}
31      * <li>From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()}
32      * <li>From the {@link Class#getClassLoader() callingClass.getClassLoader() }
33      * </ul>
34      *
35      * @param resourceName The name of the resource to load
36      * @param callingClass The Class object of the calling object
37      */

38     public static URL JavaDoc getResource(String JavaDoc resourceName, Class JavaDoc callingClass) {
39         URL JavaDoc url = null;
40
41         url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
42
43         if (url == null) {
44             url = ClassLoaderUtils.class.getClassLoader().getResource(resourceName);
45         }
46
47         if (url == null) {
48             url = callingClass.getClassLoader().getResource(resourceName);
49         }
50
51         return url;
52     }
53
54     /**
55      * This is a convenience method to load a resource as a stream.
56      * <p/>
57      * The algorithm used to find the resource is given in getResource()
58      *
59      * @param resourceName The name of the resource to load
60      * @param callingClass The Class object of the calling object
61      */

62     public static InputStream JavaDoc getResourceAsStream(String JavaDoc resourceName, Class JavaDoc callingClass) {
63         URL JavaDoc url = getResource(resourceName, callingClass);
64
65         try {
66             return (url != null) ? url.openStream() : null;
67         } catch (IOException JavaDoc e) {
68             return null;
69         }
70     }
71
72     /**
73      * Load a class with a given name.
74      * <p/>
75      * It will try to load the class in the following order:
76      * <ul>
77      * <li>From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()}
78      * <li>Using the basic {@link Class#forName(java.lang.String) }
79      * <li>From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()}
80      * <li>From the {@link Class#getClassLoader() callingClass.getClassLoader() }
81      * </ul>
82      *
83      * @param className The name of the class to load
84      * @param callingClass The Class object of the calling object
85      * @throws ClassNotFoundException If the class cannot be found anywhere.
86      */

87     public static Class JavaDoc loadClass(String JavaDoc className, Class JavaDoc callingClass) throws ClassNotFoundException JavaDoc {
88         try {
89             return Thread.currentThread().getContextClassLoader().loadClass(className);
90         } catch (ClassNotFoundException JavaDoc e) {
91             try {
92                 return Class.forName(className);
93             } catch (ClassNotFoundException JavaDoc ex) {
94                 try {
95                     return ClassLoaderUtils.class.getClassLoader().loadClass(className);
96                 } catch (ClassNotFoundException JavaDoc exc) {
97                     return callingClass.getClassLoader().loadClass(className);
98                 }
99             }
100         }
101     }
102
103     /**
104      * Prints the current classloader hierarchy - useful for debugging.
105      */

106     public static void printClassLoader() {
107         System.out.println("ClassLoaderUtils.printClassLoader");
108         printClassLoader(Thread.currentThread().getContextClassLoader());
109     }
110
111     /**
112      * Prints the classloader hierarchy from a given classloader - useful for debugging.
113      */

114     public static void printClassLoader(ClassLoader JavaDoc cl) {
115         System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")");
116
117         if (cl != null) {
118             printClassLoader(cl.getParent());
119         }
120     }
121 }
122
Popular Tags