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