KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > ClassLoaderUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 import java.net.URL JavaDoc;
6 import java.net.URLClassLoader JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.File JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.List JavaDoc;
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13
14 public class ClassLoaderUtil {
15
16     // ---------------------------------------------------------------- add class path
17

18     /**
19      * Adds additional path to system class loader CLASSPATH during runtime.
20      * @see #addClassPath(String, java.net.URLClassLoader)
21      */

22     public static void addClassPath(String JavaDoc path) {
23         addClassPath(path, (URLClassLoader JavaDoc) ClassLoader.getSystemClassLoader());
24     }
25
26     /**
27      * Adds additional path to system class loader CLASSPATH during runtime.
28      * This is done by using reflection since <code>addURL</code> method of <code>URLClassLoader</code>
29      * is protected.
30      */

31     public static void addClassPath(String JavaDoc path, URLClassLoader JavaDoc classLoader) {
32         try {
33             ReflectUtil.invokeDeclared(URLClassLoader JavaDoc.class, classLoader, "addURL",
34                     new Class JavaDoc[]{URL JavaDoc.class},
35                     new Object JavaDoc[]{new File JavaDoc(path).toURL()});
36         } catch (Throwable JavaDoc th) {
37             throw new RuntimeException JavaDoc("Unable to add class path'" + path + "'.", th);
38         }
39     }
40
41
42     // ---------------------------------------------------------------- define class
43

44     /**
45      * Defines a class from byte array into the system class loader.
46      */

47     public static Class JavaDoc defineClass(String JavaDoc className, byte[] classData) {
48         return defineClass(className, classData, ClassLoader.getSystemClassLoader());
49
50     }
51
52     /**
53      * Defines a class from byte array into the specified class loader.
54      */

55     public static Class JavaDoc defineClass(String JavaDoc className, byte[] classData, ClassLoader JavaDoc classLoader) {
56         try {
57             return (Class JavaDoc) ReflectUtil.invokeDeclared(ClassLoader JavaDoc.class, classLoader, "defineClass",
58                     new Class JavaDoc[]{String JavaDoc.class, byte[].class, int.class, int.class},
59                     new Object JavaDoc[]{className, classData, new Integer JavaDoc(0), new Integer JavaDoc(classData.length)});
60
61         } catch (Throwable JavaDoc th) {
62             throw new RuntimeException JavaDoc("Unable to define class '" + className + "'.", th);
63         }
64     }
65
66
67     // ---------------------------------------------------------------- classpath
68

69
70     /**
71      * Returns complete class path from all availiable <code>URLClassLoaders</code>
72      * starting from class loader that has loaded the specified class.
73      */

74     public static URL JavaDoc[] getFullClassPath(Class JavaDoc clazz) {
75         return getFullClassPath(clazz.getClassLoader());
76     }
77
78     /**
79      * Returns complete class path from all availiable <code>URLClassLoader</code>s.
80      */

81     public static URL JavaDoc[] getFullClassPath(ClassLoader JavaDoc classLoader) {
82         List JavaDoc list = new ArrayList JavaDoc();
83         while (classLoader != null) {
84             if (classLoader instanceof URLClassLoader JavaDoc) {
85                 URL JavaDoc[] urls = ((URLClassLoader JavaDoc) classLoader).getURLs();
86                 for (int i = 0; i < urls.length; i++) {
87                     list.add(urls[i]);
88                 }
89             }
90             classLoader = classLoader.getParent();
91         }
92
93         URL JavaDoc[] result = new URL JavaDoc[list.size()];
94         for (int i = 0; i < result.length; i++) {
95             result[i] = (URL JavaDoc) list.get(i);
96         }
97         return result;
98     }
99
100     // ---------------------------------------------------------------- get resource
101

102     public static URL JavaDoc getResource(String JavaDoc resourceName) {
103         return getResource(resourceName, ClassLoaderUtil.class);
104     }
105
106     public static URL JavaDoc getResource(String JavaDoc resourceName, Class JavaDoc callingClass) {
107         URL JavaDoc url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
108         if (url == null) {
109             url = (jodd.util.ClassLoaderUtil.class).getClassLoader().getResource(resourceName);
110         }
111         if ((url == null) && (callingClass != null)) {
112             ClassLoader JavaDoc cl = callingClass.getClassLoader();
113             if (cl != null) {
114                 url = cl.getResource(resourceName);
115             }
116         }
117         if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) {
118             return getResource('/' + resourceName, callingClass);
119         }
120         return url;
121     }
122
123     // ---------------------------------------------------------------- get resource stream
124

125     public static InputStream JavaDoc getResourceAsStream(String JavaDoc resourceName) throws IOException JavaDoc {
126         return getResourceAsStream(resourceName, ClassLoaderUtil.class);
127     }
128
129
130     public static InputStream JavaDoc getResourceAsStream(String JavaDoc resourceName, Class JavaDoc callingClass) throws IOException JavaDoc {
131         URL JavaDoc url = getResource(resourceName, callingClass);
132         if (url != null) {
133             return url.openStream();
134         }
135         return null;
136     }
137
138     // ---------------------------------------------------------------- load class
139

140     /**
141      * Loads class dynamically.
142      */

143     public static Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
144         return loadClass(className, ClassLoaderUtil.class);
145     }
146
147     /**
148      * Loads class dynamically.
149      */

150     public static Class JavaDoc loadClass(String JavaDoc className, Class JavaDoc callingClass) throws ClassNotFoundException JavaDoc {
151         try {
152             return Thread.currentThread().getContextClassLoader().loadClass(className);
153         } catch (ClassNotFoundException JavaDoc cnfex1) {
154             try {
155                 return Class.forName(className);
156             } catch (ClassNotFoundException JavaDoc cnfex2) {
157                 try {
158                     return (jodd.util.ClassLoaderUtil.class).getClassLoader().loadClass(className);
159                 } catch (ClassNotFoundException JavaDoc cnfex3) {
160                     if (callingClass == null) {
161                         throw cnfex3;
162                     }
163                     return callingClass.getClassLoader().loadClass(className);
164                 }
165             }
166         }
167     }
168
169 }
170
Popular Tags