/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.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.objectweb.jonas_jms;

import javax.jms.InvalidDestinationException;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TemporaryTopic;
import javax.jms.Topic;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.jms.XATopicConnection;
import javax.jms.XATopicSession;
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 JTopicSession extends JSession implements TopicSession {

    // Underlaying Objects
    protected XATopicConnection xatc;
    protected TopicSession ts = null;
    protected XATopicSession xats = null;

    /**
     * Constructor
     */
    public JTopicSession(JConnection jconn, XATopicConnection xatc) {
	super(jconn);
	this.xatc = xatc;
    }

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

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

    protected TopicSession getMOMTopicSession() throws JMSException {
	Transaction tx = null;
	try {
	    tx = tm.getTransaction();
	} catch (SystemException e) {
	    TraceJms.logger.log(BasicLevel.ERROR,"cannot get Transaction");
	}
	if (tx == null) {
	    if (ts == null) {
		ts = xatc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
		jconn.sessionOpen(this);
	    }
	    return ts;
	} else {
	    if (xats == null) {
		xats = xatc.createXATopicSession();
		if (currtx != null) {
		    TraceJms.logger.log(BasicLevel.ERROR,"mixed transactions");
		}
		currtx = tx;
		xares = xats.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.DEBUG,"transaction rolled back");
		    throw new JMSException(e.toString());
		}
	    } 
	    return xats.getTopicSession();
	}
    }

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

    // -----------------------------------------------------------------------
    // TopicSession Implementation
    // -----------------------------------------------------------------------

    /**
     *
     */
    public Topic createTopic(String topicName) throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMTopicSession().createTopic(topicName);
    }

    /**
     *
     */
    public TopicSubscriber createSubscriber(Topic topic) 
	throws JMSException{
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMTopicSession().createSubscriber(topic);
    }
    
    /**
     *
     */
    public TopicSubscriber createSubscriber(Topic topic, 
					    String messageSelector, 
					    boolean noLocal) throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMTopicSession().createSubscriber(topic, messageSelector, noLocal);
    }

    /**
     *
     */
    public TopicSubscriber createDurableSubscriber(Topic topic, 
						   String name) throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMTopicSession().createDurableSubscriber(topic, name);
    }
 
    /**
     *
     */
    public TopicSubscriber createDurableSubscriber(Topic topic, 
						   String name, 
						   String messageSelector, 
						   boolean noLocal) throws JMSException{ 
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMTopicSession().createDurableSubscriber(topic, name, messageSelector, noLocal);
    }
   
    /**
     *
     */
    public TopicPublisher createPublisher(Topic topic) throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMTopicSession().createPublisher(topic);
    }

    /**
     *
     */
    public TemporaryTopic createTemporaryTopic() throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	return getMOMTopicSession().createTemporaryTopic();
    }
  
    /**
     *
     */
    public void unsubscribe(java.lang.String name) throws JMSException, InvalidDestinationException {
	TraceJms.logger.log(BasicLevel.DEBUG, "");
	getMOMTopicSession().unsubscribe(name);
    }
}
