KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > util > ContextClassLoader


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.util;
9
10 import java.io.InputStream JavaDoc;
11 import java.net.URL JavaDoc;
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     /**
21      * Loads a class starting from the given class loader (can be null, then use default class loader)
22      *
23      * @param loader
24      * @param name of class to load
25      * @return
26      * @throws ClassNotFoundException
27      */

28     public static Class JavaDoc forName(final ClassLoader JavaDoc loader, final String JavaDoc name) throws ClassNotFoundException JavaDoc {
29         Class JavaDoc klass = null;
30         if (loader != null) {
31             klass = Class.forName(name, false, loader);
32         } else {
33             klass = Class.forName(name, false, ClassLoader.getSystemClassLoader());
34         }
35         return klass;
36     }
37
38
39     /**
40      * Loads a class from the context class loader or, if that fails, from the default class loader.
41      *
42      * @param name is the name of the class to load.
43      * @return a <code>Class</code> object.
44      * @throws ClassNotFoundException if the class was not found.
45      */

46     public static Class JavaDoc forName(final String JavaDoc name) throws ClassNotFoundException JavaDoc {
47         Class JavaDoc cls = null;
48         try {
49             cls = Class.forName(name, false, Thread.currentThread().getContextClassLoader());
50         } catch (Exception JavaDoc e) {
51             cls = Class.forName(name);
52         }
53         return cls;
54     }
55
56     /**
57      * Loads a resource from the context class loader or, if that fails, from the default class loader.
58      *
59      * @param name is the name of the resource to load.
60      * @return a <code>URL</code> object.
61      */

62     public static URL JavaDoc loadResource(final String JavaDoc name) {
63         try {
64             return Thread.currentThread().getContextClassLoader().getResource(name);
65         } catch (Exception JavaDoc e) {
66             return ClassLoader JavaDoc.class.getClassLoader().getResource(name);
67         }
68     }
69
70 // /**
71
// * Loads a resource from the context class loader or, if that fails, from the default class loader, as stream
72
// *
73
// * @param name is the name of the resource to load.
74
// * @return a <code>InputStream</code> object.
75
// */
76
// public static InputStream getResourceAsStream(final String name) {
77
// InputStream stream = null;
78
// ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
79
// if (contextClassLoader != null) {
80
// stream = contextClassLoader.getResourceAsStream(name);
81
// }
82
// if (stream == null) {
83
// ClassLoader classLoader = ClassLoader.class.getClassLoader();
84
// if (classLoader != null) {
85
// stream = classLoader.getResourceAsStream(name);
86
// }
87
// }
88
// return stream;
89
// }
90

91     /**
92      * Returns the context class loader.
93      *
94      * @return the context class loader
95      */

96     public static ClassLoader JavaDoc getLoader() {
97         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
98         if (loader == null) {
99             loader = ClassLoader JavaDoc.class.getClassLoader();
100         }
101         return loader;
102     }
103 }
Popular Tags