/**
 * 
 * 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 java.lang.reflect.Method;

import javax.jms.MessageListener;
import javax.resource.spi.LocalTransaction;
import javax.resource.spi.UnavailableException;
import javax.resource.spi.endpoint.MessageEndpoint;
import javax.resource.spi.endpoint.MessageEndpointFactory;
import javax.transaction.TransactionManager;
import javax.transaction.xa.XAResource;

import org.springframework.beans.factory.InitializingBean;

/**
 * A factory of {@link MessageEndpoint} instances, either using XA transactions,
 * Local (JMS) transactions or regular JMS message acknowledgements.
 * <p/>
 * To use XA you must set the transactionManager property via the
 * {@link #setTransactionManager(javax.transaction.TransactionManager)}
 * method. 
 * 
 * To use a local JMS transaction, then the XAResouce object passed in the {@see #createEndpoint(XAResource)}
 * call must implement {@see javax.resource.spi.LocalTransaction}.
 *
 * @version $Revision: 1.1.1.1 $
 */
public class DefaultEndpointFactory implements MessageEndpointFactory, InitializingBean {
    private MessageListener messageListener;
    private TransactionManager transactionManager;

    public DefaultEndpointFactory() {
    }

    public DefaultEndpointFactory(MessageListener messageListener) {
        this.messageListener = messageListener;
    }

    public DefaultEndpointFactory(MessageListener messageListener, TransactionManager transactionManager) {
        this.messageListener = messageListener;
        this.transactionManager = transactionManager;
    }

    public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException {
        if (transactionManager != null) {
            return new XAEndpoint(messageListener, xaResource, transactionManager);
        }
        else if (xaResource instanceof LocalTransaction) {
            return new LocalTransactionEndpoint(messageListener, (LocalTransaction)xaResource);
        }
        return new AcknowledgeEndpoint(messageListener);
    }

    public boolean isDeliveryTransacted(Method method) throws NoSuchMethodException {
        return transactionManager != null;
    }

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

    // Properties
    //-------------------------------------------------------------------------
    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;
    }
}
