KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > SpyMessage


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.jms.Destination JavaDoc;
36 import javax.jms.JMSException JavaDoc;
37 import javax.jms.Message JavaDoc;
38 import javax.jms.MessageFormatException JavaDoc;
39 import javax.jms.MessageNotWriteableException JavaDoc;
40 import javax.jms.Session JavaDoc;
41
42 import org.jboss.util.Primitives;
43 import org.jboss.util.Strings;
44
45 /**
46  * This class implements javax.jms.Message
47  *
48  * @author Norbert Lataille (Norbert.Lataille@m4x.org)
49  * @author Hiram Chirino (Cojonudo14@hotmail.com)
50  * @author David Maplesden (David.Maplesden@orion.co.nz)
51  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
52  * @version $Revision: 38291 $
53  */

54 public class SpyMessage implements Serializable JavaDoc, Message JavaDoc, Comparable JavaDoc, Cloneable JavaDoc, Externalizable JavaDoc
55 {
56    // Constants -----------------------------------------------------
57

58    /** The serialVersionUID */
59    private final static long serialVersionUID = 467206190892964404L;
60
61    /**
62     * Standard property for delivery count
63     */

64    public static final String JavaDoc PROPERTY_DELIVERY_COUNT = "JMSXDeliveryCount";
65
66    /**
67      * JBoss-vendor specific property for scheduling a JMS message. In
68      * milliseconds since January 1, 1970.
69      */

70    public static final String JavaDoc PROPERTY_SCHEDULED_DELIVERY = "JMS_JBOSS_SCHEDULED_DELIVERY";
71
72    /**
73      * JBoss-vendor specific property specifying redelivery delay of a message.
74      * The message will be rescheduled for delivery from the time at which it
75      * was unacknowledged, plus the given period.
76      */

77    public static final String JavaDoc PROPERTY_REDELIVERY_DELAY = "JMS_JBOSS_REDELIVERY_DELAY";
78
79    /**
80      * JBoss-vendor specific property for getting the count of redelivery
81      * attempts of a message.
82      */

83    public static final String JavaDoc PROPERTY_REDELIVERY_COUNT = "JMS_JBOSS_REDELIVERY_COUNT";
84
85    /**
86      * JBoss-vendor specific property specifying the limit of redelivery
87      * attempts of a message. The message will be redelivered a given number of
88      * times. If not set, the container default is used.
89      */

90    public static final String JavaDoc PROPERTY_REDELIVERY_LIMIT = "JMS_JBOSS_REDELIVERY_LIMIT";
91
92    /**
93    * JBoss-vendor property name holding original destination.
94    */

95    public static final String JavaDoc PROPERTY_ORIG_DESTINATION = "JBOSS_ORIG_DESTINATION";
96
97    /**
98    * JBoss-vendor property name holding original expiration value.
99    */

100    public static final String JavaDoc PROPERTY_ORIG_EXPIRATION = "JBOSS_ORIG_EXPIRATION";
101
102    /**
103    * JBoss-vendor property name holding original message ID value.
104    */

105    public static final String JavaDoc PROPERTY_ORIG_MESSAGEID = "JBOSS_ORIG_MESSAGEID";
106
107    /** An object message */
108    protected static final byte OBJECT_MESS = 1;
109    /** An bytes message */
110    protected static final byte BYTES_MESS = 2;
111    /** A map message */
112    protected static final byte MAP_MESS = 3;
113    /** A text message */
114    protected static final byte TEXT_MESS = 4;
115    /** A stream message */
116    protected static final byte STREAM_MESS = 5;
117    /** An encapsulated message */
118    protected static final byte ENCAP_MESS = 6;
119    /** A plain message */
120    protected static final byte SPY_MESS = 7;
121
122    /** A byte property */
123    protected static final int BYTE = 0;
124    /** A short property */
125    protected static final int SHORT = 1;
126    /** An integer property */
127    protected static final int INT = 2;
128    /** A long property */
129    protected static final int LONG = 3;
130    /** A float property */
131    protected static final int FLOAT = 4;
132    /** A double property */
133    protected static final int DOUBLE = 5;
134    /** A boolean property */
135    protected static final int BOOLEAN = 6;
136    /** A string property */
137    protected static final int STRING = 7;
138    /** An object property */
139    protected static final int OBJECT = 8;
140    /** A null property */
141    protected static final int NULL = 9;
142
143    /** Reserved identifiers */
144    private static final HashSet JavaDoc reservedIdentifiers = new HashSet JavaDoc();
145
146    // Attributes ----------------------------------------------------
147

148    /** The message header */
149    public Header header = new Header();
150
151    /** The acknowledgement request for this message */
152    public transient AcknowledgementRequest ack;
153
154    /** The session for this message */
155    public transient SpySession session;
156
157    // Static --------------------------------------------------------
158

159    static
160    {
161       reservedIdentifiers.add("NULL");
162       reservedIdentifiers.add("TRUE");
163       reservedIdentifiers.add("FALSE");
164       reservedIdentifiers.add("NOT");
165       reservedIdentifiers.add("AND");
166       reservedIdentifiers.add("OR");
167       reservedIdentifiers.add("BETWEEN");
168       reservedIdentifiers.add("LIKE");
169       reservedIdentifiers.add("IN");
170       reservedIdentifiers.add("IS");
171       reservedIdentifiers.add("ESCAPE");
172    }
173
174    /**
175      * Write a message
176      *
177      * @param message the message
178      * @param out the output
179      * @throws IOException for any error
180      */

181    public static void writeMessage(SpyMessage message, ObjectOutput JavaDoc out) throws IOException JavaDoc
182    {
183       if (message instanceof SpyEncapsulatedMessage)
184          out.writeByte(ENCAP_MESS);
185       else if (message instanceof SpyObjectMessage)
186          out.writeByte(OBJECT_MESS);
187       else if (message instanceof SpyBytesMessage)
188          out.writeByte(BYTES_MESS);
189       else if (message instanceof SpyMapMessage)
190          out.writeByte(MAP_MESS);
191       else if (message instanceof SpyTextMessage)
192          out.writeByte(TEXT_MESS);
193       else if (message instanceof SpyStreamMessage)
194          out.writeByte(STREAM_MESS);
195       else
196          out.writeByte(SPY_MESS);
197       message.writeExternal(out);
198    }
199
200    /**
201      * Read a message
202      *
203      * @param in the input
204      * @return the message
205      * @throws IOException for any error
206      */

207    public static SpyMessage readMessage(ObjectInput JavaDoc in) throws IOException JavaDoc
208    {
209       SpyMessage message = null;
210       byte type = in.readByte();
211       switch (type)
212       {
213          case OBJECT_MESS :
214             message = MessagePool.getObjectMessage();
215             break;
216          case BYTES_MESS :
217             message = MessagePool.getBytesMessage();
218             break;
219          case MAP_MESS :
220             message = MessagePool.getMapMessage();
221             break;
222          case STREAM_MESS :
223             message = MessagePool.getStreamMessage();
224             break;
225          case TEXT_MESS :
226             message = MessagePool.getTextMessage();
227             break;
228          case ENCAP_MESS :
229             message = MessagePool.getEncapsulatedMessage();
230             break;
231          default :
232             message = MessagePool.getMessage();
233       }
234       try
235       {
236          message.readExternal(in);
237       }
238       catch (ClassNotFoundException JavaDoc cnf)
239       {
240          throw new IOException JavaDoc("Class not found when reading in spy message.");
241       }
242       return message;
243    }
244
245    // Constructors --------------------------------------------------
246

247    // Public --------------------------------------------------------
248

249    /**
250     * Clear the message body
251     *
252     * @throws JMSException for any error
253     */

254    public void clearBody() throws JMSException JavaDoc
255    {
256       //Inherited classes clear their content here
257
header.msgReadOnly = false;
258    }
259
260    /**
261     * Acknowledge a message
262     *
263     * @throws JMSException for any error
264     */

265    public void acknowledge() throws JMSException JavaDoc
266    {
267       if (session == null)
268          throw new JMSException JavaDoc("This message was not recieved from the provider");
269
270       if (session.acknowledgeMode == Session.CLIENT_ACKNOWLEDGE)
271          doAcknowledge();
272    }
273
274    /**
275     * Set the message to read only
276     */

277    public void setReadOnlyMode()
278    {
279       header.jmsPropertiesReadWrite = false;
280       header.msgReadOnly = true;
281    }
282
283    /**
284     * Clone the message
285     *
286     * @return the cloned message
287     * @throws JMSException for any error
288     */

289    public SpyMessage myClone() throws JMSException JavaDoc
290    {
291       SpyMessage result = MessagePool.getMessage();
292       result.copyProps(this);
293       return result;
294    }
295
296    /**
297     * Copy the properties
298     *
299     * @param original the message with original properties
300     * @throws JMSException for any error
301     */

302    public void copyProps(SpyMessage original) throws JMSException JavaDoc
303    {
304       try
305       {
306          this.setJMSCorrelationID(original.getJMSCorrelationID());
307       }
308       catch (JMSException JavaDoc e)
309       {
310          //must be as bytes
311
this.setJMSCorrelationIDAsBytes(original.getJMSCorrelationIDAsBytes());
312       }
313       this.setJMSDeliveryMode(original.getJMSDeliveryMode());
314       this.setJMSDestination(original.getJMSDestination());
315       this.setJMSExpiration(original.getJMSExpiration());
316       this.setJMSMessageID(original.getJMSMessageID());
317       this.setJMSPriority(original.getJMSPriority());
318       this.setJMSRedelivered(original.getJMSRedelivered());
319       this.setJMSReplyTo(original.getJMSReplyTo());
320       this.setJMSTimestamp(original.getJMSTimestamp());
321       this.setJMSType(original.getJMSType());
322       this.header.jmsProperties.putAll(original.header.jmsProperties);
323
324       //Spy Message special header.jmsPropertiess
325
this.header.jmsPropertiesReadWrite = original.header.jmsPropertiesReadWrite;
326       this.header.msgReadOnly = original.header.msgReadOnly;
327       this.header.producerClientId = original.header.producerClientId;
328       if (original.header.durableSubscriberID != null)
329          this.header.durableSubscriberID = new DurableSubscriptionID(original.header.durableSubscriberID.clientID,
330                original.header.durableSubscriberID.subscriptionName, original.header.durableSubscriberID.selector);
331    }
332
333    /**
334      * Test whether a message has expired
335      *
336      * @return true when expired false otherwise
337      */

338    public boolean isOutdated()
339    {
340       if (header.jmsExpiration == 0)
341          return false;
342       long ts = System.currentTimeMillis();
343       return header.jmsExpiration < ts;
344    }
345
346    /**
347     * Actually acknowledge a message
348     *
349     * @throws JMSException for any error
350     */

351    public void doAcknowledge() throws JMSException JavaDoc
352    {
353       session.doAcknowledge(this, getAcknowledgementRequest(true));
354    }
355
356    /**
357     * Create an acknowledgement request for the message
358     */

359    public void createAcknowledgementRequest(int subscriptionId)
360    {
361       ack = new AcknowledgementRequest();
362       ack.destination = header.jmsDestination;
363       ack.messageID = header.jmsMessageID;
364       ack.subscriberId = subscriptionId;
365    }
366
367    /**
368     * Get an acknowledgement request for the message
369     *
370     * @param isAck true for an ack, false for a nack
371     * @throws JMSException for any error
372     */

373    public AcknowledgementRequest getAcknowledgementRequest(boolean isAck) throws JMSException JavaDoc
374    {
375       //don't know if we have to copy but to be on safe side...
376
AcknowledgementRequest item = new AcknowledgementRequest(isAck);
377       item.destination = ack.destination;
378       item.messageID = ack.messageID;
379       item.subscriberId = ack.subscriberId;
380       return item;
381    }
382
383    // Comparable implementation -------------------------------------
384

385    public int compareTo(Object JavaDoc o)
386    {
387       SpyMessage sm = (SpyMessage) o;
388
389       if (header.jmsPriority > sm.header.jmsPriority)
390       {
391          return -1;
392       }
393       if (header.jmsPriority < sm.header.jmsPriority)
394       {
395          return 1;
396       }
397       return (int) (header.messageId - sm.header.messageId);
398    }
399
400    // Message implementation ----------------------------------------
401

402    public String JavaDoc getJMSMessageID()
403    {
404       return header.jmsMessageID;
405    }
406
407    public void setJMSMessageID(String JavaDoc id) throws JMSException JavaDoc
408    {
409       header.jmsMessageID = id;
410    }
411
412    public long getJMSTimestamp()
413    {
414       return header.jmsTimeStamp;
415    }
416
417    public void setJMSTimestamp(long timestamp) throws JMSException JavaDoc
418    {
419       header.jmsTimeStamp = timestamp;
420    }
421
422    public byte[] getJMSCorrelationIDAsBytes() throws JMSException JavaDoc
423    {
424       if (header.jmsCorrelationID)
425          throw new JMSException JavaDoc("JMSCorrelationID is a string");
426       return header.jmsCorrelationIDbyte;
427    }
428
429    public void setJMSCorrelationIDAsBytes(byte[] correlationID) throws JMSException JavaDoc
430    {
431       header.jmsCorrelationID = false;
432       header.jmsCorrelationIDbyte = (byte[]) correlationID.clone();
433       header.jmsCorrelationIDString = null;
434    }
435
436    public void setJMSCorrelationID(String JavaDoc correlationID) throws JMSException JavaDoc
437    {
438       header.jmsCorrelationID = true;
439       header.jmsCorrelationIDString = correlationID;
440       header.jmsCorrelationIDbyte = null;
441    }
442
443    public String JavaDoc getJMSCorrelationID() throws JMSException JavaDoc
444    {
445       if (!header.jmsCorrelationID)
446          throw new JMSException JavaDoc("JMSCorrelationID is an array");
447       return header.jmsCorrelationIDString;
448    }
449
450    public Destination JavaDoc getJMSReplyTo()
451    {
452       return header.jmsReplyTo;
453    }
454
455    public void setJMSReplyTo(Destination JavaDoc replyTo) throws JMSException JavaDoc
456    {
457       header.jmsReplyTo = replyTo;
458    }
459
460    public Destination JavaDoc getJMSDestination()
461    {
462       return header.jmsDestination;
463    }
464
465    public void setJMSDestination(Destination JavaDoc destination) throws JMSException JavaDoc
466    {
467       header.jmsDestination = destination;
468    }
469
470    public int getJMSDeliveryMode()
471    {
472       return header.jmsDeliveryMode;
473    }
474
475    public void setJMSDeliveryMode(int deliveryMode) throws JMSException JavaDoc
476    {
477       header.jmsDeliveryMode = deliveryMode;
478    }
479
480    public boolean getJMSRedelivered()
481    {
482       return header.jmsRedelivered;
483    }
484
485    public void setJMSRedelivered(boolean redelivered) throws JMSException JavaDoc
486    {
487       header.jmsRedelivered = redelivered;
488    }
489
490    public String JavaDoc getJMSType()
491    {
492       return header.jmsType;
493    }
494
495    public void setJMSType(String JavaDoc type) throws JMSException JavaDoc
496    {
497       header.jmsType = type;
498    }
499
500    public long getJMSExpiration()
501    {
502       return header.jmsExpiration;
503    }
504
505    public void setJMSExpiration(long expiration) throws JMSException JavaDoc
506    {
507       header.jmsExpiration = expiration;
508    }
509
510    public int getJMSPriority()
511    {
512       return header.jmsPriority;
513    }
514
515    public void setJMSPriority(int priority) throws JMSException JavaDoc
516    {
517       if (priority < 0 || priority > 10)
518          throw new JMSException JavaDoc("Unsupported priority '" + priority + "': priority must be from 0-10");
519       header.jmsPriority = priority;
520    }
521
522    public void clearProperties() throws JMSException JavaDoc
523    {
524       header.jmsProperties.clear();
525       header.jmsPropertiesReadWrite = true;
526    }
527
528    public boolean propertyExists(String JavaDoc name) throws JMSException JavaDoc
529    {
530       return header.jmsProperties.containsKey(name);
531    }
532
533    public boolean getBooleanProperty(String JavaDoc name) throws JMSException JavaDoc
534    {
535       Object JavaDoc value = header.jmsProperties.get(name);
536       if (value == null)
537          return Boolean.valueOf(null).booleanValue();
538
539       if (value instanceof Boolean JavaDoc)
540          return ((Boolean JavaDoc) value).booleanValue();
541       else if (value instanceof String JavaDoc)
542          return Boolean.valueOf((String JavaDoc) value).booleanValue();
543       else
544          throw new MessageFormatException JavaDoc("Invalid conversion");
545    }
546
547    public byte getByteProperty(String JavaDoc name) throws JMSException JavaDoc
548    {
549       Object JavaDoc value = header.jmsProperties.get(name);
550       if (value == null)
551          throw new NumberFormatException JavaDoc("Message property '" + name + "' not set.");
552
553       if (value instanceof Byte JavaDoc)
554          return ((Byte JavaDoc) value).byteValue();
555       else if (value instanceof String JavaDoc)
556          return Byte.parseByte((String JavaDoc) value);
557       else
558          throw new MessageFormatException JavaDoc("Invalid conversion");
559    }
560
561    public short getShortProperty(String JavaDoc name) throws JMSException JavaDoc
562    {
563       Object JavaDoc value = header.jmsProperties.get(name);
564       if (value == null)
565          throw new NumberFormatException JavaDoc("Message property '" + name + "' not set.");
566
567       if (value instanceof Byte JavaDoc)
568          return ((Byte JavaDoc) value).shortValue();
569       else if (value instanceof Short JavaDoc)
570          return ((Short JavaDoc) value).shortValue();
571       else if (value instanceof String JavaDoc)
572          return Short.parseShort((String JavaDoc) value);
573       else
574          throw new MessageFormatException JavaDoc("Invalid conversion");
575    }
576
577    public int getIntProperty(String JavaDoc name) throws JMSException JavaDoc
578    {
579       Object JavaDoc value = header.jmsProperties.get(name);
580       if (value == null)
581          throw new NumberFormatException JavaDoc("Message property '" + name + "' not set.");
582
583       if (value instanceof Byte JavaDoc)
584          return ((Byte JavaDoc) value).intValue();
585       else if (value instanceof Short JavaDoc)
586          return ((Short JavaDoc) value).intValue();
587       else if (value instanceof Integer JavaDoc)
588          return ((Integer JavaDoc) value).intValue();
589       else if (value instanceof String JavaDoc)
590          return Integer.parseInt((String JavaDoc) value);
591       else
592          throw new MessageFormatException JavaDoc("Invalid conversion");
593    }
594
595    public long getLongProperty(String JavaDoc name) throws JMSException JavaDoc
596    {
597       Object JavaDoc value = header.jmsProperties.get(name);
598       if (value == null)
599          throw new NumberFormatException JavaDoc("Message property '" + name + "' not set.");
600
601       if (value instanceof Byte JavaDoc)
602          return ((Byte JavaDoc) value).longValue();
603       else if (value instanceof Short JavaDoc)
604          return ((Short JavaDoc) value).longValue();
605       else if (value instanceof Integer JavaDoc)
606          return ((Integer JavaDoc) value).longValue();
607       else if (value instanceof Long JavaDoc)
608          return ((Long JavaDoc) value).longValue();
609       else if (value instanceof String JavaDoc)
610          return Long.parseLong((String JavaDoc) value);
611       else
612          throw new MessageFormatException JavaDoc("Invalid conversion");
613    }
614
615    public float getFloatProperty(String JavaDoc name) throws JMSException JavaDoc
616    {
617       Object JavaDoc value = header.jmsProperties.get(name);
618       if (value == null)
619          return Float.valueOf(null).floatValue();
620
621       if (value instanceof Float JavaDoc)
622          return ((Float JavaDoc) value).floatValue();
623       else if (value instanceof String JavaDoc)
624          return Float.parseFloat((String JavaDoc) value);
625       else
626          throw new MessageFormatException JavaDoc("Invalid conversion");
627    }
628
629    public double getDoubleProperty(String JavaDoc name) throws JMSException JavaDoc
630    {
631       Object JavaDoc value = header.jmsProperties.get(name);
632       if (value == null)
633          return Double.valueOf(null).doubleValue();
634
635       if (value instanceof Float JavaDoc)
636          return ((Float JavaDoc) value).doubleValue();
637       else if (value instanceof Double JavaDoc)
638          return ((Double JavaDoc) value).doubleValue();
639       else if (value instanceof String JavaDoc)
640          return Double.parseDouble((String JavaDoc) value);
641       else
642          throw new MessageFormatException JavaDoc("Invalid conversion");
643    }
644
645    public String JavaDoc getStringProperty(String JavaDoc name) throws JMSException JavaDoc
646    {
647       Object JavaDoc value = header.jmsProperties.get(name);
648       if (value == null)
649          return null;
650
651       if (value instanceof Boolean JavaDoc)
652          return ((Boolean JavaDoc) value).toString();
653       else if (value instanceof Byte JavaDoc)
654          return ((Byte JavaDoc) value).toString();
655       else if (value instanceof Short JavaDoc)
656          return ((Short JavaDoc) value).toString();
657       else if (value instanceof Integer JavaDoc)
658          return ((Integer JavaDoc) value).toString();
659       else if (value instanceof Long JavaDoc)
660          return ((Long JavaDoc) value).toString();
661       else if (value instanceof Float JavaDoc)
662          return ((Float JavaDoc) value).toString();
663       else if (value instanceof Double JavaDoc)
664          return ((Double JavaDoc) value).toString();
665       else if (value instanceof String JavaDoc)
666          return (String JavaDoc) value;
667       else
668          throw new MessageFormatException JavaDoc("Invalid conversion");
669    }
670
671    public Object JavaDoc getObjectProperty(String JavaDoc name) throws JMSException JavaDoc
672    {
673       Object JavaDoc value = header.jmsProperties.get(name);
674       return value;
675    }
676
677    public Enumeration JavaDoc getPropertyNames() throws JMSException JavaDoc
678    {
679       Enumeration JavaDoc names = Collections.enumeration(header.jmsProperties.keySet());
680       return names;
681    }
682
683    public void setBooleanProperty(String JavaDoc name, boolean value) throws JMSException JavaDoc
684    {
685       if (!header.jmsPropertiesReadWrite)
686          throw new MessageNotWriteableException JavaDoc("Properties are read-only");
687       Boolean JavaDoc b = Primitives.valueOf(value);
688       checkProperty(name, b);
689       header.jmsProperties.put(name, b);
690    }
691
692    public void setByteProperty(String JavaDoc name, byte value) throws JMSException JavaDoc
693    {
694       if (!header.jmsPropertiesReadWrite)
695          throw new MessageNotWriteableException JavaDoc("Properties are read-only");
696       Byte JavaDoc b = new Byte JavaDoc(value);
697       checkProperty(name, b);
698       header.jmsProperties.put(name, b);
699    }
700
701    public void setShortProperty(String JavaDoc name, short value) throws JMSException JavaDoc
702    {
703       if (!header.jmsPropertiesReadWrite)
704          throw new MessageNotWriteableException JavaDoc("Properties are read-only");
705       Short JavaDoc s = new Short JavaDoc(value);
706       checkProperty(name, s);
707       header.jmsProperties.put(name, s);
708    }
709
710    public void setIntProperty(String JavaDoc name, int value) throws JMSException JavaDoc
711    {
712       if (!header.jmsPropertiesReadWrite)
713          throw new MessageNotWriteableException JavaDoc("Properties are read-only");
714       Integer JavaDoc i = new Integer JavaDoc(value);
715       checkProperty(name, i);
716       header.jmsProperties.put(name, i);
717    }
718
719    public void setLongProperty(String JavaDoc name, long value) throws JMSException JavaDoc
720    {
721       if (!header.jmsPropertiesReadWrite)
722          throw new MessageNotWriteableException JavaDoc("Properties are read-only");
723       Long JavaDoc l = new Long JavaDoc(value);
724       checkProperty(name, l);
725       header.jmsProperties.put(name, l);
726    }
727
728    public void setFloatProperty(String JavaDoc name, float value) throws JMSException JavaDoc
729    {
730       if (!header.jmsPropertiesReadWrite)
731          throw new MessageNotWriteableException JavaDoc("Properties are read-only");
732       Float JavaDoc f = new Float JavaDoc(value);
733       checkProperty(name, f);
734       header.jmsProperties.put(name, f);
735    }
736
737    public void setDoubleProperty(String JavaDoc name, double value) throws JMSException JavaDoc
738    {
739       if (!header.jmsPropertiesReadWrite)
740          throw new MessageNotWriteableException JavaDoc("Properties are read-only");
741       Double JavaDoc d = new Double JavaDoc(value);
742       checkProperty(name, d);
743       header.jmsProperties.put(name, d);
744    }
745
746    public void setStringProperty(String JavaDoc name, String JavaDoc value) throws JMSException JavaDoc
747    {
748       if (!header.jmsPropertiesReadWrite)
749          throw new MessageNotWriteableException JavaDoc("Properties are read-only");
750       checkProperty(name, value);
751       header.jmsProperties.put(name, value);
752    }
753
754    public void setObjectProperty(String JavaDoc name, Object JavaDoc value) throws JMSException JavaDoc
755    {
756       if (!header.jmsPropertiesReadWrite)
757          throw new MessageNotWriteableException JavaDoc("Properties are read-only");
758       checkProperty(name, value);
759       if (value instanceof Boolean JavaDoc)
760          header.jmsProperties.put(name, value);
761       else if (value instanceof Byte JavaDoc)
762          header.jmsProperties.put(name, value);
763       else if (value instanceof Short JavaDoc)
764          header.jmsProperties.put(name, value);
765       else if (value instanceof Integer JavaDoc)
766          header.jmsProperties.put(name, value);
767       else if (value instanceof Long JavaDoc)
768          header.jmsProperties.put(name, value);
769       else if (value instanceof Float JavaDoc)
770          header.jmsProperties.put(name, value);
771       else if (value instanceof Double JavaDoc)
772          header.jmsProperties.put(name, value);
773       else if (value instanceof String JavaDoc)
774          header.jmsProperties.put(name, value);
775       else if (value == null)
776          header.jmsProperties.put(name, null);
777       else
778          throw new MessageFormatException JavaDoc("Invalid object type");
779    }
780
781    // Externalizable implementation ---------------------------------
782

783    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
784    {
785       SpyDestination.writeDest(out, header.jmsDestination);
786       out.writeInt(header.jmsDeliveryMode);
787       out.writeLong(header.jmsExpiration);
788       out.writeInt(header.jmsPriority);
789       writeString(out, header.jmsMessageID);
790       out.writeLong(header.jmsTimeStamp);
791       out.writeBoolean(header.jmsCorrelationID);
792       writeString(out, header.jmsCorrelationIDString);
793       if (header.jmsCorrelationIDbyte == null)
794          out.writeInt(-1);
795       else
796       {
797          out.writeInt(header.jmsCorrelationIDbyte.length);
798          out.write(header.jmsCorrelationIDbyte);
799       }
800       SpyDestination.writeDest(out, header.jmsReplyTo);
801       writeString(out, header.jmsType);
802       out.writeBoolean(header.jmsRedelivered);
803       out.writeBoolean(header.jmsPropertiesReadWrite);
804       out.writeBoolean(header.msgReadOnly);
805       writeString(out, header.producerClientId);
806       //write out header.jmsPropertiess
807
java.util.Set JavaDoc entrySet = header.jmsProperties.entrySet();
808       out.writeInt(entrySet.size());
809       for (java.util.Iterator JavaDoc it = entrySet.iterator(); it.hasNext(); )
810       {
811          Map.Entry JavaDoc me = (Map.Entry JavaDoc)it.next();
812          out.writeUTF((String JavaDoc)me.getKey());
813          Object JavaDoc value = me.getValue();
814          if (value == null)
815          {
816             out.writeByte(OBJECT);
817             out.writeObject(value);
818          }
819          else if (value instanceof String JavaDoc)
820          {
821             out.writeByte(STRING);
822             out.writeUTF((String JavaDoc) value);
823          }
824          else if (value instanceof Integer JavaDoc)
825          {
826             out.writeByte(INT);
827             out.writeInt(((Integer JavaDoc) value).intValue());
828          }
829          else if (value instanceof Boolean JavaDoc)
830          {
831             out.writeByte(BOOLEAN);
832             out.writeBoolean(((Boolean JavaDoc) value).booleanValue());
833          }
834          else if (value instanceof Byte JavaDoc)
835          {
836             out.writeByte(BYTE);
837             out.writeByte(((Byte JavaDoc) value).byteValue());
838          }
839          else if (value instanceof Short JavaDoc)
840          {
841             out.writeByte(SHORT);
842             out.writeShort(((Short JavaDoc) value).shortValue());
843          }
844          else if (value instanceof Long JavaDoc)
845          {
846             out.writeByte(LONG);
847             out.writeLong(((Long JavaDoc) value).longValue());
848          }
849          else if (value instanceof Float JavaDoc)
850          {
851             out.writeByte(FLOAT);
852             out.writeFloat(((Float JavaDoc) value).floatValue());
853          }
854          else if (value instanceof Double JavaDoc)
855          {
856             out.writeByte(DOUBLE);
857             out.writeDouble(((Double JavaDoc) value).doubleValue());
858          }
859          else
860          {
861             out.writeByte(OBJECT);
862             out.writeObject(value);
863          }
864       }
865    }
866
867    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
868    {
869       header.jmsDestination = SpyDestination.readDest(in);
870       header.jmsDeliveryMode = in.readInt();
871       header.jmsExpiration = in.readLong();
872       header.jmsPriority = in.readInt();
873       header.jmsMessageID = readString(in);
874       header.jmsTimeStamp = in.readLong();
875       header.jmsCorrelationID = in.readBoolean();
876       header.jmsCorrelationIDString = readString(in);
877       int length = in.readInt();
878       if (length < 0)
879          header.jmsCorrelationIDbyte = null;
880       else
881       {
882          header.jmsCorrelationIDbyte = new byte[length];
883          in.readFully(header.jmsCorrelationIDbyte);
884       }
885       header.jmsReplyTo = SpyDestination.readDest(in);
886       header.jmsType = readString(in);
887       header.jmsRedelivered = in.readBoolean();
888       header.jmsPropertiesReadWrite = in.readBoolean();
889       header.msgReadOnly = in.readBoolean();
890       header.producerClientId = readString(in);
891       //read in header.jmsPropertiess
892
header.jmsProperties = new HashMap JavaDoc();
893       int size = in.readInt();
894       for (int i = 0; i < size; i++)
895       {
896          String JavaDoc key = in.readUTF();
897          byte type = in.readByte();
898          Object JavaDoc value = null;
899          switch (type)
900          {
901             case BYTE :
902                value = new Byte JavaDoc(in.readByte());
903                break;
904             case SHORT :
905                value = new Short JavaDoc(in.readShort());
906                break;
907             case INT :
908                value = new Integer JavaDoc(in.readInt());
909                break;
910             case LONG :
911                value = new Long JavaDoc(in.readLong());
912                break;
913             case FLOAT :
914                value = new Float JavaDoc(in.readFloat());
915                break;
916             case DOUBLE :
917                value = new Double JavaDoc(in.readDouble());
918                break;
919             case BOOLEAN :
920                value = Primitives.valueOf(in.readBoolean());
921                break;
922             case STRING :
923                value = in.readUTF();
924                break;
925             default :
926                value = in.readObject();
927          }
928          header.jmsProperties.put(key, value);
929       }
930    }
931
932    // Object overrides ----------------------------------------------
933

934    public String JavaDoc toString()
935    {
936       return getClass().getName() + " {\n" + header + "\n" + "}";
937    }
938
939    // Package protected ---------------------------------------------
940

941    /**
942      * Check a property is valid
943      *
944      * @param name the name
945      * @param value the value
946      * @throws JMSException for any error
947      */

948    void checkProperty(String JavaDoc name, Object JavaDoc value) throws JMSException JavaDoc
949    {
950       if (name == null)
951          throw new IllegalArgumentException JavaDoc("The name of a property must not be null.");
952
953       if (name.equals(""))
954          throw new IllegalArgumentException JavaDoc("The name of a property must not be an empty String.");
955
956       if (reservedIdentifiers.contains(name))
957          throw new IllegalArgumentException JavaDoc("The property name '" + name + "' is reserved due to selector syntax.");
958
959       if (name.regionMatches(false, 0, "JMS_", 0, 4))
960       {
961          if (name.equals(PROPERTY_SCHEDULED_DELIVERY))
962          {
963             if (!(value instanceof Long JavaDoc))
964                throw new JMSException JavaDoc(name + " must be Long: " + value);
965          }
966          else if (name.equals(PROPERTY_REDELIVERY_DELAY))
967          {
968             if (!(value instanceof Number JavaDoc))
969                throw new JMSException JavaDoc(name + " must be Number: " + value);
970          }
971          else if (name.equals(PROPERTY_REDELIVERY_COUNT))
972          {
973             if (!(value instanceof Number JavaDoc))
974                throw new JMSException JavaDoc(name + " must be Number: " + value);
975          }
976          else if (name.equals(PROPERTY_REDELIVERY_LIMIT))
977          {
978             if (!(value instanceof Number JavaDoc))
979                throw new JMSException JavaDoc(name + " must be Number: " + value);
980          }
981          else if (name.equals(PROPERTY_ORIG_EXPIRATION))
982          {
983             if (!(value instanceof Long JavaDoc))
984                throw new JMSException JavaDoc(name + " must be Long: " + value);
985          }
986          else if (name.equals(PROPERTY_ORIG_DESTINATION))
987          {
988             // no validation
989
}
990          else if (name.equals(PROPERTY_ORIG_MESSAGEID))
991          {
992             // no validation
993
}
994          else
995          {
996             throw new JMSException JavaDoc("Illegal property name: " + name);
997          }
998       }
999
1000      if (name.regionMatches(false, 0, "JMSX", 0, 4))
1001      {
1002         if (name.equals("JMSXGroupID"))
1003            return;
1004         if (name.equals("JMSXGroupSeq"))
1005            return;
1006         throw new JMSException JavaDoc("Illegal property name: " + name);
1007      }
1008
1009      if (Strings.isValidJavaIdentifier(name) == false)
1010         throw new IllegalArgumentException JavaDoc("The property name '" + name + "' is not a valid java identifier.");
1011
1012   }
1013
1014   /**
1015     * Clear a message
1016     *
1017     * @throws JMSException for any error
1018     */

1019   void clearMessage() throws JMSException JavaDoc
1020   {
1021      clearBody();
1022      this.ack = null;
1023      this.session = null;
1024      //Set by send() method
1025
this.header.jmsDestination = null;
1026      this.header.jmsDeliveryMode = -1;
1027      this.header.jmsExpiration = 0;
1028      this.header.jmsPriority = -1;
1029      this.header.jmsMessageID = null;
1030      this.header.jmsTimeStamp = 0;
1031      //Set by the client
1032
this.header.jmsCorrelationID = true;
1033      this.header.jmsCorrelationIDString = null;
1034      this.header.jmsCorrelationIDbyte = null;
1035      this.header.jmsReplyTo = null;
1036      this.header.jmsType = null;
1037      //Set by the provider
1038
this.header.jmsRedelivered = false;
1039      //Properties
1040
this.header.jmsProperties.clear();
1041      this.header.jmsPropertiesReadWrite = true;
1042      //Message body
1043
this.header.msgReadOnly = false;
1044      //For noLocal to be able to tell if this was a locally produced message
1045
this.header.producerClientId = null;
1046      //For durable subscriptions
1047
this.header.durableSubscriberID = null;
1048      //For ordering in the JMSServerQueue (set on the server side)
1049
this.header.messageId = 0;
1050   }
1051
1052   // Protected -----------------------------------------------------
1053

1054   // Private -------------------------------------------------------
1055

1056   /**
1057     * Write a string
1058     *
1059     * @param out the output
1060     * @param s the string
1061     * @throws IOException for any error
1062     */

1063   private static void writeString(ObjectOutput JavaDoc out, String JavaDoc s) throws IOException JavaDoc
1064   {
1065      if (s == null)
1066         out.writeByte(NULL);
1067      else
1068      {
1069         out.writeByte(STRING);
1070         out.writeUTF(s);
1071      }
1072   }
1073
1074   /**
1075     * Read a string
1076     *
1077     * @param in the input
1078     * @return the string
1079     * @throws IOException for any error
1080     */

1081   private static String JavaDoc readString(ObjectInput JavaDoc in) throws IOException JavaDoc
1082   {
1083      byte b = in.readByte();
1084      if (b == NULL)
1085         return null;
1086      else
1087         return in.readUTF();
1088   }
1089
1090   // Inner classes -------------------------------------------------
1091

1092   /**
1093     * The message headers
1094     */

1095   public static class Header
1096   {
1097      /** The destination */
1098      public Destination JavaDoc jmsDestination = null;
1099      /** The delivery mode */
1100      public int jmsDeliveryMode = -1;
1101      /** The expiration time */
1102      public long jmsExpiration = 0;
1103      /** The message priority */
1104      public int jmsPriority = -1;
1105      /** The message id */
1106      public String JavaDoc jmsMessageID = null;
1107      /** The send timestamp */
1108      public long jmsTimeStamp = 0;
1109      /** Whether the correlation is a string */
1110      public boolean jmsCorrelationID = true;
1111      /** The correlation string */
1112      public String JavaDoc jmsCorrelationIDString = null;
1113      /** The correlation in bytes */
1114      public byte[] jmsCorrelationIDbyte = null;
1115      /** The reply to destination */
1116      public Destination JavaDoc jmsReplyTo = null;
1117      /** The message type */
1118      public String JavaDoc jmsType = null;
1119      /** Set by the provider */
1120      public boolean jmsRedelivered = false;
1121      /** Properties */
1122      public HashMap JavaDoc jmsProperties = new HashMap JavaDoc();
1123      /** Whether the properties are writable */
1124      public boolean jmsPropertiesReadWrite = true;
1125      /** Message body */
1126      public boolean msgReadOnly = false;
1127      /** For noLocal to be able to tell if this was a locally produced message */
1128      public String JavaDoc producerClientId;
1129      /** For durable subscriptions */
1130      public DurableSubscriptionID durableSubscriberID = null;
1131      /** For ordering in the JMSServerQueue (set on the server side) */
1132      public transient long messageId;
1133
1134      public String JavaDoc toString()
1135      {
1136         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(100);
1137         buffer.append("Header { \n");
1138         buffer.append(" jmsDestination : ").append(jmsDestination).append('\n');
1139         buffer.append(" jmsDeliveryMode : ").append(jmsDeliveryMode).append('\n');
1140         buffer.append(" jmsExpiration : ").append(jmsExpiration).append('\n');
1141         buffer.append(" jmsPriority : ").append(jmsPriority).append('\n');
1142         buffer.append(" jmsMessageID : ").append(jmsMessageID).append('\n');
1143         buffer.append(" jmsTimeStamp : ").append(jmsTimeStamp).append('\n');
1144         buffer.append(" jmsCorrelationID: ").append(jmsCorrelationIDString).append('\n');
1145         buffer.append(" jmsReplyTo : ").append(jmsReplyTo).append('\n');
1146         buffer.append(" jmsType : ").append(jmsType).append('\n');
1147         buffer.append(" jmsRedelivered : ").append(jmsRedelivered).append('\n');
1148         buffer.append(" jmsProperties : ").append(jmsProperties).append('\n');
1149         buffer.append(" jmsPropReadWrite: ").append(jmsPropertiesReadWrite).append('\n');
1150         buffer.append(" msgReadOnly : ").append(msgReadOnly).append('\n');
1151         buffer.append(" producerClientId: ").append(producerClientId).append('\n');
1152         buffer.append('}');
1153         return buffer.toString();
1154      }
1155   }
1156}
1157
Popular Tags