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.jms.ConnectionFactory;
020    import javax.servlet.ServletContext;
021    import javax.servlet.ServletContextEvent;
022    import javax.servlet.ServletContextListener;
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.springframework.web.context.WebApplicationContext;
027    import org.springframework.web.context.support.WebApplicationContextUtils;
028    import org.springframework.web.context.support.XmlWebApplicationContext;
029    
030    /**
031     * Starts the WebConsole.
032     * 
033     * @version $Revision: 1.1 $
034     */
035    public class WebConsoleStarter implements ServletContextListener {
036        
037        private static final Log LOG = LogFactory.getLog(WebConsoleStarter.class);
038    
039        public void contextInitialized(ServletContextEvent event) {
040            LOG.debug("Initializing ActiveMQ WebConsole...");
041    
042            ServletContext servletContext = event.getServletContext();
043            WebApplicationContext context = createWebapplicationContext(servletContext);
044    
045            initializeWebClient(servletContext, context);
046    
047            LOG.info("ActiveMQ WebConsole initialized.");
048        }
049    
050        private WebApplicationContext createWebapplicationContext(ServletContext servletContext) {
051            String webconsoleType = System.getProperty("webconsole.type", "embedded");
052            String configuration = "/WEB-INF/webconsole-" + webconsoleType + ".xml";
053    
054            XmlWebApplicationContext context = new XmlWebApplicationContext();
055            context.setServletContext(servletContext);
056            context.setConfigLocations(new String[] {
057                configuration
058            });
059            context.refresh();
060            context.start();
061    
062            servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
063    
064            return context;
065        }
066    
067        private void initializeWebClient(ServletContext servletContext, WebApplicationContext context) {
068            ConnectionFactory connectionFactory = (ConnectionFactory)context.getBean("connectionFactory");
069            servletContext.setAttribute(WebClient.CONNECTION_FACTORY_ATTRIBUTE, connectionFactory);
070        }
071    
072        public void contextDestroyed(ServletContextEvent event) {
073            XmlWebApplicationContext context = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
074            if (context != null) {
075                context.stop();
076                context.destroy();
077            }
078            // do nothing, since the context is destoyed anyway
079        }
080    
081    }