KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > SpyMapMessage


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Hashtable JavaDoc;
32
33 import javax.jms.JMSException JavaDoc;
34 import javax.jms.MapMessage JavaDoc;
35 import javax.jms.MessageFormatException JavaDoc;
36 import javax.jms.MessageNotWriteableException JavaDoc;
37
38 import org.jboss.util.Primitives;
39
40 /**
41  * This class implements javax.jms.MapMessage
42  *
43  * @author Norbert Lataille (Norbert.Lataille@m4x.org)
44  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
45  * @version $Revision: 37459 $
46  */

47 public class SpyMapMessage extends SpyMessage implements MapMessage JavaDoc, Externalizable JavaDoc
48 {
49    // Constants -----------------------------------------------------
50

51    /** The serialVersionUID */
52    private final static long serialVersionUID = -4917633165373197269L;
53
54    // Attributes ----------------------------------------------------
55

56    /** The content */
57    HashMap JavaDoc content;
58
59    // Static --------------------------------------------------------
60

61    // Constructors --------------------------------------------------
62

63    /**
64      * Create a new SpyMapMessage
65      */

66    public SpyMapMessage()
67    {
68       content = new HashMap JavaDoc();
69    }
70
71    // Public --------------------------------------------------------
72

73    // MapMessage implementation -------------------------------------
74

75    public void setBoolean(String JavaDoc name, boolean value) throws JMSException JavaDoc
76    {
77       checkName(name);
78       if (header.msgReadOnly)
79          throw new MessageNotWriteableException JavaDoc("Message is ReadOnly !");
80       synchronized (content)
81       {
82          content.put(name, Primitives.valueOf(value));
83       }
84    }
85
86    public void setByte(String JavaDoc name, byte value) throws JMSException JavaDoc
87    {
88       checkName(name);
89       if (header.msgReadOnly)
90          throw new MessageNotWriteableException JavaDoc("Message is ReadOnly !");
91       synchronized (content)
92       {
93          content.put(name, new Byte JavaDoc(value));
94       }
95    }
96
97    public void setShort(String JavaDoc name, short value) throws JMSException JavaDoc
98    {
99       checkName(name);
100       if (header.msgReadOnly)
101          throw new MessageNotWriteableException JavaDoc("Message is ReadOnly !");
102       synchronized (content)
103       {
104          content.put(name, new Short JavaDoc(value));
105       }
106    }
107
108    public void setChar(String JavaDoc name, char value) throws JMSException JavaDoc
109    {
110       checkName(name);
111       if (header.msgReadOnly)
112          throw new MessageNotWriteableException JavaDoc("Message is ReadOnly !");
113       synchronized (content)
114       {
115          content.put(name, new Character JavaDoc(value));
116       }
117    }
118
119    public void setInt(String JavaDoc name, int value) throws JMSException JavaDoc
120    {
121       checkName(name);
122       if (header.msgReadOnly)
123          throw new MessageNotWriteableException JavaDoc("Message is ReadOnly !");
124       synchronized (content)
125       {
126          content.put(name, new Integer JavaDoc(value));
127       }
128    }
129
130    public void setLong(String JavaDoc name, long value) throws JMSException JavaDoc
131    {
132       checkName(name);
133       if (header.msgReadOnly)
134          throw new MessageNotWriteableException JavaDoc("Message is ReadOnly !");
135       synchronized (content)
136       {
137          content.put(name, new Long JavaDoc(value));
138       }
139    }
140
141    public void setFloat(String JavaDoc name, float value) throws JMSException JavaDoc
142    {
143       checkName(name);
144       if (header.msgReadOnly)
145          throw new MessageNotWriteableException JavaDoc("Message is ReadOnly !");
146       synchronized (content)
147       {
148          content.put(name, new Float JavaDoc(value));
149       }
150    }
151
152    public void setDouble(String JavaDoc name, double value) throws JMSException JavaDoc
153    {
154       checkName(name);
155       if (header.msgReadOnly)
156          throw new MessageNotWriteableException JavaDoc("Message is ReadOnly !");
157       synchronized (content)
158       {
159          content.put(name, new Double JavaDoc(value));
160       }
161    }
162
163    public void setString(String JavaDoc name, String JavaDoc value) throws JMSException JavaDoc
164    {
165       checkName(name);
166       if (header.msgReadOnly)
167          throw new MessageNotWriteableException JavaDoc("Message is ReadOnly !");
168       synchronized (content)
169       {
170          content.put(name, value);
171       }
172    }
173
174    public void setBytes(String JavaDoc name, byte[] value) throws JMSException JavaDoc
175    {
176       checkName(name);
177       if (header.msgReadOnly)
178          throw new MessageNotWriteableException JavaDoc("Message is ReadOnly !");
179       synchronized (content)
180       {
181          content.put(name, value.clone());
182       }
183    }
184
185    public void setBytes(String JavaDoc name, byte[] value, int offset, int length) throws JMSException JavaDoc
186    {
187       checkName(name);
188       if (header.msgReadOnly)
189          throw new MessageNotWriteableException JavaDoc("Message is ReadOnly !");
190
191       if (offset + length > value.length)
192          throw new JMSException JavaDoc("Array is too small");
193       byte[] temp = new byte[length];
194       for (int i = 0; i < length; i++)
195          temp[i] = value[i + offset];
196
197       synchronized (content)
198       {
199          content.put(name, temp);
200       }
201    }
202
203    public void setObject(String JavaDoc name, Object JavaDoc value) throws JMSException JavaDoc
204    {
205       checkName(name);
206       if (header.msgReadOnly)
207          throw new MessageNotWriteableException JavaDoc("Message is ReadOnly !");
208
209       synchronized (content)
210       {
211          if (value instanceof Boolean JavaDoc)
212             content.put(name, value);
213          else if (value instanceof Byte JavaDoc)
214             content.put(name, value);
215          else if (value instanceof Short JavaDoc)
216             content.put(name, value);
217          else if (value instanceof Character JavaDoc)
218             content.put(name, value);
219          else if (value instanceof Integer JavaDoc)
220             content.put(name, value);
221          else if (value instanceof Long JavaDoc)
222             content.put(name, value);
223          else if (value instanceof Float JavaDoc)
224             content.put(name, value);
225          else if (value instanceof Double JavaDoc)
226             content.put(name, value);
227          else if (value instanceof String JavaDoc)
228             content.put(name, value);
229          else if (value instanceof byte[])
230             content.put(name, ((byte[]) value).clone());
231          else
232             throw new MessageFormatException JavaDoc("Invalid object type.");
233       }
234    }
235
236    public boolean getBoolean(String JavaDoc name) throws JMSException JavaDoc
237    {
238       Object JavaDoc value;
239       synchronized (content)
240       {
241          value = content.get(name);
242       }
243       if (value == null)
244          return Boolean.valueOf(null).booleanValue();
245
246       if (value instanceof Boolean JavaDoc)
247          return ((Boolean JavaDoc) value).booleanValue();
248       else if (value instanceof String JavaDoc)
249          return Boolean.valueOf((String JavaDoc) value).booleanValue();
250       else
251          throw new MessageFormatException JavaDoc("Invalid conversion");
252    }
253
254    public byte getByte(String JavaDoc name) throws JMSException JavaDoc
255    {
256       Object JavaDoc value;
257       synchronized (content)
258       {
259          value = content.get(name);
260       }
261       if (value == null)
262          return Byte.parseByte(null);
263
264       if (value instanceof Byte JavaDoc)
265          return ((Byte JavaDoc) value).byteValue();
266       else if (value instanceof String JavaDoc)
267          return Byte.parseByte((String JavaDoc) value);
268       else
269          throw new MessageFormatException JavaDoc("Invalid conversion");
270    }
271
272    public short getShort(String JavaDoc name) throws JMSException JavaDoc
273    {
274       Object JavaDoc value;
275       synchronized (content)
276       {
277          value = content.get(name);
278       }
279       if (value == null)
280          return Short.parseShort(null);
281
282       if (value instanceof Byte JavaDoc)
283          return ((Byte JavaDoc) value).shortValue();
284       else if (value instanceof Short JavaDoc)
285          return ((Short JavaDoc) value).shortValue();
286       else if (value instanceof String JavaDoc)
287          return Short.parseShort((String JavaDoc) value);
288       else
289          throw new MessageFormatException JavaDoc("Invalid conversion");
290    }
291
292    public char getChar(String JavaDoc name) throws JMSException JavaDoc
293    {
294       Object JavaDoc value;
295       synchronized (content)
296       {
297          value = content.get(name);
298       }
299       if (value == null)
300          throw new NullPointerException JavaDoc("Invalid conversion");
301
302       if (value instanceof Character JavaDoc)
303          return ((Character JavaDoc) value).charValue();
304       else
305          throw new MessageFormatException JavaDoc("Invalid conversion");
306    }
307
308    public int getInt(String JavaDoc name) throws JMSException JavaDoc
309    {
310       Object JavaDoc value;
311       synchronized (content)
312       {
313          value = content.get(name);
314       }
315       if (value == null)
316          return Integer.parseInt(null);
317
318       if (value instanceof Byte JavaDoc)
319          return ((Byte JavaDoc) value).intValue();
320       else if (value instanceof Short JavaDoc)
321          return ((Short JavaDoc) value).intValue();
322       else if (value instanceof Integer JavaDoc)
323          return ((Integer JavaDoc) value).intValue();
324       else if (value instanceof String JavaDoc)
325          return Integer.parseInt((String JavaDoc) value);
326       else
327          throw new MessageFormatException JavaDoc("Invalid conversion");
328    }
329
330    public long getLong(String JavaDoc name) throws JMSException JavaDoc
331    {
332       Object JavaDoc value;
333       synchronized (content)
334       {
335          value = content.get(name);
336       }
337       if (value == null)
338          return Long.parseLong(null);
339
340       if (value instanceof Byte JavaDoc)
341          return ((Byte JavaDoc) value).longValue();
342       else if (value instanceof Short JavaDoc)
343          return ((Short JavaDoc) value).longValue();
344       else if (value instanceof Integer JavaDoc)
345          return ((Integer JavaDoc) value).longValue();
346       else if (value instanceof Long JavaDoc)
347          return ((Long JavaDoc) value).longValue();
348       else if (value instanceof String JavaDoc)
349          return Long.parseLong((String JavaDoc) value);
350       else
351          throw new MessageFormatException JavaDoc("Invalid conversion");
352    }
353
354    public float getFloat(String JavaDoc name) throws JMSException JavaDoc
355    {
356       Object JavaDoc value;
357       synchronized (content)
358       {
359          value = content.get(name);
360       }
361       if (value == null)
362          return Float.parseFloat(null);
363
364       if (value instanceof Float JavaDoc)
365          return ((Float JavaDoc) value).floatValue();
366       else if (value instanceof String JavaDoc)
367          return Float.parseFloat((String JavaDoc) value);
368       else
369          throw new MessageFormatException JavaDoc("Invalid conversion");
370    }
371
372    public double getDouble(String JavaDoc name) throws JMSException JavaDoc
373    {
374       Object JavaDoc value;
375       synchronized (content)
376       {
377          value = content.get(name);
378       }
379       if (value == null)
380          return Double.parseDouble(null);
381
382       if (value instanceof Float JavaDoc)
383          return ((Float JavaDoc) value).doubleValue();
384       else if (value instanceof Double JavaDoc)
385          return ((Double JavaDoc) value).doubleValue();
386       else if (value instanceof String JavaDoc)
387          return Double.parseDouble((String JavaDoc) value);
388       else
389          throw new MessageFormatException JavaDoc("Invalid conversion");
390    }
391
392    public String JavaDoc getString(String JavaDoc name) throws JMSException JavaDoc
393    {
394       Object JavaDoc value;
395       synchronized (content)
396       {
397          value = content.get(name);
398       }
399       if (value == null)
400          return null;
401
402       if (value instanceof Boolean JavaDoc)
403          return ((Boolean JavaDoc) value).toString();
404       else if (value instanceof Byte JavaDoc)
405          return ((Byte JavaDoc) value).toString();
406       else if (value instanceof Short JavaDoc)
407          return ((Short JavaDoc) value).toString();
408       else if (value instanceof Character JavaDoc)
409          return ((Character JavaDoc) value).toString();
410       else if (value instanceof Integer JavaDoc)
411          return ((Integer JavaDoc) value).toString();
412       else if (value instanceof Long JavaDoc)
413          return ((Long JavaDoc) value).toString();
414       else if (value instanceof Float JavaDoc)
415          return ((Float JavaDoc) value).toString();
416       else if (value instanceof Double JavaDoc)
417          return ((Double JavaDoc) value).toString();
418       else if (value instanceof String JavaDoc)
419          return (String JavaDoc) value;
420       else
421          throw new MessageFormatException JavaDoc("Invalid conversion");
422    }
423
424    public byte[] getBytes(String JavaDoc name) throws JMSException JavaDoc
425    {
426       Object JavaDoc value;
427       synchronized (content)
428       {
429          value = content.get(name);
430       }
431       if (value == null)
432          return null;
433       if (value instanceof byte[])
434          return (byte[]) value;
435       else
436          throw new MessageFormatException JavaDoc("Invalid conversion");
437    }
438
439    public Object JavaDoc getObject(String JavaDoc name) throws JMSException JavaDoc
440    {
441       synchronized (content)
442       {
443          return content.get(name);
444       }
445    }
446
447    public Enumeration JavaDoc getMapNames() throws JMSException JavaDoc
448    {
449       synchronized (content)
450       {
451          return Collections.enumeration(new HashMap JavaDoc(content).keySet());
452       }
453    }
454
455    public boolean itemExists(String JavaDoc name) throws JMSException JavaDoc
456    {
457       synchronized (content)
458       {
459          return content.containsKey(name);
460       }
461    }
462
463    // SpyMessage overrides ------------------------------------------
464

465    public void clearBody() throws JMSException JavaDoc
466    {
467       content = new HashMap JavaDoc();
468       super.clearBody();
469    }
470
471    public SpyMessage myClone() throws JMSException JavaDoc
472    {
473       SpyMapMessage result = MessagePool.getMapMessage();
474       result.copyProps(this);
475       synchronized (content)
476       {
477          result.content = (HashMap JavaDoc) this.content.clone();
478       }
479       return result;
480    }
481
482    // Externalizable implementation ---------------------------------
483

484    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
485    {
486       super.writeExternal(out);
487       // TODO: For backwards compatbility this should really
488
// write out a hashtable. But if the content contained nulls
489
// the old client wouldn't be able to deserialize it anyway.
490
out.writeObject(content);
491    }
492
493    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
494    {
495       super.readExternal(in);
496       Object JavaDoc object = in.readObject();
497       // This is a hack for backwards compatibility
498
if (object instanceof Hashtable JavaDoc)
499       {
500          Hashtable JavaDoc ht = (Hashtable JavaDoc) object;
501          content = new HashMap JavaDoc(ht.size());
502          content.putAll(ht);
503       }
504       else
505          content = (HashMap JavaDoc) object;
506    }
507    
508    // Package protected ---------------------------------------------
509

510    // Protected -----------------------------------------------------
511

512    // Private -------------------------------------------------------
513

514    /**
515      * Check the name
516      *
517      * @param name the name
518      */

519    private void checkName(String JavaDoc name)
520    {
521       if (name == null)
522          throw new IllegalArgumentException JavaDoc("Name must not be null.");
523
524       if (name.equals(""))
525          throw new IllegalArgumentException JavaDoc("Name must not be an empty String.");
526    }
527
528    // Inner classes -------------------------------------------------
529

530 }
531 /*
532  * vim:ts=3:sw=3:et
533  */

534
Popular Tags