KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > message > MessageHeader


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  * Copyright 2001 Dan Greff
20  */

21 package com.presumo.jms.message;
22
23 import java.io.DataInput JavaDoc;
24 import java.io.DataOutput JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import javax.jms.Destination JavaDoc;
28
29 /**
30  * Encapsulates the javax.jms.Message's header.
31  *
32  * @author Dan Greff
33  */

34 final class MessageHeader
35 {
36   private static final int JMS_DESTINATION = 1;
37   private static final int JMS_MESSAGEID = 2;
38   private static final int JMS_DELIVERYMODE = 4;
39   private static final int JMS_CORRELATION = 8;
40   private static final int JMS_EXPIRATION = 16;
41   private static final int JMS_TIMESTAMP = 32;
42   private static final int JMS_PRIORITY = 64;
43   private static final int JMS_REPLYTO = 128;
44   private static final int JMS_REDELIV = 256;
45   private static final int JMS_TYPE = 512;
46   private static final int JMS_SYSTEM_TYPE = 1024;
47   
48   // Unused space
49
// 2^11 = 2048
50
// 2^12 = 4096
51
// 2^14 = 16384
52

53   //
54
// For speed, JmsMessage reads these directly, but for OOP it
55
// sets them via the accessor methods so that this class can
56
// handle the bitmask complexity
57
//
58
String JavaDoc jmsDestination;
59   MessageID jmsMessageID;
60   int jmsDeliveryMode;
61   MessageID jmsCorrelationID;
62   long jmsExpiration;
63   long jmsTimestamp;
64   int jmsPriority = javax.jms.Message.DEFAULT_PRIORITY;
65   String JavaDoc jmsReplyTo;
66   boolean jmsRedelivered;
67   String JavaDoc jmsType;
68   int jmsSystemMsgType;
69   
70   private int bitMask;
71   
72     /////////////////////////////////////////////////////////////////////////
73
// Constructor //
74
/////////////////////////////////////////////////////////////////////////
75
MessageHeader()
76   {
77     super();
78   }
79   
80
81     /////////////////////////////////////////////////////////////////////////
82
// Public Methods //
83
/////////////////////////////////////////////////////////////////////////
84

85   public String JavaDoc toString()
86   {
87     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
88     result.append("Destination: ");
89     result.append( ((jmsDestination == null) ? "none" : jmsDestination) );
90     result.append('\n');
91     
92     result.append("MessageID: " );
93     result.append(jmsMessageID.toString());
94     result.append('\n');
95     if (jmsDeliveryMode == javax.jms.DeliveryMode.PERSISTENT)
96       result.append("PERSISTENT");
97     else
98       result.append("NON-PERSITENT");
99     result.append(": priority=");
100     result.append(jmsPriority);
101     if (jmsRedelivered)
102       result.append(" : REDELIVERED");
103     result.append('\n');
104     
105     if (jmsCorrelationID != null) {
106       result.append("CorrelationID: ");
107       result.append(jmsCorrelationID.toString());
108       result.append('\n');
109     }
110     
111     if (jmsReplyTo != null) {
112       result.append("ReplyTo: ");
113       result.append(jmsReplyTo.toString());
114       result.append('\n');
115     }
116         
117     if (jmsExpiration != 0) {
118       result.append("Expiration: ");
119       result.append(jmsExpiration);
120       result.append('\n');
121     }
122     
123     if (jmsTimestamp != 0) {
124       result.append("Timestamp: ");
125       result.append(jmsTimestamp);
126       result.append('\n');
127     }
128
129     return result.toString();
130   }
131
132     /////////////////////////////////////////////////////////////////////////
133
// Package Methods //
134
/////////////////////////////////////////////////////////////////////////
135

136   /**
137    * Custom data marshaling mechanism.
138    */

139   final void marshal(DataOutput JavaDoc out) throws IOException JavaDoc
140   {
141     out.writeShort(bitMask);
142   
143     if ((bitMask & JMS_DESTINATION) != 0)
144       out.writeUTF(jmsDestination);
145     
146     if ((bitMask & JMS_MESSAGEID) != 0)
147       jmsMessageID.marshal(out);
148
149     if ((bitMask & JMS_DELIVERYMODE) != 0)
150       out.writeByte(jmsDeliveryMode);
151     
152     if ((bitMask & JMS_CORRELATION) != 0)
153       jmsCorrelationID.marshal(out);
154     
155     if ((bitMask & JMS_EXPIRATION) != 0)
156       out.writeLong(jmsExpiration);
157     
158     if ((bitMask & JMS_TIMESTAMP) != 0)
159       out.writeLong(jmsTimestamp);
160     
161     if ((bitMask & JMS_PRIORITY) != 0)
162       out.writeByte(jmsPriority);
163     
164     if ((bitMask & JMS_REPLYTO) != 0)
165       out.writeUTF(jmsReplyTo.toString());
166     
167     if ((bitMask & JMS_REDELIV) != 0)
168       out.writeBoolean(jmsRedelivered);
169     
170     if ((bitMask & JMS_TYPE) != 0)
171       out.writeUTF(jmsType);
172     if ((bitMask & JMS_SYSTEM_TYPE) != 0)
173       out.writeByte(jmsSystemMsgType);
174   }
175   
176   /**
177    * Reads in state saved via the custom marshaling mechanism
178    */

179   void unmarshal(DataInput JavaDoc in) throws IOException JavaDoc
180   {
181
182     bitMask = in.readShort();
183    
184     if ((bitMask & JMS_DESTINATION) != 0)
185       jmsDestination = in.readUTF();
186     
187     if ((bitMask & JMS_MESSAGEID) != 0)
188       jmsMessageID = MessageID.unmarshal(in);
189
190     if ((bitMask & JMS_DELIVERYMODE) != 0)
191       jmsDeliveryMode = in.readByte();
192       
193     if ((bitMask & JMS_CORRELATION) != 0)
194       jmsCorrelationID = MessageID.unmarshal(in);
195       
196     if ((bitMask & JMS_EXPIRATION) != 0)
197       jmsExpiration = in.readLong();
198       
199     if ((bitMask & JMS_TIMESTAMP) != 0)
200       jmsTimestamp = in.readLong();
201       
202     if ((bitMask & JMS_PRIORITY) != 0)
203       jmsPriority = in.readByte();
204       
205     if ((bitMask & JMS_REPLYTO) != 0)
206       jmsReplyTo = in.readUTF();
207       
208     if ((bitMask & JMS_REDELIV) != 0)
209       jmsRedelivered = in.readBoolean();
210     
211     if ((bitMask & JMS_TYPE) != 0)
212       jmsType = in.readUTF();
213       
214     if ((bitMask & JMS_SYSTEM_TYPE) != 0)
215       jmsSystemMsgType = in.readByte();
216   }
217
218   
219   final void setJMSMessageID(MessageID messageID)
220   {
221     jmsMessageID = messageID;
222     bitMask |= JMS_MESSAGEID;
223   }
224
225   final void setJMSTimestamp(long timestamp)
226   {
227     jmsTimestamp = timestamp;
228     bitMask |= JMS_TIMESTAMP;
229   }
230   
231   final void setJMSCorrelationID(MessageID correlationID)
232   {
233     jmsCorrelationID = correlationID;
234     bitMask |= JMS_CORRELATION;
235   }
236
237   final void setJMSReplyTo(Destination JavaDoc replyTo)
238   {
239     jmsReplyTo = replyTo.toString();
240     bitMask |= JMS_REPLYTO;
241   }
242
243   final void setJMSDestination(Destination JavaDoc destination)
244   {
245     jmsDestination = destination.toString();
246     bitMask |= JMS_DESTINATION;
247   }
248   
249   final void setJMSDeliveryMode(int deliveryMode)
250   {
251     jmsDeliveryMode = deliveryMode;
252     bitMask |= JMS_DELIVERYMODE;
253   }
254
255   final void setJMSRedelivered(boolean redelivered)
256   {
257     jmsRedelivered = redelivered;
258     bitMask |= JMS_REDELIV;
259   }
260
261   final void setJMSType(String JavaDoc type)
262   {
263     jmsType = type;
264     bitMask |= JMS_TYPE;
265   }
266
267   final void setJMSExpiration(long expiration)
268   {
269     jmsExpiration = expiration;
270     bitMask |= JMS_EXPIRATION;
271   }
272   
273   final void setJMSPriority(int priority)
274   {
275     jmsPriority = priority;
276     bitMask |= JMS_PRIORITY;
277   }
278
279   final void setJMSSystemMsgType(int type)
280   {
281     jmsSystemMsgType = type;
282     bitMask |= JMS_SYSTEM_TYPE;
283
284     if (type == SystemMessageConstants.DEFAULT)
285       bitMask ^= JMS_SYSTEM_TYPE;
286   }
287 }
288
Popular Tags