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 org.osgi.framework.FrameworkUtil;
020    import org.slf4j.Logger;
021    import org.slf4j.LoggerFactory;
022    import org.springframework.web.context.WebApplicationContext;
023    import org.springframework.web.context.support.WebApplicationContextUtils;
024    import org.springframework.web.context.support.XmlWebApplicationContext;
025    
026    import javax.jms.ConnectionFactory;
027    import javax.servlet.ServletContext;
028    import javax.servlet.ServletContextEvent;
029    import javax.servlet.ServletContextListener;
030    
031    /**
032     * Starts the WebConsole.
033     * 
034     * 
035     */
036    public class WebConsoleStarter implements ServletContextListener {
037        
038        private static final Logger LOG = LoggerFactory.getLogger(WebConsoleStarter.class);
039    
040        public void contextInitialized(ServletContextEvent event) {
041            LOG.debug("Initializing ActiveMQ WebConsole...");
042    
043            ServletContext servletContext = event.getServletContext();
044            WebApplicationContext context = createWebapplicationContext(servletContext);
045    
046            initializeWebClient(servletContext, context);
047    
048            LOG.info("ActiveMQ WebConsole initialized.");
049        }
050    
051        private WebApplicationContext createWebapplicationContext(ServletContext servletContext) {
052    
053            String webconsoleType = System.getProperty("webconsole.type", "embedded");
054    
055            // detect osgi
056            try {
057                if (OsgiUtil.isOsgi()) {
058                    webconsoleType = "osgi";
059                }
060            } catch (NoClassDefFoundError ignore) {
061            }
062    
063    
064            String configuration = "/WEB-INF/webconsole-" + webconsoleType + ".xml";
065    
066            LOG.info("Web console type: " + webconsoleType);
067    
068            XmlWebApplicationContext context = new XmlWebApplicationContext();
069            context.setServletContext(servletContext);
070            context.setConfigLocations(new String[] {
071                configuration
072            });
073            context.refresh();
074            context.start();
075    
076            servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
077    
078            return context;
079        }
080    
081        private void initializeWebClient(ServletContext servletContext, WebApplicationContext context) {
082            ConnectionFactory connectionFactory = (ConnectionFactory)context.getBean("connectionFactory");
083            servletContext.setAttribute(WebClient.CONNECTION_FACTORY_ATTRIBUTE, connectionFactory);
084            WebClient.initContext(servletContext);
085        }
086    
087        public void contextDestroyed(ServletContextEvent event) {
088            XmlWebApplicationContext context = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
089            if (context != null) {
090                context.stop();
091                context.destroy();
092            }
093            // do nothing, since the context is destroyed anyway
094        }
095    
096        static class OsgiUtil {
097            static boolean isOsgi() {
098                return (FrameworkUtil.getBundle(WebConsoleStarter.class) != null);
099            }
100        }
101    
102    }