KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > jms > MantaMessage


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Nimo 20-MAR-2004.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.api.jms;
47
48 import java.util.Enumeration JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Hashtable JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.Map JavaDoc;
53
54
55 import javax.jms.Destination JavaDoc;
56 import javax.jms.JMSException JavaDoc;
57 import javax.jms.Message JavaDoc;
58 import javax.jms.MessageFormatException JavaDoc;
59 import javax.jms.MessageNotWriteableException JavaDoc;
60
61 import java.io.IOException JavaDoc;
62 import java.io.Serializable JavaDoc;
63
64 import org.mr.core.util.byteable.Byteable;
65 import org.mr.core.util.byteable.ByteableInputStream;
66 import org.mr.core.util.byteable.ByteableOutputStream;
67 import org.mr.core.util.byteable.ByteableRegistry;
68
69
70
71 /**
72  * This class implements the javax.jms.Message interface.
73  *
74  * @author Nimo 20-MAR-2004
75  *
76  * The Message interface is the root interface of all JMS messages. It defines the message
77  * header and the acknowledge method used for all messages. Most message-oriented middleware
78  * (MOM) products treat messages as lightweight entities that consist of a header and a
79  * payload. The header contains fields used for message routing and identification; the
80  * payload contains the application data being sent. Within this general form, the definition
81  * of a message varies significantly across products. It would be quite difficult for the JMS
82  * API to support all of these message models. With this in mind, the JMS message model has
83  * the following goals:
84  *
85  * Provide a single, unified message API
86  * Provide an API suitable for creating messages that match the format used by provider-native
87  * messaging applications Support the development of heterogeneous applications that span
88  * operating systems, machine architectures, and computer languages Support messages
89  * containing objects in the Java programming language ("Java objects") Support messages
90  * containing Extensible Markup Language (XML) pages
91  * JMS messages are composed of the following parts:
92  *
93  * Header - All messages support the same set of header fields. Header fields contain values
94  * used by both clients and providers to identify and route messages.
95  * Properties - Each message contains a built-in facility for supporting application-defined
96  * property values. Properties provide an efficient mechanism for supporting
97  * application-defined message filtering.
98  * Body - The JMS API defines several types of message body, which cover the majority of
99  * messaging styles currently in use.
100  *
101  */

102 public class MantaMessage implements Serializable JavaDoc, Byteable, Message JavaDoc{
103     
104    String JavaDoc JMSMessageId;
105    long JMSTimeStamp;
106    long JMSExpiration;
107    String JavaDoc JMSCorrelationId=null;
108    Destination JavaDoc JMSReplyTo, JMSDestination;
109    protected String JavaDoc connId;
110    
111    
112    protected MantaSession creatingSession = null;
113    protected Map JavaDoc properties = null;
114    protected long timeAccepted;
115     
116     
117 // constants for use by producers and consumers.
118
public static final int NON_PERSISTENT = 1;
119     public static final int PERSISTENT = 2;
120     public static final int MIN_PRIORITY = 0;
121     public static final int MAX_PRIORITY = 9;
122     protected String JavaDoc originalClient;
123     
124     //protected Map backupProperties;
125

126     /*
127      * flags:
128      *
129      * 00000000 00000000 00000000 0000xxxx Priority, 0-9.
130      * 00000000 00000000 00000000 000x0000 Persistent on.
131      * 00000000 00000000 00000000 00x00000 Message redelivered.
132      * 00000000 00000000 00000000 0x000000 Message in writeable state.
133      * 00000000 00000000 00000xxx 00000000 Message type bits.
134      * 00000000 0000000x 00000000 00000000 Properties writeable.
135      * 00000000 000000x0 00000000 00000000 Original properties saved.
136      * 00000000 00000x00 00000000 00000000 Original body saved.
137      *
138      */

139     int flags;
140     //priority goes on rightmost four bits.
141
final static int IS_PERSISTENT = 1<<4; //0..0 0001 0000 - for persistency mask.
142
final static int IS_REDELIVERED = 1<<5; //0..0 0010 0000 - for redeliver mask.
143
final static int IS_WRITEABLE = 1<<6; //0..0 0100 0000 - for writeable mask.
144
//bit 7 is kept in the meantime
145
final static int MANTA_MESSAGE = 0x00000000; //1...1 0000 1111 1111 for text message
146
final static int BYTES_MESSAGE = 0x00000100; //1...1 0001 1111 1111 for bytes message
147
final static int OBJECT_MESSAGE = 0x00000200; //1...1 0010 1111 1111 for object message
148
final static int MAP_MESSAGE = 0x00000300; //1...1 0011 1111 1111 for map message
149
final static int STREAM_MESSAGE = 0x00000400; //1...1 0100 1111 1111 for stream message
150
final static int TEXT_MESSAGE = 0x00000500;
151     final static int ONLY_MSG_TYPES = 0x0FFFF0FF; //get the JMStype of the message.
152

153     final static int PROPERTIES_WRITEABLE = 0x00010000; //only prop bit is on.
154
final static int PROPERTIES_NOT_WRITEABLE = 0x0FFEFFFF;
155     final static int ORIG_PROPERTIES_SAVED = 0x00020000;
156     final static int ORIG_BODY_SAVED = 0x00040000;
157     
158     final static int REVERSE_WRITEABLE = 0xFFFFFFBF; //only writeable bit is 0.
159
final static int PRIORITY_GETTER = 0x0000000F; //four rightmost bits.
160
final static String JavaDoc BYT_M = "BytesMessage";
161     final static String JavaDoc MAP_M = "MapMessage";
162     final static String JavaDoc OBJ_M = "ObjectMessage";
163     final static String JavaDoc REG_M = "Message";
164     final static String JavaDoc STR_M = "StreamMessage";
165     final static String JavaDoc TXT_M = "TextMessage";
166     final static String JavaDoc[] MESSAGE_TYPES = new String JavaDoc[] {
167         REG_M, BYT_M, OBJ_M, MAP_M, STR_M,TXT_M
168     };
169     
170     //these are constants for the primitive values for use when marshalling.
171
final static byte BYTE = 1;
172     final static byte BOOLEAN = 2;
173     final static byte SHORT = 3;
174     final static byte INTEGER = 4;
175     final static byte DOUBLE = 5;
176     final static byte FLOAT = 6;
177     final static byte STRING = 7;
178     final static byte LONG = 8;
179     final static byte BYTEARR = 9;
180     final static byte CHAR = 10;
181
182    /**
183     * Default constructor for message -
184     * Creates the properties which must be in a Hashtable.
185     *
186     */

187     MantaMessage(){
188         properties = new Hashtable JavaDoc();
189         flags = 0 | IS_WRITEABLE | PROPERTIES_WRITEABLE |
190                     DEFAULT_PRIORITY | IS_PERSISTENT; //set flags.
191

192     }//MantaMessage
193

194     //
195
MantaMessage(MantaSession sess) {
196        this();
197        creatingSession = sess;
198     }
199     
200     /**
201      * Gets the client id for the message.
202      * @return Message's client id
203      */

204     public final String JavaDoc getClientId() {
205         return originalClient;
206     }//getClientId
207

208     
209     
210     /**
211      * Sets the client's id for the message
212      *
213      * @param clientId - the new id.
214      */

215     public final void setClientId(String JavaDoc clientId) {
216         originalClient = clientId;
217     }//setClientId
218

219
220     /**
221      * Gets the message id
222      */

223     public final String JavaDoc getJMSMessageID() throws JMSException JavaDoc{
224         return JMSMessageId;
225     }//getJMSMessageID
226

227     /**
228      * Sets the message id
229      */

230     public final void setJMSMessageID(String JavaDoc id) throws JMSException JavaDoc{
231
232         JMSMessageId = id;
233     }//setJMSMessageID
234

235     /**
236      * gets the JMS Timestamp for this message
237      */

238     public final long getJMSTimestamp() throws JMSException JavaDoc{
239         return JMSTimeStamp;
240     }//getJMSTimestamp
241

242
243     /**
244      * Sets the JMS timestamp for this message
245      */

246     public final void setJMSTimestamp(long timestamp) throws JMSException JavaDoc{
247         JMSTimeStamp = timestamp;
248     }//setJMSTimestamp
249

250     
251     /**
252      * Sets the time that this message was accepted at
253      *
254      * @param acTime - the time (millis)
255      */

256
257     public final void setTimeAccepted(long acTime) {
258         timeAccepted = acTime;
259     }//setTimeAccepted
260

261     /**
262      * Gets the time this message was accepted in
263      *
264      * @return the time (millis)
265      */

266     public final long getTimeAccepted() {
267         return timeAccepted;
268     }//getTimeAccepted
269

270
271     /**
272      * Gets the correlation ID as an array of bytes for the message.
273      *
274      * @return byte[] the correlation ID as a byte array
275      */

276     public final byte[] getJMSCorrelationIDAsBytes() throws JMSException JavaDoc{
277                 
278         if (JMSCorrelationId!=null)
279             return JMSCorrelationId.getBytes();
280         else
281             return null;
282     }//getJMSCorrelationIDAsBytes
283

284
285     /**
286      * Sets the correlation id for this message.
287      */

288     public final void setJMSCorrelationIDAsBytes(byte[] correlationIDAsBytes) throws JMSException JavaDoc{
289         //checkWriteableState(false);
290

291         JMSCorrelationId = new String JavaDoc(correlationIDAsBytes);
292     }//setJMSCorrelationIDAsBytes
293

294     /**
295      * Sets the correlation id for this message.
296      */

297
298     public final void setJMSCorrelationID(String JavaDoc correlationID) throws JMSException JavaDoc{
299         JMSCorrelationId = correlationID;
300     }//setJMSCorrelationID
301

302
303     /**
304      * Gets the correlation id for this message.
305      */

306     public final String JavaDoc getJMSCorrelationID() throws JMSException JavaDoc{
307             
308         return JMSCorrelationId;
309     }//getJMSCorrelationID
310

311
312     /**
313      * Gets the JMSReplyto field for the message - where the producer wants
314      * returning messages to go.
315      */

316     public final Destination JavaDoc getJMSReplyTo() throws JMSException JavaDoc{
317         
318         return JMSReplyTo;
319     }//getJMSReplyTo
320

321
322     /**
323      * Sets the JMSReplyTo for the message - where the producer wants the receivers
324      * to send it answers for this message.
325      */

326     public final void setJMSReplyTo(Destination JavaDoc replyTo) throws JMSException JavaDoc{
327         //checkWriteableState(false);
328

329         JMSReplyTo = replyTo;
330
331     }//setJMSReplyTo
332

333
334     /**
335      * Gets the destination object that this message is going to.
336      */

337     public final Destination JavaDoc getJMSDestination() throws JMSException JavaDoc{
338         return JMSDestination;
339     }//getJMSDestination
340

341
342     /**
343      * Sets the destination for this message.
344      */

345     public final void setJMSDestination(Destination JavaDoc destination) throws JMSException JavaDoc{
346         //checkWriteableState(false);
347
JMSDestination = destination;
348
349     }//setJMSDestination
350

351
352     /**
353      * Returns the JMS delivery mode for this message : persistent or not.
354      */

355     public final int getJMSDeliveryMode() throws JMSException JavaDoc{
356         
357         return ((flags & IS_PERSISTENT) >>4) + 1;
358     }//getJMSDeliveryMode
359

360     /**
361      * Sets the delivery mode for this message: persistent or not.
362      */

363     public final void setJMSDeliveryMode(int deliveryMode) throws JMSException JavaDoc{
364         //checkWriteableState(false);
365
flags = (flags & 0x0FFFFFEF) | ((deliveryMode-1)<<4);
366         
367     }//setJMSDeliveryMode
368

369     
370     /**
371      * Returns whether this message was redelivered by the producer.
372      */

373     public final boolean getJMSRedelivered() throws JMSException JavaDoc{
374         return ((flags & IS_REDELIVERED) > 0);
375     }//getJMSRedelivered
376

377     /**
378      * Marks the message as redelivered by the producer.
379      */

380     public final void setJMSRedelivered(boolean redelivered) throws JMSException JavaDoc{
381         // According to the JMS specification this method should be ignored.
382
// Only the server sets this information
383
checkWriteableState(false);
384     }//setJMSRedelivered
385

386
387     /**
388      * Returns the type of the message (MapMessage, BytesMessage, StreamMessage,
389      * ObjectMessage, TextMessage)
390      */

391     public final String JavaDoc getJMSType() throws JMSException JavaDoc{
392         int fl = (flags&0x00000F00) >> 8;
393         return MESSAGE_TYPES[fl];
394     }//getJMSType
395

396     /**
397      * Sets the type of the message.
398      * @see getJMSType
399      */

400     public final void setJMSType(String JavaDoc type) throws JMSException JavaDoc{
401         checkWriteableState(false);
402         //ignore if comes from outside.
403
}//setJMSType
404

405
406     /**
407      * Returns the expiration time for this message.
408      */

409     public final long getJMSExpiration() throws JMSException JavaDoc{
410     
411         return JMSExpiration;
412         
413     }//getJMSExpiration
414

415     /**
416      * Sets the expiration time for this message.
417      */

418     public final void setJMSExpiration(long expiration) throws JMSException JavaDoc{
419         //checkWriteableState(false);
420
this.JMSExpiration = expiration;
421     }//setJMSExpiration
422

423     /**
424      * Gets the priority for this message (the JMS priority)
425      */

426     public final int getJMSPriority() throws JMSException JavaDoc{
427                     
428         return PRIORITY_GETTER & flags;
429         
430     }//getJMSPriority
431

432     /**
433      * Sets the priority for this message (the JMS priority)
434      */

435     public final void setJMSPriority(int priority) throws JMSException JavaDoc{
436         if (priority>MAX_PRIORITY||priority<0)
437             
438             throw new JMSException JavaDoc("MNJMS0004E : FAILED IN METHOD setJMSPriority(). PRIORITY "+priority+" OUT OF RANGE.");
439             
440         flags = flags>>4<<4 | priority;
441     }//setJMSPriority
442

443     /**
444      * Clears the message properties.
445      */

446     
447     public void clearProperties() throws JMSException JavaDoc{
448         
449 // if (
450
// (creatingSession.sessionAcknowledgementMode==Session.CLIENT_ACKNOWLEDGE ||
451
// creatingSession.sessionAcknowledgementMode==Session.SESSION_TRANSACTED)
452
// && (flags&ORIG_PROPERTIES_SAVED)==0){
453
//
454
// backupProperties = new Hashtable(properties);
455
// properties=new Hashtable();
456
// flags = flags|ORIG_PROPERTIES_SAVED;
457
//
458
// }
459
// else
460
properties.clear();
461         
462         flags=flags|PROPERTIES_WRITEABLE;
463     }//clearProperties
464

465     //retrieve old properties and body.
466

467 // protected void retrieveOldProperties() {
468
// if (backupProperties!=null)
469
// properties = new Hashtable(backupProperties);
470
//
471
// }
472

473 // protected void retrieveOldBody() {
474
// }
475
/**
476      * Check whether a property name exists on the message.
477      */

478     public final boolean propertyExists(String JavaDoc name) throws JMSException JavaDoc{
479         
480         if (name!=null && name.length()>0)
481             return properties.containsKey(name);
482         else
483             return false;
484     }//propertyExists
485

486
487     /**
488      * Get a boolean property from this message.
489      */

490     public final boolean getBooleanProperty(String JavaDoc name) throws JMSException JavaDoc {
491         checkPropertyName(name);
492         Object JavaDoc o = properties.get(name);
493         if (o instanceof Boolean JavaDoc)
494             return ((Boolean JavaDoc)o).booleanValue();
495         else if (o==null ||o instanceof String JavaDoc)
496             return Boolean.valueOf((String JavaDoc)o).booleanValue();
497         else
498             throw new MessageFormatException JavaDoc("MNJMS0004F : FAILED ON METHOD getBooleanProperty(). CONVERSION TO BOOLEAN FROM "+o.getClass().getName()+" IS INVALID.");
499     }//getBooleanProperty
500

501     /**
502      * Get a byte property from this message.
503      */

504     public final byte getByteProperty(String JavaDoc name) throws JMSException JavaDoc {
505         
506         checkPropertyName(name);
507         Object JavaDoc o = properties.get(name);
508         if (o instanceof Byte JavaDoc)
509             return ((Number JavaDoc)o).byteValue();
510         else if ( o == null || o instanceof String JavaDoc )
511             try {
512                 return Byte.valueOf((String JavaDoc)o).byteValue();
513             }
514             catch (NumberFormatException JavaDoc nfe) {
515                 throw new MessageFormatException JavaDoc("MNJMS00050 : FAILED ON METHOD getByteProperty(). STRING "+o+" IS INVALID FOR CONVERSION TO BYTE.");
516             }
517         else
518             throw new MessageFormatException JavaDoc("MNJMS00051 : FAILED ON METHOD getByteProperty(). CONVERSION TO BYTE FROM "+o.getClass().getName()+" IS INVALID.");
519     }//getByteProperty
520

521     /**
522      * Get a short property from this message.
523      */

524     public final short getShortProperty(String JavaDoc name) throws JMSException JavaDoc {
525         
526         checkPropertyName(name);
527         Object JavaDoc o = properties.get(name);
528         if (o instanceof Byte JavaDoc || o instanceof Short JavaDoc)
529             return ((Number JavaDoc)o).shortValue();
530         else if (o == null || o instanceof String JavaDoc )
531             try {
532                 return Short.valueOf((String JavaDoc)o).shortValue();
533             }
534         catch (NumberFormatException JavaDoc nfe) {
535             throw new MessageFormatException JavaDoc("MNJMS00052 : FAILED ON METHOD getShortProperty(). STRING "+o+" IS INVALID FOR CONVERSION TO SHORT.");
536         }
537         else
538             throw new MessageFormatException JavaDoc("MNJMS00053 : FAILED ON METHOD getShortProperty(). CONVERSION TO SHORT FROM "+o.getClass().getName()+" IS INVALID.");
539     }//getShortProperty
540

541     /**
542      * Get an int property from this message.
543      */

544     public final int getIntProperty(String JavaDoc name) throws JMSException JavaDoc {
545         
546         checkPropertyName(name);
547         Object JavaDoc o = properties.get(name);
548         if (o instanceof Byte JavaDoc || o instanceof Short JavaDoc || o instanceof Integer JavaDoc)
549             return ((Number JavaDoc)o).intValue();
550         else if (o == null ||o instanceof String JavaDoc )
551             try {
552                 return Integer.valueOf((String JavaDoc)o).intValue();
553             }
554         catch (NumberFormatException JavaDoc nfe) {
555             throw new MessageFormatException JavaDoc("MNJMS00054 : FAILED ON METHOD getIntProperty(). STRING "+o+" IS INVALID FOR CONVERSION TO INTEGER.");
556         }
557         else
558             throw new MessageFormatException JavaDoc("MNJMS00055 : FAILED ON METHOD getIntProperty(). CONVERSION TO INTEGER FROM "+o.getClass().getName()+" IS INVALID.");
559     }//getIntProperty
560

561     /**
562      * Get a long property from this message.
563      */

564     public final long getLongProperty(String JavaDoc name) throws JMSException JavaDoc {
565         
566         checkPropertyName(name);
567         Object JavaDoc o = properties.get(name);
568         if (o instanceof Byte JavaDoc || o instanceof Short JavaDoc || o instanceof Integer JavaDoc || o instanceof Long JavaDoc)
569             return ((Number JavaDoc)o).longValue();
570         else if ( o == null ||o instanceof String JavaDoc )
571             try {
572                 return Long.valueOf((String JavaDoc)o).longValue();
573             }
574         catch (NumberFormatException JavaDoc nfe) {
575             throw new MessageFormatException JavaDoc("MNJMS00056 : FAILED ON METHOD getLongProperty(). STRING "+o+" IS INVALID FOR CONVERSION TO LONG.");
576         }
577         else
578             throw new MessageFormatException JavaDoc("MNJMS00057 : FAILED ON METHOD getLongProperty(). CONVERSION TO LONG FROM "+o.getClass().getName()+" IS INVALID.");
579     }//getLongProperty
580

581     /**
582      * Get a float property from this message.
583      */

584     public final float getFloatProperty(String JavaDoc name) throws JMSException JavaDoc {
585         
586         checkPropertyName(name);
587         Object JavaDoc o = properties.get(name);
588         if (o instanceof Float JavaDoc)
589             return ((Number JavaDoc)o).floatValue();
590         else if ( o == null || o instanceof String JavaDoc )
591             try {
592                 return Float.valueOf((String JavaDoc)o).floatValue();
593             }
594         catch (NumberFormatException JavaDoc nfe) {
595             throw new MessageFormatException JavaDoc("MNJMS00058 : FAILED ON METHOD getFloatProperty(). STRING "+o+" IS INVALID FOR CONVERSION TO FLOAT.");
596         }
597         else
598             throw new MessageFormatException JavaDoc("MNJMS00059 : FAILED ON METHOD getFloatProperty(). CONVERSION TO FLOAT FROM "+o.getClass().getName()+" IS INVALID.");
599     }//getFloatProperty
600

601     /**
602      * Get a double property from this message.
603      */

604     public final double getDoubleProperty(String JavaDoc name) throws JMSException JavaDoc {
605         
606         checkPropertyName(name);
607         Object JavaDoc o = properties.get(name);
608         if (o instanceof Float JavaDoc || o instanceof Double JavaDoc)
609             return ((Number JavaDoc)o).doubleValue();
610         else if ( o == null |o instanceof String JavaDoc )
611             try {
612                 return Double.valueOf((String JavaDoc)o).doubleValue();
613             }
614         catch (NumberFormatException JavaDoc nfe) {
615             throw new MessageFormatException JavaDoc("MNJMS0005A : FAILED ON METHOD getDoubleProperty(). STRING "+o+" IS INVALID FOR CONVERSION TO DOUBLE.");
616         }
617         else
618             throw new MessageFormatException JavaDoc("MNJMS0005B : FAILED ON METHOD getDoubleProperty(). CONVERSION TO DOUBLE FROM "+o.getClass().getName()+" IS INVALID.");
619     
620     }//getDoubleProperty
621

622     /**
623      * Get a string property from this message.
624      */

625     public final String JavaDoc getStringProperty(String JavaDoc name) throws JMSException JavaDoc {
626         
627         checkPropertyName(name);
628         Object JavaDoc o = properties.get(name);
629         
630         if (o != null)
631             return o.toString();
632         return null;
633     }//getStringProperty
634

635     /**
636      * Get an object property from this message.
637      */

638     public final Object JavaDoc getObjectProperty(String JavaDoc name) throws JMSException JavaDoc {
639         checkPropertyName(name);
640         return properties.get(name);
641     }//getObjectProperty
642

643     /**
644      * Get a list of the property names for this message.
645      */

646     public final Enumeration JavaDoc getPropertyNames() throws JMSException JavaDoc{
647         
648 // Get a final Iterator on the keySet (it cannnot be modified)
649
final Iterator JavaDoc i = properties.keySet().iterator();
650         
651         //Return an anonymous inner wrapper class which wraps an Iterator inside an Enumeration
652
return new Enumeration JavaDoc() {
653             public boolean hasMoreElements() {
654                 return i.hasNext();
655             }//hasMoreElements
656

657             public Object JavaDoc nextElement() {
658                 return i.next();
659             }//nextElement
660
};
661         
662     }//getPropertyNames
663

664     
665     protected void checkPropertyName(String JavaDoc name) throws JMSException JavaDoc {
666         if (name==null || name.length()==0)
667             throw new IllegalArgumentException JavaDoc("MNJMS0005C : ILLEGAL PROPERTY NAME. MAY NOT BE NULL OR EMPTY.");
668     }
669     /**
670      * Set a boolean property on this message.
671      */

672     public final void setBooleanProperty(String JavaDoc name, boolean value) throws JMSException JavaDoc {
673         checkWriteableState(true);
674         checkPropertyName(name);
675         properties.put(name, new Boolean JavaDoc(value));
676         
677     }//setBooleanProperty
678

679     /**
680      * Set a byte property on this message.
681      */

682     public final void setByteProperty(String JavaDoc name,byte value) throws JMSException JavaDoc {
683         checkWriteableState(true);
684         checkPropertyName(name);
685         properties.put(name, new Byte JavaDoc(value));
686         
687     }//setByteProperty
688

689     /**
690      * Set a short property on this message.
691      */

692     public final void setShortProperty(String JavaDoc name, short value) throws JMSException JavaDoc {
693         checkWriteableState(true);
694         checkPropertyName(name);
695         properties.put(name, new Short JavaDoc(value));
696         
697     }//setShortProperty
698

699     /**
700      * Set an int property on this message.
701      */

702     public final void setIntProperty(String JavaDoc name,int value) throws JMSException JavaDoc {
703         checkWriteableState(true);
704         checkPropertyName(name);
705         properties.put(name, new Integer JavaDoc(value));
706         
707     }///setIntProperty
708

709     /**
710      * Set a long property on this message.
711      */

712     public final void setLongProperty(String JavaDoc name, long value) throws JMSException JavaDoc{
713         checkWriteableState(true);
714         checkPropertyName(name);
715         properties.put(name, new Long JavaDoc(value));
716         
717     }//setLongProperty
718

719     /**
720      * Set a float property on this message.
721      */

722     public final void setFloatProperty(String JavaDoc name,float value) throws JMSException JavaDoc{
723         checkWriteableState(true);
724         checkPropertyName(name);
725         properties.put(name, new Float JavaDoc(value));
726         
727     }//setFloatProperty
728

729     /**
730      * Set a double property on this message.
731      */

732     public final void setDoubleProperty(String JavaDoc name, double value) throws JMSException JavaDoc{
733         checkWriteableState(true);
734         checkPropertyName(name);
735         properties.put(name, new Double JavaDoc(value));
736         
737     }//setDoubleProperty
738

739     /**
740      * Set a string property on this message.
741      */

742     public final void setStringProperty(String JavaDoc name, String JavaDoc value) throws JMSException JavaDoc{
743         checkWriteableState(true);
744         checkPropertyName(name);
745         properties.put(name, value);
746         
747     }//setStringProperty
748

749     /**
750      * Set an object property on this message.
751      */

752     public final void setObjectProperty(String JavaDoc name, Object JavaDoc value) throws JMSException JavaDoc {
753         checkWriteableState(true);
754         checkPropertyName(name);
755         if (value instanceof Boolean JavaDoc || value instanceof Number JavaDoc || value instanceof String JavaDoc)
756             properties.put(name, value);
757         else
758             throw new MessageFormatException JavaDoc("MNJMS0005D : INVALID OBJECT TYPE "+value.getClass().getName()+" FOR METHOD setObjectProperty().");
759            
760     }//setObjectProperty
761

762     /**
763      * Acknowledge this message to the provider.
764      */

765     public final void acknowledge() throws JMSException JavaDoc{
766         if (creatingSession==null)
767            throw new IllegalStateException JavaDoc("MNJMS0005E : METHOD acknowledge() FAILED. PARENT SESSION IS NULL.");
768         if (creatingSession.getAcknowledgeMode()==javax.jms.Session.CLIENT_ACKNOWLEDGE)
769             creatingSession.ackMessage(null);
770         //nimo 6-apr-2005
771
//in the spec, section 3.x.x it says that acking on auto_ack should be
772
//ignored. so i jusr decided to really ack if it's client-ack
773
}//acknowledge
774

775     /**
776      * Clears the body of the message.
777      */

778     public void clearBody() throws JMSException JavaDoc{
779         
780         setWriteableState(true);
781     
782     }//clearBody
783

784     /**
785      * Check whether this message is writeable. Received messages are not writeable.
786      * @return - the writeable state for this message.
787      */

788     protected final boolean isWriteable(){
789         return ((flags & IS_WRITEABLE) >0);
790     }//isWriteable
791

792     
793     public final void setWriteableState(boolean writeable) {
794         if (writeable)
795             flags = flags | IS_WRITEABLE | PROPERTIES_WRITEABLE;
796             
797         else
798             flags = flags & REVERSE_WRITEABLE & PROPERTIES_NOT_WRITEABLE;
799         
800     }//setWriteableState
801

802     protected final void checkWriteableState(boolean prop) throws JMSException JavaDoc {
803         if ((prop && ( (flags & PROPERTIES_WRITEABLE)==0) )||
804             (!prop && ( (flags & IS_WRITEABLE)==0) ))
805             
806            throw new MessageNotWriteableException JavaDoc("MNJMS0005F : FAILED ON METHOD checkWriteableState(). MESSAGE IS NOT WRITEABLE.");
807     }//checkWriteableState
808

809     
810     public final static String JavaDoc BYTEABLENAME ="MantaMsg";
811     public String JavaDoc getByteableName() {
812         
813         return BYTEABLENAME;
814     }
815
816
817     /* (non-Javadoc)
818      * @see org.mr.core.util.byteable.Byteable#toBytes(org.mr.core.util.byteable.ByteableOutputStream)
819      */

820     public void toBytes(ByteableOutputStream out) throws IOException JavaDoc {
821         
822         
823         //write all the little properties.
824
out.writeASCIIString(JMSMessageId);
825         out.writeInt(flags);
826         out.writeLong(JMSExpiration);
827         out.writeLong(JMSTimeStamp);
828         out.writeASCIIString(this.JMSCorrelationId);
829         out.writeByteable((Byteable)this.JMSReplyTo);
830         out.writeByteable((Byteable)this.JMSDestination);
831         
832         //write the properties number and then properties:
833
out.writeInt(properties.size());
834         Iterator JavaDoc propIt = properties.keySet().iterator();
835         
836         while (propIt.hasNext()) {
837             
838             //get the key and value
839
String JavaDoc key = (String JavaDoc)propIt.next();
840             Object JavaDoc val = properties.get(key);
841             
842             //write the property name
843
out.writeUTF(key);
844             
845             //check what type it is, write the type code and the value.
846
if (val instanceof Boolean JavaDoc) {
847                 out.write(BOOLEAN);
848                 out.writeBoolean( ((Boolean JavaDoc)val).booleanValue());
849             }
850             else if (val instanceof Byte JavaDoc) {
851                 out.write(BYTE);
852                 out.writeByte( ((Byte JavaDoc)val).byteValue());
853             }
854             else if (val instanceof Short JavaDoc) {
855                 out.write(SHORT);
856                 out.writeShort( ((Short JavaDoc)val).shortValue());
857             }
858             else if (val instanceof Integer JavaDoc) {
859                 out.write(INTEGER);
860                 out.writeInt( ((Integer JavaDoc)val).intValue());
861             }
862             else if (val instanceof Long JavaDoc) {
863                 out.write(LONG);
864                 out.writeLong( ((Long JavaDoc)val).longValue());
865             }
866             else if (val instanceof String JavaDoc) {
867                 out.write(STRING);
868                 out.writeUTF((String JavaDoc)val);
869             }
870             else if (val instanceof Float JavaDoc) {
871                 out.write(FLOAT);
872                 out.writeFloat( ((Float JavaDoc)val).floatValue());
873             }
874             else if (val instanceof Double JavaDoc) {
875                 out.write(DOUBLE);
876                 out.writeDouble( ((Double JavaDoc)val).doubleValue());
877                 
878             }
879             
880         }
881         
882         out.writeASCIIString(this.originalClient);
883     }
884         
885     
886
887     public Byteable createInstance(ByteableInputStream in) throws IOException JavaDoc {
888         
889         MantaMessage result = new MantaMessage();
890         return createHeadersAndProperties(result,in);
891             
892     }
893
894     MantaMessage createHeadersAndProperties(MantaMessage result, ByteableInputStream in) throws IOException JavaDoc {
895         
896         result.JMSMessageId = in.readASCIIString();
897         result.flags = in.readInt();
898         result.JMSExpiration = in.readLong();
899         result.JMSTimeStamp = in.readLong();
900         result.JMSCorrelationId = in.readASCIIString();
901         
902         result.JMSReplyTo = (MantaDestination)in.readByteable();//new MantaDestination(in.readUTF());
903
result.JMSDestination = (MantaDestination)in.readByteable();//new MantaDestination(in.readUTF());
904
result.connId = null;
905         
906         //build the properties - first: how many are there.
907
int size = in.readInt();
908         for (int i = 0; i<size ; i++) {
909             //key, type, value.
910
String JavaDoc key = in.readUTF();
911             byte type = in.readByte();
912             switch(type) {
913             case BYTE : result.properties.put(key, new Byte JavaDoc(in.readByte())); break;
914             case BOOLEAN : result.properties.put(key, new Boolean JavaDoc(in.readBoolean()));break;
915             case SHORT : result.properties.put(key,new Short JavaDoc(in.readShort())); break;
916             case INTEGER : result.properties.put(key,new Integer JavaDoc(in.readInt())); break;
917             case DOUBLE : result.properties.put(key,new Double JavaDoc(in.readDouble()));break;
918             case FLOAT : result.properties.put(key,new Float JavaDoc(in.readFloat()));break;
919             case STRING : result.properties.put(key,in.readUTF());break;
920             case LONG : result.properties.put(key,new Long JavaDoc(in.readLong()));break;
921             default : break; //should be an exception probably.
922
}
923             
924         }
925         result.originalClient = in.readASCIIString();
926         result.flags = result.flags & PROPERTIES_NOT_WRITEABLE;
927         return result;
928     }
929
930
931     /* (non-Javadoc)
932      * @see org.mr.core.util.byteable.Byteable#registerToByteableRegistry()
933      */

934     public void registerToByteableRegistry() {
935         ByteableRegistry.registerByteableFactory(getByteableName() , this);
936         
937     }
938     
939     public static void register() throws JMSException JavaDoc{
940         MantaMessage instance = new MantaMessage();
941         instance.registerToByteableRegistry();
942     }
943     
944     
945     public MantaMessage makeCopy() throws JMSException JavaDoc {
946         MantaMessage copy = new MantaMessage();
947         initCopy(copy);
948         return copy;
949         
950     }
951     
952     protected void setConnId(String JavaDoc id){
953         connId=id;
954     }
955     
956     public String JavaDoc getConnId(){
957         return connId;
958     }
959     
960     public void repSession(MantaSession session) {
961         this.creatingSession=session;
962     }
963     public void initCopy(MantaMessage copy){
964         
965         copy.JMSMessageId=JMSMessageId;
966         copy.JMSTimeStamp=JMSTimeStamp;
967         copy.JMSExpiration=JMSExpiration;
968         copy.JMSCorrelationId=JMSCorrelationId;
969         copy.JMSReplyTo=JMSReplyTo;
970         copy.JMSDestination=JMSDestination;
971         copy.flags = flags;
972         copy.connId=connId;
973         copy.creatingSession = creatingSession;
974         copy.properties = new HashMap JavaDoc(properties);
975         copy.timeAccepted = timeAccepted;
976         copy.originalClient= originalClient;
977     }
978     
979     
980     //-------------classes needed for Bytabling stuff
981

982 }//MantaMessage
Popular Tags