KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > portlet > util > ClassLoaderUtils


1 package com.opensymphony.webwork.portlet.util;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.util.Enumeration JavaDoc;
7
8 public class ClassLoaderUtils {
9
10     public ClassLoaderUtils() {
11     }
12
13     public static Class JavaDoc loadClass(String JavaDoc className, Class JavaDoc callingClass) throws ClassNotFoundException JavaDoc {
14         try {
15             return Thread.currentThread().getContextClassLoader().loadClass(className);
16         } catch (ClassNotFoundException JavaDoc e) {
17             try {
18                 return Class.forName(className);
19             } catch (ClassNotFoundException JavaDoc ex) {
20
21                 try {
22                     return (com.opensymphony.webwork.portlet.util.ClassLoaderUtils.class).getClassLoader().loadClass(className);
23                 } catch (ClassNotFoundException JavaDoc exc) {
24                     try {
25                         return callingClass.getClassLoader().loadClass(className);
26                     } catch (ClassNotFoundException JavaDoc exce) {
27                         throw exce;
28                     }
29                 }
30             }
31         }
32
33     }
34
35     public static URL JavaDoc getResource(String JavaDoc resourceName, Class JavaDoc callingClass) {
36         URL JavaDoc url = null;
37         url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
38         if (url == null)
39             url = (com.opensymphony.webwork.portlet.util.ClassLoaderUtils.class).getClassLoader().getResource(resourceName);
40         if (url == null)
41             url = callingClass.getClassLoader().getResource(resourceName);
42         return url;
43     }
44
45     public static Enumeration JavaDoc getResources(String JavaDoc resourceName, Class JavaDoc callingClass) throws IOException JavaDoc {
46         Enumeration JavaDoc urls = Thread.currentThread().getContextClassLoader().getResources(resourceName);
47         if (urls == null) {
48             urls = (com.opensymphony.webwork.portlet.util.ClassLoaderUtils.class).getClassLoader().getResources(resourceName);
49             if (urls == null)
50                 urls = callingClass.getClassLoader().getResources(resourceName);
51         }
52         return urls;
53     }
54
55     public static InputStream JavaDoc getResourceAsStream(String JavaDoc resourceName, Class JavaDoc callingClass) {
56         try {
57             URL JavaDoc url = getResource(resourceName, callingClass);
58             return url == null ? null : url.openStream();
59         } catch (IOException JavaDoc e) {
60             return null;
61         }
62     }
63
64     public static void printClassLoader() {
65         System.out.println("ClassLoaderUtils.printClassLoader");
66         printClassLoader(Thread.currentThread().getContextClassLoader());
67     }
68
69     public static void printClassLoader(ClassLoader JavaDoc cl) {
70         System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")");
71         if (cl != null)
72             printClassLoader(cl.getParent());
73     }
74 }
Popular Tags