/*
 * 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.ConnectionConsumer;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.ServerSessionPool;
import javax.jms.XAConnection;
import javax.jms.XAQueueConnection;
import javax.jms.XAQueueConnectionFactory;

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

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

public class JQueueConnection extends JConnection implements QueueConnection {

    // The XAQueueConnection used in the MOM
    protected XAQueueConnection xaqc = null;


    /**
     * Constructor of a JQueueConnection for a specified user.
     * 
     * @param user user's name
     * @param passwd user's password 
     */
    public JQueueConnection(JConnectionFactory jcf, 
                            XAQueueConnectionFactory xaqcf, 
                            String user, String passwd) throws JMSException {
        super(jcf, user);

        // Create the underlaying XAQueueConnection
        xaqc = xaqcf.createXAQueueConnection(user, passwd);
        this.xac = (XAConnection) xaqc;
    }

    /**
     * Constructor of a JQueueConnection for an anonymous user.
     */
    public JQueueConnection(JConnectionFactory jcf, XAQueueConnectionFactory xaqcf) 
        throws JMSException {

        super(jcf, INTERNAL_USER_NAME);
        xaqc = xaqcf.createXAQueueConnection();
        this.xac = (XAConnection) xaqc;
    }

    // -----------------------------------------------------------------------
    // QueueConnection implementation
    // -----------------------------------------------------------------------

    /**
     * Create a connection consumer for this connection 
     * @param queue - the queue to access
     * @param selector - only messages with properties matching the message
     * selector expression aredelivered
     * @param sessionPool - the server session pool to associate with this connection consumer.
     * @param maxMessages - the maximum number of messages that can be assigned
     * to a server session at one time.
     * @return the connection consumer.
     * @throws JMSException - if JMS Connection fails to create a a connection consumer
     * due to some internal error or invalid arguments for sessionPool and message selector.
     * @throws InvalidSelectorException - if the message selector is invalid.
     */
    public ConnectionConsumer createConnectionConsumer(Queue queue, 
                                                       String selector, 
                                                       ServerSessionPool pool, 
                                                       int maxmessages)	throws JMSException {
        TraceJms.logger.log(BasicLevel.DEBUG,  "");
        return xaqc.createConnectionConsumer(queue, selector, pool, maxmessages);
    }

    /**
     * Create a Queue Session
     * @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 queue session.
     * @throws JMSException - if JMS Connection fails to create a session.
     */
    public QueueSession createQueueSession(boolean transacted, int acknowledgeMode) throws JMSException {
        TraceJms.logger.log(BasicLevel.DEBUG, "");
        return new JQueueSession(this, xaqc);
    }
}
