1 11 12 package org.jivesoftware.messenger.container; 13 14 import org.jivesoftware.util.Log; 15 16 import java.io.File ; 17 import java.io.FilenameFilter ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.net.URLClassLoader ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 36 class PluginClassLoader { 37 38 private URLClassLoader classLoader; 39 private final List <URL > list = new ArrayList <URL >(); 40 41 42 49 public PluginClassLoader(File pluginDir) throws SecurityException { 50 addDirectory(pluginDir); 51 } 52 53 59 public void addDirectory(File directory) { 60 try { 61 File classesDir = new File (directory, "classes"); 62 if (classesDir.exists()) { 63 list.add(classesDir.toURL()); 64 } 65 File libDir = new File (directory, "lib"); 66 File [] jars = libDir.listFiles(new FilenameFilter () { 67 public boolean accept(File dir, String name) { 68 return name.endsWith(".jar") || name.endsWith(".zip"); 69 } 70 }); 71 if (jars != null) { 72 for (int i = 0; i < jars.length; i++) { 73 if (jars[i] != null && jars[i].isFile()) { 74 list.add(jars[i].toURL()); 75 } 76 } 77 } 78 } 79 catch (MalformedURLException mue) { 80 Log.error(mue); 81 } 82 } 83 84 90 public void addURL(URL url) { 91 list.add(url); 92 } 93 94 98 public void initialize(){ 99 Iterator urls = list.iterator(); 100 URL [] urlArray = new URL [list.size()]; 101 for (int i = 0; urls.hasNext(); i++) { 102 urlArray[i] = (URL )urls.next(); 103 } 104 classLoader = new URLClassLoader (urlArray, findParentClassLoader()); 105 } 106 107 117 public Class loadClass(String name) throws ClassNotFoundException , IllegalAccessException , 118 InstantiationException , SecurityException { 119 return classLoader.loadClass(name); 120 } 121 122 125 public void destroy() { 126 classLoader = null; 127 } 128 129 134 private ClassLoader findParentClassLoader() { 135 ClassLoader parent = Thread.currentThread().getContextClassLoader(); 136 if (parent == null) { 137 parent = this.getClass().getClassLoader(); 138 } 139 if (parent == null) { 140 parent = ClassLoader.getSystemClassLoader(); 141 } 142 return parent; 143 } 144 } | Popular Tags |