1 /*** 2 * 3 * Copyright 2004 Protique Ltd 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 **/ 18 package org.codehaus.activemq; 19 20 import org.codehaus.activemq.broker.BrokerContainerFactory; 21 import org.codehaus.activemq.util.JMSExceptionHelper; 22 23 import javax.jms.JMSException; 24 import java.lang.reflect.Method; 25 26 /*** 27 * Helper methods to avoid a runtime dependency on Spring unless its 28 * used to configure the ActiveMQ broker via the XML configuration 29 * 30 * @version $Revision: 1.1 $ 31 */ 32 public class XmlConfigHelper { 33 private static final Class[] ARGUMENT_TYPES = {String.class}; 34 public static final String SPRING_CLASS_NAME = "org.codehaus.activemq.spring.SpringBrokerContainerFactory"; 35 36 /*** 37 * Creates an instance of the broker factory which uses the Spring XML configuration file 38 * mechanism. 39 */ 40 public static BrokerContainerFactory createBrokerContainerFactory(String xmlConfig) throws JMSException { 41 try { 42 Class factoryClass = getSpringFactoryClass(); 43 Method method = factoryClass.getMethod("newFactory", ARGUMENT_TYPES); 44 if (method == null) { 45 throw new JMSException("Could not find newFactory() method - classpath strangeness occurred"); 46 } 47 return (BrokerContainerFactory) method.invoke(null, new Object[]{xmlConfig}); 48 } 49 catch (JMSException e) { 50 throw e; 51 } 52 catch (ClassNotFoundException e) { 53 throw JMSExceptionHelper.newJMSException("Could not configure broker using XML configuration file as Spring factory class could not be loaded. Maybe you need the Spring.jar on your classpath? Reason: " + e, e); 54 } 55 catch (Exception e) { 56 throw JMSExceptionHelper.newJMSException("Could not configure broker using XML configuration file as attempt to use Spring factory failed. Reason: " + e, e); 57 } 58 } 59 60 private static Class getSpringFactoryClass() throws ClassNotFoundException { 61 Class answer = null; 62 try { 63 answer = Thread.currentThread().getContextClassLoader().loadClass(SPRING_CLASS_NAME); 64 } 65 catch (ClassNotFoundException e) { 66 answer = XmlConfigHelper.class.getClassLoader().loadClass(SPRING_CLASS_NAME); 67 } 68 return answer; 69 } 70 }