001: package org.mockejb.jms;
002:
003: import javax.jms.*;
004:
005: /**
006: * Sends message to destination.
007: *
008: * @author Dimitar Gospodinov
009: */
010: class MockProducer implements MessageProducer {
011:
012: private MockDestination destination;
013: private int deliveryMode = DeliveryMode.PERSISTENT;
014: private int priority = 4;
015: private long timeToLive = 0;
016: private static int messageId = 1;
017:
018: /**
019: * Creates producer for the specified destination.
020: * @param destination
021: */
022: MockProducer(MockDestination destination) {
023: this .destination = destination;
024: }
025:
026: /**
027: * Sends message.
028: * @param msg
029: */
030: public void send(Message msg) throws JMSException {
031: send(msg, getDeliveryMode(), getPriority(), getTimeToLive());
032: }
033:
034: /**
035: * Sends message with specified delivery mode, priority and time to live.
036: * @param msg
037: * @param deliveryMode
038: * @param priority
039: * @param timeToLive
040: */
041: public void send(Message msg, int deliveryMode, int priority,
042: long timeToLive) throws JMSException {
043:
044: checkDestination(false);
045: MockProducer.sendMessage((MockDestination) getDestination(),
046: msg, deliveryMode, priority, timeToLive);
047: }
048:
049: /**
050: * Generates unique number used to create message Id.
051: * @return message Id
052: */
053: protected static synchronized int getMessageId() {
054: return messageId++;
055: }
056:
057: /**
058: * Sends <code>msg</code> to <code>destination</code> using specified
059: * delivery mode, priority and time to live.
060: * @param destination
061: * @param msg
062: * @param deliveryMode
063: * @param priority
064: * @param timeToLive
065: * @throws JMSException
066: */
067: protected static void sendMessage(MockDestination destination,
068: Message msg, int deliveryMode, int priority, long timeToLive)
069: throws JMSException {
070:
071: msg.setJMSMessageID("ID:MockMessage "
072: + MockProducer.getMessageId());
073: msg.setJMSDestination(destination);
074: msg.setJMSDeliveryMode(deliveryMode);
075: msg.setJMSPriority(priority);
076: msg.setJMSTimestamp(System.currentTimeMillis());
077: if (timeToLive == 0) {
078: msg.setJMSExpiration(0);
079: } else {
080: msg.setJMSExpiration(msg.getJMSTimestamp() + timeToLive);
081: }
082: destination.addMessage(msg);
083: }
084:
085: /**
086: * Sends message to destination.
087: */
088: public void send(Destination destination, Message msg)
089: throws JMSException {
090: send(destination, msg, getDeliveryMode(), getPriority(),
091: getTimeToLive());
092: }
093:
094: /**
095: * Sends message to destination, with specified delivery mode,
096: * priority and time to live.
097: */
098: public void send(Destination destination, Message msg,
099: int deliveryMode, int priority, long timeToLive)
100: throws JMSException {
101:
102: checkDestination(true);
103: if (destination instanceof MockDestination) {
104: MockProducer.sendMessage((MockDestination) destination,
105: msg, deliveryMode, priority, timeToLive);
106: }
107: throw new InvalidDestinationException(
108: "Invalid destination specified!");
109: }
110:
111: /**
112: * Returns destination for this producer.
113: */
114: public Destination getDestination() throws JMSException {
115: return destination;
116: }
117:
118: /**
119: * Does nothing.
120: * @see javax.jms.MessageProducer#close()
121: */
122: public void close() throws JMSException {
123: // Does nothing.
124: }
125:
126: /**
127: * Does nothing.
128: * @see javax.jms.MessageProducer#setDisableMessageID(boolean)
129: */
130: public void setDisableMessageID(boolean arg0) throws JMSException {
131: // Does nothing.
132: }
133:
134: /**
135: * Always returns <code>false</code>
136: * @see javax.jms.MessageProducer#getDisableMessageID()
137: */
138: public boolean getDisableMessageID() throws JMSException {
139: return false;
140: }
141:
142: /**
143: * Does nothing.
144: * @see javax.jms.MessageProducer#setDisableMessageTimestamp(boolean)
145: */
146: public void setDisableMessageTimestamp(boolean arg0)
147: throws JMSException {
148: // Does nothing.
149: }
150:
151: /**
152: * Always returns <code>false</code>
153: * @see javax.jms.MessageProducer#getDisableMessageTimestamp()
154: */
155: public boolean getDisableMessageTimestamp() throws JMSException {
156: return false;
157: }
158:
159: /**
160: * @see javax.jms.MessageProducer#setDeliveryMode(int)
161: */
162: public void setDeliveryMode(int deliveryMode) throws JMSException {
163: this .deliveryMode = deliveryMode;
164: }
165:
166: /**
167: * @see javax.jms.MessageProducer#getDeliveryMode()
168: */
169: public int getDeliveryMode() throws JMSException {
170: return deliveryMode;
171: }
172:
173: /**
174: * @see javax.jms.MessageProducer#setPriority(int)
175: */
176: public void setPriority(int priority) throws JMSException {
177: this .priority = priority;
178: }
179:
180: /**
181: * @see javax.jms.MessageProducer#getPriority()
182: */
183: public int getPriority() throws JMSException {
184: return priority;
185: }
186:
187: /**
188: * @see javax.jms.MessageProducer#setTimeToLive(long)
189: */
190: public void setTimeToLive(long timeToLive) throws JMSException {
191: this .timeToLive = timeToLive;
192: }
193:
194: /**
195: * @see javax.jms.MessageProducer#getTimeToLive()
196: */
197: public long getTimeToLive() throws JMSException {
198: return timeToLive;
199: }
200:
201: /**
202: * Checks if there is or there is not destination specified for this producer.
203: * Method is used in cases when <code>UnsupportedOperationException</code>
204: * should be raised by a <code>MessageProducer</code> (for example
205: * calling <code>send(Message)</code> when the producer has been created
206: * without specifying a destination).
207: * @param unidentifiedDestinationCheck
208: * @throws UnsupportedOperationException if unsupported operation is attempted
209: */
210: protected void checkDestination(boolean unidentifiedDestinationCheck) {
211: if (unidentifiedDestinationCheck) {
212: if (destination != null) {
213: throw new UnsupportedOperationException();
214: }
215: } else {
216: if (destination == null) {
217: throw new UnsupportedOperationException();
218: }
219: }
220: }
221: }
|