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.web;
019    
020    import javax.servlet.ServletContext;
021    import javax.servlet.ServletContextListener;
022    import javax.servlet.ServletContextEvent;
023    import javax.jms.JMSException;
024    
025    import org.activemq.spring.SpringBrokerContainerFactory;
026    import org.activemq.broker.BrokerContainer;
027    import org.springframework.core.io.Resource;
028    import org.springframework.web.context.support.ServletContextResource;
029    
030    /**
031     * Used to configure and instance of ActiveMQ <tt>BrokerContainer</tt> using
032     * ActiveMQ/Spring's xml configuration.
033     * <p/>
034     * The configuration file is specified via the context init parameter <tt>brokerURI</tt>,
035     * typically:
036     * <code>
037     * &lt;context-param&gt;
038     * &lt;param-name&gt;brokerURI&lt;/param-name&gt;
039     * &lt;param-value&gt;/WEB-INF/activemq.xml&lt;/param-value&gt;
040     * &lt;/context-param&gt;
041     * </code>
042     *
043     * As a a default, if a <tt>brokerURI</tt> is not specified it will look up for <tt>activemq.xml</tt>
044     *
045     * @version $Revision: 1.1 $
046     */
047    public class SpringBrokerContextListener implements ServletContextListener {
048    
049            /** broker uri context parameter name: <tt>brokerURI</tt> */
050            public static final String INIT_PARAM_BROKER_URI = "brokerURI";
051    
052        /** the broker container instance */
053        private BrokerContainer brokerContainer;
054    
055        /**
056         * Set the broker container to be used by this listener
057         * @param container the container to be used.
058         */
059        protected void setBrokerContainer(BrokerContainer container){
060            this.brokerContainer = container;
061        }
062    
063        /**
064         * Return the broker container.
065         */
066        protected BrokerContainer getBrokerContainer(){
067            return this.brokerContainer;
068        }
069    
070        public void contextInitialized(ServletContextEvent event) {
071            ServletContext context = event.getServletContext();
072            context.log("Creating ActiveMQ Broker...");
073            brokerContainer = createBroker(context);
074    
075            context.log("Starting ActiveMQ Broker");
076            try {
077                brokerContainer.start();
078    
079                context.log("Started ActiveMQ Broker");
080            } catch (JMSException e) {
081                context.log("Failed to start ActiveMQ broker: " + e, e);
082            }
083        }
084    
085        public void contextDestroyed(ServletContextEvent event) {
086            ServletContext context = event.getServletContext();
087            if (brokerContainer != null) {
088                try {
089                    brokerContainer.stop();
090                } catch (JMSException e) {
091                   context.log("Failed to stop the ActiveMQ Broker: " + e, e);
092                }
093                brokerContainer = null;
094            }
095        }
096    
097        /**
098         * Factory method to create a new ActiveMQ Broker
099         */
100        protected BrokerContainer createBroker(ServletContext context) {
101            String brokerURI = context.getInitParameter(INIT_PARAM_BROKER_URI);
102            if (brokerURI == null) {
103                brokerURI = "activemq.xml";
104            }
105            context.log("Loading ActiveMQ Broker configuration from: " + brokerURI);
106            Resource resource = new ServletContextResource(context, brokerURI);
107            return SpringBrokerContainerFactory.newInstance(resource);
108        }
109    }
110