001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.util;
006:
007: import java.io.IOException;
008: import java.io.InputStream;
009: import java.net.URL;
010:
011: /**
012: * This class is extremely useful for loading resources and classes in a fault tolerant manner
013: * that works across different applications servers.
014: * <p/>
015: * It has come out of many months of frustrating use of multiple application servers at Atlassian,
016: * please don't change things unless you're sure they're not going to break in one server or another!
017: *
018: * @author $Author: plightbo $
019: * @version $Revision: 1282 $
020: */
021: public class ClassLoaderUtils {
022:
023: /**
024: * Load a given resource.
025: * <p/>
026: * This method will try to load the resource using the following methods (in order):
027: * <ul>
028: * <li>From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()}
029: * <li>From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()}
030: * <li>From the {@link Class#getClassLoader() callingClass.getClassLoader() }
031: * </ul>
032: *
033: * @param resourceName The name of the resource to load
034: * @param callingClass The Class object of the calling object
035: */
036: public static URL getResource(String resourceName,
037: Class callingClass) {
038: URL url = null;
039:
040: url = Thread.currentThread().getContextClassLoader()
041: .getResource(resourceName);
042:
043: if (url == null) {
044: url = ClassLoaderUtils.class.getClassLoader().getResource(
045: resourceName);
046: }
047:
048: if (url == null) {
049: url = callingClass.getClassLoader().getResource(
050: resourceName);
051: }
052:
053: return url;
054: }
055:
056: /**
057: * This is a convenience method to load a resource as a stream.
058: * <p/>
059: * The algorithm used to find the resource is given in getResource()
060: *
061: * @param resourceName The name of the resource to load
062: * @param callingClass The Class object of the calling object
063: */
064: public static InputStream getResourceAsStream(String resourceName,
065: Class callingClass) {
066: URL url = getResource(resourceName, callingClass);
067:
068: try {
069: return (url != null) ? url.openStream() : null;
070: } catch (IOException e) {
071: return null;
072: }
073: }
074:
075: /**
076: * Load a class with a given name.
077: * <p/>
078: * It will try to load the class in the following order:
079: * <ul>
080: * <li>From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()}
081: * <li>Using the basic {@link Class#forName(java.lang.String) }
082: * <li>From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()}
083: * <li>From the {@link Class#getClassLoader() callingClass.getClassLoader() }
084: * </ul>
085: *
086: * @param className The name of the class to load
087: * @param callingClass The Class object of the calling object
088: * @throws ClassNotFoundException If the class cannot be found anywhere.
089: */
090: public static Class loadClass(String className, Class callingClass)
091: throws ClassNotFoundException {
092: try {
093: return Thread.currentThread().getContextClassLoader()
094: .loadClass(className);
095: } catch (ClassNotFoundException e) {
096: try {
097: return Class.forName(className);
098: } catch (ClassNotFoundException ex) {
099: try {
100: return ClassLoaderUtils.class.getClassLoader()
101: .loadClass(className);
102: } catch (ClassNotFoundException exc) {
103: return callingClass.getClassLoader().loadClass(
104: className);
105: }
106: }
107: }
108: }
109:
110: /**
111: * Prints the current classloader hierarchy - useful for debugging.
112: */
113: public static void printClassLoader() {
114: System.out.println("ClassLoaderUtils.printClassLoader");
115: printClassLoader(Thread.currentThread().getContextClassLoader());
116: }
117:
118: /**
119: * Prints the classloader hierarchy from a given classloader - useful for debugging.
120: */
121: public static void printClassLoader(ClassLoader cl) {
122: System.out.println("ClassLoaderUtils.printClassLoader(cl = "
123: + cl + ")");
124:
125: if (cl != null) {
126: printClassLoader(cl.getParent());
127: }
128: }
129: }
|