001    package com.mockrunner.jms;
002    
003    import java.io.Serializable;
004    import java.util.ArrayList;
005    import java.util.Collections;
006    import java.util.List;
007    
008    import javax.jms.JMSException;
009    import javax.jms.MessageProducer;
010    
011    import com.mockrunner.mock.jms.MockConnection;
012    import com.mockrunner.mock.jms.MockMessageProducer;
013    import com.mockrunner.mock.jms.MockQueueSender;
014    import com.mockrunner.mock.jms.MockSession;
015    import com.mockrunner.mock.jms.MockTopicPublisher;
016    
017    /**
018     * This class is used to create generic producers that are not
019     * associated with a destination.
020     * If you create a <code>MessageProducer</code> with a <code>null</code>
021     * destination, this class is used to create a <code>MessageProducer</code>
022     * which is not associated with any destination.
023     * If the session used to create the producer is a <code>QueueSession</code>,
024     * you'll get a <code>QueueSender</code>. For a <code>TopicSession</code>,
025     * you'll get a <code>TopicPublisher</code>. For a generic session,
026     * you'll get a generic instance of <code>MessageProducer</code>.
027     */
028    public class GenericTransmissionManager implements Serializable
029    {
030        private MockConnection connection;
031        private MockSession session;
032        private List messageProducerList;
033        
034        public GenericTransmissionManager(MockConnection connection, MockSession session)
035        {
036            this.connection = connection;
037            this.session = session;
038            messageProducerList = new ArrayList();
039        }
040    
041        /**
042         * Closes all producers.
043         */
044        public void closeAll()
045        {
046            closeAllMessageProducers();
047        }
048    
049        /**
050         * Closes all producers.
051         */
052        public void closeAllMessageProducers()
053        {
054            for(int ii = 0; ii < messageProducerList.size(); ii++)
055            {
056                MessageProducer producer = (MessageProducer)messageProducerList.get(ii);
057                try
058                {
059                    producer.close();
060                }
061                catch(JMSException exc)
062                {
063                
064                }
065            }
066        }
067        
068        /**
069         * Creates a new <code>MessageProducer</code>.
070         * @return the created <code>MessageProducer</code>
071         */
072        public MockMessageProducer createMessageProducer()
073        {
074            MockMessageProducer producer = new MockMessageProducer(connection, session, null);
075            messageProducerList.add(producer);
076            return producer;
077        }
078        
079        /**
080         * Creates a new <code>QueueSender</code>.
081         * @return the created <code>QueueSender</code>
082         */
083        public MockQueueSender createQueueSender()
084        {
085            MockQueueSender producer = new MockQueueSender(connection, session, null);
086            messageProducerList.add(producer);
087            return producer;
088        }
089        
090        /**
091         * Creates a new <code>TopicPublisher</code>.
092         * @return the created <code>TopicPublisher</code>
093         */
094        public MockTopicPublisher createTopicPublisher()
095        {
096            MockTopicPublisher producer = new MockTopicPublisher(connection, session, null);
097            messageProducerList.add(producer);
098            return producer;
099        }
100    
101        /**
102         * Returns a <code>MessageProducer</code> by its index or
103         * <code>null</code>, if no such <code>MessageProducer</code> is
104         * present.
105         * @param index the index of the <code>MessageProducer</code>
106         * @return the <code>MessageProducer</code>
107         */
108        public MockMessageProducer getMessageProducer(int index)
109        {
110            if(messageProducerList.size() <= index || index < 0) return null;
111            return (MockMessageProducer)messageProducerList.get(index);
112        }
113        
114        /**
115         * Returns the list of all <code>MessageProducer</code> objects.
116         * @return the list of <code>MessageProducer</code> objects
117         */
118        public List getMessageProducerList()
119        {
120            return Collections.unmodifiableList(messageProducerList);
121        }
122    }