KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjoram > MapMessage


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (INRIA)
22  * Contributor(s): Nicolas Tachker (ScalAgent)
23  */

24 package com.scalagent.kjoram;
25
26 import com.scalagent.kjoram.excepts.*;
27 import com.scalagent.kjoram.messages.ConversionHelper;
28
29 import java.util.*;
30
31 public class MapMessage extends Message
32 {
33   /** The wrapped hashtable. */
34   private Hashtable map;
35   /** <code>true</code> if the message body is read-only. */
36   private boolean RObody = false;
37
38
39   /**
40    * Instanciates a bright new <code>MapMessage</code>.
41    */

42   MapMessage()
43   {
44     super();
45     map = new Hashtable();
46   }
47
48   /**
49    * Instanciates a <code>MapMessage</code> wrapping a consumed MOM
50    * message containing an hashtable.
51    *
52    * @param sess The consuming session.
53    * @param momMsg The MOM message to wrap.
54    *
55    * @exception MessageFormatException In case of a problem when getting the
56    * MOM message data.
57    */

58   MapMessage(Session sess, com.scalagent.kjoram.messages.Message momMsg)
59   throws MessageFormatException
60   {
61     super(sess, momMsg);
62     try {
63       map = momMsg.getMap();
64     }
65     catch (Exception JavaDoc exc) {
66       MessageFormatException jE =
67         new MessageFormatException("Error while getting the body.");
68       jE.setLinkedException(exc);
69       throw jE;
70     }
71     RObody = true;
72   }
73   
74
75   /**
76    * API method.
77    *
78    * @exception JMSException Actually never thrown.
79    */

80   public void clearBody() throws JMSException
81   {
82     super.clearBody();
83     map.clear();
84     RObody = false;
85   }
86
87
88   /**
89    * API method.
90    *
91    * @exception MessageNotWriteableException If the message body is read-only.
92    */

93   public void setBoolean(String JavaDoc name, boolean value) throws JMSException
94   {
95     setObject(name, new Boolean JavaDoc(value));
96   }
97  
98   /**
99    * API method.
100    *
101    * @exception MessageNotWriteableException If the message body is read-only.
102    */

103   public void setByte(String JavaDoc name, byte value) throws JMSException
104   {
105     setObject(name, new Byte JavaDoc(value));
106   }
107  
108   /**
109    * API method.
110    *
111    * @exception MessageNotWriteableException If the message body is read-only.
112    */

113   public void setBytes(String JavaDoc name, byte[] value) throws JMSException
114   {
115     setObject(name, value);
116   }
117  
118   /**
119    * API method.
120    *
121    * @exception MessageNotWriteableException If the message body is read-only.
122    */

123   public void setBytes(String JavaDoc name, byte[] value, int offset, int length)
124               throws JMSException
125   {
126     byte[] buff = new byte[length];
127
128     for (int i = 0; i < length; i++)
129       buff[i] = value[i + offset];
130
131     setObject(name, buff);
132   }
133  
134   /**
135    * API method.
136    *
137    * @exception MessageNotWriteableException If the message body is read-only.
138    */

139   public void setChar(String JavaDoc name, char value) throws JMSException
140   {
141     setObject(name, new Character JavaDoc(value));
142   }
143  
144   /**
145    * API method.
146    *
147    * @exception MessageNotWriteableException If the message body is read-only.
148    */

149 // public void setDouble(String name, double value) throws JMSException
150
// {
151
// setObject(name, new Double(value));
152
// }
153

154   /**
155    * API method.
156    *
157    * @exception MessageNotWriteableException If the message body is read-only.
158    */

159 // public void setFloat(String name, float value) throws JMSException
160
// {
161
// setObject(name, new Float(value));
162
// }
163

164   /**
165    * API method.
166    *
167    * @exception MessageNotWriteableException If the message body is read-only.
168    */

169   public void setInt(String JavaDoc name, int value) throws JMSException
170   {
171     setObject(name, new Integer JavaDoc(value));
172   }
173  
174   /**
175    * API method.
176    *
177    * @exception MessageNotWriteableException If the message body is read-only.
178    */

179   public void setLong(String JavaDoc name, long value) throws JMSException
180   {
181     setObject(name, new Long JavaDoc(value));
182   }
183   
184   /**
185    * API method.
186    *
187    * @exception MessageNotWriteableException If the message body is read-only.
188    */

189   public void setShort(String JavaDoc name, short value) throws JMSException
190   {
191     setObject(name, new Short JavaDoc(value));
192   }
193  
194   /**
195    * API method.
196    *
197    * @exception MessageNotWriteableException If the message body is read-only.
198    */

199   public void setString(String JavaDoc name, String JavaDoc value) throws JMSException
200   {
201     setObject(name, value);
202   }
203
204   /**
205    * API method.
206    *
207    * @exception MessageNotWriteableException If the message body is read-only.
208    * @exception MessageFormatException If the value type is invalid.
209    */

210   public void setObject(String JavaDoc name, Object JavaDoc value) throws JMSException
211   {
212     if (RObody)
213       throw new MessageNotWriteableException("Can't set a value as the message"
214                                              + " body is read-only.");
215     if (name == null || name.equals(""))
216       throw new IllegalArgumentException JavaDoc("Invalid null or empty value name.");
217
218     if (value == null)
219       return;
220
221     if (value instanceof Boolean JavaDoc ||
222         value instanceof Character JavaDoc
223         || value instanceof Long JavaDoc
224         || value instanceof Short JavaDoc
225         || value instanceof Byte JavaDoc
226         || value instanceof Integer JavaDoc
227         || value instanceof String JavaDoc
228         || value instanceof byte[])
229       map.put(name, value);
230     else
231       throw new MessageFormatException("Can't set non Java primitive type as"
232                                        + " a map value.");
233   }
234
235   /**
236    * API method.
237    *
238    * @exception MessageFormatException If the value type is invalid.
239    */

240   public boolean getBoolean(String JavaDoc name) throws JMSException
241   {
242     try {
243       return ConversionHelper.toBoolean(map.get(name));
244     }
245     catch (MessageValueException mE) {
246       throw new MessageFormatException(mE.getMessage());
247     }
248   }
249
250   /**
251    * API method.
252    *
253    * @exception MessageFormatException If the value type is invalid.
254    */

255   public byte getByte(String JavaDoc name) throws JMSException
256   {
257     try {
258       return ConversionHelper.toByte(map.get(name));
259     }
260     catch (MessageValueException mE) {
261       throw new MessageFormatException(mE.getMessage());
262     }
263   }
264
265   /**
266    * API method.
267    *
268    * @exception MessageFormatException If the value type is invalid.
269    */

270   public byte[] getBytes(String JavaDoc name) throws JMSException
271   {
272     try {
273       return ConversionHelper.toBytes(map.get(name));
274     }
275     catch (MessageValueException mE) {
276       throw new MessageFormatException(mE.getMessage());
277     }
278   }
279
280   /**
281    * API method.
282    *
283    * @exception MessageFormatException If the value type is invalid.
284    */

285   public char getChar(String JavaDoc name) throws JMSException
286   {
287     try {
288       return ConversionHelper.toChar(map.get(name));
289     }
290     catch (MessageValueException mE) {
291       throw new MessageFormatException(mE.getMessage());
292     }
293   }
294  
295   /**
296    * API method.
297    *
298    * @exception MessageFormatException If the value type is invalid.
299    */

300 // public double getDouble(String name) throws JMSException
301
// {
302
// try {
303
// return ConversionHelper.toDouble(map.get(name));
304
// }
305
// catch (MessageValueException mE) {
306
// throw new MessageFormatException(mE.getMessage());
307
// }
308
// }
309

310   /**
311    * API method.
312    *
313    * @exception MessageFormatException If the value type is invalid.
314    */

315 // public float getFloat(String name) throws JMSException
316
// {
317
// try {
318
// return ConversionHelper.toFloat(map.get(name));
319
// }
320
// catch (MessageValueException mE) {
321
// throw new MessageFormatException(mE.getMessage());
322
// }
323
// }
324

325   /**
326    * API method.
327    *
328    * @exception MessageFormatException If the value type is invalid.
329    */

330   public int getInt(String JavaDoc name) throws JMSException
331   {
332     try {
333       return ConversionHelper.toInt(map.get(name));
334     }
335     catch (MessageValueException mE) {
336       throw new MessageFormatException(mE.getMessage());
337     }
338   }
339
340   /**
341    * API method.
342    *
343    * @exception MessageFormatException If the value type is invalid.
344    */

345   public long getLong(String JavaDoc name) throws JMSException
346   {
347     try {
348       return ConversionHelper.toLong(map.get(name));
349     }
350     catch (MessageValueException mE) {
351       throw new MessageFormatException(mE.getMessage());
352     }
353   }
354  
355   /**
356    * API method.
357    *
358    * @exception JMSException Actually never thrown.
359    */

360   public Object JavaDoc getObject(String JavaDoc name) throws JMSException
361   {
362     return map.get(name);
363   }
364
365   /**
366    * API method.
367    *
368    * @exception MessageFormatException If the value type is invalid.
369    */

370   public short getShort(String JavaDoc name) throws JMSException
371   {
372     try {
373       return ConversionHelper.toShort(map.get(name));
374     }
375     catch (MessageValueException mE) {
376       throw new MessageFormatException(mE.getMessage());
377     }
378   }
379
380   /**
381    * API method.
382    *
383    * @exception JMSException Actually never thrown.
384    */

385   public String JavaDoc getString(String JavaDoc name) throws JMSException
386   {
387     return ConversionHelper.toString(map.get(name));
388   }
389
390   /**
391    * API method.
392    *
393    * @exception JMSException Actually never thrown.
394    */

395   public boolean itemExists(String JavaDoc name) throws JMSException
396   {
397     return (map.get(name) != null);
398   }
399
400   /**
401    * API method.
402    *
403    * @exception JMSException Actually never thrown.
404    */

405   public Enumeration getMapNames() throws JMSException
406   {
407     return map.keys();
408   }
409  
410
411   /**
412    * Method actually preparing the message for sending by transfering the
413    * local body into the wrapped MOM message.
414    *
415    * @exception Exception If an error occurs while serializing.
416    */

417   protected void prepare() throws Exception JavaDoc
418   {
419     super.prepare();
420
421     momMsg.clearBody();
422     momMsg.setMap(map);
423   }
424 }
425
Popular Tags