/**
 *
 * 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.gbean;

import javax.jms.JMSException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
import org.apache.geronimo.gbean.GBeanLifecycle;
import org.apache.geronimo.gbean.GConstructorInfo;
import org.activemq.ActiveMQConnection;
import org.activemq.ActiveMQConnectionFactory;
import org.activemq.broker.BrokerConnector;
import org.activemq.broker.impl.BrokerConnectorImpl;
import org.activemq.io.WireFormat;
import org.activemq.io.impl.DefaultWireFormat;

/**
 * Default implementation of the ActiveMQ connector
 *
 * @version $Revision: 1.1.1.1 $
 */
public class ActiveMQConnectorGBean implements GBeanLifecycle {

    private Log log = LogFactory.getLog(getClass().getName());

    private BrokerConnector brokerConnector;
    private ActiveMQContainer container;
    private WireFormat wireFormat = new DefaultWireFormat();
    private String url = ActiveMQConnection.DEFAULT_URL;

    public ActiveMQConnectorGBean(ActiveMQContainer container) {
        this.container = container;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public WireFormat getWireFormat() {
        return wireFormat;
    }

    public void setWireFormat(WireFormat wireFormat) {
        this.wireFormat = wireFormat;
    }

    public synchronized void doStart() throws Exception {
    	ClassLoader old = Thread.currentThread().getContextClassLoader();
    	Thread.currentThread().setContextClassLoader(ActiveMQContainerGBean.class.getClassLoader());
    	try {
	        if (brokerConnector == null) {
	            brokerConnector = createBrokerConnector();
	            brokerConnector.start();
	            ActiveMQConnectionFactory.registerBroker(url, brokerConnector);
	        }
    	} finally {
        	Thread.currentThread().setContextClassLoader(old);
    	}
    }

    public synchronized void doStop() throws Exception {
        if (brokerConnector != null) {
            ActiveMQConnectionFactory.unregisterBroker(url);
            BrokerConnector temp = brokerConnector;
            brokerConnector = null;
            temp.stop();
        }
    }

    public synchronized void doFail() {
        if (brokerConnector != null) {
            BrokerConnector temp = brokerConnector;
            brokerConnector = null;
            try {
                temp.stop();
            }
            catch (JMSException e) {
                log.info("Caught while closing due to failure: " + e, e);
            }
        }
    }

    protected BrokerConnector createBrokerConnector() throws Exception {
        return new BrokerConnectorImpl(container.getBrokerContainer(), url, wireFormat);
    }

    public static final GBeanInfo GBEAN_INFO;

    static {
        GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("ActiveMQ Message Broker Connector", ActiveMQConnectorGBean.class, "JMSConnector");
        infoFactory.addAttribute("url", String.class.getName(), true);
        infoFactory.addAttribute("wireFormat", WireFormat.class.getName(), false);
        infoFactory.addReference("activeMQContainer", ActiveMQContainer.class);
        infoFactory.setConstructor(new GConstructorInfo(new String[]{"activeMQContainer"}));
        GBEAN_INFO = infoFactory.getBeanInfo();
    }

    public static GBeanInfo getGBeanInfo() {
        return GBEAN_INFO;
    }
}