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.web;
019
020 import org.activemq.broker.BrokerContainer;
021 import org.activemq.spring.SpringBrokerContainerFactory;
022 import org.springframework.core.io.Resource;
023 import org.springframework.web.context.support.ServletContextResource;
024
025 import javax.jms.JMSException;
026 import javax.servlet.ServletContext;
027 import javax.servlet.ServletException;
028 import javax.servlet.http.HttpServlet;
029 import javax.servlet.http.HttpServletRequest;
030 import javax.servlet.http.HttpServletResponse;
031 import java.io.IOException;
032
033 /**
034 * This servlet when initialized will create an ActiveMQ message broker
035 *
036 * @version $Revision: 1.1 $
037 */
038 public class SpringBrokerServlet extends HttpServlet {
039 private BrokerContainer brokerContainer;
040
041 public void init() throws ServletException {
042 log("Creating ActiveMQ Broker");
043 brokerContainer = createBroker(getServletContext());
044
045 log("Starting ActiveMQ Broker");
046 try {
047 brokerContainer.start();
048
049 log("Started ActiveMQ Broker");
050 }
051 catch (JMSException e) {
052 throw new ServletException("Failed to start ActiveMQ broker: " + e, e);
053 }
054 }
055
056 public void destroy() {
057 if (brokerContainer != null) {
058 try {
059 brokerContainer.stop();
060 }
061 catch (JMSException e) {
062 log("Failed to stop the ActiveMQ Broker: " + e, e);
063 }
064 }
065 super.destroy();
066 }
067
068 public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
069 getServletContext().log("Attempt to call service method on SpringBrokerServlet as [" +
070 request.getRequestURI() + "] was ignored");
071 response.sendError(HttpServletResponse.SC_BAD_REQUEST);
072 }
073
074 public String getServletInfo() {
075 return "SpringBrokerServlet for deploying the ActiveMQ Broker in a servlet engine";
076 }
077
078
079 /**
080 * Factory method to create a new ActiveMQ Broker
081 */
082 protected BrokerContainer createBroker(ServletContext context) {
083 String brokerURI = context.getInitParameter("brokerURI");
084 if (brokerURI == null) {
085 brokerURI = "activemq.xml";
086 }
087
088 log("Loading ActiveMQ Broker configuration from: " + brokerURI);
089
090 Resource resource = new ServletContextResource(context, brokerURI);
091 return SpringBrokerContainerFactory.newInstance(resource);
092 }
093
094 }