KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > message > MessageImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.message;
30
31 import com.caucho.jms.session.SessionImpl;
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34 import com.caucho.util.NullEnumeration;
35
36 import javax.jms.Destination JavaDoc;
37 import javax.jms.JMSException JavaDoc;
38 import javax.jms.Message JavaDoc;
39 import javax.jms.MessageFormatException JavaDoc;
40 import javax.jms.MessageNotReadableException JavaDoc;
41 import javax.jms.MessageNotWriteableException JavaDoc;
42 import java.lang.ref.SoftReference JavaDoc;
43 import java.util.Collections JavaDoc;
44 import java.util.Enumeration JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.HashSet JavaDoc;
47 import java.util.logging.Level JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 /**
51  * A basic message.
52  */

53 public class MessageImpl implements Message JavaDoc {
54   static final Logger JavaDoc log = Log.open(MessageImpl.class);
55   static final L10N L = new L10N(MessageImpl.class);
56
57   private static final HashSet JavaDoc<String JavaDoc> _reserved;
58
59   private volatile SoftReference JavaDoc<SessionImpl> _sessionRef;
60   
61   private String JavaDoc _messageId;
62   private String JavaDoc _correlationId;
63   
64   private long _timestamp;
65   private long _expiration;
66   
67   private Destination JavaDoc _destination;
68   private Destination JavaDoc _replyTo;
69
70   private int _deliveryMode;
71   private boolean _isRedelivered;
72
73   private String JavaDoc _messageType;
74   private int _priority = 4;
75
76   private HashMap JavaDoc<String JavaDoc,Object JavaDoc> _properties;
77   
78   private boolean _isHeaderWriteable = true;
79   private boolean _isBodyWriteable = true;
80
81   public MessageImpl()
82   {
83   }
84
85   /**
86    * Sets the session.
87    */

88   public void setSession(SessionImpl session)
89   {
90     _sessionRef = new SoftReference JavaDoc<SessionImpl>(session);
91   }
92
93   /**
94    * Returns the message id.
95    */

96   public String JavaDoc getJMSMessageID()
97     throws JMSException JavaDoc
98   {
99     return _messageId;
100   }
101
102   /**
103    * Sets the message id.
104    *
105    * @param id the new message id
106    */

107   public void setJMSMessageID(String JavaDoc id)
108     throws JMSException JavaDoc
109   {
110     _messageId = id;
111   }
112
113   /**
114    * Returns the time the message was sent.
115    */

116   public long getJMSTimestamp()
117     throws JMSException JavaDoc
118   {
119     return _timestamp;
120   }
121
122   /**
123    * Sets the time the message was sent.
124    *
125    * @param time the message timestamp
126    */

127   public void setJMSTimestamp(long time)
128     throws JMSException JavaDoc
129   {
130     _timestamp = time;
131   }
132
133   /**
134    * Returns the correlation id.
135    */

136   public byte []getJMSCorrelationIDAsBytes()
137     throws JMSException JavaDoc
138   {
139     try {
140       if (_correlationId == null)
141         return null;
142       else
143         return _correlationId.getBytes("UTF8");
144     } catch (Throwable JavaDoc e) {
145       log.log(Level.WARNING, e.toString(), e);
146       
147       return null;
148     }
149   }
150
151   /**
152    * Sets the correlation id.
153    *
154    * @param id the correlation id
155    */

156   public void setJMSCorrelationIDAsBytes(byte []id)
157     throws JMSException JavaDoc
158   {
159     try {
160       _correlationId = new String JavaDoc(id, 0, id.length, "UTF8");
161     } catch (Exception JavaDoc e) {
162       log.log(Level.WARNING, e.toString(), e);
163     }
164   }
165
166   /**
167    * Returns the correlation id.
168    */

169   public String JavaDoc getJMSCorrelationID()
170     throws JMSException JavaDoc
171   {
172     return _correlationId;
173   }
174
175   /**
176    * Sets the correlation id.
177    *
178    * @param id the correlation id
179    */

180   public void setJMSCorrelationID(String JavaDoc id)
181     throws JMSException JavaDoc
182   {
183     _correlationId = id;
184   }
185
186   /**
187    * Gets the reply-to destination
188    */

189   public Destination JavaDoc getJMSReplyTo()
190     throws JMSException JavaDoc
191   {
192     return _replyTo;
193   }
194
195   /**
196    * Sets the reply-to destination
197    *
198    * @param replyTo the destination
199    */

200   public void setJMSReplyTo(Destination JavaDoc replyTo)
201     throws JMSException JavaDoc
202   {
203     _replyTo = replyTo;
204   }
205
206   /**
207    * Gets the destination
208    */

209   public Destination JavaDoc getJMSDestination()
210     throws JMSException JavaDoc
211   {
212     return _destination;
213   }
214
215   /**
216    * Sets the reply-to destination
217    *
218    * @param destination the destination
219    */

220   public void setJMSDestination(Destination JavaDoc destination)
221     throws JMSException JavaDoc
222   {
223     _destination = destination;
224   }
225
226   /**
227    * Gets the delivery model
228    */

229   public int getJMSDeliveryMode()
230     throws JMSException JavaDoc
231   {
232     return _deliveryMode;
233   }
234
235   /**
236    * Sets the delivery mode
237    *
238    * @param deliveryMode the delivery mode
239    */

240   public void setJMSDeliveryMode(int deliveryMode)
241     throws JMSException JavaDoc
242   {
243     _deliveryMode = deliveryMode;
244   }
245
246   /**
247    * Returns if the message is being redelivered.
248    */

249   public boolean getJMSRedelivered()
250     throws JMSException JavaDoc
251   {
252     return _isRedelivered;
253   }
254
255   /**
256    * Sets if the message is being redelivered.
257    *
258    * @param deliveryMode the delivery mode
259    */

260   public void setJMSRedelivered(boolean isRedelivered)
261     throws JMSException JavaDoc
262   {
263     _isRedelivered = isRedelivered;
264   }
265
266   /**
267    * Returns the message type
268    */

269   public String JavaDoc getJMSType()
270     throws JMSException JavaDoc
271   {
272     return _messageType;
273   }
274
275   /**
276    * Sets the message type.
277    *
278    * @param type the delivery mode
279    */

280   public void setJMSType(String JavaDoc type)
281     throws JMSException JavaDoc
282   {
283     _messageType = type;
284   }
285
286   /**
287    * Returns the message expiration time.
288    */

289   public long getJMSExpiration()
290     throws JMSException JavaDoc
291   {
292     return _expiration;
293   }
294
295   /**
296    * Sets the message expiration type.
297    *
298    * @param time the expiration time
299    */

300   public void setJMSExpiration(long time)
301     throws JMSException JavaDoc
302   {
303     _expiration = time;
304   }
305
306   /**
307    * Returns the message priority.
308    */

309   public int getJMSPriority()
310     throws JMSException JavaDoc
311   {
312     return _priority;
313   }
314
315   /**
316    * Sets the message priority.
317    *
318    * @param priority the priority
319    */

320   public void setJMSPriority(int priority)
321     throws JMSException JavaDoc
322   {
323     _priority = priority;
324   }
325
326   /**
327    * Clears the message properties, making them writeable.
328    */

329   public void clearProperties()
330     throws JMSException JavaDoc
331   {
332     if (_properties != null)
333       _properties.clear();
334     _isHeaderWriteable = true;
335   }
336
337   /**
338    * Returns true if the property exists.
339    */

340   public boolean propertyExists(String JavaDoc name)
341     throws JMSException JavaDoc
342   {
343     if (_properties == null)
344       return false;
345     
346     return _properties.keySet().contains(name);
347   }
348
349   /**
350    * Returns a boolean property with the given name.
351    */

352   public boolean getBooleanProperty(String JavaDoc name)
353     throws JMSException JavaDoc
354   {
355     return ObjectConverter.toBoolean(getObjectProperty(name));
356   }
357
358   /**
359    * Returns a property as a byte
360    */

361   public byte getByteProperty(String JavaDoc name)
362     throws JMSException JavaDoc
363   {
364     return ObjectConverter.toByte(getObjectProperty(name));
365   }
366
367   /**
368    * Returns a property as a short
369    */

370   public short getShortProperty(String JavaDoc name)
371     throws JMSException JavaDoc
372   {
373     return ObjectConverter.toShort(getObjectProperty(name));
374   }
375
376   /**
377    * Returns a property as an integer
378    */

379   public int getIntProperty(String JavaDoc name)
380     throws JMSException JavaDoc
381   {
382     return ObjectConverter.toInt(getObjectProperty(name));
383   }
384
385   /**
386    * Returns a property as a long
387    */

388   public long getLongProperty(String JavaDoc name)
389     throws JMSException JavaDoc
390   {
391     return ObjectConverter.toLong(getObjectProperty(name));
392   }
393
394   /**
395    * Returns a property as a float
396    */

397   public float getFloatProperty(String JavaDoc name)
398     throws JMSException JavaDoc
399   {
400     return ObjectConverter.toFloat(getObjectProperty(name));
401   }
402
403   /**
404    * Returns a property as a double
405    */

406   public double getDoubleProperty(String JavaDoc name)
407     throws JMSException JavaDoc
408   {
409     return ObjectConverter.toDouble(getObjectProperty(name));
410   }
411
412   /**
413    * Returns a string property.
414    */

415   public String JavaDoc getStringProperty(String JavaDoc name)
416     throws JMSException JavaDoc
417   {
418     Object JavaDoc prop = getObjectProperty(name);
419
420     if (prop == null)
421       return null;
422
423     return String.valueOf(prop);
424   }
425
426   /**
427    * Returns a string property.
428    */

429   public Object JavaDoc getObjectProperty(String JavaDoc name)
430     throws JMSException JavaDoc
431   {
432     if (_properties == null || name == null)
433       return null;
434     else
435       return _properties.get(name);
436   }
437
438   /**
439    * Returns an enumeration of the message's properties.
440    */

441   public Enumeration getPropertyNames()
442     throws JMSException JavaDoc
443   {
444     if (_properties == null)
445       return NullEnumeration.create();
446     else
447       return Collections.enumeration(_properties.keySet());
448   }
449
450   /**
451    * Sets a boolean property.
452    *
453    * @param name the property name
454    * @param value the property's value
455    */

456   public void setBooleanProperty(String JavaDoc name, boolean value)
457     throws JMSException JavaDoc
458   {
459     setObjectProperty(name, new Boolean JavaDoc(value));
460   }
461
462   /**
463    * Sets a byte property.
464    *
465    * @param name the property name
466    * @param value the property's value
467    */

468   public void setByteProperty(String JavaDoc name, byte value)
469     throws JMSException JavaDoc
470   {
471     setObjectProperty(name, new Byte JavaDoc(value));
472   }
473
474   /**
475    * Sets a short property.
476    *
477    * @param name the property name
478    * @param value the property's value
479    */

480   public void setShortProperty(String JavaDoc name, short value)
481     throws JMSException JavaDoc
482   {
483     setObjectProperty(name, new Short JavaDoc(value));
484   }
485
486   /**
487    * Sets an integer property.
488    *
489    * @param name the property name
490    * @param value the property's value
491    */

492   public void setIntProperty(String JavaDoc name, int value)
493     throws JMSException JavaDoc
494   {
495     setObjectProperty(name, new Integer JavaDoc(value));
496   }
497
498   /**
499    * Sets a long property.
500    *
501    * @param name the property name
502    * @param value the property's value
503    */

504   public void setLongProperty(String JavaDoc name, long value)
505     throws JMSException JavaDoc
506   {
507     setObjectProperty(name, new Long JavaDoc(value));
508   }
509
510   /**
511    * Sets a float property.
512    *
513    * @param name the property name
514    * @param value the property's value
515    */

516   public void setFloatProperty(String JavaDoc name, float value)
517     throws JMSException JavaDoc
518   {
519     setObjectProperty(name, new Float JavaDoc(value));
520   }
521
522   /**
523    * Sets a double property.
524    *
525    * @param name the property name
526    * @param value the property's value
527    */

528   public void setDoubleProperty(String JavaDoc name, double value)
529     throws JMSException JavaDoc
530   {
531     setObjectProperty(name, new Double JavaDoc(value));
532   }
533
534   /**
535    * Sets a string property.
536    *
537    * @param name the property name
538    * @param value the property's value
539    */

540   public void setStringProperty(String JavaDoc name, String JavaDoc value)
541     throws JMSException JavaDoc
542   {
543     setObjectProperty(name, value);
544   }
545
546   /**
547    * Sets an object property.
548    *
549    * @param name the property name
550    * @param value the property's value
551    */

552   public void setObjectProperty(String JavaDoc name, Object JavaDoc value)
553     throws JMSException JavaDoc
554   {
555     checkPropertyWriteable();
556
557     if (name == null)
558       throw new NullPointerException JavaDoc();
559     if (isReserved(name))
560       throw new JMSException JavaDoc(L.l("'{0}' is a reserved property name.",
561                  name));
562
563     if (! (value == null ||
564        value instanceof Number JavaDoc ||
565        value instanceof String JavaDoc ||
566        value instanceof Boolean JavaDoc))
567       throw new MessageFormatException JavaDoc(L.l("{0} is an illegal object property value",
568                        value.getClass().getName()));
569     
570     if (_properties == null)
571       _properties = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
572
573     _properties.put(name, value);
574   }
575   
576   /**
577    * Acknowledge receipt of this message.
578    */

579   public void acknowledge()
580     throws JMSException JavaDoc
581   {
582     SoftReference JavaDoc<SessionImpl> sessionRef = _sessionRef;
583     SessionImpl session;
584
585     if (sessionRef != null && (session = sessionRef.get()) != null) {
586       session.acknowledge();
587     }
588
589     _sessionRef = null;
590   }
591   
592   /**
593    * Clears the body, setting write mode.
594    */

595   public void clearBody()
596     throws JMSException JavaDoc
597   {
598     _isBodyWriteable = true;
599   }
600
601   /**
602    * Sets the body for reading.
603    */

604   public void setReceive()
605     throws JMSException JavaDoc
606   {
607     _isHeaderWriteable = false;
608     _isBodyWriteable = false;
609   }
610
611   /**
612    * Sets the body for reading.
613    */

614   protected void setBodyReadOnly()
615     throws JMSException JavaDoc
616   {
617     _isBodyWriteable = false;
618   }
619
620   /**
621    * Returns the properties.
622    */

623   public HashMap JavaDoc<String JavaDoc,Object JavaDoc> getProperties()
624   {
625     return _properties;
626   }
627   
628   public MessageImpl copy()
629   {
630     MessageImpl msg = new MessageImpl();
631
632     copy(msg);
633
634     return msg;
635   }
636
637   protected void checkHeaderWriteable()
638     throws JMSException JavaDoc
639   {
640     if (! _isHeaderWriteable)
641       throw new MessageNotWriteableException JavaDoc(L.l("received messages can't be written."));
642   }
643
644   protected void checkPropertyWriteable()
645     throws JMSException JavaDoc
646   {
647     if (! _isHeaderWriteable)
648       throw new MessageNotWriteableException JavaDoc(L.l("properties for received messages are read-only."));
649   }
650
651   protected void checkBodyWriteable()
652     throws JMSException JavaDoc
653   {
654     if (! _isBodyWriteable)
655       throw new MessageNotWriteableException JavaDoc(L.l("received messages can't be written."));
656   }
657
658   protected void checkBodyReadable()
659     throws JMSException JavaDoc
660   {
661     if (_isBodyWriteable)
662       throw new MessageNotReadableException JavaDoc(L.l("write-only messages can't be read until reset() is called."));
663   }
664
665   protected void copy(MessageImpl newMsg)
666   {
667     if (_properties != null) {
668       newMsg._properties = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>(_properties);
669     }
670
671     newMsg._messageId = _messageId;
672     newMsg._correlationId = _correlationId;
673     
674     newMsg._timestamp = _timestamp;
675     newMsg._expiration = _expiration;
676     
677     newMsg._destination = _destination;
678     newMsg._replyTo = _replyTo;
679
680     newMsg._deliveryMode = _deliveryMode;
681     newMsg._isRedelivered = _isRedelivered;
682     
683     newMsg._messageType = _messageType;
684     newMsg._priority = _priority;
685   }
686
687   public String JavaDoc toString()
688   {
689     if (_messageId != null)
690       return getClass().getName() + "[" + _messageId + "]";
691     else
692       return getClass().getName() + "@" + System.identityHashCode(this);
693   }
694
695   public static boolean isReserved(String JavaDoc name)
696   {
697     return _reserved.contains(name.toUpperCase());
698   }
699
700   static {
701     _reserved = new HashSet JavaDoc<String JavaDoc>();
702     _reserved.add("TRUE");
703     _reserved.add("FALSE");
704     _reserved.add("NULL");
705     _reserved.add("NOT");
706     _reserved.add("AND");
707     _reserved.add("OR");
708     _reserved.add("BETWEEN");
709     _reserved.add("LIKE");
710     _reserved.add("IN");
711     _reserved.add("IS");
712     _reserved.add("ESCAPE");
713   }
714 }
715
716
Popular Tags