1 /***************************************************************************************
2 * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3 * http://aspectwerkz.codehaus.org *
4 * ---------------------------------------------------------------------------------- *
5 * The software in this package is published under the terms of the LGPL license *
6 * a copy of which has been included with this distribution in the license.txt file. *
7 **************************************************************************************/
8 package org.codehaus.aspectwerkz;
9
10 import java.io.InputStream;
11 import java.net.URL;
12
13 /***
14 * Utility methods dealing with the context class loader. Fail-over is provided to the default class loader.
15 *
16 * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
17 */
18 public final class ContextClassLoader {
19 /***
20 * Loads a class from the context class loader or, if that fails, from the default class loader.
21 *
22 * @param name is the name of the class to load.
23 * @return a <code>Class</code> object.
24 * @throws ClassNotFoundException if the class was not found.
25 */
26 public static Class loadClass(final String name) throws ClassNotFoundException {
27 Class cls = null;
28 try {
29 cls = Thread.currentThread().getContextClassLoader().loadClass(name);
30 } catch (Exception e) {
31 cls = Class.forName(name);
32 }
33 return cls;
34 }
35
36 /***
37 * Loads a resource from the context class loader or, if that fails, from the default class loader.
38 *
39 * @param name is the name of the resource to load.
40 * @return a <code>URL</code> object.
41 */
42 public static URL loadResource(final String name) {
43 try {
44 return Thread.currentThread().getContextClassLoader().getResource(name);
45 } catch (Exception e) {
46 return ClassLoader.class.getClassLoader().getResource(name);
47 }
48 }
49
50 /***
51 * Loads a resource from the context class loader or, if that fails, from the default class loader, as stream
52 *
53 * @param name is the name of the resource to load.
54 * @return a <code>InputStream</code> object.
55 */
56 public static InputStream getResourceAsStream(final String name) {
57 InputStream stream = null;
58 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
59 if (contextClassLoader != null) {
60 stream = contextClassLoader.getResourceAsStream(name);
61 }
62 if (stream == null) {
63 ClassLoader classLoader = ClassLoader.class.getClassLoader();
64 if (classLoader != null) {
65 stream = classLoader.getResourceAsStream(name);
66 }
67 }
68 return stream;
69 }
70
71 /***
72 * Loads a resource from the given class loader as stream
73 *
74 * @param name is the name of the resource to load.
75 * @param loader
76 * @return a <code>InputStream</code> object.
77 */
78 public static InputStream getResourceAsStream(final String name, final ClassLoader loader) {
79 InputStream stream = null;
80 if (loader != null) {
81 return loader.getResourceAsStream(name);
82 } else {
83 return ClassLoader.getSystemClassLoader().getResourceAsStream(name);
84 }
85 }
86
87 /***
88 * Returns the context class loader.
89 *
90 * @return the context class loader
91 */
92 public static ClassLoader getLoader() {
93 ClassLoader loader = Thread.currentThread().getContextClassLoader();
94 if (loader == null) {
95 loader = ClassLoader.class.getClassLoader();
96 }
97 return loader;
98 }
99 }