KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > util > ContextClassLoader


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.util;
5
6 import java.net.URL JavaDoc;
7
8 /**
9  * Utility methods dealing with the context class loader. Fail-over is provided to the default class loader.
10  *
11  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
12  */

13 public final class ContextClassLoader {
14
15   /**
16    * Loads a class starting from the given class loader (can be null, then use default class loader)
17    *
18    * @param loader
19    * @param name of class to load
20    * @return
21    * @throws ClassNotFoundException
22    */

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

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

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

86   /**
87    * Returns the context class loader.
88    *
89    * @return the context class loader
90    */

91   public static ClassLoader JavaDoc getLoader() {
92     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
93     if (loader == null) {
94       loader = ClassLoader JavaDoc.class.getClassLoader();
95     }
96     return loader;
97   }
98
99   /**
100    * Returns the given loader or the sytem classloader if loader is null
101    *
102    * @param loader
103    * @return
104    */

105   public static ClassLoader JavaDoc getLoaderOrSystemLoader(ClassLoader JavaDoc loader) {
106     if (loader != null) {
107       return loader;
108     } else {
109       return ClassLoader.getSystemClassLoader();
110     }
111   }
112 }
Popular Tags