/*
 * 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 java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.Referenceable;
import org.objectweb.jonas_jms.api.JmsManager;

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

public class JConnectionFactory implements Referenceable, Serializable {
    protected JmsManager jms;
    private String name;
    private LinkedList connectionpool = new LinkedList();

    /**
     * Constructor.
     */
    public JConnectionFactory(String name) {
	this.name = name;
	jms = JmsManagerImpl.getJmsManager();
    }

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

    /**
     * Free a Connection and return it to the pool
     */
    public void freeJConnection(JConnection con) {
	TraceJms.debug( "JConnectionFactory: freeJConnection");
	synchronized(connectionpool) {
	    connectionpool.addLast(con);
	}
    }


    /**
     * Get a Connection from the pool for an anonymous user
     *
     * @return a Connection for an anonymous user
     */
    public JConnection getJConnection() {
        return getJConnection(JConnection.INTERNAL_USER_NAME);
    }

    /**
     * Get a Connection from the pool for the specified user
     * 
     * @param user User wanting a connection
     * @return Connection from the pool for the specified user
     */
    public JConnection getJConnection(String user) {
	TraceJms.debug( "JConnectionFactory: getJConnection");
	JConnection con = null;
	synchronized(connectionpool) {
	    Iterator i = connectionpool.iterator();
	    while (i.hasNext()) {
		con = (JConnection) i.next();
		if (con.getUser().equals(user)) {
		    i.remove();
		    break;
		}
	    }
	}
	return con;
    }

    // -----------------------------------------------------------------------
    // Referenceable implementation
    // -----------------------------------------------------------------------

    public Reference getReference() throws NamingException {
	TraceJms.debug( "JConnectionFactory: getReference");
	return new Reference(getClass().getName(), "org.objectweb.jonas_jms.JObjectFactory", null);
    }
}
