1 5 package com.opensymphony.webwork.util; 6 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 import java.net.URL ; 10 11 12 22 public class ClassLoaderUtils { 23 25 38 public static URL getResource(String resourceName, Class callingClass) { 39 URL url = null; 40 41 url = Thread.currentThread().getContextClassLoader().getResource(resourceName); 42 43 if (url == null) { 44 url = ClassLoaderUtils.class.getClassLoader().getResource(resourceName); 45 } 46 47 if (url == null) { 48 url = callingClass.getClassLoader().getResource(resourceName); 49 } 50 51 return url; 52 } 53 54 62 public static InputStream getResourceAsStream(String resourceName, Class callingClass) { 63 URL url = getResource(resourceName, callingClass); 64 65 try { 66 return (url != null) ? url.openStream() : null; 67 } catch (IOException e) { 68 return null; 69 } 70 } 71 72 87 public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException { 88 try { 89 return Thread.currentThread().getContextClassLoader().loadClass(className); 90 } catch (ClassNotFoundException e) { 91 try { 92 return Class.forName(className); 93 } catch (ClassNotFoundException ex) { 94 try { 95 return ClassLoaderUtils.class.getClassLoader().loadClass(className); 96 } catch (ClassNotFoundException exc) { 97 return callingClass.getClassLoader().loadClass(className); 98 } 99 } 100 } 101 } 102 103 106 public static void printClassLoader() { 107 System.out.println("ClassLoaderUtils.printClassLoader"); 108 printClassLoader(Thread.currentThread().getContextClassLoader()); 109 } 110 111 114 public static void printClassLoader(ClassLoader cl) { 115 System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")"); 116 117 if (cl != null) { 118 printClassLoader(cl.getParent()); 119 } 120 } 121 } 122 | Popular Tags |