001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.util;
005:
006: import java.net.URL;
007:
008: /**
009: * Utility methods dealing with the context class loader. Fail-over is provided to the default class loader.
010: *
011: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
012: */
013: public final class ContextClassLoader {
014:
015: /**
016: * Loads a class starting from the given class loader (can be null, then use default class loader)
017: *
018: * @param loader
019: * @param name of class to load
020: * @return
021: * @throws ClassNotFoundException
022: */
023: public static Class forName(final ClassLoader loader,
024: final String name) throws ClassNotFoundException {
025: Class klass = null;
026: if (loader != null) {
027: klass = Class.forName(name, false, loader);
028: } else {
029: klass = Class.forName(name, false, ClassLoader
030: .getSystemClassLoader());
031: }
032: return klass;
033: }
034:
035: /**
036: * Loads a class from the context class loader or, if that fails, from the default class loader.
037: *
038: * @param name is the name of the class to load.
039: * @return a <code>Class</code> object.
040: * @throws ClassNotFoundException if the class was not found.
041: */
042: public static Class forName(final String name)
043: throws ClassNotFoundException {
044: Class cls = null;
045: try {
046: cls = Class.forName(name, false, Thread.currentThread()
047: .getContextClassLoader());
048: } catch (Exception e) {
049: cls = Class.forName(name);
050: }
051: return cls;
052: }
053:
054: /**
055: * Loads a resource from the context class loader or, if that fails, from the default class loader.
056: *
057: * @param name is the name of the resource to load.
058: * @return a <code>URL</code> object.
059: */
060: public static URL loadResource(final String name) {
061: try {
062: return Thread.currentThread().getContextClassLoader()
063: .getResource(name);
064: } catch (Exception e) {
065: return ClassLoader.class.getClassLoader().getResource(name);
066: }
067: }
068:
069: // /**
070: // * Loads a resource from the context class loader or, if that fails, from the default class loader, as stream
071: // *
072: // * @param name is the name of the resource to load.
073: // * @return a <code>InputStream</code> object.
074: // */
075: // public static InputStream getResourceAsStream(final String name) {
076: // InputStream stream = null;
077: // ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
078: // if (contextClassLoader != null) {
079: // stream = contextClassLoader.getResourceAsStream(name);
080: // }
081: // if (stream == null) {
082: // ClassLoader classLoader = ClassLoader.class.getClassLoader();
083: // if (classLoader != null) {
084: // stream = classLoader.getResourceAsStream(name);
085: // }
086: // }
087: // return stream;
088: // }
089:
090: /**
091: * Returns the context class loader.
092: *
093: * @return the context class loader
094: */
095: public static ClassLoader getLoader() {
096: ClassLoader loader = Thread.currentThread()
097: .getContextClassLoader();
098: if (loader == null) {
099: loader = ClassLoader.class.getClassLoader();
100: }
101: return loader;
102: }
103:
104: /**
105: * Returns the given loader or the sytem classloader if loader is null
106: *
107: * @param loader
108: * @return
109: */
110: public static ClassLoader getLoaderOrSystemLoader(ClassLoader loader) {
111: if (loader != null) {
112: return loader;
113: } else {
114: return ClassLoader.getSystemClassLoader();
115: }
116: }
117: }
|