/** 
 * 
 * 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.codehaus.activemq.ra;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.ActiveMQSession;
import org.codehaus.activemq.message.ActiveMQMessage;

import javax.jms.JMSException;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.resource.ResourceException;
import javax.resource.spi.endpoint.MessageEndpoint;
import javax.resource.spi.work.Work;

/**
 * @version $Revision: 1.1 $
 */
public class InboundEndpointWork implements SessionAndProducer, Work {

    private static final Log log = LogFactory.getLog(InboundEndpointWork.class);

    private final ActiveMQSession session;
    private final MessageEndpoint endpoint;
    private final CircularQueue workers;
    private MessageProducer messageProducer;
    private ActiveMQMessage message;


    /**
     * @param session
     * @param endpoint
     * @param workers
     * @throws javax.jms.JMSException
     */
    public InboundEndpointWork(ActiveMQSession session, MessageEndpoint endpoint, CircularQueue workers) throws JMSException {
        this.session = session;
        this.endpoint = endpoint;
        this.workers = workers;
        session.setMessageListener((MessageListener) endpoint);
    }

    public Session getSession() {
        return session;
    }

    public MessageProducer getMessageProducer() throws JMSException {
        if (messageProducer == null) {
            messageProducer = getSession().createProducer(null);
        }
        return messageProducer;
    }

    public ActiveMQMessage getMessage() {
        return message;
    }

    public void setMessage(ActiveMQMessage message) {
        this.message = message;
    }

    public void release() {
    }

    /**
     * @see java.lang.Runnable#run()
     */
    public void run() {
        try {

            SessionAndProducerHelper.register(this);
            endpoint.beforeDelivery(ActiveMQBaseEndpointWorker.ON_MESSAGE_METHOD);
            try {
                session.dispatch(message);
                session.run();
            }
            finally {
                endpoint.afterDelivery();
                SessionAndProducerHelper.unregister(this);
            }

        }
        catch (NoSuchMethodException e) {
            log.info("worker: ", e);
        }
        catch (ResourceException e) {
            log.info("worker: ", e);
        }
        finally {
            workers.returnObject(this);
        }
    }

}