001: package com.mockrunner.mock.jms;
002:
003: import java.io.UnsupportedEncodingException;
004: import java.util.Enumeration;
005: import java.util.HashMap;
006: import java.util.Map;
007: import java.util.Vector;
008:
009: import javax.jms.DeliveryMode;
010: import javax.jms.Destination;
011: import javax.jms.JMSException;
012: import javax.jms.Message;
013: import javax.jms.MessageFormatException;
014: import javax.jms.MessageNotWriteableException;
015:
016: import com.mockrunner.base.NestedApplicationException;
017:
018: /**
019: * Mock implementation of JMS <code>Message</code>.
020: */
021: public class MockMessage implements Message, Cloneable {
022: private String messageId;
023: private long timestamp;
024: private String correlationId;
025: private Destination replyTo;
026: private Destination destination;
027: private int deliveryMode;
028: private boolean redelivered;
029: private String type;
030: private long expiration;
031: private int priority;
032: private boolean acknowledged;
033: private Map properties;
034: private boolean isInWriteMode;
035: private boolean isInWriteModeProperties;
036:
037: public MockMessage() {
038: messageId = null;
039: timestamp = System.currentTimeMillis();
040: deliveryMode = DeliveryMode.PERSISTENT;
041: redelivered = false;
042: expiration = 0;
043: priority = 4;
044: acknowledged = false;
045: properties = new HashMap();
046: isInWriteMode = true;
047: isInWriteModeProperties = true;
048: }
049:
050: public boolean isAcknowledged() {
051: return acknowledged;
052: }
053:
054: public String getJMSMessageID() throws JMSException {
055: return messageId;
056: }
057:
058: public void setJMSMessageID(String messageId) throws JMSException {
059: this .messageId = messageId;
060: }
061:
062: public long getJMSTimestamp() throws JMSException {
063: return timestamp;
064: }
065:
066: public void setJMSTimestamp(long timestamp) throws JMSException {
067: this .timestamp = timestamp;
068: }
069:
070: public byte[] getJMSCorrelationIDAsBytes() throws JMSException {
071: if (null == correlationId)
072: return null;
073: try {
074: return correlationId.getBytes("ISO-8859-1");
075: } catch (UnsupportedEncodingException exc) {
076: throw new JMSException(exc.getMessage());
077: }
078: }
079:
080: public void setJMSCorrelationIDAsBytes(byte[] correlationId)
081: throws JMSException {
082: try {
083: if (null == correlationId) {
084: this .correlationId = null;
085: } else {
086: this .correlationId = new String(correlationId,
087: "ISO-8859-1");
088: }
089: } catch (UnsupportedEncodingException exc) {
090: throw new JMSException(exc.getMessage());
091: }
092: }
093:
094: public void setJMSCorrelationID(String correlationId)
095: throws JMSException {
096: this .correlationId = correlationId;
097: }
098:
099: public String getJMSCorrelationID() throws JMSException {
100: return correlationId;
101: }
102:
103: public Destination getJMSReplyTo() throws JMSException {
104: return replyTo;
105: }
106:
107: public void setJMSReplyTo(Destination replyTo) throws JMSException {
108: this .replyTo = replyTo;
109: }
110:
111: public Destination getJMSDestination() throws JMSException {
112: return destination;
113: }
114:
115: public void setJMSDestination(Destination destination)
116: throws JMSException {
117: this .destination = destination;
118: }
119:
120: public int getJMSDeliveryMode() throws JMSException {
121: return deliveryMode;
122: }
123:
124: public void setJMSDeliveryMode(int deliveryMode)
125: throws JMSException {
126: this .deliveryMode = deliveryMode;
127: }
128:
129: public boolean getJMSRedelivered() throws JMSException {
130: return redelivered;
131: }
132:
133: public void setJMSRedelivered(boolean redelivered)
134: throws JMSException {
135: this .redelivered = redelivered;
136: }
137:
138: public String getJMSType() throws JMSException {
139: return type;
140: }
141:
142: public void setJMSType(String type) throws JMSException {
143: this .type = type;
144: }
145:
146: public long getJMSExpiration() throws JMSException {
147: return expiration;
148: }
149:
150: public void setJMSExpiration(long expiration) throws JMSException {
151: this .expiration = expiration;
152: }
153:
154: public int getJMSPriority() throws JMSException {
155: return priority;
156: }
157:
158: public void setJMSPriority(int priority) throws JMSException {
159: this .priority = priority;
160: }
161:
162: public void clearProperties() throws JMSException {
163: isInWriteModeProperties = true;
164: properties.clear();
165: }
166:
167: public boolean propertyExists(String name) throws JMSException {
168: return properties.containsKey(name);
169: }
170:
171: public boolean getBooleanProperty(String name) throws JMSException {
172: Object value = getObjectProperty(name);
173: if (value == null) {
174: return Boolean.valueOf(null).booleanValue();
175: }
176: if (value instanceof String) {
177: return Boolean.valueOf((String) value).booleanValue();
178: }
179: if (value instanceof Boolean) {
180: return ((Boolean) value).booleanValue();
181: }
182: throw new MessageFormatException("Cannot convert property "
183: + name + " of type " + value.getClass().getName()
184: + " to boolean");
185: }
186:
187: public byte getByteProperty(String name) throws JMSException {
188: Object value = getObjectProperty(name);
189: if (value == null) {
190: return Byte.valueOf(null).byteValue();
191: }
192: if (value instanceof String) {
193: return Byte.valueOf((String) value).byteValue();
194: }
195: if (value instanceof Byte) {
196: return ((Number) value).byteValue();
197: }
198: throw new MessageFormatException("Cannot convert property "
199: + name + " of type " + value.getClass().getName()
200: + " to byte");
201: }
202:
203: public short getShortProperty(String name) throws JMSException {
204: Object value = getObjectProperty(name);
205: if (value == null) {
206: return Short.valueOf(null).shortValue();
207: }
208: if (value instanceof String) {
209: return Short.valueOf((String) value).shortValue();
210: }
211: if ((value instanceof Short) || (value instanceof Byte)) {
212: return ((Number) value).shortValue();
213: }
214: throw new MessageFormatException("Cannot convert property "
215: + name + " of type " + value.getClass().getName()
216: + " to short");
217: }
218:
219: public int getIntProperty(String name) throws JMSException {
220: Object value = getObjectProperty(name);
221: if (value == null) {
222: return Integer.valueOf(null).intValue();
223: }
224: if (value instanceof String) {
225: return Integer.valueOf((String) value).intValue();
226: }
227: if ((value instanceof Integer) || (value instanceof Short)
228: || (value instanceof Byte)) {
229: return ((Number) value).intValue();
230: }
231: throw new MessageFormatException("Cannot convert property "
232: + name + " of type " + value.getClass().getName()
233: + " to int");
234: }
235:
236: public long getLongProperty(String name) throws JMSException {
237: Object value = getObjectProperty(name);
238: if (value == null) {
239: return Long.valueOf(null).longValue();
240: }
241: if (value instanceof String) {
242: return Long.valueOf((String) value).longValue();
243: }
244: if ((value instanceof Long) || (value instanceof Integer)
245: || (value instanceof Short) || (value instanceof Byte)) {
246: return ((Number) value).longValue();
247: }
248: throw new MessageFormatException("Cannot convert property "
249: + name + " of type " + value.getClass().getName()
250: + " to long");
251: }
252:
253: public float getFloatProperty(String name) throws JMSException {
254: Object value = getObjectProperty(name);
255: if (value == null) {
256: return Float.valueOf(null).floatValue();
257: }
258: if (value instanceof String) {
259: return Float.valueOf((String) value).floatValue();
260: }
261: if (value instanceof Float) {
262: return ((Number) value).floatValue();
263: }
264: throw new MessageFormatException("Cannot convert property "
265: + name + " of type " + value.getClass().getName()
266: + " to float");
267: }
268:
269: public double getDoubleProperty(String name) throws JMSException {
270: Object value = getObjectProperty(name);
271: if (value == null) {
272: return Double.valueOf(null).doubleValue();
273: }
274: if (value instanceof String) {
275: return Double.valueOf((String) value).doubleValue();
276: }
277: if ((value instanceof Double) || (value instanceof Float)) {
278: return ((Number) value).doubleValue();
279: }
280: throw new MessageFormatException("Cannot convert property "
281: + name + " of type " + value.getClass().getName()
282: + " to double");
283: }
284:
285: public String getStringProperty(String name) throws JMSException {
286: Object value = getObjectProperty(name);
287: if (null == value)
288: return null;
289: return value.toString();
290: }
291:
292: public Object getObjectProperty(String name) throws JMSException {
293: return properties.get(name);
294: }
295:
296: public Enumeration getPropertyNames() throws JMSException {
297: return new Vector(properties.keySet()).elements();
298: }
299:
300: public void setBooleanProperty(String name, boolean value)
301: throws JMSException {
302: setObjectProperty(name, new Boolean(value));
303: }
304:
305: public void setByteProperty(String name, byte value)
306: throws JMSException {
307: setObjectProperty(name, new Byte(value));
308: }
309:
310: public void setShortProperty(String name, short value)
311: throws JMSException {
312: setObjectProperty(name, new Short(value));
313: }
314:
315: public void setIntProperty(String name, int value)
316: throws JMSException {
317: setObjectProperty(name, new Integer(value));
318: }
319:
320: public void setLongProperty(String name, long value)
321: throws JMSException {
322: setObjectProperty(name, new Long(value));
323: }
324:
325: public void setFloatProperty(String name, float value)
326: throws JMSException {
327: setObjectProperty(name, new Float(value));
328: }
329:
330: public void setDoubleProperty(String name, double value)
331: throws JMSException {
332: setObjectProperty(name, new Double(value));
333: }
334:
335: public void setStringProperty(String name, String value)
336: throws JMSException {
337: setObjectProperty(name, value);
338: }
339:
340: public void setObjectProperty(String name, Object object)
341: throws JMSException {
342: if (!isInWriteModeProperties) {
343: throw new MessageNotWriteableException(
344: "Message is in read mode");
345: }
346: if (null == name || name.length() <= 0) {
347: throw new IllegalArgumentException(
348: "Property names must not be null or empty strings");
349: }
350: if (null == object)
351: return;
352: if ((object instanceof String) || (object instanceof Number)
353: || (object instanceof Boolean)) {
354: properties.put(name, object);
355: return;
356: }
357: throw new MessageFormatException(object.getClass().getName()
358: + " not a valid type");
359: }
360:
361: public void acknowledge() throws JMSException {
362: acknowledged = true;
363: }
364:
365: public void clearBody() throws JMSException {
366: isInWriteMode = true;
367: }
368:
369: public void setReadOnly(boolean isReadOnly) {
370: isInWriteMode = !isReadOnly;
371: }
372:
373: public void setReadOnlyProperties(boolean isReadOnly) {
374: isInWriteModeProperties = !isReadOnly;
375: }
376:
377: public Object clone() {
378: try {
379: MockMessage clone = (MockMessage) super .clone();
380: clone.properties = new HashMap(properties);
381: return clone;
382: } catch (CloneNotSupportedException exc) {
383: throw new NestedApplicationException(exc);
384: }
385: }
386:
387: protected boolean isInWriteMode() {
388: return isInWriteMode;
389: }
390: }
|