001    package com.mockrunner.jms;
002    
003    import java.util.ArrayList;
004    import java.util.Collections;
005    import java.util.HashMap;
006    import java.util.Iterator;
007    import java.util.List;
008    import java.util.Map;
009    
010    import javax.jms.JMSException;
011    import javax.jms.TopicPublisher;
012    import javax.jms.TopicSubscriber;
013    
014    import com.mockrunner.mock.jms.MockConnection;
015    import com.mockrunner.mock.jms.MockSession;
016    import com.mockrunner.mock.jms.MockTopic;
017    import com.mockrunner.mock.jms.MockTopicPublisher;
018    import com.mockrunner.mock.jms.MockTopicSubscriber;
019    
020    /**
021     * This class is used to create topic publishers and subscribers. 
022     * It can be also used to access all created classes in tests.
023     */
024    public class TopicTransmissionManager
025    {
026        private MockConnection connection;
027        private MockSession session;
028        private List topicPublisherList;
029        private List topicSubscriberList;
030        private Map topicDurableSubscriberMap;
031    
032        public TopicTransmissionManager(MockConnection connection, MockSession session)
033        {
034            this.connection = connection;
035            this.session = session;
036            topicPublisherList = new ArrayList();
037            topicSubscriberList = new ArrayList();
038            topicDurableSubscriberMap = new HashMap();
039        }
040    
041        /**
042         * Closes all senders, receivers, browsers, publishers and subscribers.
043         */
044        public void closeAll()
045        {
046            closeAllTopicPublishers();
047            closeAllTopicSubscribers();
048            closeAllTopicDurableSubscribers();
049        }
050       
051        /**
052         * Closes all topic publishers.
053         */
054        public void closeAllTopicPublishers()
055        {
056            for(int ii = 0; ii < topicPublisherList.size(); ii++)
057            {
058                TopicPublisher publisher = (TopicPublisher)topicPublisherList.get(ii);
059                try
060                {
061                    publisher.close();
062                }
063                catch(JMSException exc)
064                {
065    
066                }
067            }
068        }
069    
070        /**
071         * Closes all topic subscribers.
072         */
073        public void closeAllTopicSubscribers()
074        {
075            for(int ii = 0; ii < topicSubscriberList.size(); ii++)
076            {
077                TopicSubscriber subscriber = (TopicSubscriber)topicSubscriberList.get(ii);
078                try
079                {
080                    subscriber.close();
081                }
082                catch(JMSException exc)
083                {
084    
085                }
086            }
087        }
088    
089        /**
090         * Closes all durable topic subscribers.
091         */
092        public void closeAllTopicDurableSubscribers()
093        {
094            Iterator keys = topicDurableSubscriberMap.keySet().iterator();
095            while(keys.hasNext())
096            {
097                TopicSubscriber subscriber = (TopicSubscriber)topicDurableSubscriberMap.get(keys.next());
098                try
099                {
100                    subscriber.close();
101                }
102                catch(JMSException exc)
103                {
104    
105                }
106            }
107        }
108        
109        /**
110         * Creates a new <code>TopicPublisher</code> for the specified
111         * <code>Topic</code>. Usually this method is called
112         * by {@link com.mockrunner.mock.jms.MockTopicSession#createPublisher}.
113         * @param topic the <code>Topic</code>
114         * @return the created <code>TopicPublisher</code>
115         */
116        public MockTopicPublisher createTopicPublisher(MockTopic topic)
117        {
118            MockTopicPublisher publisher = new MockTopicPublisher(connection, session, topic);
119            topicPublisherList.add(publisher);
120            return publisher;
121        }
122    
123        /**
124         * Returns a <code>TopicPublisher</code> by its index resp.
125         * <code>null</code>, if no such <code>TopicPublisher</code> is
126         * present.
127         * @param index the index of the <code>TopicPublisher</code>
128         * @return the <code>TopicPublisher</code>
129         */
130        public MockTopicPublisher getTopicPublisher(int index)
131        {
132            if(topicPublisherList.size() <= index || index < 0) return null;
133            return (MockTopicPublisher)topicPublisherList.get(index);
134        }
135    
136        /**
137         * Returns a <code>TopicPublisher</code> by the name of its
138         * corresponding <code>Topic</code>. If there's more than
139         * one <code>TopicPublisher</code> object for the specified name,
140         * the first one will be returned.
141         * @param topicName the name of the <code>Topic</code>
142         * @return the <code>TopicPublisher</code>
143         */
144        public MockTopicPublisher getTopicPublisher(String topicName)
145        {
146            List publishers = getTopicPublisherList(topicName);
147            if(publishers.size() <= 0) return null;
148            return (MockTopicPublisher)publishers.get(0);
149        }
150    
151        /**
152         * Returns the list of the <code>TopicPublisher</code> objects
153         * for a specific <code>Topic</code>.
154         * @param topicName the name of the <code>Topic</code>
155         * @return the list of <code>TopicPublisher</code> objects
156         */
157        public List getTopicPublisherList(String topicName)
158        {
159            List resultList = new ArrayList();
160            for(int ii = 0; ii < topicPublisherList.size(); ii++)
161            {
162                TopicPublisher publisher = (TopicPublisher)topicPublisherList.get(ii);
163                try
164                {
165                    if(publisher.getTopic().getTopicName().equals(topicName))
166                    {
167                        resultList.add(publisher);
168                    }
169                }
170                catch(JMSException exc)
171                {
172            
173                }
174            }
175            return Collections.unmodifiableList(resultList);
176        }
177    
178        /**
179         * Returns the list of all <code>TopicPublisher</code> objects.
180         * @return the list of <code>TopicPublisher</code> objects
181         */
182        public List getTopicPublisherList()
183        {
184            return Collections.unmodifiableList(topicPublisherList);
185        }
186        
187        /**
188         * Creates a new <code>TopicSubscriber</code> for the specified
189         * <code>Topic</code>. Usually this method is called
190         * by {@link com.mockrunner.mock.jms.MockTopicSession#createSubscriber}.
191         * @param topic the <code>Topic</code>
192         * @param messageSelector the message selector
193         * @param noLocal the no local flag
194         * @return the created <code>TopicSubscriber</code>
195         */
196        public MockTopicSubscriber createTopicSubscriber(MockTopic topic, String messageSelector, boolean noLocal)
197        {
198            MockTopicSubscriber subscriber = new MockTopicSubscriber(connection, session, topic, messageSelector, noLocal);
199            subscriber.setDurable(false);
200            topicSubscriberList.add(subscriber);
201            return subscriber;
202        }
203    
204        /**
205         * Returns a <code>TopicSubscriber</code> by its index resp.
206         * <code>null</code>, if no such <code>TopicSubscriber</code> is
207         * present.
208         * @param index the index of the <code>TopicSubscriber</code>
209         * @return the <code>TopicSubscriber</code>
210         */
211        public MockTopicSubscriber getTopicSubscriber(int index)
212        {
213            if(topicSubscriberList.size() <= index || index < 0) return null;
214            return (MockTopicSubscriber)topicSubscriberList.get(index);
215        }
216    
217        /**
218         * Returns a <code>TopicSubscriber</code> by the name of its
219         * corresponding <code>Topic</code>. If there's more than
220         * one <code>TopicSubscriber</code> object for the specified name,
221         * the first one will be returned.
222         * @param topicName the name of the <code>Topic</code>
223         * @return the <code>TopicSubscriber</code>
224         */
225        public MockTopicSubscriber getTopicSubscriber(String topicName)
226        {
227            List subscribers = getTopicSubscriberList(topicName);
228            if(subscribers.size() <= 0) return null;
229            return (MockTopicSubscriber)subscribers.get(0);
230        }
231    
232        /**
233         * Returns the list of the <code>TopicSubscriber</code> objects
234         * for a specific <code>Topic</code>.
235         * @param topicName the name of the <code>Topic</code>
236         * @return the list of <code>TopicSubscriber</code> objects
237         */
238        public List getTopicSubscriberList(String topicName)
239        {
240            List resultList = new ArrayList();
241            for(int ii = 0; ii < topicSubscriberList.size(); ii++)
242            {
243                TopicSubscriber subscriber = (TopicSubscriber)topicSubscriberList.get(ii);
244                try
245                {
246                    if(subscriber.getTopic().getTopicName().equals(topicName))
247                    {
248                        resultList.add(subscriber);
249                    }
250                }
251                catch(JMSException exc)
252                {
253        
254                }
255            }
256            return Collections.unmodifiableList(resultList);
257        }
258    
259        /**
260         * Returns the list of all <code>TopicSubscriber</code> objects.
261         * @return the list of <code>TopicSubscriber</code> objects
262         */
263        public List getTopicSubscriberList()
264        {
265            return Collections.unmodifiableList(topicSubscriberList);
266        }
267        
268        /**
269         * Creates a new durable <code>TopicSubscriber</code> for the specified
270         * <code>Topic</code>. Usually this method is called
271         * by {@link com.mockrunner.mock.jms.MockTopicSession#createDurableSubscriber}.
272         * @param topic the <code>Topic</code>
273         * @param name the name of the <code>TopicSubscriber</code>
274         * @param messageSelector the message selector
275         * @param noLocal the no local flag
276         * @return the created <code>TopicSubscriber</code>
277         */
278        public MockTopicSubscriber createDurableTopicSubscriber(MockTopic topic, String name, String messageSelector, boolean noLocal)
279        {
280            MockTopicSubscriber subscriber = new MockTopicSubscriber(connection, session, topic, messageSelector, noLocal);
281            subscriber.setDurable(true);
282            subscriber.setName(name);
283            topicDurableSubscriberMap.put(name, subscriber);
284            return subscriber;
285        }
286    
287        /**
288         * Returns a durable <code>TopicSubscriber</code> by its name resp.
289         * <code>null</code>, if no such durable <code>TopicSubscriber</code> is
290         * present.
291         * @param name the name of the <code>TopicSubscriber</code>
292         * @return the <code>TopicSubscriber</code>
293         */
294        public MockTopicSubscriber getDurableTopicSubscriber(String name)
295        {
296            return (MockTopicSubscriber)topicDurableSubscriberMap.get(name);
297        }
298        
299        /**
300         * Deletes a durable <code>TopicSubscriber</code>.
301         * @param name the name of the <code>TopicSubscriber</code>
302         */
303        public void removeTopicDurableSubscriber(String name)
304        {
305            topicDurableSubscriberMap.remove(name);
306        }
307        
308        /**
309         * Returns the map of all durable <code>TopicSubscriber</code> objects
310         * for a specific <code>Topic</code>.
311         * @param topicName the name of the <code>Topic</code>
312         * @return the map of <code>TopicSubscriber</code> objects
313         */
314        public Map getDurableTopicSubscriberMap(String topicName)
315        {
316            Map resultMap = new HashMap();
317            Iterator subscriberNames = topicDurableSubscriberMap.keySet().iterator();
318            while(subscriberNames.hasNext())
319            {
320                Object nextName = subscriberNames.next();
321                MockTopicSubscriber subscriber = (MockTopicSubscriber)topicDurableSubscriberMap.get(nextName);
322                try
323                {
324                    if(null != subscriber && subscriber.getTopic().getTopicName().equals(topicName))
325                    {
326                        resultMap.put(nextName, subscriber);
327                    }
328                }
329                catch(JMSException exc)
330                {
331                    
332                }
333            }
334            return resultMap;
335        }
336    
337        /**
338         * Returns the map of all durable <code>TopicSubscriber</code> objects.
339         * @return the map of <code>TopicSubscriber</code> objects
340         */
341        public Map getDurableTopicSubscriberMap()
342        {
343            return Collections.unmodifiableMap(topicDurableSubscriberMap);
344        }
345    }