KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > Message


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2006 ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): ScalAgent Distributed Technologies
21  * Contributor(s):
22  */

23 package org.objectweb.joram.client.jms;
24
25 import java.io.Serializable JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import java.util.*;
33
34 import javax.jms.JMSException JavaDoc;
35 import javax.jms.IllegalStateException JavaDoc;
36 import javax.jms.MessageEOFException JavaDoc;
37 import javax.jms.MessageFormatException JavaDoc;
38 import javax.jms.MessageNotWriteableException JavaDoc;
39
40 import org.objectweb.joram.shared.stream.StreamUtil;
41 import org.objectweb.joram.shared.messages.ConversionHelper;
42 import org.objectweb.joram.shared.excepts.MessageValueException;
43
44 import org.objectweb.util.monolog.api.BasicLevel;
45 import org.objectweb.joram.shared.JoramTracing;
46
47 /**
48  * Implements the <code>javax.jms.Message</code> interface.
49  * <p>
50  * A Joram message encapsulates a proprietary message which is also used
51  * for effective MOM transport facility.
52  */

53 public class Message implements javax.jms.Message JavaDoc {
54   org.objectweb.joram.shared.messages.Message momMsg;
55
56   /**
57    * Constructs a bright new <code>Message</code>.
58    */

59   Message() {
60     momMsg = new org.objectweb.joram.shared.messages.Message();
61   }
62
63   public static Message wrapMomMessage(Session session,
64                                        org.objectweb.joram.shared.messages.Message momMsg) throws JMSException JavaDoc {
65     switch (momMsg.type) {
66     case org.objectweb.joram.shared.messages.Message.SIMPLE:
67       return new Message(session, momMsg);
68     case org.objectweb.joram.shared.messages.Message.TEXT:
69       return new TextMessage(session, momMsg);
70     case org.objectweb.joram.shared.messages.Message.OBJECT:
71       return new ObjectMessage(session, momMsg);
72     case org.objectweb.joram.shared.messages.Message.MAP:
73       return new MapMessage(session, momMsg);
74     case org.objectweb.joram.shared.messages.Message.STREAM:
75       return new StreamMessage(session, momMsg);
76     case org.objectweb.joram.shared.messages.Message.BYTES:
77       return new BytesMessage(session, momMsg);
78     default:
79       throw new JMSException JavaDoc("Unknow message type: " + momMsg.type);
80     }
81   }
82
83   /**
84    * If the message is actually consumed, the session that consumes it,
85    * <code>null</code> otherwise.
86    */

87   protected transient Session session = null;
88
89   /**
90    * The JMSDestination field. This field is only use with non Joram
91    * destination.
92    */

93   protected transient javax.jms.Destination JavaDoc jmsDest = null;
94
95   /**
96    * Instanciates a <code>Message</code> wrapping a consumed
97    * MOM simple message.
98    *
99    * @param session The consuming session.
100    * @param momMsg The MOM message to wrap.
101    */

102   Message(Session session,
103           org.objectweb.joram.shared.messages.Message momMsg) {
104     this.session = session;
105     this.momMsg = momMsg;
106     setReadOnly();
107   }
108
109   /**
110    * API method.
111    *
112    * @exception IllegalStateException If the session is closed.
113    * @exception JMSException If the acknowledgement fails for any other
114    * reason.
115    */

116   public void acknowledge() throws JMSException JavaDoc {
117     if ((session == null) ||
118         session.getTransacted() ||
119         (session.getAcknowledgeMode() != javax.jms.Session.CLIENT_ACKNOWLEDGE))
120       return;
121     session.acknowledge();
122   }
123
124   /** <code>true</code> if the message body is read-only. */
125   protected boolean RObody = false;
126
127   /**
128    * API method.
129    *
130    * @exception JMSException Actually never thrown.
131    */

132   public void clearBody() throws JMSException JavaDoc {
133     momMsg.body = null;
134     RObody = false;
135   }
136
137   /** set message read-only */
138   public void setReadOnly() {
139     propertiesRO = true;
140     RObody = true;
141   }
142
143   /**
144    * Returns the message identifier.
145    * API method.
146    *
147    * @exception JMSException Actually never thrown.
148    */

149   public final String JavaDoc getJMSMessageID() throws JMSException JavaDoc {
150     return momMsg.id;
151   }
152  
153   /**
154    * API method.
155    *
156    * @exception JMSException Actually never thrown.
157    */

158   public final void setJMSMessageID(String JavaDoc id) throws JMSException JavaDoc {
159     momMsg.id = id;
160   }
161  
162   /**
163    * Returns the message priority.
164    * API method.
165    *
166    * @exception JMSException Actually never thrown.
167    */

168   public final int getJMSPriority() throws JMSException JavaDoc {
169     return momMsg.priority;
170   }
171    
172   /**
173    * API method.
174    *
175    * @exception JMSException If the priority value is incorrect.
176    */

177   public final void setJMSPriority(int priority) throws JMSException JavaDoc {
178     if (priority >= 0 && priority <= 9)
179       momMsg.priority = priority;
180     else
181       throw new JMSException JavaDoc("Priority of "+ priority +" is not valid"
182                              + " (should be an integer between 0 and 9).");
183   }
184
185   /**
186    * Returns the message destination.
187    * This field is set by <code>Session.send()</code>, it can be overloaded
188    * for received messages.
189    * API method.
190    *
191    * @exception JMSException Actually never thrown.
192    */

193   public final javax.jms.Destination JavaDoc getJMSDestination() throws JMSException JavaDoc {
194     if (jmsDest != null) return jmsDest;
195
196     if (momMsg.toId != null) {
197       try {
198         return Destination.newInstance(momMsg.toId, null, momMsg.toType);
199       } catch (Exception JavaDoc exc) {
200         // The destination name is unknown
201
throw new JMSException JavaDoc(exc.getMessage());
202       }
203     }
204     return null;
205   }
206
207   /**
208    * Set the message destination.
209    * This field is set when message is sent. This method can be used to
210    * change the value for a message that has been received.
211    * API method.
212    *
213    * @exception JMSException If the destination id not a Joram's one.
214    */

215   public final void setJMSDestination(javax.jms.Destination JavaDoc dest) throws JMSException JavaDoc {
216     jmsDest = dest;
217     if (dest == null) {
218       momMsg.setDestination(null, null);
219     } else if (dest instanceof org.objectweb.joram.client.jms.Destination) {
220       Destination d = (org.objectweb.joram.client.jms.Destination) dest;
221       momMsg.toId = d.getName();
222       momMsg.toType = d.getType();
223     }
224   }
225
226   /**
227    * Returns the message expiration time.
228    * API method.
229    *
230    * @exception JMSException Actually never thrown.
231    */

232   public final long getJMSExpiration() throws JMSException JavaDoc {
233     return momMsg.expiration;
234   }
235
236   /**
237    * API method.
238    *
239    * @exception JMSException Actually never thrown.
240    */

241   public final void setJMSExpiration(long expiration) throws JMSException JavaDoc {
242     if (expiration >= 0)
243       momMsg.expiration = expiration;
244   }
245
246   /**
247    * Gets an indication of whether this message is being redelivered.
248    * API method.
249    *
250    * @exception JMSException Actually never thrown.
251    */

252   public final boolean getJMSRedelivered() throws JMSException JavaDoc {
253     return momMsg.redelivered;
254   }
255
256   /**
257    * API method.
258    *
259    * @exception JMSException Actually never thrown.
260    */

261   public final void setJMSRedelivered(boolean redelivered) throws JMSException JavaDoc {
262     momMsg.redelivered = redelivered;
263   }
264
265   /**
266    * Gets the Destination object to which a reply to this message should
267    * be sent.
268    * API method.
269    *
270    * @exception JMSException Actually never thrown.
271    */

272   public final javax.jms.Destination JavaDoc getJMSReplyTo() throws JMSException JavaDoc {
273     if (momMsg.replyToId != null) {
274       // The destination name is unknown
275
try {
276         return Destination.newInstance(momMsg.replyToId, null, momMsg.replyToType);
277       } catch (Exception JavaDoc exc) {
278         throw new JMSException JavaDoc(exc.getMessage());
279       }
280     }
281     return null;
282   }
283
284   /**
285    * API method.
286    *
287    * @exception JMSException If the destination id not a Joram's one.
288    */

289   public final void setJMSReplyTo(javax.jms.Destination JavaDoc replyTo) throws JMSException JavaDoc {
290     try {
291       Destination d = (org.objectweb.joram.client.jms.Destination) replyTo;
292       momMsg.replyToId = d.getName();
293       momMsg.replyToType = d.getType();
294     } catch (NullPointerException JavaDoc npe) {
295       momMsg.replyToId = null;
296       momMsg.replyToType = null;
297     } catch (ClassCastException JavaDoc cce) {
298       throw new JMSException JavaDoc("Destination is not Joram compatible.");
299     }
300   }
301
302   /**
303    * Returns the message time stamp.
304    * API method.
305    *
306    * @exception JMSException Actually never thrown.
307    */

308   public final long getJMSTimestamp() throws JMSException JavaDoc {
309     return momMsg.timestamp;
310   }
311   
312   /**
313    * API method.
314    *
315    * @exception JMSException Actually never thrown.
316    */

317   public final void setJMSTimestamp(long timestamp) throws JMSException JavaDoc {
318     momMsg.timestamp = timestamp;
319   }
320
321   /**
322    * Returns the message correlation identifier.
323    * API method.
324    *
325    * @exception JMSException Actually never thrown.
326    */

327   public final String JavaDoc getJMSCorrelationID() throws JMSException JavaDoc {
328     return momMsg.correlationId;
329   }
330
331   /**
332    * API method.
333    *
334    * @exception JMSException Actually never thrown.
335    */

336   public final void setJMSCorrelationID(String JavaDoc correlationID) throws JMSException JavaDoc {
337     momMsg.correlationId = correlationID;
338   }
339   
340   /**
341    * API method.
342    *
343    * @exception MessageFormatException In case of a problem while retrieving
344    * the field.
345    */

346   public final byte[] getJMSCorrelationIDAsBytes() throws JMSException JavaDoc {
347     try {
348       return ConversionHelper.toBytes(momMsg.correlationId);
349     } catch (MessageValueException mE) {
350       throw new MessageFormatException JavaDoc(mE.getMessage());
351     }
352   }
353   
354   /**
355    * API method.
356    *
357    * @exception JMSException Actually never thrown.
358    */

359   public final void setJMSCorrelationIDAsBytes(byte[] correlationID) {
360     momMsg.correlationId = ConversionHelper.toString(correlationID);
361   }
362
363   /**
364    * Returns <code>true</code> if the message is persistent.
365    * API method.
366    *
367    * @exception JMSException Actually never thrown.
368    */

369   public final int getJMSDeliveryMode() throws JMSException JavaDoc {
370     if (momMsg.persistent)
371       return javax.jms.DeliveryMode.PERSISTENT;
372     else
373       return javax.jms.DeliveryMode.NON_PERSISTENT;
374   }
375
376   /**
377    * API method.
378    *
379    * @exception JMSException If the delivery mode is incorrect.
380    */

381   public final void setJMSDeliveryMode(int deliveryMode) throws JMSException JavaDoc {
382     if (deliveryMode != javax.jms.DeliveryMode.PERSISTENT &&
383         deliveryMode != javax.jms.DeliveryMode.NON_PERSISTENT)
384       throw new JMSException JavaDoc("Invalid delivery mode.");
385
386     momMsg.persistent = (deliveryMode == javax.jms.DeliveryMode.PERSISTENT);
387   }
388
389   /**
390    * API method.
391    *
392    * @exception JMSException Actually never thrown.
393    */

394   public final String JavaDoc getJMSType() throws JMSException JavaDoc {
395     return ConversionHelper.toString(momMsg.getOptionalHeader("JMSType"));
396   }
397
398   /**
399    * API method.
400    *
401    * @exception JMSException Actually never thrown.
402    */

403   public final void setJMSType(String JavaDoc type) throws JMSException JavaDoc {
404     momMsg.setOptionalHeader("JMSType", type);
405   }
406
407   /**
408    * Copies all of the mappings from the optionalHeader of this message to
409    * the specified hashtable. These mappings will replace any mappings that
410    * this Hashtable had for any of the keys currently in the optional header.
411    */

412   public void getOptionalHeader(Hashtable h) {
413     if (momMsg.optionalHeader == null) return;
414     momMsg.optionalHeader.copyInto(h);
415   }
416
417   // =========================================================
418
// API part about properties
419
// =========================================================
420

421   /** <code>true</code> if the properties are read-only. */
422   public boolean propertiesRO = false;
423
424   /**
425    * API method.
426    *
427    * @exception JMSException Actually never thrown.
428    */

429   public final void clearProperties() throws JMSException JavaDoc {
430     propertiesRO = false;
431     if (momMsg.properties == null) return;
432
433     momMsg.properties.clear();
434     momMsg.properties = null;
435   }
436
437   /**
438    * Resets the read-only flag, in order to allow the modification
439    * of message properties.
440    *
441    * @exception JMSException Actually never thrown.
442    */

443   public final void resetPropertiesRO() throws JMSException JavaDoc {
444     propertiesRO = false;
445   }
446
447   /**
448    * API method.
449    *
450    * @param name The property name.
451    *
452    * @exception JMSException Actually never thrown.
453    */

454   public final boolean propertyExists(String JavaDoc name) throws JMSException JavaDoc {
455     if (momMsg.properties == null)
456       return false;
457
458     return momMsg.properties.containsKey(name);
459   }
460
461   /**
462    * Copies all of the mappings from the properties of this message to
463    * the specified hashtable. These mappings will replace any mappings that
464    * this Hashtable had for any of the keys currently in the properties.
465    */

466   public void getProperties(Hashtable h) {
467     if (momMsg.properties == null) return;
468     momMsg.properties.copyInto(h);
469   }
470
471   /**
472    * API method.
473    *
474    * @exception JMSException Actually never thrown.
475    */

476   public final Enumeration getPropertyNames() throws JMSException JavaDoc {
477     if (momMsg.properties == null)
478       return (new Hashtable()).keys();
479
480     return momMsg.properties.keys();
481   }
482
483   /**
484    * API method.
485    *
486    * @param name The property name.
487    * @param value The property value.
488    *
489    * @exception MessageNotWriteableException If the message is read-only.
490    * @exception JMSException If the property name is invalid.
491    */

492   public final void setBooleanProperty(String JavaDoc name, boolean value) throws JMSException JavaDoc {
493     doSetProperty(name, new Boolean JavaDoc(value));
494   }
495
496   /**
497    * API method.
498    *
499    * @param name The property name.
500    * @param value The property value.
501    *
502    * @exception MessageNotWriteableException If the message is read-only.
503    * @exception JMSException If the property name is invalid.
504    */

505   public final void setByteProperty(String JavaDoc name, byte value) throws JMSException JavaDoc {
506     doSetProperty(name, new Byte JavaDoc(value));
507   }
508
509   /**
510    * API method.
511    *
512    * @param name The property name.
513    * @param value The property value.
514    *
515    * @exception MessageNotWriteableException If the message is read-only.
516    * @exception JMSException If the property name is invalid.
517    */

518   public final void setShortProperty(String JavaDoc name, short value) throws JMSException JavaDoc {
519     doSetProperty(name, new Short JavaDoc(value));
520   }
521
522   /**
523    * API method.
524    *
525    * @param name The property name.
526    * @param value The property value.
527    *
528    * @exception MessageNotWriteableException If the message is read-only.
529    * @exception JMSException If the property name is invalid.
530    */

531   public final void setIntProperty(String JavaDoc name, int value) throws JMSException JavaDoc {
532     doSetProperty(name, new Integer JavaDoc(value));
533   }
534
535   /**
536    * API method.
537    *
538    * @param name The property name.
539    * @param value The property value.
540    *
541    * @exception MessageNotWriteableException If the message is read-only.
542    * @exception JMSException If the property name is invalid.
543    */

544   public final void setLongProperty(String JavaDoc name, long value) throws JMSException JavaDoc {
545     doSetProperty(name, new Long JavaDoc(value));
546   }
547
548   /**
549    * API method.
550    *
551    * @param name The property name.
552    * @param value The property value.
553    *
554    * @exception MessageNotWriteableException If the message is read-only.
555    * @exception JMSException If the property name is invalid.
556    */

557   public final void setFloatProperty(String JavaDoc name, float value) throws JMSException JavaDoc {
558     doSetProperty(name, new Float JavaDoc(value));
559   }
560
561   /**
562    * API method.
563    *
564    * @param name The property name.
565    * @param value The property value.
566    *
567    * @exception MessageNotWriteableException If the message is read-only.
568    * @exception JMSException If the property name is invalid.
569    */

570   public final void setDoubleProperty(String JavaDoc name, double value) throws JMSException JavaDoc {
571     doSetProperty(name, new Double JavaDoc(value));
572   }
573
574   /**
575    * API method.
576    *
577    * @param name The property name.
578    * @param value The property value.
579    *
580    * @exception MessageNotWriteableException If the message is read-only.
581    * @exception JMSException If the property name is invalid.
582    */

583   public final void setStringProperty(String JavaDoc name, String JavaDoc value) throws JMSException JavaDoc {
584     doSetProperty(name, value);
585   }
586
587   /**
588    * API method, sets a property value.
589    *
590    * @param name The property name.
591    * @param value The property value.
592    *
593    * @exception MessageROException
594    * If the message properties are read-only.
595    * @exception MessageFormatException
596    * If the value is not a Java primitive object.
597    * @exception IllegalArgumentException
598    * If the key name is illegal (null or empty string).
599    *
600    * @exception MessageFormatException If the property type is invalid.
601    * @exception MessageNotWriteableException If the message is read-only.
602    * @exception JMSException If the property name is invalid, or if the
603    * object is invalid.
604    */

605   public final void setObjectProperty(String JavaDoc name, Object JavaDoc value) throws JMSException JavaDoc {
606     if (value instanceof Boolean JavaDoc ||
607         value instanceof Number JavaDoc ||
608         value instanceof String JavaDoc) {
609       doSetProperty(name, value);
610     } else {
611       throw new MessageFormatException JavaDoc("Can't set non primitive Java object as a property value.");
612     }
613   }
614  
615   /**
616    * Method actually setting a new property.
617    *
618    * @param name The property name.
619    * @param value The property value.
620    *
621    * @exception MessageFormatException If the property type is invalid.
622    * @exception MessageNotWriteableException If the message is read-only.
623    * @exception JMSException If the name is invalid.
624    * @exception IllegalArgumentException If the name string is null or empty.
625    */

626   private final void doSetProperty(String JavaDoc name, Object JavaDoc value) throws JMSException JavaDoc {
627     if (name == null || name.equals(""))
628       throw new IllegalArgumentException JavaDoc("Invalid property name: " + name);
629
630     if (name.startsWith("JMSX")) {
631       if (name.equals("JMSXGroupID")) {
632         momMsg.setOptionalHeader(name, ConversionHelper.toString(value));
633       } else if (name.equals("JMSXGroupSeq")) {
634         try {
635           momMsg.setOptionalHeader(name,
636                                    new Integer JavaDoc(ConversionHelper.toInt(value)));
637         } catch (MessageValueException mE) {
638           throw new MessageFormatException JavaDoc(mE.getMessage());
639         }
640       } else {
641         throw new JMSException JavaDoc("Property names with prefix 'JMSX' are reserved.");
642       }
643     } else if (name.startsWith("JMS")) {
644       throw new JMSException JavaDoc("Property names with prefix 'JMS' are reserved.");
645     } else if (name.equalsIgnoreCase("NULL") ||
646                name.equalsIgnoreCase("TRUE") ||
647                name.equalsIgnoreCase("FALSE") ||
648                name.equalsIgnoreCase("NOT") ||
649                name.equalsIgnoreCase("AND") ||
650                name.equalsIgnoreCase("OR") ||
651                name.equalsIgnoreCase("BETWEEN") ||
652                name.equalsIgnoreCase("LIKE") ||
653                name.equalsIgnoreCase("IN") ||
654                name.equalsIgnoreCase("IS") ||
655                name.equalsIgnoreCase("ESCAPE")) {
656       throw new JMSException JavaDoc("Invalid property name cannot use SQL terminal: " + name);
657     } else {
658       if (propertiesRO)
659         throw new MessageNotWriteableException JavaDoc("Can't set property as the message properties are READ-ONLY.");
660
661       momMsg.setProperty(name, value);
662     }
663   }
664
665   /**
666    * API method.
667    *
668    * @param name The property name.
669    *
670    * @exception MessageFormatException If the property type is invalid.
671    * @exception JMSException If the name is invalid.
672    */

673   public final boolean getBooleanProperty(String JavaDoc name) throws JMSException JavaDoc {
674     try {
675       return ConversionHelper.toBoolean(doGetProperty(name));
676     } catch (MessageValueException mE) {
677       throw new MessageFormatException JavaDoc(mE.getMessage());
678     }
679   }
680   
681   /**
682    * API method.
683    *
684    * @param name The property name.
685    *
686    * @exception MessageFormatException If the property type is invalid.
687    * @exception JMSException If the name is invalid.
688    */

689   public final byte getByteProperty(String JavaDoc name) throws JMSException JavaDoc {
690     try {
691       return ConversionHelper.toByte(doGetProperty(name));
692     } catch (MessageValueException mE) {
693       throw new MessageFormatException JavaDoc(mE.getMessage());
694     }
695   }
696
697   /**
698    * API method.
699    *
700    * @param name The property name.
701    *
702    * @exception MessageFormatException If the property type is invalid.
703    * @exception JMSException If the name is invalid.
704    */

705   public final short getShortProperty(String JavaDoc name) throws JMSException JavaDoc {
706     try {
707       return ConversionHelper.toShort(doGetProperty(name));
708     } catch (MessageValueException mE) {
709       throw new MessageFormatException JavaDoc(mE.getMessage());
710     }
711   }
712
713   /**
714    * API method.
715    *
716    * @param name The property name.
717    *
718    * @exception MessageFormatException If the property type is invalid.
719    * @exception JMSException If the name is invalid.
720    */

721   public final int getIntProperty(String JavaDoc name) throws JMSException JavaDoc {
722     try {
723       return ConversionHelper.toInt(doGetProperty(name));
724     } catch (MessageValueException mE) {
725       throw new MessageFormatException JavaDoc(mE.getMessage());
726     }
727   }
728
729   /**
730    * API method.
731    *
732    * @param name The property name.
733    *
734    * @exception MessageFormatException If the property type is invalid.
735    * @exception JMSException If the name is invalid.
736    */

737   public final long getLongProperty(String JavaDoc name) throws JMSException JavaDoc {
738     try {
739       return ConversionHelper.toLong(doGetProperty(name));
740     } catch (MessageValueException mE) {
741       throw new MessageFormatException JavaDoc(mE.getMessage());
742     }
743   }
744
745   /**
746    * API method.
747    *
748    * @param name The property name.
749    *
750    * @exception MessageFormatException If the property type is invalid.
751    * @exception JMSException If the name is invalid.
752    */

753   public final float getFloatProperty(String JavaDoc name) throws JMSException JavaDoc {
754     try {
755       return ConversionHelper.toFloat(doGetProperty(name));
756     } catch (MessageValueException mE) {
757       throw new MessageFormatException JavaDoc(mE.getMessage());
758     }
759   }
760
761   /**
762    * API method.
763    *
764    * @param name The property name.
765    *
766    * @exception MessageFormatException If the property type is invalid.
767    * @exception JMSException If the name is invalid.
768    */

769   public final double getDoubleProperty(String JavaDoc name) throws JMSException JavaDoc {
770     try {
771       return ConversionHelper.toDouble(doGetProperty(name));
772     } catch (MessageValueException mE) {
773       throw new MessageFormatException JavaDoc(mE.getMessage());
774     }
775   }
776
777   /**
778    * API method.
779    *
780    * @param name The property name.
781    *
782    * @exception MessageFormatException If the property type is invalid.
783    * @exception JMSException If the name is invalid.
784    */

785   public final String JavaDoc getStringProperty(String JavaDoc name) throws JMSException JavaDoc {
786       return ConversionHelper.toString(doGetProperty(name));
787   }
788
789   /**
790    * API method.
791    *
792    * @param name The property name.
793    *
794    * @exception JMSException If the name is invalid.
795    */

796   public final Object JavaDoc getObjectProperty(String JavaDoc name) throws JMSException JavaDoc {
797     return doGetProperty(name);
798   }
799
800   /**
801    * Method actually getting a property.
802    *
803    * @param name The property name.
804    */

805   private final Object JavaDoc doGetProperty(String JavaDoc name) {
806     if (name == null || name.equals(""))
807       throw new IllegalArgumentException JavaDoc("Invalid property name: " + name);
808
809     if (name.startsWith("JMSX")) {
810       if (name.equals("JMSXDeliveryCount"))
811         return new Integer JavaDoc(momMsg.deliveryCount);
812       else
813         return momMsg.getOptionalHeader(name);
814     } else if (name.startsWith("JMS_JORAM")) {
815       if (name.equals("JMS_JORAM_DELETEDDEST"))
816         return new Boolean JavaDoc(momMsg.deletedDest);
817       else if (name.equals("JMS_JORAM_NOTWRITABLE"))
818         return new Boolean JavaDoc(momMsg.notWriteable);
819       else if (name.equals("JMS_JORAM_EXPIRED"))
820         return new Boolean JavaDoc(momMsg.expired);
821       else if (name.equals("JMS_JORAM_UNDELIVERABLE"))
822         return new Boolean JavaDoc(momMsg.undeliverable);
823     } else {
824       return momMsg.getProperty(name);
825     }
826     return null;
827   }
828
829   /**
830    * Converts a non-Joram JMS message into a Joram message.
831    *
832    * @exception JMSException If an error occurs while building the message.
833    */

834   static public Message convertJMSMessage(javax.jms.Message JavaDoc jmsMsg) throws JMSException JavaDoc {
835     Message joramMsg = null;
836     if (jmsMsg instanceof javax.jms.TextMessage JavaDoc) {
837       joramMsg = new TextMessage();
838       ((javax.jms.TextMessage JavaDoc) joramMsg).setText(((javax.jms.TextMessage JavaDoc) jmsMsg).getText());
839     } else if (jmsMsg instanceof javax.jms.ObjectMessage JavaDoc) {
840       joramMsg = new ObjectMessage();
841       ((javax.jms.ObjectMessage JavaDoc) joramMsg).setObject(((javax.jms.ObjectMessage JavaDoc) jmsMsg).getObject());
842     } else if (jmsMsg instanceof javax.jms.StreamMessage JavaDoc) {
843       joramMsg = new StreamMessage();
844       try {
845         ((javax.jms.StreamMessage JavaDoc) jmsMsg).reset();
846         while (true)
847           ((StreamMessage) joramMsg).writeObject(((javax.jms.StreamMessage JavaDoc) jmsMsg).readObject());
848       } catch (Exception JavaDoc mE) {}
849     } else if (jmsMsg instanceof javax.jms.BytesMessage JavaDoc) {
850       joramMsg = new BytesMessage();
851       try {
852         ((javax.jms.BytesMessage JavaDoc) jmsMsg).reset();
853         while (true)
854           ((BytesMessage) joramMsg).writeByte(((javax.jms.BytesMessage JavaDoc) jmsMsg).readByte());
855       } catch (Exception JavaDoc mE) {}
856     } else if (jmsMsg instanceof javax.jms.MapMessage JavaDoc) {
857       joramMsg = new MapMessage();
858       Enumeration mapNames = ((javax.jms.MapMessage JavaDoc) jmsMsg).getMapNames();
859       String JavaDoc mapName;
860       while (mapNames.hasMoreElements()) {
861         mapName = (String JavaDoc) mapNames.nextElement();
862         ((javax.jms.MapMessage JavaDoc) joramMsg).setObject(mapName,
863                                                ((javax.jms.MapMessage JavaDoc)
864                                                 jmsMsg).getObject(mapName));
865       }
866     } else {
867       joramMsg = new Message();
868     }
869
870     joramMsg.setJMSDestination(jmsMsg.getJMSDestination());
871     joramMsg.setJMSCorrelationID(jmsMsg.getJMSCorrelationID());
872     joramMsg.setJMSReplyTo(jmsMsg.getJMSReplyTo());
873     joramMsg.setJMSType(jmsMsg.getJMSType());
874     joramMsg.setJMSMessageID(jmsMsg.getJMSMessageID());
875
876     Enumeration names = jmsMsg.getPropertyNames();
877     if (names != null) {
878       String JavaDoc name;
879       while (names.hasMoreElements()) {
880         name = (String JavaDoc) names.nextElement();
881         try {
882           joramMsg.setObjectProperty(name, jmsMsg.getObjectProperty(name));
883         } catch (JMSException JavaDoc e) {
884           // Joram not support other Optional JMSX, just ignore.
885
if (! name.startsWith("JMSX") && ! name.startsWith("JMS_"))
886             throw e;
887         }
888       }
889     }
890
891     return joramMsg;
892   }
893
894   /**
895    * Method preparing the message for sending; resets header values, and
896    * serializes the body (done in subclasses).
897    *
898    * @exception MessageFormatException If an error occurs while serializing.
899    */

900   protected void prepare() throws JMSException JavaDoc {
901     momMsg.redelivered = false;
902     momMsg.deletedDest = false;
903     momMsg.expired = false;
904     momMsg.notWriteable = false;
905     momMsg.undeliverable = false;
906   }
907
908   
909   /**
910    * @return the momMsg
911    */

912   public org.objectweb.joram.shared.messages.Message getMomMsg() {
913     return momMsg;
914   }
915
916   public void toString(StringBuffer JavaDoc strbuf) {
917     try {
918       strbuf.append('(');
919       strbuf.append(super.toString());
920       strbuf.append(",JMSMessageID=").append(getJMSMessageID());
921       try {
922         strbuf.append(",JMSDestination=").append(getJMSDestination());
923       } catch (JMSException JavaDoc exc) {
924         JoramTracing.dbgClient.log(BasicLevel.ERROR, "Message.toString()", exc);
925       }
926       strbuf.append(",JMSCorrelationID=").append(getJMSCorrelationID());
927       strbuf.append(",JMSDeliveryMode=").append(getJMSDeliveryMode());
928       strbuf.append(",JMSExpiration=").append(getJMSExpiration());
929       strbuf.append(",JMSPriority=").append(getJMSPriority());
930       strbuf.append(",JMSRedelivered=").append(getJMSRedelivered());
931       try {
932         strbuf.append(",JMSReplyTo=").append(getJMSReplyTo());
933       } catch (JMSException JavaDoc exc) {
934         JoramTracing.dbgClient.log(BasicLevel.ERROR, "Message.toString()", exc);
935       }
936       strbuf.append(",JMSTimestamp=").append(getJMSTimestamp());
937       strbuf.append(",JMSType=").append(getJMSType());
938       strbuf.append(')');
939     } catch (JMSException JavaDoc exc) {
940       // Should never happened
941
}
942   }
943 }
944
Popular Tags