/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.codehaus.activemq.pool;

import org.apache.commons.pool.ObjectPool;
import org.codehaus.activemq.util.JMSExceptionHelper;

import javax.jms.*;

/**
 * Represents a proxy {@link Connection} which is-a
 * {@link TopicConnection} and {@link QueueConnection} which is pooled and on {@link #close()}
 * will return itself to the pool.
 * 
 * @version $Revision: 1.1 $
 */
public class PooledConnection implements TopicConnection, QueueConnection {

    private TopicConnection topicConnection;
    private QueueConnection queueConnection;
    private ObjectPool pool;

    public PooledConnection(TopicConnection topicConnection, ObjectPool pool) {
        this.topicConnection = topicConnection;
        this.pool = pool;
        this.queueConnection = (QueueConnection) topicConnection;
    }

    public void close() throws JMSException {
        // lets add ourselves back into the pool
        TopicConnection tmp = topicConnection;
        topicConnection = null;
        queueConnection = null;
        try {
            pool.returnObject(tmp);
        }
        catch (Exception e) {
            throw JMSExceptionHelper.newJMSException(e);
        }
    }

    public void start() throws JMSException {
        // TODO should we start connections first before pooling them?
        getTopicConnection().start();
    }

    public void stop() throws JMSException {
        //getTopicConnection().stop();
    }


    public ConnectionConsumer createConnectionConsumer(Destination destination, String s, ServerSessionPool serverSessionPool, int i) throws JMSException {
        return getTopicConnection().createConnectionConsumer(destination, s, serverSessionPool, i);
    }

    public ConnectionConsumer createConnectionConsumer(Topic topic, String s, ServerSessionPool serverSessionPool, int i) throws JMSException {
        return getTopicConnection().createConnectionConsumer(topic, s, serverSessionPool, i);
    }

    public ConnectionConsumer createDurableConnectionConsumer(Topic topic, String s, String s1, ServerSessionPool serverSessionPool, int i) throws JMSException {
        return getTopicConnection().createDurableConnectionConsumer(topic, s, s1, serverSessionPool, i);
    }

    public Session createSession(boolean b, int i) throws JMSException {
        return getTopicConnection().createSession(b, i);
    }

    public TopicSession createTopicSession(boolean b, int i) throws JMSException {
        return getTopicConnection().createTopicSession(b, i);
    }

    public String getClientID() throws JMSException {
        return getTopicConnection().getClientID();
    }

    public ExceptionListener getExceptionListener() throws JMSException {
        return getTopicConnection().getExceptionListener();
    }

    public ConnectionMetaData getMetaData() throws JMSException {
        return getTopicConnection().getMetaData();
    }

    public void setExceptionListener(ExceptionListener exceptionListener) throws JMSException {
        getTopicConnection().setExceptionListener(exceptionListener);
    }

    public void setClientID(String s) throws JMSException {
        getTopicConnection().setClientID(s);
    }

    public QueueSession createQueueSession(boolean b, int i) throws JMSException {
        return getQueueConnection().createQueueSession(b, i);
    }

    public ConnectionConsumer createConnectionConsumer(Queue queue, String s, ServerSessionPool serverSessionPool, int i) throws JMSException {
        return getQueueConnection().createConnectionConsumer(queue, s, serverSessionPool, i);
    }

    protected TopicConnection getTopicConnection() throws JMSException {
        if (topicConnection == null) {
            throw createAlreadyClosedException();
        }
        return topicConnection;
    }

    protected QueueConnection getQueueConnection() throws JMSException {
        if (queueConnection == null) {
            throw createAlreadyClosedException();
        }
        return queueConnection;
    }

    protected JMSException createAlreadyClosedException() {
        return new JMSException("This connection has already been closed");
    }
}
