1 23 24 package org.enhydra.xml.xmlc.driver; 25 26 import java.io.File ; 27 import java.lang.reflect.Constructor ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.lang.reflect.Method ; 30 import java.net.URL ; 31 import java.net.URLClassLoader ; 32 33 import org.enhydra.xml.driver.TestError; 34 import org.enhydra.xml.driver.TestException; 35 import org.enhydra.xml.driver.TestFileOps; 36 37 43 public class TestClassLoaderFactory { 44 47 private static final String LOADER_ENHYDRA = "enhydra"; 48 private static final String LOADER_MULTI = "multi"; 49 private static final String LOADER_URL = "url"; 50 51 52 private Class loadClass(String className) 53 throws ClassNotFoundException { 54 return getClass().getClassLoader().loadClass(className); 55 } 56 57 58 private ClassLoader mkMultiClassLoader(URL [] classPath, 59 ClassLoader parent) 60 throws NoSuchMethodException , InvocationTargetException , 61 IllegalAccessException , InstantiationException , 62 ClassNotFoundException { 63 Class logClass = loadClass("com.lutris.logging.LogChannel"); 65 Class loaderClass 66 = loadClass("com.lutris.classloader.MultiClassLoader"); 67 68 Constructor constr 70 = loaderClass.getConstructor(new Class []{URL [].class, logClass}); 71 ClassLoader loader 72 = (ClassLoader )constr.newInstance(new Object []{classPath, null}); 73 74 Method setParent = loaderClass.getMethod("setParent", 76 new Class []{ClassLoader .class}); 77 setParent.invoke(loader, new Object []{parent}); 78 return loader; 79 } 80 81 82 private ClassLoader mkEnhydraClassLoader(URL [] classPath, 83 ClassLoader parent) 84 throws NoSuchMethodException , InvocationTargetException , 85 IllegalAccessException , InstantiationException , 86 ClassNotFoundException { 87 Class loaderClass 88 = loadClass("org.enhydra.classloader.EnhydraClassLoader"); 89 90 Constructor constr 92 = loaderClass.getConstructor(new Class []{ClassLoader .class, 93 URL [].class}); 94 return (ClassLoader )constr.newInstance(new Object []{parent, classPath}); 95 } 96 97 100 private ClassLoader mkURLClassLoader(URL [] classPath, 101 ClassLoader parent) { 102 return URLClassLoader.newInstance(classPath, parent); 103 } 104 105 108 public ClassLoader mkTestClassLoader(String classLoaderName, 109 URL [] classPath, 110 ClassLoader parent) { 111 try { 112 if (classLoaderName.equals(LOADER_MULTI)) { 113 return mkMultiClassLoader(classPath, parent); 114 } else if (classLoaderName.equals(LOADER_ENHYDRA)) { 115 return mkEnhydraClassLoader(classPath, parent); 116 } else if (classLoaderName.equals(LOADER_URL)) { 117 return mkURLClassLoader(classPath, parent); 118 } else { 119 throw new TestError("invalid classloader name \"" 120 + classLoaderName + "\""); 121 } 122 } catch (NoSuchMethodException except) { 123 throw new TestException(except); 124 } catch (InvocationTargetException except) { 125 throw new TestException(except); 126 } catch (ClassNotFoundException except) { 127 throw new TestException(except); 128 } catch (IllegalAccessException except) { 129 throw new TestException(except); 130 } catch (InstantiationException except) { 131 throw new TestException(except); 132 } 133 } 134 135 138 public ClassLoader mkTestClassLoader(URL [] classPath, 139 ClassLoader parent) { 140 return mkTestClassLoader(LOADER_URL, classPath, parent); 142 } 143 144 147 public ClassLoader mkTestClassLoader(File [] classPath, 148 ClassLoader parent) { 149 URL [] urlClassPath = new URL [classPath.length]; 150 for (int idx = 0; idx < classPath.length; idx++) { 151 urlClassPath[idx] = TestFileOps.dirToUrl(classPath[idx]); 152 } 153 154 return mkTestClassLoader(LOADER_URL, urlClassPath, parent); 156 } 157 } 158 | Popular Tags |