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.QueueBrowser;
010    import javax.jms.QueueReceiver;
011    import javax.jms.QueueSender;
012    
013    import com.mockrunner.mock.jms.MockConnection;
014    import com.mockrunner.mock.jms.MockQueue;
015    import com.mockrunner.mock.jms.MockQueueBrowser;
016    import com.mockrunner.mock.jms.MockQueueReceiver;
017    import com.mockrunner.mock.jms.MockQueueSender;
018    import com.mockrunner.mock.jms.MockSession;
019    
020    /**
021     * This class is used to create queue senders and receivers.
022     * It can be also used to access all created classes in tests.
023     */
024    public class QueueTransmissionManager implements Serializable
025    {
026        private MockConnection connection;
027        private MockSession session;
028        private List queueSenderList;
029        private List queueReceiverList;
030        private List queueBrowserList;
031        
032        public QueueTransmissionManager(MockConnection connection, MockSession session)
033        {
034            this.connection = connection;
035            this.session = session;
036            queueSenderList = new ArrayList();
037            queueReceiverList = new ArrayList();
038            queueBrowserList = new ArrayList();
039        }
040    
041        /**
042         * Closes all senders, receivers, browsers, publishers and subscribers.
043         */
044        public void closeAll()
045        {
046            closeAllQueueSenders();
047            closeAllQueueReceivers();
048            closeAllQueueBrowsers();
049        }
050    
051        /**
052         * Closes all queue senders.
053         */
054        public void closeAllQueueSenders()
055        {
056            for(int ii = 0; ii < queueSenderList.size(); ii++)
057            {
058                QueueSender sender = (QueueSender)queueSenderList.get(ii);
059                try
060                {
061                    sender.close();
062                }
063                catch(JMSException exc)
064                {
065                
066                }
067            }
068        }
069    
070        /**
071         * Closes all queue receivers.
072         */
073        public void closeAllQueueReceivers()
074        {
075            for(int ii = 0; ii < queueReceiverList.size(); ii++)
076            {
077                QueueReceiver receiver = (QueueReceiver)queueReceiverList.get(ii);
078                try
079                {
080                    receiver.close();
081                }
082                catch(JMSException exc)
083                {
084        
085                }
086            }
087        }
088    
089        /**
090         * Closes all queue browsers.
091         */
092        public void closeAllQueueBrowsers()
093        {
094            for(int ii = 0; ii < queueBrowserList.size(); ii++)
095            {
096                QueueBrowser browser = (QueueBrowser)queueBrowserList.get(ii);
097                try
098                {
099                    browser.close();
100                }
101                catch(JMSException exc)
102                {
103    
104                }
105            }
106        }
107        
108        /**
109         * Creates a new <code>QueueSender</code> for the specified
110         * <code>Queue</code>. Usually this method is called
111         * by {@link com.mockrunner.mock.jms.MockQueueSession#createSender}.
112         * @param queue the <code>Queue</code>
113         * @return the created <code>QueueSender</code>
114         */
115        public MockQueueSender createQueueSender(MockQueue queue)
116        {
117            MockQueueSender sender = new MockQueueSender(connection, session, queue);
118            queueSenderList.add(sender);
119            return sender;
120        }
121    
122        /**
123         * Returns a <code>QueueSender</code> by its index or
124         * <code>null</code>, if no such <code>QueueSender</code> is
125         * present.
126         * @param index the index of the <code>QueueSender</code>
127         * @return the <code>QueueSender</code>
128         */
129        public MockQueueSender getQueueSender(int index)
130        {
131            if(queueSenderList.size() <= index || index < 0) return null;
132            return (MockQueueSender)queueSenderList.get(index);
133        }
134        
135        /**
136         * Returns a <code>QueueSender</code> by the name of its
137         * corresponding <code>Queue</code>. If there's more than
138         * one <code>QueueSender</code> object for the specified name,
139         * the first one will be returned.
140         * @param queueName the name of the <code>Queue</code>
141         * @return the <code>QueueSender</code>
142         */
143        public MockQueueSender getQueueSender(String queueName)
144        {
145            List senders = getQueueSenderList(queueName);
146            if(senders.size() <= 0) return null;
147            return (MockQueueSender)senders.get(0);
148        }
149        
150        /**
151         * Returns the list of the <code>QueueSender</code> objects
152         * for a specific <code>Queue</code>.
153         * @param queueName the name of the <code>Queue</code>
154         * @return the list of <code>QueueSender</code> objects
155         */
156        public List getQueueSenderList(String queueName)
157        {
158            List resultList = new ArrayList();
159            for(int ii = 0; ii < queueSenderList.size(); ii++)
160            {
161                QueueSender sender = (QueueSender)queueSenderList.get(ii);
162                try
163                {
164                    if(sender.getQueue().getQueueName().equals(queueName))
165                    {
166                        resultList.add(sender);
167                    }
168                }
169                catch(JMSException exc)
170                {
171                
172                }
173            }
174            return Collections.unmodifiableList(resultList);
175        }
176        
177        /**
178         * Returns the list of all <code>QueueSender</code> objects.
179         * @return the list of <code>QueueSender</code> objects
180         */
181        public List getQueueSenderList()
182        {
183            return Collections.unmodifiableList(queueSenderList);
184        }
185    
186        /**
187         * Creates a new <code>QueueReceiver</code> for the specified
188         * <code>Queue</code>. Usually this method is called
189         * by {@link com.mockrunner.mock.jms.MockQueueSession#createReceiver}.
190         * @param queue the <code>Queue</code>
191         * @param messageSelector the message selector
192         * @return the created <code>QueueReceiver</code>
193         */
194        public MockQueueReceiver createQueueReceiver(MockQueue queue, String messageSelector)
195        {
196            MockQueueReceiver receiver = new MockQueueReceiver(connection, session, queue, messageSelector);
197            queueReceiverList.add(receiver);
198            return receiver;
199        }
200    
201        /**
202         * Returns a <code>QueueReceiver</code> by its index or
203         * <code>null</code>, if no such <code>QueueReceiver</code> is
204         * present.
205         * @param index the index of the <code>QueueReceiver</code>
206         * @return the <code>QueueReceiver</code>
207         */
208        public MockQueueReceiver getQueueReceiver(int index)
209        {
210            if(queueReceiverList.size() <= index || index < 0) return null;
211            return (MockQueueReceiver)queueReceiverList.get(index);
212        }
213        
214        /**
215         * Returns a <code>QueueReceiver</code> by the name of its
216         * corresponding <code>Queue</code>. If there's more than
217         * one <code>QueueReceiver</code> object for the specified name,
218         * the first one will be returned.
219         * @param queueName the name of the <code>Queue</code>
220         * @return the <code>QueueReceiver</code>
221         */
222        public MockQueueReceiver getQueueReceiver(String queueName)
223        {
224            List receivers = getQueueReceiverList(queueName);
225            if(receivers.size() <= 0) return null;
226            return (MockQueueReceiver)receivers.get(0);
227        }
228    
229        /**
230         * Returns the list of the <code>QueueReceiver</code> objects
231         * for a specific <code>Queue</code>.
232         * @param queueName the name of the <code>Queue</code>
233         * @return the list of <code>QueueReceiver</code> objects
234         */
235        public List getQueueReceiverList(String queueName)
236        {
237            List resultList = new ArrayList();
238            for(int ii = 0; ii < queueReceiverList.size(); ii++)
239            {
240                QueueReceiver receiver = (QueueReceiver)queueReceiverList.get(ii);
241                try
242                {
243                    if(receiver.getQueue().getQueueName().equals(queueName))
244                    {
245                        resultList.add(receiver);
246                    }
247                }
248                catch(JMSException exc)
249                {
250                
251                }
252            }
253            return Collections.unmodifiableList(resultList);
254        }
255        
256        /**
257         * Returns the list of <code>QueueReceiver</code> objects.
258         * @return the <code>QueueReceiver</code> list
259         */
260        public List getQueueReceiverList()
261        {
262            return Collections.unmodifiableList(queueReceiverList);
263        }
264    
265        /**
266         * Creates a new <code>QueueBrowser</code> for the specified
267         * <code>Queue</code>. Usually this method is called
268         * by {@link com.mockrunner.mock.jms.MockQueueSession#createBrowser}.
269         * @param queue the <code>Queue</code>
270         * @param messageSelector the message selector
271         * @return the created <code>QueueBrowser</code>
272         */
273        public MockQueueBrowser createQueueBrowser(MockQueue queue, String messageSelector)
274        {
275            MockQueueBrowser browser = new MockQueueBrowser(connection, queue, messageSelector);
276            queueBrowserList.add(browser);
277            return browser;
278        }
279    
280        /**
281         * Returns a <code>QueueBrowser</code> by its index or
282         * <code>null</code>, if no such <code>QueueBrowser</code> is
283         * present.
284         * @param index the index of the <code>QueueBrowser</code>
285         * @return the <code>QueueBrowser</code>
286         */
287        public MockQueueBrowser getQueueBrowser(int index)
288        {
289            if(queueBrowserList.size() <= index || index < 0) return null;
290            return (MockQueueBrowser)queueBrowserList.get(index);
291        }
292        
293        /**
294         * Returns a <code>QueueBrowser</code> by the name of its
295         * corresponding <code>Queue</code>. If there's more than
296         * one <code>QueueBrowser</code> object for the specified name,
297         * the first one will be returned.
298         * @param queueName the name of the <code>Queue</code>
299         * @return the <code>QueueBrowser</code>
300         */
301        public MockQueueBrowser getQueueBrowser(String queueName)
302        {
303            List browsers = getQueueBrowserList(queueName);
304            if(browsers.size() <= 0) return null;
305            return (MockQueueBrowser)browsers.get(0);
306        }
307    
308        /**
309         * Returns the list of the <code>QueueBrowser</code> objects
310         * for a specific <code>Queue</code>.
311         * @param queueName the name of the <code>Queue</code>
312         * @return the list of <code>QueueBrowser</code> objects
313         */
314        public List getQueueBrowserList(String queueName)
315        {
316            List resultList = new ArrayList();
317            for(int ii = 0; ii < queueBrowserList.size(); ii++)
318            {
319                QueueBrowser browser = (QueueBrowser)queueBrowserList.get(ii);
320                try
321                {
322                    if(browser.getQueue().getQueueName().equals(queueName))
323                    {
324                        resultList.add(browser);
325                    }
326                }
327                catch(JMSException exc)
328                {
329                
330                }
331            }
332            return Collections.unmodifiableList(resultList);
333        }
334        
335        /**
336         * Returns the list of <code>QueueBrowser</code> objects.
337         * @return the <code>QueueBrowser</code> list
338         */
339        public List getQueueBrowserList()
340        {
341            return Collections.unmodifiableList(queueBrowserList);
342        }
343    }