/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.activemq.web;

import org.activemq.broker.BrokerContainer;
import org.activemq.spring.SpringBrokerContainerFactory;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.ServletContextResource;

import javax.jms.JMSException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * This servlet when initialized will create an ActiveMQ message broker
 *
 * @version $Revision: 1.1 $
 */
public class SpringBrokerServlet extends HttpServlet {
    private BrokerContainer brokerContainer;

    public void init() throws ServletException {
        log("Creating ActiveMQ Broker");
        brokerContainer = createBroker(getServletContext());

        log("Starting ActiveMQ Broker");
        try {
            brokerContainer.start();

            log("Started ActiveMQ Broker");
        }
        catch (JMSException e) {
            throw new ServletException("Failed to start ActiveMQ broker: " + e, e);
        }
    }

    public void destroy() {
        if (brokerContainer != null) {
            try {
                brokerContainer.stop();
            }
            catch (JMSException e) {
                log("Failed to stop the ActiveMQ Broker: " + e, e);
            }
        }
        super.destroy();
    }

    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        getServletContext().log("Attempt to call service method on SpringBrokerServlet as [" +
                request.getRequestURI() + "] was ignored");
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }

    public String getServletInfo() {
        return "SpringBrokerServlet for deploying the ActiveMQ Broker in a servlet engine";
    }


    /**
     * Factory method to create a new ActiveMQ Broker
     */
    protected BrokerContainer createBroker(ServletContext context) {
        String brokerURI = context.getInitParameter("brokerURI");
        if (brokerURI == null) {
            brokerURI = "activemq.xml";
        }

        log("Loading ActiveMQ Broker configuration from: " + brokerURI);

        Resource resource = new ServletContextResource(context, brokerURI);
        return SpringBrokerContainerFactory.newInstance(resource);
    }

}
