Added ability to load extension dirs to the spi classes.

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@417653 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrian T. Co 2006-06-28 03:11:11 +00:00
parent ffcbd0c015
commit 6086520836
4 changed files with 92 additions and 11 deletions

View File

@ -33,7 +33,7 @@ public class JmsClientSystemProperties extends AbstractObjectProperties {
protected String samplers = SAMPLER_TP + "," + SAMPLER_CPU; // Start both samplers
protected String spiClass = "org.apache.activemq.tool.spi.ActiveMQClassLoaderSPI";
protected String spiClass = "org.apache.activemq.tool.spi.ActiveMQReflectionSPI";
protected String clientPrefix = "JmsClient";
protected int numClients = 1;
protected int totalDests = 1;

View File

@ -16,7 +16,7 @@
*/
package org.apache.activemq.tool.spi;
public class ActiveMQClassLoaderSPI extends ClassLoaderSPIConnectionFactory {
public class ActiveMQReflectionSPI extends ReflectionSPIConnectionFactory {
public String getClassName() {
return "org.apache.activemq.ActiveMQConnectionFactory";
}

View File

@ -16,22 +16,65 @@
*/
package org.apache.activemq.tool.spi;
import org.apache.activemq.tool.properties.ReflectionUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.jms.ConnectionFactory;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
public abstract class ClassLoaderSPIConnectionFactory implements SPIConnectionFactory {
public ConnectionFactory createConnectionFactory(Properties settings) throws Exception {
Class factoryClass = Class.forName(getClassName());
ConnectionFactory factory = (ConnectionFactory)factoryClass.newInstance();
configureConnectionFactory(factory, settings);
return factory;
private static final Log log = LogFactory.getLog(ClassLoaderSPIConnectionFactory.class);
public static final String KEY_EXT_DIR = "extDir";
public final ConnectionFactory createConnectionFactory(Properties settings) throws Exception {
// Load new context class loader
ClassLoader newClassLoader = getContextClassLoader(settings);
Thread.currentThread().setContextClassLoader(newClassLoader);
return instantiateConnectionFactory(newClassLoader, settings);
}
public void configureConnectionFactory(ConnectionFactory jmsFactory, Properties settings) throws Exception {
ReflectionUtil.configureClass(jmsFactory, settings);
protected ClassLoader getContextClassLoader(Properties settings) {
String extDir = (String)settings.remove(KEY_EXT_DIR);
if (extDir != null) {
StringTokenizer tokens = new StringTokenizer(extDir, ";,");
List urls = new ArrayList();
while (tokens.hasMoreTokens()) {
String dir = tokens.nextToken();
try {
File f = new File(dir);
dir = f.getAbsolutePath();
System.out.println(dir);
urls.add(f.toURL());
File[] files = f.listFiles();
if( files!=null ) {
for (int j = 0; j < files.length; j++) {
if( files[j].getName().endsWith(".zip") || files[j].getName().endsWith(".jar") ) {
dir = files[j].getAbsolutePath();
urls.add(files[j].toURL());
}
}
}
} catch (Exception e) {
log.warn("Failed to load ext dir: " + dir + ". Reason: " + e);
}
}
URL u[] = new URL[urls.size()];
urls.toArray(u);
return new URLClassLoader(u, ClassLoaderSPIConnectionFactory.class.getClassLoader());
}
return ClassLoaderSPIConnectionFactory.class.getClassLoader();
}
public abstract String getClassName();
protected abstract ConnectionFactory instantiateConnectionFactory(ClassLoader cl, Properties settings) throws Exception;
}

View File

@ -0,0 +1,38 @@
/**
*
* Copyright 2005-2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.tool.spi;
import org.apache.activemq.tool.properties.ReflectionUtil;
import javax.jms.ConnectionFactory;
import java.util.Properties;
public abstract class ReflectionSPIConnectionFactory extends ClassLoaderSPIConnectionFactory {
public ConnectionFactory instantiateConnectionFactory(ClassLoader cl, Properties settings) throws Exception {
Class factoryClass = cl.loadClass(getClassName());
ConnectionFactory factory = (ConnectionFactory)factoryClass.newInstance();
configureConnectionFactory(factory, settings);
return factory;
}
public void configureConnectionFactory(ConnectionFactory jmsFactory, Properties settings) throws Exception {
ReflectionUtil.configureClass(jmsFactory, settings);
}
public abstract String getClassName();
}