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