/*
 * 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.JMSSecurityException;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.XAQueueConnectionFactory;

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

/**
 * JQueueConnectionFactory wraps a XAQueueConnectionFactory.
 *
 * @author Laurent Chauvirey, Frederic Maistre, Nicolas Tachker
 * Contributor(s):
 * Philippe Durieux
 * Jeff Mesnil
 * Philippe Coq
 */
public class JQueueConnectionFactory extends JConnectionFactory implements QueueConnectionFactory {

    private XAQueueConnectionFactory xaqcf;

    /**
     * Constructor.
     * The underlaying XAQueueConnectionFactory is found in the JmsManager.
     */
    public JQueueConnectionFactory(String name) {
        this.name = name;
        jms = JmsManagerImpl.getJmsManager();
        xacf = jms.getXAQueueConnectionFactory();
        xaqcf = (XAQueueConnectionFactory) xacf;
    }

    // -----------------------------------------------------------------------
    // QueueConnectionFactory implementation
    // -----------------------------------------------------------------------

    /**
     * Create a queue connection for an anonymous user.
     *
     * @return a newly created queue connection.
     * @throws JMSException - if JMS Provider fails to create Queue Connection
     * due to some internal error. required resources for a Queue Connection.
     * @throws JMSSecurityException - if client authentication fails due to
     * invalid user name or password.
     */
    public QueueConnection createQueueConnection() throws JMSException {
    TraceJms.logger.log(BasicLevel.DEBUG,"");
    JQueueConnection qc = (JQueueConnection) getJConnection();
        if (qc == null) {
            qc = new JQueueConnection(this, xaqcf);
        }
        return (QueueConnection) qc;
    }

    /**
     * Create a queue connection with specified user identity.
     * The connection is created in stopped mode. No messages will
     * be delivered until Connection.start method is explicitly called.
     *
     * @param userName - the caller's user name
     * @param password - the caller's password
     *
     * @throws JMSException - if JMS Provider fails to create Queue Connection
     * due to some internal error. required resources for a Queue Connection.
     * @throws JMSSecurityException - if client authentication fails due to
     * invalid user name or password.
     */
    public QueueConnection createQueueConnection(String userName, String password) throws JMSException {
    TraceJms.logger.log(BasicLevel.DEBUG,"");
    JQueueConnection qc = (JQueueConnection) getJConnection(userName);
    if (qc == null) {
        qc = new JQueueConnection(this, xaqcf, userName, password);
    }
    return (QueueConnection) qc;
    }


}
