001: package dalma.endpoints.jms.impl;
002:
003: import javax.jms.Destination;
004: import javax.jms.JMSException;
005: import javax.jms.Message;
006: import javax.jms.MessageFormatException;
007: import java.util.Collections;
008: import java.util.Enumeration;
009: import java.util.HashMap;
010: import java.util.Map;
011: import java.io.Serializable;
012:
013: /**
014: *
015: * @ThirdParty this class contains code released under ASL.
016: * @author Kohsuke Kawaguchi
017: */
018: public abstract class MessageImpl<T extends Message> implements
019: Message, Serializable {
020: String jmsMessageId;
021: long jmsTimestamp;
022: private String jmsCorrelationID;
023: private Destination jmsReplyTo;
024: private Destination jmsDestination;
025: private int jmsDeliveryMode;
026: private boolean jmsRedelivered;
027: private String jmsType;
028: private long jmsExpiration;
029: private int jmsPriority;
030: private Map<String, Object> properties;
031:
032: /**
033: * If this wrapper is still connected to the original message,
034: * this field points to that. Otherwise null.
035: */
036: private transient Message original;
037:
038: protected MessageImpl() {
039: }
040:
041: public MessageImpl wrap(T s) throws JMSException {
042: this .original = s;
043: jmsMessageId = s.getJMSMessageID();
044: jmsTimestamp = s.getJMSTimestamp();
045: jmsCorrelationID = s.getJMSCorrelationID();
046: jmsReplyTo = s.getJMSReplyTo(); // TODO
047: jmsDestination = s.getJMSDestination(); // TODO
048: jmsDeliveryMode = s.getJMSDeliveryMode();
049: jmsRedelivered = s.getJMSRedelivered();
050: jmsType = s.getJMSType();
051: jmsExpiration = s.getJMSExpiration();
052: jmsPriority = s.getJMSPriority();
053: properties = new HashMap<String, Object>();
054: Enumeration e = s.getPropertyNames();
055: while (e.hasMoreElements()) {
056: String key = (String) e.nextElement();
057: properties.put(key, s.getObjectProperty(key));
058: }
059: clearBody();
060: return this ;
061: }
062:
063: protected void writeTo(T d) throws JMSException {
064: d.setJMSMessageID(jmsMessageId);
065: d.setJMSTimestamp(jmsTimestamp);
066: d.setJMSCorrelationID(jmsCorrelationID);
067: d.setJMSReplyTo(jmsReplyTo);
068: d.setJMSDestination(jmsDestination);
069: d.setJMSDeliveryMode(jmsDeliveryMode);
070: d.setJMSRedelivered(jmsRedelivered);
071: d.setJMSType(jmsType);
072: d.setJMSExpiration(jmsExpiration);
073: d.setJMSPriority(jmsPriority);
074: d.clearProperties();
075: Enumeration e = getPropertyNames();
076: while (e.hasMoreElements()) {
077: String key = (String) e.nextElement();
078: d.setObjectProperty(key, getObjectProperty(key));
079: }
080: d.clearBody();
081: }
082:
083: public String getJMSMessageID() throws JMSException {
084: return jmsMessageId;
085: }
086:
087: public void setJMSMessageID(String id) throws JMSException {
088: this .jmsMessageId = id;
089: }
090:
091: public long getJMSTimestamp() throws JMSException {
092: return jmsTimestamp;
093: }
094:
095: public void setJMSTimestamp(long timestamp) throws JMSException {
096: this .jmsTimestamp = timestamp;
097: }
098:
099: public byte[] getJMSCorrelationIDAsBytes() throws JMSException {
100: throw new UnsupportedOperationException();
101: }
102:
103: public void setJMSCorrelationIDAsBytes(byte[] correlationID)
104: throws JMSException {
105: throw new UnsupportedOperationException();
106: }
107:
108: public void setJMSCorrelationID(String correlationID)
109: throws JMSException {
110: this .jmsCorrelationID = correlationID;
111: }
112:
113: public String getJMSCorrelationID() throws JMSException {
114: return jmsCorrelationID;
115: }
116:
117: public Destination getJMSReplyTo() throws JMSException {
118: return jmsReplyTo;
119: }
120:
121: public void setJMSReplyTo(Destination replyTo) throws JMSException {
122: this .jmsReplyTo = replyTo;
123: }
124:
125: public Destination getJMSDestination() throws JMSException {
126: return jmsDestination;
127: }
128:
129: public void setJMSDestination(Destination destination)
130: throws JMSException {
131: this .jmsDestination = destination;
132: }
133:
134: public int getJMSDeliveryMode() throws JMSException {
135: return jmsDeliveryMode;
136: }
137:
138: public void setJMSDeliveryMode(int deliveryMode)
139: throws JMSException {
140: this .jmsDeliveryMode = deliveryMode;
141: }
142:
143: public boolean getJMSRedelivered() throws JMSException {
144: return jmsRedelivered;
145: }
146:
147: public void setJMSRedelivered(boolean redelivered)
148: throws JMSException {
149: this .jmsRedelivered = redelivered;
150: }
151:
152: public String getJMSType() throws JMSException {
153: return jmsType;
154: }
155:
156: public void setJMSType(String type) throws JMSException {
157: this .jmsType = type;
158: }
159:
160: public long getJMSExpiration() throws JMSException {
161: return jmsExpiration;
162: }
163:
164: public void setJMSExpiration(long expiration) throws JMSException {
165: this .jmsExpiration = expiration;
166: }
167:
168: public int getJMSPriority() throws JMSException {
169: return jmsPriority;
170: }
171:
172: public void setJMSPriority(int priority) throws JMSException {
173: this .jmsPriority = priority;
174: }
175:
176: public void clearProperties() throws JMSException {
177: properties.clear();
178: }
179:
180: public boolean propertyExists(String name) throws JMSException {
181: return properties.containsKey(name);
182: }
183:
184: public boolean getBooleanProperty(String name) throws JMSException {
185: return vanillaToBoolean(this .properties, name);
186: }
187:
188: public byte getByteProperty(String name) throws JMSException {
189: return vanillaToByte(this .properties, name);
190: }
191:
192: public short getShortProperty(String name) throws JMSException {
193: return vanillaToShort(this .properties, name);
194: }
195:
196: public int getIntProperty(String name) throws JMSException {
197: return vanillaToInt(this .properties, name);
198: }
199:
200: public long getLongProperty(String name) throws JMSException {
201: return vanillaToLong(this .properties, name);
202: }
203:
204: public float getFloatProperty(String name) throws JMSException {
205: return vanillaToFloat(this .properties, name);
206: }
207:
208: public double getDoubleProperty(String name) throws JMSException {
209: return vanillaToDouble(this .properties, name);
210: }
211:
212: public String getStringProperty(String name) throws JMSException {
213: return vanillaToString(this .properties, name);
214: }
215:
216: public Object getObjectProperty(String name) {
217: return this .properties != null ? this .properties.get(name)
218: : null;
219: }
220:
221: public Enumeration getPropertyNames() {
222: if (this .properties == null) {
223: this .properties = new HashMap<String, Object>();
224: }
225: return Collections.enumeration(this .properties.keySet());
226: }
227:
228: public void setBooleanProperty(String name, boolean value)
229: throws JMSException {
230: prepareProperty(name);
231: this .properties.put(name, (value) ? Boolean.TRUE
232: : Boolean.FALSE);
233: }
234:
235: public void setByteProperty(String name, byte value)
236: throws JMSException {
237: prepareProperty(name);
238: this .properties.put(name, value);
239: }
240:
241: public void setShortProperty(String name, short value)
242: throws JMSException {
243: prepareProperty(name);
244: this .properties.put(name, value);
245: }
246:
247: public void setIntProperty(String name, int value)
248: throws JMSException {
249: prepareProperty(name);
250: this .properties.put(name, value);
251: }
252:
253: public void setLongProperty(String name, long value)
254: throws JMSException {
255: prepareProperty(name);
256: this .properties.put(name, value);
257: }
258:
259: public void setFloatProperty(String name, float value)
260: throws JMSException {
261: prepareProperty(name);
262: this .properties.put(name, value);
263:
264: }
265:
266: public void setDoubleProperty(String name, double value)
267: throws JMSException {
268: prepareProperty(name);
269: this .properties.put(name, value);
270: }
271:
272: public void setStringProperty(String name, String value)
273: throws JMSException {
274: prepareProperty(name);
275: if (value == null) {
276: this .properties.remove(name);
277: } else {
278: this .properties.put(name, value);
279: }
280: }
281:
282: boolean vanillaToBoolean(Map table, String name)
283: throws JMSException {
284: boolean result = false;
285: Object value = getVanillaProperty(table, name);
286: if (value != null) {
287: if (value instanceof Boolean) {
288: result = (Boolean) value;
289: } else if (value instanceof String) {
290: // will throw a runtime exception if cannot convert
291: result = Boolean.valueOf((String) value);
292: } else {
293: throw new MessageFormatException(name
294: + " not a Boolean type");
295: }
296: }
297: return result;
298: }
299:
300: byte vanillaToByte(Map table, String name) throws JMSException {
301: byte result = 0;
302: Object value = getVanillaProperty(table, name);
303: if (value != null) {
304: if (value instanceof Byte) {
305: result = (Byte) value;
306: } else if (value instanceof String) {
307: result = Byte.valueOf((String) value);
308: } else {
309: throw new MessageFormatException(name
310: + " not a Byte type");
311: }
312: } else {
313: //object doesn't exist - so treat as a null ..
314: throw new NumberFormatException(
315: "Cannot interpret null as a Byte");
316: }
317: return result;
318: }
319:
320: short vanillaToShort(Map table, String name) throws JMSException {
321: short result = 0;
322: Object value = getVanillaProperty(table, name);
323: if (value != null) {
324: if (value instanceof Short) {
325: result = (Short) value;
326: } else if (value instanceof String) {
327: return Short.valueOf((String) value);
328: } else if (value instanceof Byte) {
329: result = (Byte) value;
330: } else {
331: throw new MessageFormatException(name
332: + " not a Short type");
333: }
334: } else {
335: throw new NumberFormatException(name + " is null");
336: }
337: return result;
338: }
339:
340: int vanillaToInt(Map table, String name) throws JMSException {
341: int result = 0;
342: Object value = getVanillaProperty(table, name);
343: if (value != null) {
344: if (value instanceof Integer) {
345: result = (Integer) value;
346: } else if (value instanceof String) {
347: result = Integer.valueOf((String) value);
348: } else if (value instanceof Byte) {
349: result = ((Byte) value).intValue();
350: } else if (value instanceof Short) {
351: result = ((Short) value).intValue();
352: } else {
353: throw new MessageFormatException(name
354: + " not an Integer type");
355: }
356: } else {
357: throw new NumberFormatException(name + " is null");
358: }
359: return result;
360: }
361:
362: long vanillaToLong(Map table, String name) throws JMSException {
363: long result = 0;
364: Object value = getVanillaProperty(table, name);
365: if (value != null) {
366: if (value instanceof Long) {
367: result = (Long) value;
368: } else if (value instanceof String) {
369: // will throw a runtime exception if cannot convert
370: result = Long.valueOf((String) value);
371: } else if (value instanceof Byte) {
372: result = (Byte) value;
373: } else if (value instanceof Short) {
374: result = (Short) value;
375: } else if (value instanceof Integer) {
376: result = (Integer) value;
377: } else {
378: throw new MessageFormatException(name
379: + " not a Long type");
380: }
381: } else {
382: throw new NumberFormatException(name + " is null");
383: }
384: return result;
385: }
386:
387: float vanillaToFloat(Map table, String name) throws JMSException {
388: float result = 0.0f;
389: Object value = getVanillaProperty(table, name);
390: if (value != null) {
391: if (value instanceof Float) {
392: result = (Float) value;
393: } else if (value instanceof String) {
394: result = Float.valueOf((String) value);
395: } else {
396: throw new MessageFormatException(name
397: + " not a Float type: " + value.getClass());
398: }
399: } else {
400: throw new NullPointerException(name + " is null");
401: }
402: return result;
403: }
404:
405: double vanillaToDouble(Map table, String name) throws JMSException {
406: double result = 0.0d;
407: Object value = getVanillaProperty(table, name);
408: if (value != null) {
409: if (value instanceof Double) {
410: result = (Double) value;
411: } else if (value instanceof String) {
412: result = Double.valueOf((String) value);
413: } else if (value instanceof Float) {
414: result = (Float) value;
415: } else {
416: throw new MessageFormatException(name
417: + " not a Double type");
418: }
419: } else {
420: throw new NullPointerException(name + " is null");
421: }
422: return result;
423: }
424:
425: Object getVanillaProperty(Map table, String name) {
426: Object result = null;
427: if (name == null) {
428: throw new NullPointerException("name supplied is null");
429: }
430: result = getReservedProperty(name);
431: if (result == null && table != null) {
432: result = table.get(name);
433: }
434: return result;
435: }
436:
437: Object getReservedProperty(String name) {
438: Object result = null;
439: // if (name != null && name.equals(DELIVERY_COUNT_NAME)){
440: // result = new Integer(deliveryCount);
441: // }
442: return result;
443: }
444:
445: String vanillaToString(Map table, String name) throws JMSException {
446: String result = null;
447: if (table != null) {
448: Object value = table.get(name);
449: if (value != null) {
450: if (value instanceof String || value instanceof Number
451: || value instanceof Boolean) {
452: result = value.toString();
453: } else {
454: throw new MessageFormatException(name
455: + " not a String type");
456: }
457: }
458: }
459: return result;
460: }
461:
462: private void prepareProperty(String name) throws JMSException {
463: if (name == null) {
464: throw new IllegalArgumentException(
465: "Invalid property name: cannot be null");
466: }
467: if (name.length() == 0) {
468: throw new IllegalArgumentException(
469: "Invalid property name: cannot be empty");
470: }
471: // if (this.readOnlyProperties) {
472: // throw new MessageNotWriteableException("Properties are read-only");
473: // }
474: if (this .properties == null) {
475: this .properties = new HashMap<String, Object>();
476: }
477: }
478:
479: public void setObjectProperty(String name, Object value)
480: throws JMSException {
481: prepareProperty(name);
482: if (value == null) {
483: this .properties.remove(name);
484: } else {
485: if (value instanceof Number || value instanceof Character
486: || value instanceof Boolean
487: || value instanceof String) {
488: this .properties.put(name, value);
489: } else {
490: throw new MessageFormatException(
491: "Cannot set property to type: "
492: + value.getClass().getName());
493: }
494: }
495: }
496:
497: public void acknowledge() throws JMSException {
498: if (original == null)
499: throw new UnsupportedOperationException(
500: "this message is serialized and therefore too late to aknowledge");
501: else
502: original.acknowledge();
503: }
504:
505: private static final long serialVersionUID = 1L;
506: }
|