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    import javax.servlet.ServletException;
021    
022    import org.apache.activemq.broker.BrokerService;
023    import org.apache.activemq.transport.TransportAcceptListener;
024    
025    /**
026     * This servlet embeds an ActiveMQ broker inside a servlet engine which is ideal
027     * for deploying ActiveMQ inside a WAR and using this servlet as a HTTP tunnel.
028     * 
029     * 
030     */
031    public class HttpEmbeddedTunnelServlet extends HttpTunnelServlet {
032        private static final long serialVersionUID = -3705734740251302361L;
033    
034        protected BrokerService broker;
035        protected HttpTransportServer transportConnector;
036    
037        public synchronized void init() throws ServletException {
038            // lets initialize the ActiveMQ broker
039            try {
040                if (broker == null) {
041                    broker = createBroker();
042    
043                    // Add the servlet connector
044                    String url = getConnectorURL();
045                    HttpTransportFactory factory = new HttpTransportFactory();
046                    transportConnector = (HttpTransportServer) factory.doBind(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    }