001: package com.mockrunner.mock.jms;
002:
003: import javax.jms.BytesMessage;
004: import javax.jms.DeliveryMode;
005: import javax.jms.Destination;
006: import javax.jms.InvalidDestinationException;
007: import javax.jms.JMSException;
008: import javax.jms.Message;
009: import javax.jms.MessageProducer;
010: import javax.jms.StreamMessage;
011:
012: /**
013: * Mock implementation of JMS <code>MessageProducer</code>.
014: */
015: public class MockMessageProducer implements MessageProducer {
016: private MockConnection connection;
017: private MockDestination destination;
018: private MockSession session;
019: private boolean closed;
020: private boolean disableMessageId;
021: private boolean disableTimestamp;
022: private int deliveryMode;
023: private int priority;
024: private long timeToLive;
025:
026: public MockMessageProducer(MockConnection connection,
027: MockSession session, MockDestination destination) {
028: this .connection = connection;
029: this .destination = destination;
030: this .session = session;
031: closed = false;
032: disableMessageId = false;
033: disableTimestamp = false;
034: deliveryMode = DeliveryMode.PERSISTENT;
035: priority = 4;
036: timeToLive = 0;
037: }
038:
039: /**
040: * Returns if this producer was closed.
041: * @return <code>true</code> if this sender is closed
042: */
043: public boolean isClosed() {
044: return closed;
045: }
046:
047: public void send(Message message) throws JMSException {
048: send(destination, message, deliveryMode, priority, timeToLive);
049: }
050:
051: public void send(Message message, int deliveryMode, int priority,
052: long timeToLive) throws JMSException {
053: send(destination, message, deliveryMode, priority, timeToLive);
054: }
055:
056: public void send(Destination destination, Message message)
057: throws JMSException {
058: send(destination, message, deliveryMode, priority, timeToLive);
059: }
060:
061: public void send(Destination destination, Message message,
062: int deliveryMode, int priority, long timeToLive)
063: throws JMSException {
064: connection.throwJMSException();
065: if (isClosed()) {
066: throw new JMSException("Producer is closed");
067: }
068: if (null == destination) {
069: throw new InvalidDestinationException(
070: "destination must not be null");
071: }
072: if ((message instanceof MockMessage)
073: && connection.getConfigurationManager()
074: .getDoCloneOnSend()) {
075: message = (MockMessage) ((MockMessage) message).clone();
076: }
077: if (destination instanceof MockQueue) {
078: setJMSMessageHeaders(message, destination, deliveryMode,
079: priority, timeToLive);
080: session.addSessionToQueue((MockQueue) destination);
081: ((MockQueue) destination).addMessage(message);
082: } else if (destination instanceof MockTopic) {
083: setJMSMessageHeaders(message, destination, deliveryMode,
084: priority, timeToLive);
085: session.addSessionToTopic((MockTopic) destination);
086: ((MockTopic) destination).addMessage(message);
087: } else {
088: throw new InvalidDestinationException(
089: "destination must be an instance of MockQueue or MockTopic");
090: }
091: }
092:
093: public Destination getDestination() throws JMSException {
094: connection.throwJMSException();
095: return destination;
096: }
097:
098: public void close() throws JMSException {
099: connection.throwJMSException();
100: closed = true;
101: }
102:
103: public void setDisableMessageID(boolean disableMessageId)
104: throws JMSException {
105: connection.throwJMSException();
106: this .disableMessageId = disableMessageId;
107: }
108:
109: public boolean getDisableMessageID() throws JMSException {
110: connection.throwJMSException();
111: return disableMessageId;
112: }
113:
114: public void setDisableMessageTimestamp(boolean disableTimestamp)
115: throws JMSException {
116: connection.throwJMSException();
117: this .disableTimestamp = disableTimestamp;
118: }
119:
120: public boolean getDisableMessageTimestamp() throws JMSException {
121: connection.throwJMSException();
122: return disableTimestamp;
123: }
124:
125: public void setDeliveryMode(int deliveryMode) throws JMSException {
126: connection.throwJMSException();
127: this .deliveryMode = deliveryMode;
128: }
129:
130: public int getDeliveryMode() throws JMSException {
131: connection.throwJMSException();
132: return deliveryMode;
133: }
134:
135: public void setPriority(int priority) throws JMSException {
136: connection.throwJMSException();
137: this .priority = priority;
138: }
139:
140: public int getPriority() throws JMSException {
141: connection.throwJMSException();
142: return priority;
143: }
144:
145: public void setTimeToLive(long timeToLive) throws JMSException {
146: connection.throwJMSException();
147: this .timeToLive = timeToLive;
148: }
149:
150: public long getTimeToLive() throws JMSException {
151: connection.throwJMSException();
152: return timeToLive;
153: }
154:
155: private void setJMSMessageHeaders(Message message,
156: Destination destination, int deliveryMode, int priority,
157: long timeToLive) throws JMSException {
158: message.setJMSDeliveryMode(deliveryMode);
159: message.setJMSPriority(priority);
160: message.setJMSDestination(destination);
161: long currentTime = System.currentTimeMillis();
162: if (!disableTimestamp) {
163: message.setJMSTimestamp(currentTime);
164: }
165: if (0 == timeToLive) {
166: message.setJMSExpiration(0);
167: } else {
168: message.setJMSExpiration(currentTime + timeToLive);
169: }
170: if (!disableMessageId) {
171: message.setJMSMessageID("ID:"
172: + String.valueOf(Math.random()));
173: }
174: if (message instanceof MockMessage) {
175: ((MockMessage) message).setReadOnly(true);
176: ((MockMessage) message).setReadOnlyProperties(true);
177: }
178: if (message instanceof BytesMessage) {
179: ((BytesMessage) message).reset();
180: }
181: if (message instanceof StreamMessage) {
182: ((StreamMessage) message).reset();
183: }
184: }
185: }
|