1 18 package org.apache.activemq.tool.spi; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import javax.jms.ConnectionFactory ; 24 import java.util.Properties ; 25 import java.util.StringTokenizer ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.io.File ; 29 import java.net.URL ; 30 import java.net.URLClassLoader ; 31 32 public abstract class ClassLoaderSPIConnectionFactory implements SPIConnectionFactory { 33 private static final Log log = LogFactory.getLog(ClassLoaderSPIConnectionFactory.class); 34 35 public static final String KEY_EXT_DIR = "extDir"; 36 37 public final ConnectionFactory createConnectionFactory(Properties settings) throws Exception { 38 39 ClassLoader newClassLoader = getContextClassLoader(settings); 41 Thread.currentThread().setContextClassLoader(newClassLoader); 42 43 return instantiateConnectionFactory(settings); 44 } 45 46 protected ClassLoader getContextClassLoader(Properties settings) { 47 String extDir = (String )settings.remove(KEY_EXT_DIR); 48 if (extDir != null) { 49 StringTokenizer tokens = new StringTokenizer (extDir, ";,"); 50 List urls = new ArrayList (); 51 while (tokens.hasMoreTokens()) { 52 String dir = tokens.nextToken(); 53 try { 54 File f = new File (dir); 55 if (!f.exists()) { 56 log.warn("Cannot find extension dir: " + f.getAbsolutePath()); 57 } else { 58 log.info("Adding extension dir: " + f.getAbsolutePath()); 59 60 urls.add(f.toURL()); 61 62 File [] files = f.listFiles(); 63 if( files!=null ) { 64 for (int j = 0; j < files.length; j++) { 65 if( files[j].getName().endsWith(".zip") || files[j].getName().endsWith(".jar") ) { 66 log.info("Adding extension dir: " + files[j].getAbsolutePath()); 67 urls.add(files[j].toURL()); 68 } 69 } 70 } 71 } 72 } catch (Exception e) { 73 log.warn("Failed to load ext dir: " + dir + ". Reason: " + e); 74 } 75 } 76 77 URL u[] = new URL [urls.size()]; 78 urls.toArray(u); 79 return new URLClassLoader (u, Thread.currentThread().getContextClassLoader()); 80 } 81 return ClassLoaderSPIConnectionFactory.class.getClassLoader(); 82 } 83 84 protected abstract ConnectionFactory instantiateConnectionFactory(Properties settings) throws Exception ; 85 } 86 | Popular Tags |