KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jms > message > MapMessageImpl


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.jms.message;
8
9 import java.io.Serializable JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import javax.jms.JMSException JavaDoc;
14 import javax.jms.MapMessage JavaDoc;
15 import javax.jms.MessageFormatException JavaDoc;
16 import javax.jms.MessageNotWriteableException JavaDoc;
17
18 /**
19  * <p/>
20  * Description: A <CODE>MapMessage</CODE> object is used to send a set of
21  * name-value pairs. The names are <CODE>String</CODE> objects, and the
22  * values are primitive data types in the Java programming language. The
23  * entries can be accessed sequentially or randomly by name. The order of the
24  * entries is undefined. <CODE>MapMessage</CODE> inherits from the <CODE>
25  * Message</CODE> interface and adds a message body that contains a Map.
26  * </p>
27  *
28  * @author <a HREF="mailto:founder_chen@yahoo.com.cn">Peter.Cheng</a>
29  * @version Revision: 1.1 Date: 2002-12-08 12:50:10
30  */

31
32 public class MapMessageImpl
33         extends JMSMessage
34         implements MapMessage JavaDoc, Serializable JavaDoc {
35
36     private HashMap JavaDoc map = new HashMap JavaDoc();
37
38     /**
39      * Default constructor.
40      */

41     public MapMessageImpl() {
42         super();
43     }
44
45     /**
46      * Returns the <CODE>boolean</CODE> value with the specified name.
47      *
48      * @param name the name of the <CODE>boolean</CODE>
49      * @return the <CODE>boolean</CODE> value with the specified name
50      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
51      * internal error.
52      * @throws javax.jms.MessageFormatException
53      * if this type conversion is invalid.
54      * @see javax.jms.MapMessage#getBoolean(String)
55      */

56     public boolean getBoolean(String JavaDoc name) throws JMSException JavaDoc {
57         return getBoolean(map.get(name));
58     }
59
60     /**
61      * Returns the <CODE>byte</CODE> value with the specified name.
62      *
63      * @param name the name of the <CODE>byte</CODE>
64      * @return the <CODE>byte</CODE> value with the specified name
65      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
66      * internal error.
67      * @throws javax.jms.MessageFormatException
68      * if this type conversion is invalid.
69      * @see javax.jms.MapMessage#getByte(String)
70      */

71     public byte getByte(String JavaDoc name) throws JMSException JavaDoc {
72         return getByte(map.get(name));
73     }
74
75     /**
76      * Returns the <CODE>short</CODE> value with the specified name.
77      *
78      * @param name the name of the <CODE>short</CODE>
79      * @return the <CODE>short</CODE> value with the specified name
80      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
81      * internal error.
82      * @throws javax.jms.MessageFormatException
83      * if this type conversion is invalid.
84      * @see javax.jms.MapMessage#getShort(String)
85      */

86     public short getShort(String JavaDoc name) throws JMSException JavaDoc {
87         return getShort(map.get(name));
88     }
89
90     /**
91      * Returns the Unicode character value with the specified name.
92      *
93      * @param name the name of the Unicode character
94      * @return the Unicode character value with the specified name
95      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
96      * internal error.
97      * @throws javax.jms.MessageFormatException
98      * if this type conversion is invalid.
99      * @see javax.jms.MapMessage#getChar(String)
100      */

101     public char getChar(String JavaDoc name) throws JMSException JavaDoc {
102         return getChar(map.get(name));
103     }
104
105     /**
106      * Returns the <CODE>int</CODE> value with the specified name.
107      *
108      * @param name the name of the <CODE>int</CODE>
109      * @return the <CODE>int</CODE> value with the specified name
110      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
111      * internal error.
112      * @throws javax.jms.MessageFormatException
113      * if this type conversion is invalid.
114      * @see javax.jms.MapMessage#getInt(String)
115      */

116     public int getInt(String JavaDoc name) throws JMSException JavaDoc {
117         return getInt(map.get(name));
118     }
119
120     /**
121      * Returns the <CODE>long</CODE> value with the specified name.
122      *
123      * @param name the name of the <CODE>long</CODE>
124      * @return the <CODE>long</CODE> value with the specified name
125      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
126      * internal error.
127      * @throws javax.jms.MessageFormatException
128      * if this type conversion is invalid.
129      * @see javax.jms.MapMessage#getLong(String)
130      */

131     public long getLong(String JavaDoc name) throws JMSException JavaDoc {
132         return getLong(map.get(name));
133     }
134
135     /**
136      * Returns the <CODE>float</CODE> value with the specified name.
137      *
138      * @param name the name of the <CODE>float</CODE>
139      * @return the <CODE>float</CODE> value with the specified name
140      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
141      * internal error.
142      * @throws javax.jms.MessageFormatException
143      * if this type conversion is invalid.
144      * @see javax.jms.MapMessage#getFloat(String)
145      */

146     public float getFloat(String JavaDoc name) throws JMSException JavaDoc {
147         return getFloat(map.get(name));
148     }
149
150     /**
151      * Returns the <CODE>double</CODE> value with the specified name.
152      *
153      * @param name the name of the <CODE>double</CODE>
154      * @return the <CODE>double</CODE> value with the specified name
155      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
156      * internal error.
157      * @throws javax.jms.MessageFormatException
158      * if this type conversion is invalid.
159      * @see javax.jms.MapMessage#getDouble(String)
160      */

161     public double getDouble(String JavaDoc name) throws JMSException JavaDoc {
162         return getDouble(map.get(name));
163     }
164
165     /**
166      * Returns the <CODE>String</CODE> value with the specified name.
167      *
168      * @param name the name of the <CODE>String</CODE>
169      * @return the <CODE>String</CODE> value with the specified name; if
170      * there is no item by this name, a null value is returned
171      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
172      * internal error.
173      * @throws javax.jms.MessageFormatException
174      * if this type conversion is invalid.
175      * @see javax.jms.MapMessage#getString(String)
176      */

177     public String JavaDoc getString(String JavaDoc name) throws JMSException JavaDoc {
178         return getString(map.get(name));
179     }
180
181     /**
182      * Returns the byte array value with the specified name.
183      *
184      * @param name the name of the byte array
185      * @return a copy of the byte array value with the specified name; if there
186      * is no item by this name, a null value is returned.
187      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
188      * internal error.
189      * @throws javax.jms.MessageFormatException
190      * if this type conversion is invalid.
191      * @see javax.jms.MapMessage#getBytes(String)
192      */

193     public byte[] getBytes(String JavaDoc name) throws JMSException JavaDoc {
194         return getBytes(map.get(name));
195     }
196
197     /**
198      * Returns the value of the object with the specified name.
199      * <p/>
200      * <P>
201      * This method can be used to return, in objectified format, an object in
202      * the Java programming language ("Java object") that had been stored in
203      * the Map with the equivalent <CODE>setObject</CODE> method call, or its
204      * equivalent primitive <CODE>set <I>type</I></CODE> method.
205      * <p/>
206      * <P>
207      * Note that byte values are returned as <CODE>byte[]</CODE>, not <CODE>
208      * Byte[]</CODE>.
209      *
210      * @param name the name of the Java object
211      * @return a copy of the Java object value with the specified name, in
212      * objectified format (for example, if the object was set as an
213      * <CODE>int</CODE>, an <CODE>Integer</CODE> is returned); if
214      * there is no item by this name, a null value is returned
215      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
216      * internal error.
217      * @see javax.jms.MapMessage#getObject(String)
218      */

219     public Object JavaDoc getObject(String JavaDoc name) throws JMSException JavaDoc {
220         return map.get(name);
221     }
222
223     /**
224      * Returns an <CODE>Enumeration</CODE> of all the names in the <CODE>
225      * MapMessage</CODE> object.
226      *
227      * @return an enumeration of all the names in this <CODE>MapMessage
228      * </CODE>
229      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
230      * internal error.
231      * @see javax.jms.MapMessage#getMapNames()
232      */

233     public Enumeration JavaDoc getMapNames() throws JMSException JavaDoc {
234         return Collections.enumeration(map.keySet());
235     }
236
237     /**
238      * Sets a <CODE>boolean</CODE> value with the specified name into the
239      * Map.
240      *
241      * @param name the name of the <CODE>boolean</CODE>
242      * @param value the <CODE>boolean</CODE> value to set in the Map
243      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
244      * some internal error.
245      * @throws javax.jms.MessageNotWriteableException
246      * if the message is in read-only mode.
247      * @see javax.jms.MapMessage#setBoolean(String, boolean)
248      */

249     public void setBoolean(String JavaDoc name, boolean value) throws JMSException JavaDoc {
250         if (isBodyModifiable()) {
251             try {
252                 map.put(name, new Boolean JavaDoc(value));
253             } catch (NullPointerException JavaDoc e) {
254                 throw new JMSException JavaDoc(e.getMessage());
255             }
256         } else {
257             throw new MessageNotWriteableException JavaDoc("MapMesage read_only");
258         }
259     }
260
261     /**
262      * Sets a <CODE>byte</CODE> value with the specified name into the Map.
263      *
264      * @param name the name of the <CODE>byte</CODE>
265      * @param value the <CODE>byte</CODE> value to set in the Map
266      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
267      * some internal error.
268      * @throws javax.jms.MessageNotWriteableException
269      * if the message is in read-only mode.
270      * @see javax.jms.MapMessage#setByte(String, byte)
271      */

272     public void setByte(String JavaDoc name, byte value) throws JMSException JavaDoc {
273         if (isBodyModifiable()) {
274             try {
275                 map.put(name, new Byte JavaDoc(value));
276             } catch (NullPointerException JavaDoc e) {
277                 throw new JMSException JavaDoc(e.getMessage());
278             }
279         } else {
280             throw new MessageNotWriteableException JavaDoc("MapMessage read_only");
281         }
282     }
283
284     /**
285      * Sets a <CODE>short</CODE> value with the specified name into the Map.
286      *
287      * @param name the name of the <CODE>short</CODE>
288      * @param value the <CODE>short</CODE> value to set in the Map
289      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
290      * some internal error.
291      * @throws javax.jms.MessageNotWriteableException
292      * if the message is in read-only mode.
293      * @see javax.jms.MapMessage#setShort(String, short)
294      */

295     public void setShort(String JavaDoc name, short value) throws JMSException JavaDoc {
296         if (isBodyModifiable()) {
297             try {
298                 map.put(name, new Short JavaDoc(value));
299             } catch (NullPointerException JavaDoc e) {
300                 throw new JMSException JavaDoc(e.getMessage());
301             }
302         } else {
303             throw new MessageNotWriteableException JavaDoc("MapMessage read_only");
304         }
305     }
306
307     /**
308      * Sets a Unicode character value with the specified name into the Map.
309      *
310      * @param name the name of the Unicode character
311      * @param value the Unicode character value to set in the Map
312      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
313      * some internal error.
314      * @throws javax.jms.MessageNotWriteableException
315      * if the message is in read-only mode.
316      * @see javax.jms.MapMessage#setChar(String, char)
317      */

318     public void setChar(String JavaDoc name, char value) throws JMSException JavaDoc {
319         if (isBodyModifiable()) {
320             try {
321                 map.put(name, new Character JavaDoc(value));
322             } catch (NullPointerException JavaDoc e) {
323                 throw new JMSException JavaDoc(e.getMessage());
324             }
325         } else {
326             throw new MessageNotWriteableException JavaDoc("MapMessage read_only");
327         }
328     }
329
330     /**
331      * Sets an <CODE>int</CODE> value with the specified name into the Map.
332      *
333      * @param name the name of the <CODE>int</CODE>
334      * @param value the <CODE>int</CODE> value to set in the Map
335      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
336      * some internal error.
337      * @throws javax.jms.MessageNotWriteableException
338      * if the message is in read-only mode.
339      * @see javax.jms.MapMessage#setInt(String, int)
340      */

341     public void setInt(String JavaDoc name, int value) throws JMSException JavaDoc {
342         if (isBodyModifiable()) {
343             try {
344                 map.put(name, new Integer JavaDoc(value));
345             } catch (NullPointerException JavaDoc e) {
346                 throw new JMSException JavaDoc(e.getMessage());
347             }
348         } else {
349             throw new MessageNotWriteableException JavaDoc("MapMessage read_only");
350         }
351     }
352
353     /**
354      * Sets a <CODE>long</CODE> value with the specified name into the Map.
355      *
356      * @param name the name of the <CODE>long</CODE>
357      * @param value the <CODE>long</CODE> value to set in the Map
358      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
359      * some internal error.
360      * @throws javax.jms.MessageNotWriteableException
361      * if the message is in read-only mode.
362      * @see javax.jms.MapMessage#setLong(String, long)
363      */

364     public void setLong(String JavaDoc name, long value) throws JMSException JavaDoc {
365         if (isBodyModifiable()) {
366             try {
367                 map.put(name, new Long JavaDoc(value));
368             } catch (NullPointerException JavaDoc e) {
369                 throw new JMSException JavaDoc(e.getMessage());
370             }
371         } else {
372             throw new MessageNotWriteableException JavaDoc("MapMessage read_only");
373         }
374     }
375
376     /**
377      * Sets a <CODE>float</CODE> value with the specified name into the Map.
378      *
379      * @param name the name of the <CODE>float</CODE>
380      * @param value the <CODE>float</CODE> value to set in the Map
381      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
382      * some internal error.
383      * @throws javax.jms.MessageNotWriteableException
384      * if the message is in read-only mode.
385      * @see javax.jms.MapMessage#setFloat(String, float)
386      */

387     public void setFloat(String JavaDoc name, float value) throws JMSException JavaDoc {
388         if (isBodyModifiable()) {
389             try {
390                 map.put(name, new Float JavaDoc(value));
391             } catch (NullPointerException JavaDoc e) {
392                 throw new JMSException JavaDoc(e.getMessage());
393             }
394         } else {
395             throw new MessageNotWriteableException JavaDoc("MapMessage read_only");
396         }
397     }
398
399     /**
400      * Sets a <CODE>double</CODE> value with the specified name into the Map.
401      *
402      * @param name the name of the <CODE>double</CODE>
403      * @param value the <CODE>double</CODE> value to set in the Map
404      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
405      * some internal error.
406      * @throws javax.jms.MessageNotWriteableException
407      * if the message is in read-only mode.
408      * @see javax.jms.MapMessage#setDouble(String, double)
409      */

410     public void setDouble(String JavaDoc name, double value) throws JMSException JavaDoc {
411         if (isBodyModifiable()) {
412             try {
413                 map.put(name, new Double JavaDoc(value));
414             } catch (NullPointerException JavaDoc e) {
415                 throw new JMSException JavaDoc(e.getMessage());
416             }
417         } else {
418             throw new MessageNotWriteableException JavaDoc("MapMessage read_only");
419         }
420     }
421
422     /**
423      * Sets a <CODE>String</CODE> value with the specified name into the Map.
424      *
425      * @param name the name of the <CODE>String</CODE>
426      * @param value the <CODE>String</CODE> value to set in the Map
427      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
428      * some internal error.
429      * @throws javax.jms.MessageNotWriteableException
430      * if the message is in read-only mode.
431      * @see javax.jms.MapMessage#setString(String, String)
432      */

433     public void setString(String JavaDoc name, String JavaDoc value) throws JMSException JavaDoc {
434         if (isBodyModifiable()) {
435             try {
436                 map.put(name, value);
437             } catch (NullPointerException JavaDoc e) {
438                 throw new JMSException JavaDoc(e.getMessage());
439             }
440         } else {
441             throw new MessageNotWriteableException JavaDoc("MapMessage read_only");
442         }
443     }
444
445     /**
446      * Sets a byte array value with the specified name into the Map.
447      *
448      * @param name the name of the byte array
449      * @param value the byte array value to set in the Map; the array is copied
450      * so that the value for <CODE>name</CODE> will not be altered
451      * by future modifications
452      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
453      * some internal error.
454      * @throws javax.jms.MessageNotWriteableException
455      * if the message is in read-only mode.
456      * @see javax.jms.MapMessage#setBytes(String, byte[])
457      */

458     public void setBytes(String JavaDoc name, byte[] value) throws JMSException JavaDoc {
459         if (isBodyModifiable()) {
460             try {
461                 int len = value.length;
462                 byte[] valueCopy = new byte[len];
463                 System.arraycopy(value, 0, valueCopy, 0, len);
464                 map.put(name, valueCopy);
465             } catch (NullPointerException JavaDoc e) {
466                 throw new JMSException JavaDoc(e.getMessage());
467             }
468         } else {
469             throw new MessageNotWriteableException JavaDoc("MapMmessage read_only");
470         }
471
472     }
473
474     /**
475      * Sets a portion of the byte array value with the specified name into the
476      * Map.
477      *
478      * @param name the name of the byte array
479      * @param value the byte array value to set in the Map
480      * @param offset the initial offset within the byte array
481      * @param length the number of bytes to use
482      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
483      * some internal error.
484      * @throws javax.jms.MessageNotWriteableException
485      * if the message is in read-only mode.
486      * @see javax.jms.MapMessage#setBytes(String, byte[], int, int)
487      */

488     public void setBytes(String JavaDoc name, byte[] value, int offset, int length)
489             throws JMSException JavaDoc {
490         if (isBodyModifiable()) {
491             try {
492                 byte[] newValue = new byte[length];
493                 System.arraycopy(value, offset, newValue, 0, length);
494                 map.put(name, newValue);
495             } catch (NullPointerException JavaDoc e) {
496                 throw new JMSException JavaDoc(e.getMessage());
497             }
498         } else {
499             throw new MessageNotWriteableException JavaDoc("MapMessageimpl read_only");
500         }
501     }
502
503     /**
504      * Sets an object value with the specified name into the Map.
505      * <p/>
506      * <P>
507      * This method works only for the objectified primitive object types (
508      * <code>Integer</code>,<code>Double</code>,<code>Long</code>
509      * &nbsp;...), <code>String</code> objects, and byte arrays.
510      *
511      * @param name the name of the Java object
512      * @param value the Java object value to set in the Map
513      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
514      * some internal error.
515      * @throws javax.jms.MessageFormatException
516      * if the object is invalid.
517      * @throws javax.jms.MessageNotWriteableException
518      * if the message is in read-only mode.
519      * @see javax.jms.MapMessage#setObject(String, Object)
520      */

521     public void setObject(String JavaDoc name, Object JavaDoc value) throws JMSException JavaDoc {
522         if (isBodyModifiable()) {
523             try {
524                 if ((value instanceof Boolean JavaDoc)
525                         || (value instanceof Byte JavaDoc)
526                         || (value instanceof Short JavaDoc)
527                         || (value instanceof Character JavaDoc)
528                         || (value instanceof Integer JavaDoc)
529                         || (value instanceof Long JavaDoc)
530                         || (value instanceof Float JavaDoc)
531                         || (value instanceof Double JavaDoc)
532                         || (value instanceof String JavaDoc)
533                         || (value instanceof byte[])) {
534                     map.put(name, value);
535                 } else {
536                     throw new MessageFormatException JavaDoc("MapMessagei invalid_type");
537                 }
538             } catch (NullPointerException JavaDoc e) {
539                 throw new JMSException JavaDoc(e.getMessage());
540             }
541         } else {
542             throw new MessageNotWriteableException JavaDoc("MapMessage read_only");
543         }
544     }
545
546     /**
547      * Indicates whether an item exists in this <CODE>MapMessage</CODE>
548      * object.
549      *
550      * @param name the name of the item to test
551      * @return true if the item exists
552      * @throws javax.jms.JMSException if the JMS provider fails to determine if the item exists
553      * due to some internal error.
554      * @see javax.jms.MapMessage#itemExists(String)
555      */

556     public boolean itemExists(String JavaDoc name) throws JMSException JavaDoc {
557         return map.containsKey(name);
558     }
559
560     /**
561      * Clear out the message body.
562      *
563      * @throws javax.jms.JMSException
564      */

565     public void clearBody() throws JMSException JavaDoc {
566         super.clearBody();
567         map = new HashMap JavaDoc();
568     }
569
570     /**
571      * Convert value to boolean
572      *
573      * @param value
574      * @return the converted boolean
575      * @throws javax.jms.MessageFormatException
576      * if the conversion is invalid
577      */

578     private boolean getBoolean(Object JavaDoc value) throws MessageFormatException JavaDoc {
579         if (value instanceof Boolean JavaDoc) {
580             return ((Boolean JavaDoc) value).booleanValue();
581         } else if (value instanceof String JavaDoc) {
582             return Boolean.valueOf((String JavaDoc) value).booleanValue();
583         } else if (value == null) {
584             return Boolean.valueOf((String JavaDoc) value).booleanValue();
585         } else {
586             throw new MessageFormatException JavaDoc("Can't convert value of type "
587                     + value.getClass().getName()
588                     + " to Boolean");
589         }
590     }
591
592     /**
593      * Convert value to byte
594      *
595      * @param value
596      * @return the converted byte
597      * @throws javax.jms.MessageFormatException
598      * if the conversion is invalid
599      */

600     private byte getByte(Object JavaDoc value) throws MessageFormatException JavaDoc {
601         if (value instanceof Byte JavaDoc) {
602             return ((Byte JavaDoc) value).byteValue();
603         } else if (value instanceof String JavaDoc) {
604             return Byte.parseByte((String JavaDoc) value);
605         } else if (value == null) {
606             return Byte.valueOf((String JavaDoc) value).byteValue();
607         } else {
608             throw new MessageFormatException JavaDoc("Can't convert value of type "
609                     + value.getClass().getName()
610                     + " to Byte");
611         }
612     }
613
614     /**
615      * Convert value to short
616      *
617      * @param value
618      * @return the converted short
619      * @throws javax.jms.MessageFormatException
620      * if the conversion is invalid
621      */

622     private short getShort(Object JavaDoc value) throws MessageFormatException JavaDoc {
623         if (value instanceof Short JavaDoc) {
624             return ((Short JavaDoc) value).shortValue();
625         } else if (value instanceof Byte JavaDoc) {
626             return ((Byte JavaDoc) value).shortValue();
627         } else if (value instanceof String JavaDoc) {
628             return Short.parseShort((String JavaDoc) value);
629         } else if (value == null) {
630             return Short.valueOf((String JavaDoc) value).shortValue();
631         } else {
632             throw new MessageFormatException JavaDoc("Can't convert value of type "
633                     + value.getClass().getName()
634                     + " to Short");
635         }
636     }
637
638     /**
639      * Convert value to char
640      *
641      * @param value the object to convert from
642      * @return the converted char
643      * @throws javax.jms.MessageFormatException
644      * if the conversion is invalid
645      * @throws NullPointerException if value is null
646      */

647     private char getChar(Object JavaDoc value) throws MessageFormatException JavaDoc {
648         if (value == null) {
649             return ((Character JavaDoc) null).charValue();
650         } else if (value instanceof Character JavaDoc) {
651             return ((Character JavaDoc) value).charValue();
652         } else {
653             throw new MessageFormatException JavaDoc("Can't convert value of type "
654                     + value.getClass().getName()
655                     + " to Char");
656         }
657     }
658
659     /**
660      * Convert value to int
661      *
662      * @param value
663      * @return the converted int
664      * @throws javax.jms.MessageFormatException
665      * if the conversion is invalid
666      * @throws NumberFormatException if value is a String and the conversion is invalid
667      */

668     private int getInt(Object JavaDoc value) throws MessageFormatException JavaDoc {
669         if (value instanceof Integer JavaDoc) {
670             return ((Integer JavaDoc) value).intValue();
671         } else if (value instanceof Byte JavaDoc) {
672             return ((Byte JavaDoc) value).intValue();
673         } else if (value instanceof Short JavaDoc) {
674             return ((Short JavaDoc) value).intValue();
675         } else if (value instanceof String JavaDoc) {
676             return Integer.parseInt((String JavaDoc) value);
677         } else if (value == null) {
678             return Integer.valueOf((String JavaDoc) value).intValue();
679         } else {
680             throw new MessageFormatException JavaDoc("Can't convert value of type "
681                     + value.getClass().getName()
682                     + " to Int");
683         }
684     }
685
686     /**
687      * Convert value to long
688      *
689      * @param value
690      * @return the converted long
691      * @throws javax.jms.MessageFormatException
692      * if the conversion is invalid
693      * @throws NumberFormatException if value is a String and the conversion is invalid
694      */

695     private long getLong(Object JavaDoc value) throws MessageFormatException JavaDoc {
696         if (value instanceof Long JavaDoc) {
697             return ((Long JavaDoc) value).longValue();
698         } else if (value instanceof Byte JavaDoc) {
699             return ((Byte JavaDoc) value).longValue();
700         } else if (value instanceof Short JavaDoc) {
701             return ((Short JavaDoc) value).longValue();
702         } else if (value instanceof Integer JavaDoc) {
703             return ((Integer JavaDoc) value).longValue();
704         } else if (value instanceof String JavaDoc) {
705             return Long.parseLong((String JavaDoc) value);
706         } else if (value == null) {
707             return Long.valueOf((String JavaDoc) value).longValue();
708         } else {
709             throw new MessageFormatException JavaDoc("Can't convert value of type "
710                     + value.getClass().getName()
711                     + " to Long");
712         }
713     }
714
715     /**
716      * Convert value to float
717      *
718      * @param value
719      * @return the converted float
720      * @throws javax.jms.MessageFormatException
721      * if the conversion is invalid
722      * @throws NumberFormatException if value is a String and the conversion is invalid
723      */

724     private float getFloat(Object JavaDoc value) throws MessageFormatException JavaDoc {
725         if (value instanceof Float JavaDoc) {
726             return ((Float JavaDoc) value).floatValue();
727         } else if (value instanceof String JavaDoc) {
728             return Float.parseFloat((String JavaDoc) value);
729         } else if (value == null) {
730             return Float.valueOf((String JavaDoc) value).floatValue();
731         } else {
732             throw new MessageFormatException JavaDoc("Can't convert value of type "
733                     + value.getClass().getName()
734                     + " to Float");
735         }
736     }
737
738     /**
739      * Convert value to double
740      *
741      * @param value the object to convert from
742      * @return the converted double
743      * @throws javax.jms.MessageFormatException
744      * if the conversion is invalid
745      * @throws NumberFormatException if value is a String and the conversion is invalid
746      */

747     private double getDouble(Object JavaDoc value) throws MessageFormatException JavaDoc {
748         if (value instanceof Double JavaDoc) {
749             return ((Double JavaDoc) value).doubleValue();
750         } else if (value instanceof Float JavaDoc) {
751             return ((Float JavaDoc) value).doubleValue();
752         } else if (value instanceof String JavaDoc) {
753             return Double.parseDouble((String JavaDoc) value);
754         } else if (value == null) {
755             return Double.valueOf((String JavaDoc) value).doubleValue();
756         } else {
757             throw new MessageFormatException JavaDoc("Can't convert value of type "
758                     + value.getClass().getName()
759                     + " to Double");
760         }
761     }
762
763     /**
764      * Convert value to String
765      *
766      * @param value the object to convert from
767      * @return the converted String
768      * if the conversion is invalid
769      */

770     private String JavaDoc getString(Object JavaDoc value) {
771         return (value == null) ? null : String.valueOf(value);
772     }
773
774     /**
775      * Convert value to Bytes
776      *
777      * @param value the object to convert from
778      * @return the converted bytes
779      * @throws javax.jms.MessageFormatException
780      * if the conversion invalid
781      */

782     private byte[] getBytes(Object JavaDoc value) throws MessageFormatException JavaDoc {
783         if (value == null) {
784             return (byte[]) value;
785         } else if (value instanceof byte[]) {
786             return (byte[]) value;
787         } else {
788             throw new MessageFormatException JavaDoc("Can't convert value of type "
789                     + value.getClass().getName()
790                     + " to byte[].");
791         }
792     }
793 }
794
Popular Tags