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.MessageProducer;
009:
010: import com.mockrunner.mock.jms.MockConnection;
011: import com.mockrunner.mock.jms.MockMessageProducer;
012: import com.mockrunner.mock.jms.MockQueueSender;
013: import com.mockrunner.mock.jms.MockSession;
014: import com.mockrunner.mock.jms.MockTopicPublisher;
015:
016: /**
017: * This class is used to create generic producers that are not
018: * associated with a destination.
019: * If you create a <code>MessageProducer</code> with a <code>null</code>
020: * destination, this class is used to create a <code>MessageProducer</code>
021: * which is not associated with any destination.
022: * If the session used to create the producer is a <code>QueueSession</code>,
023: * you'll get a <code>QueueSender</code>. For a <code>TopicSession</code>,
024: * you'll get a <code>TopicPublisher</code>. For a generic session,
025: * you'll get a generic instance of <code>MessageProducer</code>.
026: */
027: public class GenericTransmissionManager {
028: private MockConnection connection;
029: private MockSession session;
030: private List messageProducerList;
031:
032: public GenericTransmissionManager(MockConnection connection,
033: MockSession session) {
034: this .connection = connection;
035: this .session = session;
036: messageProducerList = new ArrayList();
037: }
038:
039: /**
040: * Closes all producers.
041: */
042: public void closeAll() {
043: closeAllMessageProducers();
044: }
045:
046: /**
047: * Closes all producers.
048: */
049: public void closeAllMessageProducers() {
050: for (int ii = 0; ii < messageProducerList.size(); ii++) {
051: MessageProducer producer = (MessageProducer) messageProducerList
052: .get(ii);
053: try {
054: producer.close();
055: } catch (JMSException exc) {
056:
057: }
058: }
059: }
060:
061: /**
062: * Creates a new <code>MessageProducer</code>.
063: * @return the created <code>MessageProducer</code>
064: */
065: public MockMessageProducer createMessageProducer() {
066: MockMessageProducer producer = new MockMessageProducer(
067: connection, session, null);
068: messageProducerList.add(producer);
069: return producer;
070: }
071:
072: /**
073: * Creates a new <code>QueueSender</code>.
074: * @return the created <code>QueueSender</code>
075: */
076: public MockQueueSender createQueueSender() {
077: MockQueueSender producer = new MockQueueSender(connection,
078: session, null);
079: messageProducerList.add(producer);
080: return producer;
081: }
082:
083: /**
084: * Creates a new <code>TopicPublisher</code>.
085: * @return the created <code>TopicPublisher</code>
086: */
087: public MockTopicPublisher createTopicPublisher() {
088: MockTopicPublisher producer = new MockTopicPublisher(
089: connection, session, null);
090: messageProducerList.add(producer);
091: return producer;
092: }
093:
094: /**
095: * Returns a <code>MessageProducer</code> by its index or
096: * <code>null</code>, if no such <code>MessageProducer</code> is
097: * present.
098: * @param index the index of the <code>MessageProducer</code>
099: * @return the <code>MessageProducer</code>
100: */
101: public MockMessageProducer getMessageProducer(int index) {
102: if (messageProducerList.size() <= index || index < 0)
103: return null;
104: return (MockMessageProducer) messageProducerList.get(index);
105: }
106:
107: /**
108: * Returns the list of all <code>MessageProducer</code> objects.
109: * @return the list of <code>MessageProducer</code> objects
110: */
111: public List getMessageProducerList() {
112: return Collections.unmodifiableList(messageProducerList);
113: }
114: }
|