001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     **/
018    package org.activemq;
019    
020    import org.activemq.broker.BrokerContainerFactory;
021    import org.activemq.util.JMSExceptionHelper;
022    
023    import javax.jms.JMSException;
024    import java.lang.reflect.Method;
025    
026    /**
027     * Helper methods to avoid a runtime dependency on Spring unless its
028     * used to configure the ActiveMQ broker via the XML configuration
029     *
030     * @version $Revision: 1.1.1.1 $
031     */
032    public class XmlConfigHelper {
033        private static final Class[] ARGUMENT_TYPES = {String.class};
034        public static final String SPRING_CLASS_NAME = "org.activemq.spring.SpringBrokerContainerFactory";
035    
036        /**
037         * Creates an instance of the broker factory which uses the Spring XML configuration file
038         * mechanism.
039         */
040        public static BrokerContainerFactory createBrokerContainerFactory(String xmlConfig) throws JMSException {
041            try {
042                Class factoryClass = getSpringFactoryClass();
043                Method method = factoryClass.getMethod("newFactory", ARGUMENT_TYPES);
044                if (method == null) {
045                    throw new JMSException("Could not find newFactory() method - classpath strangeness occurred");
046                }
047                return (BrokerContainerFactory) method.invoke(null, new Object[]{xmlConfig});
048            }
049            catch (JMSException e) {
050                throw e;
051            }
052            catch (ClassNotFoundException e) {
053                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);
054            }
055            catch (Exception e) {
056                throw JMSExceptionHelper.newJMSException("Could not configure broker using XML configuration file as attempt to use Spring factory failed. Reason: " + e, e);
057            }
058        }
059    
060        private static Class getSpringFactoryClass() throws ClassNotFoundException {
061            Class answer = null;
062            try {
063                answer = Thread.currentThread().getContextClassLoader().loadClass(SPRING_CLASS_NAME);
064            }
065            catch (ClassNotFoundException e) {
066                answer = XmlConfigHelper.class.getClassLoader().loadClass(SPRING_CLASS_NAME);
067            }
068            return answer;
069        }
070    }