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