1 package com.opensymphony.webwork.portlet.util; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.net.URL ; 6 import java.util.Enumeration ; 7 8 public class ClassLoaderUtils { 9 10 public ClassLoaderUtils() { 11 } 12 13 public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException { 14 try { 15 return Thread.currentThread().getContextClassLoader().loadClass(className); 16 } catch (ClassNotFoundException e) { 17 try { 18 return Class.forName(className); 19 } catch (ClassNotFoundException ex) { 20 21 try { 22 return (com.opensymphony.webwork.portlet.util.ClassLoaderUtils.class).getClassLoader().loadClass(className); 23 } catch (ClassNotFoundException exc) { 24 try { 25 return callingClass.getClassLoader().loadClass(className); 26 } catch (ClassNotFoundException exce) { 27 throw exce; 28 } 29 } 30 } 31 } 32 33 } 34 35 public static URL getResource(String resourceName, Class callingClass) { 36 URL 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 getResources(String resourceName, Class callingClass) throws IOException { 46 Enumeration 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 getResourceAsStream(String resourceName, Class callingClass) { 56 try { 57 URL url = getResource(resourceName, callingClass); 58 return url == null ? null : url.openStream(); 59 } catch (IOException 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 cl) { 70 System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")"); 71 if (cl != null) 72 printClassLoader(cl.getParent()); 73 } 74 } | Popular Tags |