KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > MessageImpl


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms;
8
9 import java.io.Serializable JavaDoc;
10 import java.util.Enumeration JavaDoc;
11
12 import javax.jms.BytesMessage JavaDoc;
13 import javax.jms.Destination JavaDoc;
14 import javax.jms.JMSException JavaDoc;
15 import javax.jms.MapMessage JavaDoc;
16 import javax.jms.Message JavaDoc;
17 import javax.jms.MessageNotWriteableException JavaDoc;
18 import javax.jms.ObjectMessage JavaDoc;
19 import javax.jms.StreamMessage JavaDoc;
20 import javax.jms.TextMessage JavaDoc;
21
22 import org.jboss.jms.client.SessionDelegate;
23 import org.jboss.jms.client.p2p.P2PSessionDelegate;
24 import org.jboss.jms.message.JBossMessage;
25 import org.jboss.jms.util.MessageProperties;
26 import org.jboss.util.id.GUID;
27
28 /**
29  *
30  * @author <a HREF="mailto:nathan@jboss.org">Nathan Phelps</a>
31  * @version $Revision: 1.5 $ $Date: 2003/08/21 17:37:22 $
32  */

33 public class MessageImpl implements JBossMessage, Cloneable JavaDoc, Serializable JavaDoc
34 {
35     static final String JavaDoc BYTES_MESSAGE_NAME = "BytesMessage";
36     static final String JavaDoc MAP_MESSAGE_NAME = "MapMessage";
37     static final String JavaDoc MESSAGE_NAME = "Message";
38     static final String JavaDoc OBJECT_MESSAGE_NAME = "ObjectMessage";
39     static final String JavaDoc STREAM_MESSGE_NAME = "StreamMessage";
40     static final String JavaDoc TEXT_MESSAGE_NAME = "TextMessage";
41
42     protected Object JavaDoc body = null;
43     private String JavaDoc correlationID = null;
44     private int deliveryMode = Message.DEFAULT_DELIVERY_MODE;
45
46     private Destination JavaDoc destination = null;
47     private long expiration = 0;
48     private String JavaDoc messageID = null;
49     private int priority = Message.DEFAULT_PRIORITY;
50     private MessageProperties properties = new MessageProperties();
51     private boolean readOnly = false;
52     private boolean redelivered = false;
53     private Destination JavaDoc replyTo = null;
54     private long timestamp = 0;
55     protected String JavaDoc type = null;
56
57     private P2PSessionDelegate session = null;
58     public long deliveryId = -1L;
59     private boolean local = false;
60     private String JavaDoc originClientId = null;
61
62     public static Message JavaDoc create(Class JavaDoc type) throws JMSException JavaDoc
63     {
64         if (type.equals(BytesMessage JavaDoc.class))
65         {
66             return new BytesMessageImpl();
67         }
68         if (type.equals(MapMessage JavaDoc.class))
69         {
70             return new MapMessageImpl();
71         }
72         if (type.equals(Message JavaDoc.class))
73         {
74             return new MessageImpl();
75         }
76         if (type.equals(ObjectMessage JavaDoc.class))
77         {
78             return new ObjectMessageImpl();
79         }
80         else if (type.equals(StreamMessage JavaDoc.class))
81         {
82             return new StreamMessageImpl();
83         }
84         else if (type.equals(BytesMessage JavaDoc.class))
85         {
86             return new BytesMessageImpl();
87         }
88         else if (type.equals(TextMessage JavaDoc.class))
89         {
90             return new TextMessageImpl();
91         }
92         else
93         {
94             throw new JMSException JavaDoc("'" + type.getName() + "' is an invalid message type.");
95         }
96     }
97
98     public void acknowledge() throws JMSException JavaDoc
99     {
100         if (this.session != null)
101         {
102             this.session.acknowledge(this, true);
103         }
104     }
105
106     public void clearBody()
107     {
108         // We generally let the subclass take care of this, but...
109
this.body = null;
110         this.readOnly = false;
111     }
112
113     public final void clearProperties()
114     {
115         this.properties.clear();
116     }
117
118     public final boolean getBooleanProperty(String JavaDoc name) throws JMSException JavaDoc
119     {
120         return this.properties.getBoolean(name);
121     }
122
123     public final byte getByteProperty(String JavaDoc name) throws JMSException JavaDoc
124     {
125         return this.properties.getByte(name);
126     }
127
128     public final double getDoubleProperty(String JavaDoc name) throws JMSException JavaDoc
129     {
130         return this.properties.getDouble(name);
131     }
132
133     public final float getFloatProperty(String JavaDoc name) throws JMSException JavaDoc
134     {
135         return this.properties.getFloat(name);
136     }
137
138     public final int getIntProperty(String JavaDoc name) throws JMSException JavaDoc
139     {
140         return this.properties.getInt(name);
141     }
142
143     public final String JavaDoc getJMSCorrelationID()
144     {
145         return this.correlationID;
146     }
147
148     public final byte[] getJMSCorrelationIDAsBytes()
149     {
150         return this.correlationID.getBytes();
151     }
152
153     public final int getJMSDeliveryMode()
154     {
155         return this.deliveryMode;
156     }
157
158     public final Destination JavaDoc getJMSDestination()
159     {
160         return this.destination;
161     }
162
163     public final long getJMSExpiration()
164     {
165         return this.expiration;
166     }
167
168     public final String JavaDoc getJMSMessageID()
169     {
170         return this.messageID;
171     }
172
173     public final int getJMSPriority()
174     {
175         return this.priority;
176     }
177
178     public final boolean getJMSRedelivered()
179     {
180         return this.redelivered;
181     }
182
183     public final Destination JavaDoc getJMSReplyTo()
184     {
185         return this.replyTo;
186     }
187
188     public final long getJMSTimestamp()
189     {
190         return this.timestamp;
191     }
192
193     public final String JavaDoc getJMSType()
194     {
195         return this.type;
196     }
197
198     public final long getLongProperty(String JavaDoc name) throws JMSException JavaDoc
199     {
200         return this.properties.getLong(name);
201     }
202
203     public final Object JavaDoc getObjectProperty(String JavaDoc name) throws JMSException JavaDoc
204     {
205         return this.properties.getObject(name);
206     }
207
208     public final Enumeration JavaDoc getPropertyNames() throws JMSException JavaDoc
209     {
210         return this.properties.getMapNames();
211     }
212
213     public final short getShortProperty(String JavaDoc name) throws JMSException JavaDoc
214     {
215         return this.properties.getShort(name);
216     }
217
218     public final String JavaDoc getStringProperty(String JavaDoc name) throws JMSException JavaDoc
219     {
220         return this.properties.getString(name);
221     }
222
223     public final boolean isReadOnly()
224     {
225         return this.readOnly;
226     }
227
228     public final boolean hasExpired()
229     {
230         if (this.expiration < 1)
231         {
232             return false;
233         }
234         else
235         {
236             return System.currentTimeMillis() > this.expiration;
237         }
238     }
239
240     public final void setSession(P2PSessionDelegate session)
241     {
242         this.session = session;
243     }
244
245     public final void setDeliveryId(long id)
246     {
247         this.deliveryId = id;
248     }
249
250     public final void setOriginClientID(String JavaDoc clientId)
251     {
252         this.originClientId = clientId;
253     }
254
255     public final String JavaDoc getOrigianClientID()
256     {
257         return this.originClientId;
258     }
259
260     public final void setIsLocal(boolean value)
261     {
262         this.local = value;
263     }
264
265     public boolean isLocal()
266     {
267         return this.local;
268     }
269
270     public final boolean propertyExists(String JavaDoc name)
271     {
272         return this.properties.itemExists(name);
273     }
274
275     public final void setBooleanProperty(String JavaDoc name, boolean value) throws JMSException JavaDoc
276     {
277         this.properties.setBoolean(name, value);
278     }
279
280     public final void setByteProperty(String JavaDoc name, byte value) throws JMSException JavaDoc
281     {
282         this.properties.setByte(name, value);
283     }
284
285     public final void setDoubleProperty(String JavaDoc name, double value) throws JMSException JavaDoc
286     {
287         this.properties.setDouble(name, value);
288     }
289
290     public final void setFloatProperty(String JavaDoc name, float value) throws JMSException JavaDoc
291     {
292         this.properties.setFloat(name, value);
293     }
294
295     public final void setIntProperty(String JavaDoc name, int value) throws JMSException JavaDoc
296     {
297         this.properties.setInt(name, value);
298     }
299
300     public final void setJMSCorrelationID(String JavaDoc correlationID)
301     {
302         this.correlationID = correlationID;
303     }
304
305     public final void setJMSCorrelationIDAsBytes(byte[] correlationID)
306     {
307         throw new UnsupportedOperationException JavaDoc("Not implemented.");
308     }
309
310     public final void setJMSDeliveryMode(int deliveryMode)
311     {
312         this.deliveryMode = deliveryMode;
313     }
314
315     public final void setJMSDestination(Destination JavaDoc destination)
316     {
317         this.destination = destination;
318     }
319
320     public final void setJMSExpiration(long expiration)
321     {
322         this.expiration = expiration;
323     }
324
325     public final void setJMSMessageID(String JavaDoc messageID)
326     {
327         this.messageID = messageID;
328     }
329
330     public final void setJMSPriority(int priority)
331     {
332         this.priority = priority;
333     }
334
335     public final void setJMSRedelivered(boolean redelivered)
336     {
337         this.redelivered = redelivered;
338     }
339
340     public final void setJMSReplyTo(Destination JavaDoc replyTo)
341     {
342         this.replyTo = replyTo;
343     }
344
345     public final void setJMSTimestamp(long timestamp)
346     {
347         this.timestamp = timestamp;
348     }
349
350     public final void setJMSType(String JavaDoc type)
351     {
352         this.type = type;
353     }
354
355     public final void setLongProperty(String JavaDoc name, long value) throws JMSException JavaDoc
356     {
357         this.properties.setLong(name, value);
358     }
359
360     public final void setObjectProperty(String JavaDoc name, Object JavaDoc value) throws JMSException JavaDoc
361     {
362         this.properties.setObject(name, value);
363     }
364
365     public final void setReadOnly(boolean value)
366     {
367         this.readOnly = value;
368     }
369
370     public final void setShortProperty(String JavaDoc name, short value) throws JMSException JavaDoc
371     {
372         this.properties.setShort(name, value);
373     }
374
375     public final void setStringProperty(String JavaDoc name, String JavaDoc value) throws JMSException JavaDoc
376     {
377         this.properties.setString(name, value);
378     }
379    public void generateMessageID() throws JMSException JavaDoc
380    {
381       messageID = GUID.asString();
382    }
383
384    public void generateTimestamp() throws JMSException JavaDoc
385    {
386       timestamp = System.currentTimeMillis();
387    }
388
389    public SessionDelegate getSessionDelegate() throws JMSException JavaDoc
390    {
391       return session;
392    }
393
394    public void makeReadOnly()
395    {
396       setReadOnly(true);
397       properties.setReadOnly(true);
398    }
399
400     public Object JavaDoc clone()
401     {
402         try{
403             return super.clone();
404         }
405         catch (CloneNotSupportedException JavaDoc ignored)
406         {
407             return null; // To my knowledge, we shouldn't get here...
408
}
409     }
410
411     protected void throwExceptionIfReadOnly() throws MessageNotWriteableException JavaDoc
412     {
413         if (this.readOnly)
414         {
415             throw new MessageNotWriteableException JavaDoc("Unable to write body: the message body is currently read only.");
416         }
417     }
418
419 }
Popular Tags