1 20 21 package org.jivesoftware.whack.container; 22 23 import java.io.File ; 24 import java.io.FilenameFilter ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.net.URLClassLoader ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 44 class ComponentClassLoader { 45 46 private URLClassLoader classLoader; 47 48 57 public ComponentClassLoader(File componentDir) throws MalformedURLException , SecurityException { 58 final List list = new ArrayList (); 59 File classesDir = new File (componentDir, "classes"); 60 if (classesDir.exists()) { 61 list.add(classesDir.toURL()); 62 } 63 File libDir = new File (componentDir, "lib"); 64 File [] jars = libDir.listFiles(new FilenameFilter () { 65 public boolean accept(File dir, String name) { 66 return name.endsWith(".jar") || name.endsWith(".zip"); 67 } 68 }); 69 if (jars != null) { 70 for (int i = 0; i < jars.length; i++) { 71 if (jars[i] != null && jars[i].isFile()) { 72 list.add(jars[i].toURL()); 73 } 74 } 75 } 76 Iterator urls = list.iterator(); 77 URL [] urlArray = new URL [list.size()]; 78 for (int i = 0; urls.hasNext(); i++) { 79 urlArray[i] = (URL )urls.next(); 80 } 81 classLoader = new URLClassLoader (urlArray, findParentClassLoader()); 82 } 83 84 94 public Class loadClass(String name) throws ClassNotFoundException , IllegalAccessException , 95 InstantiationException , SecurityException 96 { 97 return classLoader.loadClass(name); 98 } 99 100 103 public void destroy() { 104 classLoader = null; 105 } 106 107 112 private ClassLoader findParentClassLoader() { 113 ClassLoader parent = Thread.currentThread().getContextClassLoader(); 114 if (parent == null) { 115 parent = this.getClass().getClassLoader(); 116 } 117 if (parent == null) { 118 parent = ClassLoader.getSystemClassLoader(); 119 } 120 return parent; 121 } 122 } | Popular Tags |