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.transport.http; 018 019 import java.net.URI; 020 021 import javax.servlet.ServletException; 022 023 import org.apache.activemq.broker.BrokerService; 024 import org.apache.activemq.transport.TransportAcceptListener; 025 026 /** 027 * This servlet embeds an ActiveMQ broker inside a servlet engine which is ideal 028 * for deploying ActiveMQ inside a WAR and using this servlet as a HTTP tunnel. 029 * 030 * @version $Revision$ 031 */ 032 public class HttpEmbeddedTunnelServlet extends HttpTunnelServlet { 033 private static final long serialVersionUID = -3705734740251302361L; 034 035 protected BrokerService broker; 036 protected HttpTransportServer transportConnector; 037 038 public synchronized void init() throws ServletException { 039 // lets initialize the ActiveMQ broker 040 try { 041 if (broker == null) { 042 broker = createBroker(); 043 044 // Add the servlet connector 045 String url = getConnectorURL(); 046 transportConnector = new HttpTransportServer(new URI(url)); 047 broker.addConnector(transportConnector); 048 049 String brokerURL = getServletContext().getInitParameter("org.apache.activemq.brokerURL"); 050 if (brokerURL != null) { 051 log("Listening for internal communication on: " + brokerURL); 052 } 053 } 054 broker.start(); 055 } catch (Exception e) { 056 throw new ServletException("Failed to start embedded broker: " + e, e); 057 } 058 // now lets register the listener 059 TransportAcceptListener listener = transportConnector.getAcceptListener(); 060 getServletContext().setAttribute("transportChannelListener", listener); 061 super.init(); 062 } 063 064 /** 065 * Factory method to create a new broker 066 * 067 * @throws Exception 068 */ 069 protected BrokerService createBroker() throws Exception { 070 BrokerService answer = new BrokerService(); 071 return answer; 072 } 073 074 protected String getConnectorURL() { 075 return "http://localhost/" + getServletContext().getServletContextName(); 076 } 077 }