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