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