All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mockrunner.jms.QueueTransmissionManager Maven / Gradle / Ivy

There is a newer version: 2.0.7
Show newest version
package com.mockrunner.jms;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.jms.JMSException;
import javax.jms.QueueBrowser;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;

import com.mockrunner.mock.jms.MockConnection;
import com.mockrunner.mock.jms.MockQueue;
import com.mockrunner.mock.jms.MockQueueBrowser;
import com.mockrunner.mock.jms.MockQueueReceiver;
import com.mockrunner.mock.jms.MockQueueSender;
import com.mockrunner.mock.jms.MockSession;

/**
 * This class is used to create queue senders and receivers.
 * It can be also used to access all created classes in tests.
 */
public class QueueTransmissionManager implements Serializable
{
    private MockConnection connection;
    private MockSession session;
    private List queueSenderList;
    private List queueReceiverList;
    private List queueBrowserList;
    
    public QueueTransmissionManager(MockConnection connection, MockSession session)
    {
        this.connection = connection;
        this.session = session;
        queueSenderList = new ArrayList();
        queueReceiverList = new ArrayList();
        queueBrowserList = new ArrayList();
    }

    /**
     * Closes all senders, receivers, browsers, publishers and subscribers.
     */
    public void closeAll()
    {
        closeAllQueueSenders();
        closeAllQueueReceivers();
        closeAllQueueBrowsers();
    }

    /**
     * Closes all queue senders.
     */
    public void closeAllQueueSenders()
    {
        for(int ii = 0; ii < queueSenderList.size(); ii++)
        {
            QueueSender sender = (QueueSender)queueSenderList.get(ii);
            try
            {
                sender.close();
            }
            catch(JMSException exc)
            {
            
            }
        }
    }

    /**
     * Closes all queue receivers.
     */
    public void closeAllQueueReceivers()
    {
        for(int ii = 0; ii < queueReceiverList.size(); ii++)
        {
            QueueReceiver receiver = (QueueReceiver)queueReceiverList.get(ii);
            try
            {
                receiver.close();
            }
            catch(JMSException exc)
            {
    
            }
        }
    }

    /**
     * Closes all queue browsers.
     */
    public void closeAllQueueBrowsers()
    {
        for(int ii = 0; ii < queueBrowserList.size(); ii++)
        {
            QueueBrowser browser = (QueueBrowser)queueBrowserList.get(ii);
            try
            {
                browser.close();
            }
            catch(JMSException exc)
            {

            }
        }
    }
    
    /**
     * Creates a new QueueSender for the specified
     * Queue. Usually this method is called
     * by {@link com.mockrunner.mock.jms.MockQueueSession#createSender}.
     * @param queue the Queue
     * @return the created QueueSender
     */
    public MockQueueSender createQueueSender(MockQueue queue)
    {
        MockQueueSender sender = new MockQueueSender(connection, session, queue);
        queueSenderList.add(sender);
        return sender;
    }

    /**
     * Returns a QueueSender by its index or
     * null, if no such QueueSender is
     * present.
     * @param index the index of the QueueSender
     * @return the QueueSender
     */
    public MockQueueSender getQueueSender(int index)
    {
        if(queueSenderList.size() <= index || index < 0) return null;
        return (MockQueueSender)queueSenderList.get(index);
    }
    
    /**
     * Returns a QueueSender by the name of its
     * corresponding Queue. If there's more than
     * one QueueSender object for the specified name,
     * the first one will be returned.
     * @param queueName the name of the Queue
     * @return the QueueSender
     */
    public MockQueueSender getQueueSender(String queueName)
    {
        List senders = getQueueSenderList(queueName);
        if(senders.size() <= 0) return null;
        return (MockQueueSender)senders.get(0);
    }
    
    /**
     * Returns the list of the QueueSender objects
     * for a specific Queue.
     * @param queueName the name of the Queue
     * @return the list of QueueSender objects
     */
    public List getQueueSenderList(String queueName)
    {
        List resultList = new ArrayList();
        for(int ii = 0; ii < queueSenderList.size(); ii++)
        {
            QueueSender sender = (QueueSender)queueSenderList.get(ii);
            try
            {
                if(sender.getQueue().getQueueName().equals(queueName))
                {
                    resultList.add(sender);
                }
            }
            catch(JMSException exc)
            {
            
            }
        }
        return Collections.unmodifiableList(resultList);
    }
    
    /**
     * Returns the list of all QueueSender objects.
     * @return the list of QueueSender objects
     */
    public List getQueueSenderList()
    {
        return Collections.unmodifiableList(queueSenderList);
    }

    /**
     * Creates a new QueueReceiver for the specified
     * Queue. Usually this method is called
     * by {@link com.mockrunner.mock.jms.MockQueueSession#createReceiver}.
     * @param queue the Queue
     * @param messageSelector the message selector
     * @return the created QueueReceiver
     */
    public MockQueueReceiver createQueueReceiver(MockQueue queue, String messageSelector)
    {
        MockQueueReceiver receiver = new MockQueueReceiver(connection, session, queue, messageSelector);
        queueReceiverList.add(receiver);
        return receiver;
    }

    /**
     * Returns a QueueReceiver by its index or
     * null, if no such QueueReceiver is
     * present.
     * @param index the index of the QueueReceiver
     * @return the QueueReceiver
     */
    public MockQueueReceiver getQueueReceiver(int index)
    {
        if(queueReceiverList.size() <= index || index < 0) return null;
        return (MockQueueReceiver)queueReceiverList.get(index);
    }
    
    /**
     * Returns a QueueReceiver by the name of its
     * corresponding Queue. If there's more than
     * one QueueReceiver object for the specified name,
     * the first one will be returned.
     * @param queueName the name of the Queue
     * @return the QueueReceiver
     */
    public MockQueueReceiver getQueueReceiver(String queueName)
    {
        List receivers = getQueueReceiverList(queueName);
        if(receivers.size() <= 0) return null;
        return (MockQueueReceiver)receivers.get(0);
    }

    /**
     * Returns the list of the QueueReceiver objects
     * for a specific Queue.
     * @param queueName the name of the Queue
     * @return the list of QueueReceiver objects
     */
    public List getQueueReceiverList(String queueName)
    {
        List resultList = new ArrayList();
        for(int ii = 0; ii < queueReceiverList.size(); ii++)
        {
            QueueReceiver receiver = (QueueReceiver)queueReceiverList.get(ii);
            try
            {
                if(receiver.getQueue().getQueueName().equals(queueName))
                {
                    resultList.add(receiver);
                }
            }
            catch(JMSException exc)
            {
            
            }
        }
        return Collections.unmodifiableList(resultList);
    }
    
    /**
     * Returns the list of QueueReceiver objects.
     * @return the QueueReceiver list
     */
    public List getQueueReceiverList()
    {
        return Collections.unmodifiableList(queueReceiverList);
    }

    /**
     * Creates a new QueueBrowser for the specified
     * Queue. Usually this method is called
     * by {@link com.mockrunner.mock.jms.MockQueueSession#createBrowser}.
     * @param queue the Queue
     * @param messageSelector the message selector
     * @return the created QueueBrowser
     */
    public MockQueueBrowser createQueueBrowser(MockQueue queue, String messageSelector)
    {
        MockQueueBrowser browser = new MockQueueBrowser(connection, queue, messageSelector);
        queueBrowserList.add(browser);
        return browser;
    }

    /**
     * Returns a QueueBrowser by its index or
     * null, if no such QueueBrowser is
     * present.
     * @param index the index of the QueueBrowser
     * @return the QueueBrowser
     */
    public MockQueueBrowser getQueueBrowser(int index)
    {
        if(queueBrowserList.size() <= index || index < 0) return null;
        return (MockQueueBrowser)queueBrowserList.get(index);
    }
    
    /**
     * Returns a QueueBrowser by the name of its
     * corresponding Queue. If there's more than
     * one QueueBrowser object for the specified name,
     * the first one will be returned.
     * @param queueName the name of the Queue
     * @return the QueueBrowser
     */
    public MockQueueBrowser getQueueBrowser(String queueName)
    {
        List browsers = getQueueBrowserList(queueName);
        if(browsers.size() <= 0) return null;
        return (MockQueueBrowser)browsers.get(0);
    }

    /**
     * Returns the list of the QueueBrowser objects
     * for a specific Queue.
     * @param queueName the name of the Queue
     * @return the list of QueueBrowser objects
     */
    public List getQueueBrowserList(String queueName)
    {
        List resultList = new ArrayList();
        for(int ii = 0; ii < queueBrowserList.size(); ii++)
        {
            QueueBrowser browser = (QueueBrowser)queueBrowserList.get(ii);
            try
            {
                if(browser.getQueue().getQueueName().equals(queueName))
                {
                    resultList.add(browser);
                }
            }
            catch(JMSException exc)
            {
            
            }
        }
        return Collections.unmodifiableList(resultList);
    }
    
    /**
     * Returns the list of QueueBrowser objects.
     * @return the QueueBrowser list
     */
    public List getQueueBrowserList()
    {
        return Collections.unmodifiableList(queueBrowserList);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy