/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@ow2.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.ow2.jonas.lib.jms;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TemporaryQueue;
import javax.jms.XAQueueConnection;
import javax.jms.XAQueueSession;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;

import org.objectweb.util.monolog.api.BasicLevel;

/**
 * @author Laurent Chauvirey, Frederic Maistre, Nicolas Tachker
 * Contributor(s): 
 * Philippe Durieux
 * Philippe Coq
 */
public class JQueueSession extends JSession implements QueueSession {

    // Underlaying Objects
    protected XAQueueConnection xaqc;
    protected QueueSession qs = null;
    protected XAQueueSession xaqs = null;
    
    /**
     * Constructor
     */
    public JQueueSession(JConnection jconn, XAQueueConnection xaqc) {
	super(jconn);
	this.xaqc = xaqc;
    }

    // -----------------------------------------------------------------------
    // Internal Methods
    // -----------------------------------------------------------------------

    /**
     * Get the underlaying MOM Session.
     */
    protected Session getMOMSession() throws JMSException {
	return getMOMQueueSession();
    }

    protected QueueSession getMOMQueueSession() throws JMSException {
	Transaction tx = null;
	try {
	    tx = tm.getTransaction();
	} catch (SystemException e) {
	    TraceJms.logger.log(BasicLevel.ERROR,"cannot get Transaction");
	}
	if (tx == null) {
	    if (qs == null) {
		qs = xaqc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
		jconn.sessionOpen(this);
	    }
	    return qs;
	} else {
	    if (xaqs == null) {
		xaqs = xaqc.createXAQueueSession();
		if (currtx != null) {
		    TraceJms.logger.log(BasicLevel.ERROR,"mixed transactions");
		}
		currtx = tx;
		xares = xaqs.getXAResource();
		try {
		    tx.enlistResource(this.getXAResource());
            txover = false;
		} catch (SystemException e) {
		    TraceJms.logger.log(BasicLevel.ERROR,"cannot enlist session:"+e);
		    throw new JMSException(e.toString());
		} catch (RollbackException e) {
		    TraceJms.logger.log(BasicLevel.ERROR,"transaction rolled back");
		    throw new JMSException(e.toString());
		}
	    } 
	    return xaqs.getQueueSession();
	}
    }

    protected void MOMSessionClose() {
	try {
	    if (xaqs != null) {
		xaqs.close();
		xaqs = null;
	    }
	    if (qs != null) {
		qs.close();
		qs = null;
		jconn.sessionClose(this);
	    }
	} catch (JMSException e) {
	    TraceJms.logger.log(BasicLevel.ERROR,"exception:"+e);
	}
    }

    // -----------------------------------------------------------------------
    // QueueSession Implementation
    // -----------------------------------------------------------------------

    /**
     *
     */
    public QueueBrowser createBrowser(Queue queue) throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMQueueSession().createBrowser(queue);
    }

    /**
     *
     */
    public QueueBrowser createBrowser(Queue queue, String messageSelector) throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMQueueSession().createBrowser(queue, messageSelector);
    }

    /**
     *
     */
    public Queue createQueue(String queueName) throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMQueueSession().createQueue(queueName);
    }

    /**
     *
     */
    public QueueReceiver createReceiver(Queue queue) throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMQueueSession().createReceiver(queue);
    }

    /**
     *
     */
    public QueueReceiver createReceiver(Queue queue, String messageSelector) throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMQueueSession().createReceiver(queue, messageSelector);
    }
    
    /**
     *
     */
    public QueueSender createSender(Queue queue) throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMQueueSession().createSender(queue);
    }

    /**
     *
     */
    public TemporaryQueue createTemporaryQueue() throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMQueueSession().createTemporaryQueue();
    }


}
