/*
 * 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.JMSException;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.XATopicConnectionFactory;

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

/**
 * JTopicConnectionFactory wraps a XATopicConnectionFactory.
 *
 * @author Laurent Chauvirey, Frederic Maistre, Nicolas Tachker
 * Contributor(s): 
 * Philippe Durieux
 * Jeff Mesnil connection anonymous
 *
 */
public class JTopicConnectionFactory extends JConnectionFactory implements TopicConnectionFactory {
    
    private XATopicConnectionFactory xatcf;

    /**
     * Constructor.
     * The underlaying XATopicConnectionFactory is found in the JmsManager.
     */
    public JTopicConnectionFactory(String name) {
        this.name = name;
        jms = JmsManagerImpl.getJmsManager();
        xacf = jms.getXATopicConnectionFactory();
        xatcf = (XATopicConnectionFactory) xacf;
    }

    // -----------------------------------------------------------------------
    // TopicConnectionFactory implementation
    // -----------------------------------------------------------------------

    /**
     * Create a topic connection for an anonymous user.
     *
     * @return a newly created topic connection.
     * @throws JMSException - if JMS Provider fails to create a Topic 
     * Connection due to some internal error.
     * @throws JMSSecurityException - if client authentication fails due to 
     * invalid user name or password.
     */
    public TopicConnection createTopicConnection() throws JMSException {
        // Try to reuse a connection from the pool.
        // Create a new one if no connection available.
	TraceJms.logger.log(BasicLevel.DEBUG,"");
        JTopicConnection tc = (JTopicConnection) getJConnection();
        if (tc == null) {
            tc = new JTopicConnection(this, xatcf);
        }
        return (TopicConnection) tc;
    }

    /**
     * Create a topic 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 Topic Connection 
     * due to some internal error. required resources for a Topic Connection.
     * @throws JMSSecurityException - if client authentication fails due to 
     * invalid user name or password.
     */
    public TopicConnection createTopicConnection(String userName, String password) 
        throws JMSException {
        TraceJms.logger.log(BasicLevel.DEBUG,"");
        // Try to reuse a connection from the pool.
        // Create a new one if no connection available.
        JTopicConnection tc = (JTopicConnection) getJConnection(userName);
        if (tc == null) {
            tc = new JTopicConnection(this, xatcf, userName, password);
        }
        return (TopicConnection) tc;
    }

} 
