KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > MapMessage


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 2000 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):ScalAgent Distributed Technologies
23  */

24 package org.objectweb.joram.client.jms;
25
26 import org.objectweb.joram.shared.excepts.MessageValueException;
27
28 import java.io.*;
29 import java.util.*;
30
31 import javax.jms.JMSException JavaDoc;
32 import javax.jms.MessageFormatException JavaDoc;
33 import javax.jms.MessageNotWriteableException JavaDoc;
34
35 import org.objectweb.joram.shared.messages.ConversionHelper;
36 import org.objectweb.joram.shared.excepts.MessageValueException;
37
38 /**
39  * Implements the <code>javax.jms.MapMessage</code> interface.
40  */

41 public final class MapMessage extends Message implements javax.jms.MapMessage JavaDoc {
42   /** The wrapped hashmap. */
43   private transient HashMap map;
44
45   /**
46    * Instanciates a bright new <code>MapMessage</code>.
47    */

48   MapMessage() {
49     super();
50     momMsg.type = momMsg.MAP;
51
52     map = new HashMap();
53   }
54
55   /**
56    * Instanciates a <code>MapMessage</code> wrapping a consumed MOM
57    * message containing an hashtable.
58    *
59    * @param sess The consuming session.
60    * @param momMsg The MOM message to wrap.
61    *
62    * @exception MessageFormatException In case of a problem when getting the
63    * MOM message data.
64    */

65   MapMessage(Session session,
66              org.objectweb.joram.shared.messages.Message momMsg) throws MessageFormatException JavaDoc {
67     super(session, momMsg);
68
69     ByteArrayInputStream bais = null;
70     ObjectInputStream ois = null;
71     try {
72       bais = new ByteArrayInputStream(momMsg.body);
73       ois = new ObjectInputStream(bais);
74       map = (HashMap) ois.readObject();
75     } catch (Exception JavaDoc exc) {
76       MessageFormatException JavaDoc jE =
77         new MessageFormatException JavaDoc("Error while getting the body.");
78       jE.setLinkedException(exc);
79       throw jE;
80     } finally {
81       try {
82         ois.close();
83       } catch (IOException exc) {}
84       try {
85         bais.close();
86       } catch (IOException exc) {}
87     }
88   }
89
90   /**
91    * API method.
92    *
93    * @exception JMSException Actually never thrown.
94    */

95   public void clearBody() throws JMSException JavaDoc {
96     super.clearBody();
97     map.clear();
98   }
99
100
101   /**
102    * API method.
103    *
104    * @exception MessageNotWriteableException If the message body is read-only.
105    */

106   public void setBoolean(String JavaDoc name, boolean value) throws JMSException JavaDoc {
107     setObject(name, new Boolean JavaDoc(value));
108   }
109  
110   /**
111    * API method.
112    *
113    * @exception MessageNotWriteableException If the message body is read-only.
114    */

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

124   public void setBytes(String JavaDoc name, byte[] value) throws JMSException JavaDoc {
125     setObject(name, value);
126   }
127  
128   /**
129    * API method.
130    *
131    * @exception MessageNotWriteableException If the message body is read-only.
132    */

133   public void setBytes(String JavaDoc name, byte[] value, int offset, int length) throws JMSException JavaDoc {
134     byte[] buff = new byte[length];
135     System.arraycopy(value, offset, buff, 0, length);
136     setObject(name, buff);
137   }
138  
139   /**
140    * API method.
141    *
142    * @exception MessageNotWriteableException If the message body is read-only.
143    */

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

153   public void setDouble(String JavaDoc name, double value) throws JMSException JavaDoc {
154     setObject(name, new Double JavaDoc(value));
155   }
156  
157   /**
158    * API method.
159    *
160    * @exception MessageNotWriteableException If the message body is read-only.
161    */

162   public void setFloat(String JavaDoc name, float value) throws JMSException JavaDoc {
163     setObject(name, new Float JavaDoc(value));
164   }
165  
166   /**
167    * API method.
168    *
169    * @exception MessageNotWriteableException If the message body is read-only.
170    */

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

180   public void setLong(String JavaDoc name, long value) throws JMSException JavaDoc {
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 JavaDoc {
190     setObject(name, new Short JavaDoc(value));
191   }
192  
193   /**
194    * API method.
195    *
196    * @exception MessageNotWriteableException If the message body is read-only.
197    */

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

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

230   public boolean getBoolean(String JavaDoc name) throws JMSException JavaDoc {
231     try {
232       return ConversionHelper.toBoolean(map.get(name));
233     } catch (MessageValueException mE) {
234       throw new MessageFormatException JavaDoc(mE.getMessage());
235     }
236   }
237
238   /**
239    * API method.
240    *
241    * @exception MessageFormatException If the value type is invalid.
242    */

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

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

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

282   public double getDouble(String JavaDoc name) throws JMSException JavaDoc {
283     try {
284       return ConversionHelper.toDouble(map.get(name));
285     } catch (MessageValueException mE) {
286       throw new MessageFormatException JavaDoc(mE.getMessage());
287     }
288   }
289
290   /**
291    * API method.
292    *
293    * @exception MessageFormatException If the value type is invalid.
294    */

295   public float getFloat(String JavaDoc name) throws JMSException JavaDoc {
296     try {
297       return ConversionHelper.toFloat(map.get(name));
298     } catch (MessageValueException mE) {
299       throw new MessageFormatException JavaDoc(mE.getMessage());
300     }
301   }
302
303   /**
304    * API method.
305    *
306    * @exception MessageFormatException If the value type is invalid.
307    */

308   public int getInt(String JavaDoc name) throws JMSException JavaDoc {
309     try {
310       return ConversionHelper.toInt(map.get(name));
311     } catch (MessageValueException mE) {
312       throw new MessageFormatException JavaDoc(mE.getMessage());
313     }
314   }
315
316   /**
317    * API method.
318    *
319    * @exception MessageFormatException If the value type is invalid.
320    */

321   public long getLong(String JavaDoc name) throws JMSException JavaDoc {
322     try {
323       return ConversionHelper.toLong(map.get(name));
324     } catch (MessageValueException mE) {
325       throw new MessageFormatException JavaDoc(mE.getMessage());
326     }
327   }
328  
329   /**
330    * API method.
331    *
332    * @exception JMSException Actually never thrown.
333    */

334   public Object JavaDoc getObject(String JavaDoc name) throws JMSException JavaDoc {
335     return map.get(name);
336   }
337
338   /**
339    * API method.
340    *
341    * @exception MessageFormatException If the value type is invalid.
342    */

343   public short getShort(String JavaDoc name) throws JMSException JavaDoc {
344     try {
345       return ConversionHelper.toShort(map.get(name));
346     } catch (MessageValueException mE) {
347       throw new MessageFormatException JavaDoc(mE.getMessage());
348     }
349   }
350
351   /**
352    * API method.
353    *
354    * @exception JMSException Actually never thrown.
355    */

356   public String JavaDoc getString(String JavaDoc name) throws JMSException JavaDoc {
357     Object JavaDoc value = map.get(name);
358     if (value instanceof byte[])
359       throw new MessageFormatException JavaDoc("Type " + value.getClass().getName()
360                                        + " can't be converted to String.");
361     return ConversionHelper.toString(map.get(name));
362   }
363
364   /**
365    * API method.
366    *
367    * @exception JMSException Actually never thrown.
368    */

369   public boolean itemExists(String JavaDoc name) throws JMSException JavaDoc {
370     return map.containsKey(name);
371   }
372
373   /**
374    * API method.
375    *
376    * @exception JMSException Actually never thrown.
377    */

378   public Enumeration getMapNames() throws JMSException JavaDoc {
379     Vector vec = new Vector();
380     if (map.keySet() != null) {
381       for (Iterator it = map.keySet().iterator(); it.hasNext(); )
382         vec.add(it.next());
383     }
384     return vec.elements();
385   }
386
387   /**
388    * Method actually preparing the message for sending by transfering the
389    * local body into the wrapped MOM message.
390    *
391    * @exception MessageFormatException If an error occurs while serializing.
392    */

393   protected void prepare() throws JMSException JavaDoc {
394     super.prepare();
395
396     try {
397       ByteArrayOutputStream baos = new ByteArrayOutputStream();
398       ObjectOutputStream oos = new ObjectOutputStream(baos);
399       oos.writeObject(map);
400       oos.flush();
401       momMsg.body = baos.toByteArray();
402       oos.close();
403       baos.close();
404     } catch (IOException exc) {
405       MessageFormatException JavaDoc jExc =
406         new MessageFormatException JavaDoc("The message body could not be serialized.");
407       jExc.setLinkedException(exc);
408       throw jExc;
409     }
410   }
411 }
412
Popular Tags