001 /** 002 * 003 * Copyright 2004 Protique Ltd 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * 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 **/ 018 package org.activemq.transport.http; 019 020 import org.activemq.broker.BrokerContainer; 021 import org.activemq.broker.impl.BrokerContainerImpl; 022 import org.activemq.transport.TransportChannelListener; 023 import org.activemq.transport.TransportServerChannelSupport; 024 025 import javax.jms.JMSException; 026 import javax.servlet.ServletException; 027 028 /** 029 * This servlet embeds an ActiveMQ broker inside a servlet engine which is 030 * ideal for deploying ActiveMQ inside a WAR and using this servlet as a HTTP tunnel. 031 * 032 * @version $Revision$ 033 */ 034 public class HttpEmbeddedTunnelServlet extends HttpTunnelServlet { 035 036 private BrokerContainer broker; 037 private ServletConnector transportConnector; 038 039 public synchronized void init() throws ServletException { 040 // lets initialize the ActiveMQ broker 041 if (broker == null) { 042 broker = createBroker(); 043 } 044 try { 045 broker.start(); 046 } 047 catch (JMSException e) { 048 throw new ServletException("Failed to start embedded broker: " + e, e); 049 } 050 // now lets register the listener 051 TransportChannelListener listener = transportConnector.getTransportChannelListener(); 052 getServletContext().setAttribute("transportChannelListener", listener); 053 super.init(); 054 } 055 056 protected static class ServletConnector extends TransportServerChannelSupport { 057 public ServletConnector(String url) { 058 super(url); 059 } 060 } 061 062 /** 063 * Factory method to create a new broker 064 */ 065 protected BrokerContainer createBroker() { 066 BrokerContainer answer = new BrokerContainerImpl(); 067 String url = getConnectorURL(); 068 transportConnector = new ServletConnector(url); 069 answer.addConnector(transportConnector); 070 071 String brokerURL = getServletContext().getInitParameter("org.activemq.brokerURL"); 072 if (brokerURL != null) { 073 log("Listening for internal communication on: " + brokerURL); 074 } 075 return answer; 076 } 077 078 protected String getConnectorURL() { 079 return "http://localhost/" + getServletContext().getServletContextName(); 080 } 081 }