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 LOG.info("ActiveMQ Jolokia REST API available at http://{}:{}/api/jolokia/", host, port); 056 } 057 } 058 059 LOG.debug("ActiveMQ WebConsole initialized."); 060 } 061 062 private WebApplicationContext createWebapplicationContext(ServletContext servletContext, String webconsoleType) { 063 String configuration = "/WEB-INF/webconsole-" + webconsoleType + ".xml"; 064 LOG.debug("Web console type: " + webconsoleType); 065 066 XmlWebApplicationContext context = new XmlWebApplicationContext(); 067 context.setServletContext(servletContext); 068 context.setConfigLocations(new String[] { 069 configuration 070 }); 071 context.refresh(); 072 context.start(); 073 074 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context); 075 076 return context; 077 } 078 079 private void initializeWebClient(ServletContext servletContext, WebApplicationContext context) { 080 ConnectionFactory connectionFactory = (ConnectionFactory)context.getBean("connectionFactory"); 081 servletContext.setAttribute(WebClient.CONNECTION_FACTORY_ATTRIBUTE, connectionFactory); 082 WebClient.initContext(servletContext); 083 } 084 085 public void contextDestroyed(ServletContextEvent event) { 086 XmlWebApplicationContext context = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()); 087 if (context != null) { 088 context.stop(); 089 context.destroy(); 090 } 091 // do nothing, since the context is destroyed anyway 092 } 093 094 private static String getWebconsoleType() { 095 String webconsoleType = System.getProperty("webconsole.type", "embedded"); 096 097 // detect osgi 098 try { 099 if (OsgiUtil.isOsgi()) { 100 webconsoleType = "osgi"; 101 } 102 } catch (NoClassDefFoundError ignore) { 103 } 104 105 return webconsoleType; 106 } 107 108 static class OsgiUtil { 109 static boolean isOsgi() { 110 return (FrameworkUtil.getBundle(WebConsoleStarter.class) != null); 111 } 112 } 113 114}