001: package org.mockejb.jms;
002:
003: import java.util.*;
004: import javax.jms.*;
005:
006: /**
007: * <code>Message</code> implementation.
008: * @author Dimitar Gospodinov
009: * @see javax.jms.Message
010: */
011: public class MessageImpl implements Message {
012:
013: private static final int MODE_READ_ONLY = 100;
014: private static final int MODE_WRITE_ONLY = 200;
015:
016: private int bodyMode = MODE_WRITE_ONLY;
017:
018: private boolean propertiesWriteable = true;
019:
020: // JMS Headers
021: private String jmsType;
022: private long jmsTimestamp;
023: private Destination jmsReplyTo;
024: private int jmsPriority;
025: private String jmsMessageId;
026: private long jmsExpiration;
027: private Destination jmsDestination;
028: private int jmsDeliveryMode;
029: private String jmsCorrelationId;
030:
031: private final PrimitiveMap properties = new PrimitiveMap();
032:
033: /**
034: * Constructs empty message.
035: */
036: public MessageImpl() {
037: super ();
038: }
039:
040: /**
041: * Constructs new <code>MessageImpl</code> by copying all
042: * header fields and properties from the specified message <code>msg</code>
043: * @param msg
044: */
045: public MessageImpl(Message msg) throws JMSException {
046: setJMSMessageID(msg.getJMSMessageID());
047: setJMSTimestamp(msg.getJMSTimestamp());
048: setJMSCorrelationID(msg.getJMSCorrelationID());
049: setJMSReplyTo(msg.getJMSReplyTo());
050: setJMSDestination(msg.getJMSDestination());
051: setJMSDeliveryMode(msg.getJMSDeliveryMode());
052: setJMSRedelivered(msg.getJMSRedelivered());
053: setJMSType(msg.getJMSType());
054: setJMSExpiration(msg.getJMSExpiration());
055: setJMSPriority(msg.getJMSPriority());
056:
057: Enumeration e = msg.getPropertyNames();
058: while (e.hasMoreElements()) {
059: String propertyName = (String) e.nextElement();
060: setObjectProperty(propertyName, msg
061: .getObjectProperty(propertyName));
062: }
063: }
064:
065: /**
066: * @see javax.jms.Message#getJMSMessageID()
067: */
068: public String getJMSMessageID() throws JMSException {
069: return jmsMessageId;
070: }
071:
072: /**
073: * @see javax.jms.Message#setJMSMessageID(java.lang.String)
074: */
075: public void setJMSMessageID(String messageId) throws JMSException {
076: jmsMessageId = messageId;
077:
078: }
079:
080: /**
081: * @see javax.jms.Message#getJMSTimestamp()
082: */
083: public long getJMSTimestamp() throws JMSException {
084: return jmsTimestamp;
085: }
086:
087: /**
088: * @see javax.jms.Message#setJMSTimestamp(long)
089: */
090: public void setJMSTimestamp(long timestamp) throws JMSException {
091: jmsTimestamp = timestamp;
092: }
093:
094: /**
095: * Not supported.
096: * @see javax.jms.Message#getJMSCorrelationIDAsBytes()
097: */
098: public byte[] getJMSCorrelationIDAsBytes() throws JMSException {
099: throw new UnsupportedOperationException();
100: }
101:
102: /**
103: * Not supported.
104: * @see javax.jms.Message#setJMSCorrelationIDAsBytes(byte[])
105: */
106: public void setJMSCorrelationIDAsBytes(byte[] arg0)
107: throws JMSException {
108: throw new UnsupportedOperationException();
109: }
110:
111: /**
112: * @see javax.jms.Message#setJMSCorrelationID(java.lang.String)
113: */
114: public void setJMSCorrelationID(String correlationId)
115: throws JMSException {
116: jmsCorrelationId = correlationId;
117: }
118:
119: /**
120: * @see javax.jms.Message#getJMSCorrelationID()
121: */
122: public String getJMSCorrelationID() throws JMSException {
123: return jmsCorrelationId;
124: }
125:
126: /**
127: * @see javax.jms.Message#getJMSReplyTo()
128: */
129: public Destination getJMSReplyTo() throws JMSException {
130: return jmsReplyTo;
131: }
132:
133: /**
134: * @see javax.jms.Message#setJMSReplyTo(javax.jms.Destination)
135: */
136: public void setJMSReplyTo(Destination replyTo) throws JMSException {
137: jmsReplyTo = replyTo;
138: }
139:
140: /**
141: * @see javax.jms.Message#getJMSDestination()
142: */
143: public Destination getJMSDestination() throws JMSException {
144: return jmsDestination;
145: }
146:
147: /**
148: * @see javax.jms.Message#setJMSDestination(javax.jms.Destination)
149: */
150: public void setJMSDestination(Destination destination)
151: throws JMSException {
152: jmsDestination = destination;
153: }
154:
155: /**
156: * @see javax.jms.Message#getJMSDeliveryMode()
157: */
158: public int getJMSDeliveryMode() throws JMSException {
159: return jmsDeliveryMode;
160: }
161:
162: /**
163: * @see javax.jms.Message#setJMSDeliveryMode(int)
164: */
165: public void setJMSDeliveryMode(int deliveryMode)
166: throws JMSException {
167: jmsDeliveryMode = deliveryMode;
168: }
169:
170: /**
171: * Always returns <code>false</code>
172: * @see javax.jms.Message#getJMSRedelivered()
173: */
174: public boolean getJMSRedelivered() throws JMSException {
175: return false;
176: }
177:
178: /**
179: * Does nothing.
180: * @see javax.jms.Message#setJMSRedelivered(boolean)
181: */
182: public void setJMSRedelivered(boolean redelivered)
183: throws JMSException {
184: // Does nothing.
185: }
186:
187: /**
188: * @see javax.jms.Message#getJMSType()
189: */
190: public String getJMSType() throws JMSException {
191: return jmsType;
192: }
193:
194: /**
195: * @see javax.jms.Message#setJMSType(java.lang.String)
196: */
197: public void setJMSType(String type) throws JMSException {
198: jmsType = type;
199: }
200:
201: /**
202: * @see javax.jms.Message#getJMSExpiration()
203: */
204: public long getJMSExpiration() throws JMSException {
205: return jmsExpiration;
206: }
207:
208: /**
209: * @see javax.jms.Message#setJMSExpiration(long)
210: */
211: public void setJMSExpiration(long expiration) throws JMSException {
212: jmsExpiration = expiration;
213: }
214:
215: /**
216: * @see javax.jms.Message#getJMSPriority()
217: */
218: public int getJMSPriority() throws JMSException {
219: return jmsPriority;
220: }
221:
222: /**
223: * @see javax.jms.Message#setJMSPriority(int)
224: */
225: public void setJMSPriority(int priority) throws JMSException {
226: jmsPriority = priority;
227: }
228:
229: /**
230: * @see javax.jms.Message#clearProperties()
231: */
232: public void clearProperties() throws JMSException {
233: propertiesWriteable = true;
234: properties.clear();
235: }
236:
237: /**
238: * @see javax.jms.Message#propertyExists(java.lang.String)
239: */
240: public boolean propertyExists(String propertyName)
241: throws JMSException {
242: return properties.containsKey(propertyName);
243: }
244:
245: /**
246: * @see javax.jms.Message#getBooleanProperty(java.lang.String)
247: */
248: public boolean getBooleanProperty(String name) throws JMSException {
249: return properties.getBoolean(name);
250: }
251:
252: /**
253: * @see javax.jms.Message#getByteProperty(java.lang.String)
254: */
255: public byte getByteProperty(String name) throws JMSException {
256: return properties.getByte(name);
257: }
258:
259: /**
260: * @see javax.jms.Message#getShortProperty(java.lang.String)
261: */
262: public short getShortProperty(String name) throws JMSException {
263: return properties.getShort(name);
264: }
265:
266: /**
267: * @see javax.jms.Message#getIntProperty(java.lang.String)
268: */
269: public int getIntProperty(String name) throws JMSException {
270: return properties.getInt(name);
271: }
272:
273: /**
274: * @see javax.jms.Message#getLongProperty(java.lang.String)
275: */
276: public long getLongProperty(String name) throws JMSException {
277: return properties.getLong(name);
278: }
279:
280: /**
281: * @see javax.jms.Message#getFloatProperty(java.lang.String)
282: */
283: public float getFloatProperty(String name) throws JMSException {
284: return properties.getFloat(name);
285: }
286:
287: /**
288: * @see javax.jms.Message#getDoubleProperty(java.lang.String)
289: */
290: public double getDoubleProperty(String name) throws JMSException {
291: return properties.getDouble(name);
292: }
293:
294: /**
295: * @see javax.jms.Message#getStringProperty(java.lang.String)
296: */
297: public String getStringProperty(String name) throws JMSException {
298: return properties.getString(name);
299: }
300:
301: /**
302: * @see javax.jms.Message#getObjectProperty(java.lang.String)
303: */
304: public Object getObjectProperty(String name) throws JMSException {
305: return properties.getObject(name);
306: }
307:
308: /**
309: * @see javax.jms.Message#getPropertyNames()
310: */
311: public Enumeration getPropertyNames() throws JMSException {
312: return properties.getNames();
313: }
314:
315: /**
316: * @see javax.jms.Message#setBooleanProperty(java.lang.String, boolean)
317: */
318: public void setBooleanProperty(String name, boolean value)
319: throws JMSException {
320:
321: checkPropertiesWriteable();
322: properties.setBoolean(name, value);
323: }
324:
325: /**
326: * @see javax.jms.Message#setByteProperty(java.lang.String, byte)
327: */
328: public void setByteProperty(String name, byte value)
329: throws JMSException {
330:
331: checkPropertiesWriteable();
332: properties.setByte(name, value);
333: }
334:
335: /**
336: * @see javax.jms.Message#setShortProperty(java.lang.String, short)
337: */
338: public void setShortProperty(String name, short value)
339: throws JMSException {
340:
341: checkPropertiesWriteable();
342: properties.setShort(name, value);
343: }
344:
345: /**
346: * @see javax.jms.Message#setIntProperty(java.lang.String, int)
347: */
348: public void setIntProperty(String name, int value)
349: throws JMSException {
350:
351: checkPropertiesWriteable();
352: properties.setInt(name, value);
353: }
354:
355: /**
356: * @see javax.jms.Message#setLongProperty(java.lang.String, long)
357: */
358: public void setLongProperty(String name, long value)
359: throws JMSException {
360:
361: checkPropertiesWriteable();
362: properties.setLong(name, value);
363: }
364:
365: /**
366: * @see javax.jms.Message#setFloatProperty(java.lang.String, float)
367: */
368: public void setFloatProperty(String name, float value)
369: throws JMSException {
370:
371: checkPropertiesWriteable();
372: properties.setFloat(name, value);
373: }
374:
375: /**
376: * @see javax.jms.Message#setDoubleProperty(java.lang.String, double)
377: */
378: public void setDoubleProperty(String name, double value)
379: throws JMSException {
380:
381: checkPropertiesWriteable();
382: properties.setDouble(name, value);
383: }
384:
385: /**
386: * @see javax.jms.Message#setStringProperty(java.lang.String, java.lang.String)
387: */
388: public void setStringProperty(String name, String value)
389: throws JMSException {
390:
391: checkPropertiesWriteable();
392: properties.setString(name, value);
393: }
394:
395: /**
396: * @see javax.jms.Message#setObjectProperty(java.lang.String, java.lang.Object)
397: */
398: public void setObjectProperty(String name, Object value)
399: throws JMSException {
400:
401: checkPropertiesWriteable();
402: properties.setObject(name, value);
403: }
404:
405: /**
406: * Does nothing.
407: * @see javax.jms.Message#acknowledge()
408: */
409: public void acknowledge() throws JMSException {
410: // Does nothing.
411: }
412:
413: /**
414: * @see javax.jms.Message#clearBody()
415: */
416: public void clearBody() throws JMSException {
417: bodyMode = MODE_WRITE_ONLY;
418: }
419:
420: // Non standard methods
421:
422: private boolean propertiesWriteable() {
423: return propertiesWriteable;
424: }
425:
426: public void setPropertiesNotWriteable() {
427: propertiesWriteable = false;
428: }
429:
430: private void checkPropertiesWriteable()
431: throws MessageNotWriteableException {
432: if (!propertiesWriteable()) {
433: throw new MessageNotWriteableException(
434: "Message is in read-only mode!");
435: }
436: }
437:
438: void setBodyReadOnly() {
439: bodyMode = MODE_READ_ONLY;
440: }
441:
442: /**
443: * Checks if this message can be read from. If not then throws
444: * <code>MessageNotReadableException</code>
445: * @throws MessageNotReadableException if read operations
446: * are not allowed.
447: */
448: void checkBodyReadable() throws JMSException {
449: if (bodyMode == MODE_WRITE_ONLY) {
450: throw new MessageNotReadableException(
451: "Message is not in Read-Only mode!");
452: }
453: }
454:
455: /**
456: * Checks if this message can be written to. If not then throws
457: * <code>MessageNotWriteableException</code>
458: * @throws MessageNotWriteableException if write operations
459: * are not allowed.
460: */
461: void checkBodyWriteable() throws JMSException {
462: if (bodyMode == MODE_READ_ONLY) {
463: throw new MessageNotWriteableException(
464: "Message is not in Write-Only mode!");
465: }
466: }
467:
468: /**
469: * Sets message body in read-only mode.
470: * @throws JMSException
471: */
472: void resetBody() throws JMSException {
473: // Does nothing for generic message
474: }
475:
476: }
|