001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.activemq.tool.spi; 018 019 import java.io.File; 020 import java.net.URL; 021 import java.net.URLClassLoader; 022 import java.util.ArrayList; 023 import java.util.List; 024 import java.util.Properties; 025 import java.util.StringTokenizer; 026 027 import javax.jms.ConnectionFactory; 028 029 import org.apache.commons.logging.Log; 030 import org.apache.commons.logging.LogFactory; 031 032 public abstract class ClassLoaderSPIConnectionFactory implements SPIConnectionFactory { 033 034 public static final String KEY_EXT_DIR = "extDir"; 035 private static final Log LOG = LogFactory.getLog(ClassLoaderSPIConnectionFactory.class); 036 037 public final ConnectionFactory createConnectionFactory(Properties settings) throws Exception { 038 039 // Load new context class loader 040 ClassLoader newClassLoader = getContextClassLoader(settings); 041 Thread.currentThread().setContextClassLoader(newClassLoader); 042 043 return instantiateConnectionFactory(settings); 044 } 045 046 protected ClassLoader getContextClassLoader(Properties settings) { 047 String extDir = (String)settings.remove(KEY_EXT_DIR); 048 if (extDir != null) { 049 StringTokenizer tokens = new StringTokenizer(extDir, ";,"); 050 List<URL> urls = new ArrayList<URL>(); 051 while (tokens.hasMoreTokens()) { 052 String dir = tokens.nextToken(); 053 try { 054 File f = new File(dir); 055 if (!f.exists()) { 056 LOG.warn("Cannot find extension dir: " + f.getAbsolutePath()); 057 } else { 058 LOG.info("Adding extension dir: " + f.getAbsolutePath()); 059 060 urls.add(f.toURL()); 061 062 File[] files = f.listFiles(); 063 if (files != null) { 064 for (int j = 0; j < files.length; j++) { 065 if (files[j].getName().endsWith(".zip") || files[j].getName().endsWith(".jar")) { 066 LOG.info("Adding extension dir: " + files[j].getAbsolutePath()); 067 urls.add(files[j].toURL()); 068 } 069 } 070 } 071 } 072 } catch (Exception e) { 073 LOG.warn("Failed to load ext dir: " + dir + ". Reason: " + e); 074 } 075 } 076 077 URL u[] = new URL[urls.size()]; 078 urls.toArray(u); 079 return new URLClassLoader(u, Thread.currentThread().getContextClassLoader()); 080 } 081 return ClassLoaderSPIConnectionFactory.class.getClassLoader(); 082 } 083 084 protected abstract ConnectionFactory instantiateConnectionFactory(Properties settings) throws Exception; 085 }