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