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 javax.jms.JMSException; 021 import javax.servlet.ServletException; 022 023 import org.activemq.broker.BrokerContainer; 024 import org.activemq.broker.BrokerContext; 025 import org.activemq.spring.SpringBrokerContainerFactory; 026 import org.activemq.transport.TransportChannelListener; 027 import org.activemq.transport.TransportServerChannelSupport; 028 import org.activemq.util.IdGenerator; 029 import org.springframework.core.io.ClassPathResource; 030 031 /** 032 * This servlet embeds an ActiveMQ broker inside a servlet engine which is 033 * ideal for deploying ActiveMQ inside a WAR and using this servlet as a HTTP tunnel. 034 * 035 * @version $Revision$ 036 */ 037 public class HttpSpringEmbeddedTunnelServlet extends HttpTunnelServlet { 038 039 private static final long serialVersionUID = 8943695476238731241L; 040 private BrokerContainer broker; 041 private ServletConnector transportConnector; 042 043 public synchronized void init() throws ServletException { 044 // lets initialize the ActiveMQ broker 045 if (broker == null) { 046 broker = createBroker(); 047 } 048 try { 049 broker.start(); 050 } 051 catch (JMSException e) { 052 throw new ServletException("Failed to start embedded broker: " + e, e); 053 } 054 // now lets register the listener 055 TransportChannelListener listener = transportConnector.getTransportChannelListener(); 056 getServletContext().setAttribute("transportChannelListener", listener); 057 super.init(); 058 } 059 060 protected static class ServletConnector extends TransportServerChannelSupport { 061 public ServletConnector(String url) { 062 super(url); 063 } 064 } 065 066 /** 067 * Factory method to create a new broker 068 */ 069 protected BrokerContainer createBroker() { 070 071 String configFile = getServletContext().getInitParameter("org.activemq.config.file"); 072 if (configFile == null) { 073 configFile="activemq.xml"; 074 } 075 076 SpringBrokerContainerFactory factory = new SpringBrokerContainerFactory(); 077 System.out.println("Loading Message Broker from "+configFile+" on the CLASSPATH"); 078 factory.setResource(new ClassPathResource(configFile)); 079 080 IdGenerator idgen = new IdGenerator(); 081 BrokerContainer answer = factory.createBrokerContainer(idgen.generateId(), BrokerContext.getInstance()); 082 083 // Add the http connector to the configuration. 084 transportConnector = new ServletConnector(getConnectorURL()); 085 answer.addConnector(transportConnector); 086 087 return answer; 088 } 089 090 protected String getConnectorURL() { 091 return "http://localhost/" + getServletContext().getServletContextName(); 092 } 093 }