/*
 * Created on May 9, 2004
 * 
 * TODO To change the template for this generated file go to Window -
 * Preferences - Java - Code Generation - Code and Comments
 */
package org.codehaus.activemq.test;
import javax.jms.Connection;
import javax.jms.ConnectionConsumer;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ServerSessionPool;
import javax.jms.Session;
import javax.jms.DeliveryMode;
import javax.jms.Topic;

import org.codehaus.activemq.ActiveMQConnectionFactory;
/**
 * @version $Revision: 1.1 $
 */
public class JmsResourceProvider {
	
	private String serverUri = "vm://localhost";
	private boolean transacted = false;
	private int ackMode = Session.AUTO_ACKNOWLEDGE;
	private boolean isTopic;
    private int deliveryMode;
    private String durableName = "DummyName";
    private String clientID = getClass().getName();


	/**
	 * @see org.codehaus.activemq.test.JmsResourceProvider#createConnectionFactory()
	 */
	public ConnectionFactory createConnectionFactory() throws JMSException {
		return new ActiveMQConnectionFactory(serverUri);
	}
	
	/**
	 * @see org.codehaus.activemq.test.JmsResourceProvider#createConnection(javax.jms.ConnectionFactory)
	 */
	public Connection createConnection(ConnectionFactory cf) throws JMSException {
        Connection connection = cf.createConnection();
        if (isDurableSubscriber()) {
            connection.setClientID(getClientID());
        }
        return connection;
	}
	/**
	 * @see org.codehaus.activemq.test.JmsResourceProvider#createSession(javax.jms.Connection)
	 */
	public Session createSession(Connection conn) throws JMSException {
		return conn.createSession(transacted, ackMode);
	}
	/**
	 * @see org.codehaus.activemq.test.JmsResourceProvider#createConsumer(javax.jms.Session,
	 *      javax.jms.Destination)
	 */
	public MessageConsumer createConsumer(Session session,
			Destination destination) throws JMSException {
        if (isDurableSubscriber()) {
            return session.createDurableSubscriber((Topic) destination, durableName);
        }
		return session.createConsumer(destination);
	}

	/**
	 * @param ssp
	 * @return
	 */
	public ConnectionConsumer createConnectionConsumer(Connection connection, Destination destination, ServerSessionPool ssp) throws JMSException {
		return connection.createConnectionConsumer(destination,null,ssp,1);
	}

	/**
	 * @see org.codehaus.activemq.test.JmsResourceProvider#createProducer(javax.jms.Session,
	 *      javax.jms.Destination)
	 */
	public MessageProducer createProducer(Session session,
			Destination destination) throws JMSException {
        MessageProducer producer = session.createProducer(destination);
        if (deliveryMode != 0) {
            producer.setDeliveryMode(deliveryMode);
        }
        return producer;
	}
	/**
	 * @see org.codehaus.activemq.test.JmsResourceProvider#createDestination(javax.jms.Session,
	 *      java.lang.String)
	 */
	public Destination createDestination(Session session, String name)
			throws JMSException {
		if( isTopic )
			return session.createTopic("TOPIC."+name);
		else 
			return session.createQueue("QUEUE."+name);
	}

    public boolean isDurableSubscriber() {
        return isTopic && deliveryMode == DeliveryMode.PERSISTENT;
    }

	/**
	 * @return Returns the ackMode.
	 */
	public int getAckMode() {
		return ackMode;
	}
	/**
	 * @param ackMode The ackMode to set.
	 */
	public void setAckMode(int ackMode) {
		this.ackMode = ackMode;
	}
	/**
	 * @return Returns the isTopic.
	 */
	public boolean isTopic() {
		return isTopic;
	}
	/**
	 * @param isTopic The isTopic to set.
	 */
	public void setTopic(boolean isTopic) {
		this.isTopic = isTopic;
	}
	/**
	 * @return Returns the serverUri.
	 */
	public String getServerUri() {
		return serverUri;
	}
	/**
	 * @param serverUri The serverUri to set.
	 */
	public void setServerUri(String serverUri) {
		this.serverUri = serverUri;
	}
	/**
	 * @return Returns the transacted.
	 */
	public boolean isTransacted() {
		return transacted;
	}
	/**
	 * @param transacted The transacted to set.
	 */
	public void setTransacted(boolean transacted) {
		this.transacted = transacted;
	}

    public int getDeliveryMode() {
        return deliveryMode;
    }

    public void setDeliveryMode(int deliveryMode) {
        this.deliveryMode = deliveryMode;
    }

    public String getClientID() {
        return clientID;
    }

    public void setClientID(String clientID) {
        this.clientID = clientID;
    }

    public String getDurableName() {
        return durableName;
    }

    public void setDurableName(String durableName) {
        this.durableName = durableName;
    }
}