/*
 * 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.ConnectionConsumer;
import javax.jms.JMSException;
import javax.jms.ServerSessionPool;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;
import javax.jms.XAConnection;
import javax.jms.XATopicConnection;
import javax.jms.XATopicConnectionFactory;

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

/*
 *
 * @author Laurent Chauvirey, Frederic Maistre, Nicolas Tachker
 * Contributor(s): 
 * Philippe Durieux
 * Jeff Mesnil connection anonymous
 *
 */

public class JTopicConnection extends JConnection implements TopicConnection {

    // The XATopicConnection used in the MOM
    protected XATopicConnection xatc = null;


    /**
     * Constructor of a JTopicConnection for a specified user.
     * 
     * @param user user's name
     * @param passwd user's password 
     */
    public JTopicConnection(JConnectionFactory jcf, XATopicConnectionFactory xatcf, String user, String passwd) throws JMSException {
	super(jcf, user);
	// Create the underlaying XATopicConnection
	xatc = xatcf.createXATopicConnection(user, passwd);
	this.xac = (XAConnection) xatc;
    }

    /**
     * Constructor of a JQueueConnection for an anonymous user.
     */
    public JTopicConnection(JConnectionFactory jcf, XATopicConnectionFactory xatcf) throws JMSException {
        super(jcf, INTERNAL_USER_NAME);
        // Create the underlying XATopicConnection
        xatc = xatcf.createXATopicConnection();
        this.xac = (XAConnection) xatc;
    }

    // -----------------------------------------------------------------------
    // TopicConnection implementation
    // -----------------------------------------------------------------------

    /**
     * Create a TopicSession
     * @param transacted - if true, the session is transacted.
     * @param acknowledgeMode - indicates whether the consumer or the client will 
     * acknowledge any messages it receives.
     * This parameter will be ignored if the session is transacted.
     * @return a newly created topic session.
     * @throws JMSException - if JMS Connection fails to create a session.
     */
    public TopicSession createTopicSession(boolean transacted, int acknowledgeMode) throws JMSException {
	TraceJms.logger.log(BasicLevel.DEBUG,  "");
	return new JTopicSession(this, xatc);
    }

    /**
     * @throws JMSException - if JMS Connection fails to create a ConnectionConsumer
     */
    public ConnectionConsumer createConnectionConsumer(Topic topic,
						       String messageSelector, 
						       ServerSessionPool sessionPool, 
						       int maxMessages) throws JMSException  {
	TraceJms.logger.log(BasicLevel.DEBUG,  "");
	return xatc.createConnectionConsumer(topic, messageSelector, sessionPool, maxMessages);
    }
}
