1 3 package jodd.util; 4 5 import java.net.URL ; 6 import java.net.URLClassLoader ; 7 import java.io.InputStream ; 8 import java.io.IOException ; 9 import java.io.File ; 10 import java.util.ArrayList ; 11 import java.util.List ; 12 import java.lang.reflect.InvocationTargetException ; 13 14 public class ClassLoaderUtil { 15 16 18 22 public static void addClassPath(String path) { 23 addClassPath(path, (URLClassLoader ) ClassLoader.getSystemClassLoader()); 24 } 25 26 31 public static void addClassPath(String path, URLClassLoader classLoader) { 32 try { 33 ReflectUtil.invokeDeclared(URLClassLoader .class, classLoader, "addURL", 34 new Class []{URL .class}, 35 new Object []{new File (path).toURL()}); 36 } catch (Throwable th) { 37 throw new RuntimeException ("Unable to add class path'" + path + "'.", th); 38 } 39 } 40 41 42 44 47 public static Class defineClass(String className, byte[] classData) { 48 return defineClass(className, classData, ClassLoader.getSystemClassLoader()); 49 50 } 51 52 55 public static Class defineClass(String className, byte[] classData, ClassLoader classLoader) { 56 try { 57 return (Class ) ReflectUtil.invokeDeclared(ClassLoader .class, classLoader, "defineClass", 58 new Class []{String .class, byte[].class, int.class, int.class}, 59 new Object []{className, classData, new Integer (0), new Integer (classData.length)}); 60 61 } catch (Throwable th) { 62 throw new RuntimeException ("Unable to define class '" + className + "'.", th); 63 } 64 } 65 66 67 69 70 74 public static URL [] getFullClassPath(Class clazz) { 75 return getFullClassPath(clazz.getClassLoader()); 76 } 77 78 81 public static URL [] getFullClassPath(ClassLoader classLoader) { 82 List list = new ArrayList (); 83 while (classLoader != null) { 84 if (classLoader instanceof URLClassLoader ) { 85 URL [] urls = ((URLClassLoader ) 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 [] result = new URL [list.size()]; 94 for (int i = 0; i < result.length; i++) { 95 result[i] = (URL ) list.get(i); 96 } 97 return result; 98 } 99 100 102 public static URL getResource(String resourceName) { 103 return getResource(resourceName, ClassLoaderUtil.class); 104 } 105 106 public static URL getResource(String resourceName, Class callingClass) { 107 URL 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 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 125 public static InputStream getResourceAsStream(String resourceName) throws IOException { 126 return getResourceAsStream(resourceName, ClassLoaderUtil.class); 127 } 128 129 130 public static InputStream getResourceAsStream(String resourceName, Class callingClass) throws IOException { 131 URL url = getResource(resourceName, callingClass); 132 if (url != null) { 133 return url.openStream(); 134 } 135 return null; 136 } 137 138 140 143 public static Class loadClass(String className) throws ClassNotFoundException { 144 return loadClass(className, ClassLoaderUtil.class); 145 } 146 147 150 public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException { 151 try { 152 return Thread.currentThread().getContextClassLoader().loadClass(className); 153 } catch (ClassNotFoundException cnfex1) { 154 try { 155 return Class.forName(className); 156 } catch (ClassNotFoundException cnfex2) { 157 try { 158 return (jodd.util.ClassLoaderUtil.class).getClassLoader().loadClass(className); 159 } catch (ClassNotFoundException cnfex3) { 160 if (callingClass == null) { 161 throw cnfex3; 162 } 163 return callingClass.getClassLoader().loadClass(className); 164 } 165 } 166 } 167 } 168 169 } 170 | Popular Tags |