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

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.jms.MessageListener;
import javax.resource.spi.ActivationSpec;
import javax.resource.spi.BootstrapContext;
import javax.resource.spi.ResourceAdapter;
import javax.resource.spi.endpoint.MessageEndpointFactory;
import javax.transaction.TransactionManager;

/**
 * A Lightweight Message Driven Object Container which is a
 * lightweight alternative to MDBs.
 *
 * @version $Revision: 1.1.1.1 $
 */
public class JCAConnector implements InitializingBean, DisposableBean {
    private ActivationSpec activationSpec;
    private BootstrapContext bootstrapContext;
    private MessageEndpointFactory endpointFactory;
    private ResourceAdapter resourceAdapter;
    private MessageListener messageListener;
    private TransactionManager transactionManager;

    public JCAConnector() {
    }

    public JCAConnector(BootstrapContext bootstrapContext, ResourceAdapter resourceAdapter) {
        this.bootstrapContext = bootstrapContext;
        this.resourceAdapter = resourceAdapter;
    }

    public void afterPropertiesSet() throws Exception {
        if (activationSpec == null) {
            throw new IllegalArgumentException("activationSpec must be set");
        }

        ResourceAdapter temp = activationSpec.getResourceAdapter();
        if (temp == null && resourceAdapter != null) {
            activationSpec.setResourceAdapter(resourceAdapter);
        }
        else if (resourceAdapter == null) {
            resourceAdapter = activationSpec.getResourceAdapter();
            if (resourceAdapter == null) {
                throw new IllegalArgumentException("resourceAdapter property must be set on the activationSpec object");
            }
        }
        if (bootstrapContext == null) {
            throw new IllegalArgumentException("bootstrapContext must be set");
        }
        if (endpointFactory == null) {
            if (messageListener == null) {
                throw new IllegalArgumentException("either the endpointFactory or messageListener properties must be set");
            }
            if (transactionManager != null) {
                endpointFactory = new DefaultEndpointFactory(messageListener, transactionManager);
            }
            else {
                // TODO should we have some way of finding a ManagedConnection or other local transaction hook?
                endpointFactory = new DefaultEndpointFactory(messageListener);
            }
        }
        resourceAdapter.endpointActivation(endpointFactory, activationSpec);
    }

    public void destroy() throws Exception {
        if (resourceAdapter != null && activationSpec != null) {
            resourceAdapter.endpointDeactivation(endpointFactory, activationSpec);
        }
    }

    // Properties
    //-------------------------------------------------------------------------
    public ActivationSpec getActivationSpec() {
        return activationSpec;
    }

    public void setActivationSpec(ActivationSpec activationSpec) {
        this.activationSpec = activationSpec;
    }

    public MessageEndpointFactory getEndpointFactory() {
        return endpointFactory;
    }

    public void setEndpointFactory(MessageEndpointFactory endpointFactory) {
        this.endpointFactory = endpointFactory;
    }

    public BootstrapContext getBootstrapContext() {
        return bootstrapContext;
    }

    public void setBootstrapContext(BootstrapContext bootstrapContext) {
        this.bootstrapContext = bootstrapContext;
    }

    public ResourceAdapter getResourceAdapter() {
        return resourceAdapter;
    }

    public void setResourceAdapter(ResourceAdapter resourceAdapter) {
        this.resourceAdapter = resourceAdapter;
    }

    public MessageListener getMessageListener() {
        return messageListener;
    }

    public void setMessageListener(MessageListener messageListener) {
        this.messageListener = messageListener;
    }

    public TransactionManager getTransactionManager() {
        return transactionManager;
    }

    public void setTransactionManager(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }
}
