KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > command > ActiveMQMapMessage


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.command;
19
20 import java.io.DataInputStream JavaDoc;
21 import java.io.DataOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.zip.DeflaterOutputStream JavaDoc;
30 import java.util.zip.InflaterInputStream JavaDoc;
31
32 import javax.jms.JMSException JavaDoc;
33 import javax.jms.MapMessage JavaDoc;
34 import javax.jms.MessageFormatException JavaDoc;
35 import javax.jms.MessageNotWriteableException JavaDoc;
36
37 import org.apache.activemq.ActiveMQConnection;
38 import org.apache.activemq.util.ByteArrayInputStream;
39 import org.apache.activemq.util.ByteArrayOutputStream;
40 import org.apache.activemq.util.ByteSequence;
41 import org.apache.activemq.util.JMSExceptionSupport;
42 import org.apache.activemq.util.MarshallingSupport;
43 import org.apache.activemq.wireformat.WireFormat;
44
45 /**
46  * A <CODE>MapMessage</CODE> object is used to send a set of name-value pairs. The names are <CODE>String</CODE>
47  * objects, and the values are primitive data types in the Java programming language. The names must have a value that
48  * is not null, and not an empty string. The entries can be accessed sequentially or randomly by name. The order of the
49  * entries is undefined. <CODE>MapMessage</CODE> inherits from the <CODE>Message</CODE> interface and adds a message
50  * body that contains a Map. <P> The primitive types can be read or written explicitly using methods for each type. They
51  * may also be read or written generically as objects. For instance, a call to <CODE>MapMessage.setInt("foo", 6)</CODE>
52  * is equivalent to <CODE> MapMessage.setObject("foo", new Integer(6))</CODE>. Both forms are provided, because the
53  * explicit form is convenient for static programming, and the object form is needed when types are not known at compile
54  * time. <P> When a client receives a <CODE>MapMessage</CODE>, it is in read-only mode. If a client attempts to write to
55  * the message at this point, a <CODE>MessageNotWriteableException</CODE> is thrown. If <CODE>clearBody</CODE> is
56  * called, the message can now be both read from and written to. <P> <CODE>MapMessage</CODE> objects support the
57  * following conversion table. The marked cases must be supported. The unmarked cases must throw a
58  * <CODE>JMSException</CODE>. The <CODE>String</CODE> -to-primitive conversions may throw a runtime exception if the
59  * primitive's <CODE>valueOf()</CODE> method does not accept it as a valid <CODE> String</CODE> representation of the
60  * primitive. <P> A value written as the row type can be read as the column type.
61  * <p/>
62  * <PRE>| | boolean byte short char int long float double String byte[] |----------------------------------------------------------------------
63  * |boolean | X X |byte | X X X X X |short | X X X X |char | X X |int | X X X |long | X X |float | X X X |double | X X
64  * |String | X X X X X X X X |byte[] | X |----------------------------------------------------------------------
65  * <p/>
66  * </PRE>
67  * <p/>
68  * <P> Attempting to read a null value as a primitive type must be treated as calling the primitive's corresponding
69  * <code>valueOf(String)</code> conversion method with a null value. Since <code>char</code> does not support a
70  * <code>String</code> conversion, attempting to read a null value as a <code>char</code> must throw a
71  * <code>NullPointerException</code>.
72  *
73  * @openwire:marshaller code="25"
74  * @see javax.jms.Session#createMapMessage()
75  * @see javax.jms.BytesMessage
76  * @see javax.jms.Message
77  * @see javax.jms.ObjectMessage
78  * @see javax.jms.StreamMessage
79  * @see javax.jms.TextMessage
80  */

81 public class ActiveMQMapMessage extends ActiveMQMessage implements MapMessage JavaDoc {
82
83     public static final byte DATA_STRUCTURE_TYPE = CommandTypes.ACTIVEMQ_MAP_MESSAGE;
84
85     transient protected Map JavaDoc map = new HashMap JavaDoc();
86
87     public Message copy() {
88         ActiveMQMapMessage copy = new ActiveMQMapMessage();
89         copy(copy);
90         return copy;
91     }
92
93     private void copy(ActiveMQMapMessage copy) {
94         storeContent();
95         super.copy(copy);
96     }
97
98     // We only need to marshal the content if we are hitting the wire.
99
public void beforeMarshall(WireFormat wireFormat) throws IOException JavaDoc {
100         super.beforeMarshall(wireFormat);
101         storeContent();
102     }
103     
104     private void storeContent() {
105         try {
106             if( getContent()==null && !map.isEmpty()) {
107                 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
108                 OutputStream JavaDoc os = bytesOut;
109                 ActiveMQConnection connection = getConnection();
110                 if( connection!= null && connection.isUseCompression() ) {
111                     compressed = true;
112                     os = new DeflaterOutputStream JavaDoc(os);
113                 }
114                 DataOutputStream JavaDoc dataOut = new DataOutputStream JavaDoc(os);
115                 MarshallingSupport.marshalPrimitiveMap(map, dataOut);
116                 dataOut.close();
117                 setContent(bytesOut.toByteSequence());
118             }
119         } catch (IOException JavaDoc e) {
120             throw new RuntimeException JavaDoc(e);
121         }
122     }
123     
124     /**
125      * Builds the message body from data
126      * @throws JMSException
127      *
128      * @throws IOException
129      */

130     private void loadContent() throws JMSException JavaDoc {
131         try {
132             if( getContent()!=null && map.isEmpty() ) {
133                 ByteSequence content = getContent();
134                 InputStream JavaDoc is = new ByteArrayInputStream(content);
135                 if( isCompressed() ) {
136                     is = new InflaterInputStream JavaDoc(is);
137                 }
138                 DataInputStream JavaDoc dataIn = new DataInputStream JavaDoc(is);
139                 map = MarshallingSupport.unmarshalPrimitiveMap(dataIn);
140                 dataIn.close();
141             }
142         } catch (IOException JavaDoc e) {
143             throw JMSExceptionSupport.create(e);
144         }
145     }
146
147     public byte getDataStructureType() {
148         return DATA_STRUCTURE_TYPE;
149     }
150     
151     public String JavaDoc getJMSXMimeType() {
152         return "jms/map-message";
153     }
154
155
156     /**
157      * Clears out the message body. Clearing a message's body does not clear its header values or property entries. <P>
158      * If this message body was read-only, calling this method leaves the message body in the same state as an empty
159      * body in a newly created message.
160      */

161     public void clearBody() throws JMSException JavaDoc {
162         super.clearBody();
163         map.clear();
164     }
165
166     /**
167      * Returns the <CODE>boolean</CODE> value with the specified name.
168      *
169      * @param name the name of the <CODE>boolean</CODE>
170      * @return the <CODE>boolean</CODE> value with the specified name
171      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
172      * @throws MessageFormatException if this type conversion is invalid.
173      */

174     public boolean getBoolean(String JavaDoc name) throws JMSException JavaDoc {
175         initializeReading();
176         Object JavaDoc value = map.get(name);
177         if (value == null) {
178             return false;
179         }
180         if (value instanceof Boolean JavaDoc) {
181             return ((Boolean JavaDoc) value).booleanValue();
182         }
183         if (value instanceof String JavaDoc) {
184             return Boolean.valueOf(value.toString()).booleanValue();
185         } else {
186             throw new MessageFormatException JavaDoc(" cannot read a boolean from " + value.getClass().getName());
187         }
188     }
189
190     /**
191      * Returns the <CODE>byte</CODE> value with the specified name.
192      *
193      * @param name the name of the <CODE>byte</CODE>
194      * @return the <CODE>byte</CODE> value with the specified name
195      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
196      * @throws MessageFormatException if this type conversion is invalid.
197      */

198     public byte getByte(String JavaDoc name) throws JMSException JavaDoc {
199         initializeReading();
200         Object JavaDoc value = map.get(name);
201         if (value == null) {
202             return 0;
203         }
204         if (value instanceof Byte JavaDoc) {
205             return ((Byte JavaDoc) value).byteValue();
206         }
207         if (value instanceof String JavaDoc) {
208             return Byte.valueOf(value.toString()).byteValue();
209         } else {
210             throw new MessageFormatException JavaDoc(" cannot read a byte from " + value.getClass().getName());
211         }
212     }
213
214     /**
215      * Returns the <CODE>short</CODE> value with the specified name.
216      *
217      * @param name the name of the <CODE>short</CODE>
218      * @return the <CODE>short</CODE> value with the specified name
219      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
220      * @throws MessageFormatException if this type conversion is invalid.
221      */

222     public short getShort(String JavaDoc name) throws JMSException JavaDoc {
223         initializeReading();
224         Object JavaDoc value = map.get(name);
225         if (value == null) {
226             return 0;
227         }
228         if (value instanceof Short JavaDoc) {
229             return ((Short JavaDoc) value).shortValue();
230         }
231         if (value instanceof Byte JavaDoc) {
232             return ((Byte JavaDoc) value).shortValue();
233         }
234         if (value instanceof String JavaDoc) {
235             return Short.valueOf(value.toString()).shortValue();
236         } else {
237             throw new MessageFormatException JavaDoc(" cannot read a short from " + value.getClass().getName());
238         }
239     }
240
241     /**
242      * Returns the Unicode character value with the specified name.
243      *
244      * @param name the name of the Unicode character
245      * @return the Unicode character value with the specified name
246      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
247      * @throws MessageFormatException if this type conversion is invalid.
248      */

249     public char getChar(String JavaDoc name) throws JMSException JavaDoc {
250         initializeReading();
251         Object JavaDoc value = map.get(name);
252         if (value == null) {
253             throw new NullPointerException JavaDoc();
254         }
255         if (value instanceof Character JavaDoc) {
256             return ((Character JavaDoc) value).charValue();
257         } else {
258             throw new MessageFormatException JavaDoc(" cannot read a short from " + value.getClass().getName());
259         }
260     }
261
262     /**
263      * Returns the <CODE>int</CODE> value with the specified name.
264      *
265      * @param name the name of the <CODE>int</CODE>
266      * @return the <CODE>int</CODE> value with the specified name
267      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
268      * @throws MessageFormatException if this type conversion is invalid.
269      */

270     public int getInt(String JavaDoc name) throws JMSException JavaDoc {
271         initializeReading();
272         Object JavaDoc value = map.get(name);
273         if (value == null) {
274             return 0;
275         }
276         if (value instanceof Integer JavaDoc) {
277             return ((Integer JavaDoc) value).intValue();
278         }
279         if (value instanceof Short JavaDoc) {
280             return ((Short JavaDoc) value).intValue();
281         }
282         if (value instanceof Byte JavaDoc) {
283             return ((Byte JavaDoc) value).intValue();
284         }
285         if (value instanceof String JavaDoc) {
286             return Integer.valueOf(value.toString()).intValue();
287         } else {
288             throw new MessageFormatException JavaDoc(" cannot read an int from " + value.getClass().getName());
289         }
290     }
291
292     /**
293      * Returns the <CODE>long</CODE> value with the specified name.
294      *
295      * @param name the name of the <CODE>long</CODE>
296      * @return the <CODE>long</CODE> value with the specified name
297      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
298      * @throws MessageFormatException if this type conversion is invalid.
299      */

300     public long getLong(String JavaDoc name) throws JMSException JavaDoc {
301         initializeReading();
302         Object JavaDoc value = map.get(name);
303         if (value == null) {
304             return 0;
305         }
306         if (value instanceof Long JavaDoc) {
307             return ((Long JavaDoc) value).longValue();
308         }
309         if (value instanceof Integer JavaDoc) {
310             return ((Integer JavaDoc) value).longValue();
311         }
312         if (value instanceof Short JavaDoc) {
313             return ((Short JavaDoc) value).longValue();
314         }
315         if (value instanceof Byte JavaDoc) {
316             return ((Byte JavaDoc) value).longValue();
317         }
318         if (value instanceof String JavaDoc) {
319             return Long.valueOf(value.toString()).longValue();
320         } else {
321             throw new MessageFormatException JavaDoc(" cannot read a long from " + value.getClass().getName());
322         }
323     }
324
325     /**
326      * Returns the <CODE>float</CODE> value with the specified name.
327      *
328      * @param name the name of the <CODE>float</CODE>
329      * @return the <CODE>float</CODE> value with the specified name
330      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
331      * @throws MessageFormatException if this type conversion is invalid.
332      */

333     public float getFloat(String JavaDoc name) throws JMSException JavaDoc {
334         initializeReading();
335         Object JavaDoc value = map.get(name);
336         if (value == null) {
337             return 0;
338         }
339         if (value instanceof Float JavaDoc) {
340             return ((Float JavaDoc) value).floatValue();
341         }
342         if (value instanceof String JavaDoc) {
343             return Float.valueOf(value.toString()).floatValue();
344         } else {
345             throw new MessageFormatException JavaDoc(" cannot read a float from " + value.getClass().getName());
346         }
347     }
348
349     /**
350      * Returns the <CODE>double</CODE> value with the specified name.
351      *
352      * @param name the name of the <CODE>double</CODE>
353      * @return the <CODE>double</CODE> value with the specified name
354      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
355      * @throws MessageFormatException if this type conversion is invalid.
356      */

357     public double getDouble(String JavaDoc name) throws JMSException JavaDoc {
358         initializeReading();
359         Object JavaDoc value = map.get(name);
360         if (value == null) {
361             return 0;
362         }
363         if (value instanceof Double JavaDoc) {
364             return ((Double JavaDoc) value).doubleValue();
365         }
366         if (value instanceof Float JavaDoc) {
367             return ((Float JavaDoc) value).floatValue();
368         }
369         if (value instanceof String JavaDoc) {
370             return Float.valueOf(value.toString()).floatValue();
371         } else {
372             throw new MessageFormatException JavaDoc(" cannot read a double from " + value.getClass().getName());
373         }
374     }
375
376     /**
377      * Returns the <CODE>String</CODE> value with the specified name.
378      *
379      * @param name the name of the <CODE>String</CODE>
380      * @return the <CODE>String</CODE> value with the specified name; if there is no item by this name, a null value is
381      * returned
382      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
383      * @throws MessageFormatException if this type conversion is invalid.
384      */

385     public String JavaDoc getString(String JavaDoc name) throws JMSException JavaDoc {
386         initializeReading();
387         Object JavaDoc value = map.get(name);
388         if (value == null) {
389             return null;
390         }
391         if (value instanceof byte[]) {
392             throw new MessageFormatException JavaDoc("Use getBytes to read a byte array");
393         } else {
394             return value.toString();
395         }
396     }
397
398     /**
399      * Returns the byte array value with the specified name.
400      *
401      * @param name the name of the byte array
402      * @return a copy of the byte array value with the specified name; if there is no item by this name, a null value is
403      * returned.
404      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
405      * @throws MessageFormatException if this type conversion is invalid.
406      */

407     public byte[] getBytes(String JavaDoc name) throws JMSException JavaDoc {
408         initializeReading();
409         Object JavaDoc value = map.get(name);
410         if ( value instanceof byte[] ) {
411             return (byte[]) value;
412         } else {
413             throw new MessageFormatException JavaDoc(" cannot read a byte[] from " + value.getClass().getName());
414         }
415     }
416
417     /**
418      * Returns the value of the object with the specified name. <P> This method can be used to return, in objectified
419      * format, an object in the Java programming language ("Java object") that had been stored in the Map with the
420      * equivalent <CODE>setObject</CODE> method call, or its equivalent primitive <CODE>set <I>type </I></CODE> method.
421      * <P> Note that byte values are returned as <CODE>byte[]</CODE>, not <CODE>Byte[]</CODE>.
422      *
423      * @param name the name of the Java object
424      * @return a copy of the Java object value with the specified name, in objectified format (for example, if the
425      * object was set as an <CODE>int</CODE>, an <CODE>Integer</CODE> is returned); if there is no item by this
426      * name, a null value is returned
427      * @throws JMSException if the JMS provider fails to read the message due to some internal error.
428      */

429     public Object JavaDoc getObject(String JavaDoc name) throws JMSException JavaDoc {
430         initializeReading();
431         return map.get(name);
432     }
433
434     /**
435      * Returns an <CODE>Enumeration</CODE> of all the names in the <CODE>MapMessage</CODE> object.
436      *
437      * @return an enumeration of all the names in this <CODE>MapMessage</CODE>
438      * @throws JMSException
439      */

440     public Enumeration JavaDoc getMapNames() throws JMSException JavaDoc {
441         initializeReading();
442         return Collections.enumeration(map.keySet());
443     }
444
445     protected void put(String JavaDoc name, Object JavaDoc value) throws JMSException JavaDoc {
446         if (name == null) {
447             throw new IllegalArgumentException JavaDoc("The name of the property cannot be null.");
448         }
449         if (name.length() == 0) {
450             throw new IllegalArgumentException JavaDoc("The name of the property cannot be an emprty string.");
451         }
452         map.put(name, value);
453     }
454
455     /**
456      * Sets a <CODE>boolean</CODE> value with the specified name into the Map.
457      *
458      * @param name the name of the <CODE>boolean</CODE>
459      * @param value the <CODE>boolean</CODE> value to set in the Map
460      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
461      * @throws IllegalArgumentException if the name is null or if the name is an empty string.
462      * @throws MessageNotWriteableException if the message is in read-only mode.
463      */

464     public void setBoolean(String JavaDoc name, boolean value) throws JMSException JavaDoc {
465         initializeWriting();
466         put(name, (value) ? Boolean.TRUE : Boolean.FALSE);
467     }
468
469     /**
470      * Sets a <CODE>byte</CODE> value with the specified name into the Map.
471      *
472      * @param name the name of the <CODE>byte</CODE>
473      * @param value the <CODE>byte</CODE> value to set in the Map
474      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
475      * @throws IllegalArgumentException if the name is null or if the name is an empty string.
476      * @throws MessageNotWriteableException if the message is in read-only mode.
477      */

478     public void setByte(String JavaDoc name, byte value) throws JMSException JavaDoc {
479         initializeWriting();
480         put(name, new Byte JavaDoc(value));
481     }
482
483     /**
484      * Sets a <CODE>short</CODE> value with the specified name into the Map.
485      *
486      * @param name the name of the <CODE>short</CODE>
487      * @param value the <CODE>short</CODE> value to set in the Map
488      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
489      * @throws IllegalArgumentException if the name is null or if the name is an empty string.
490      * @throws MessageNotWriteableException if the message is in read-only mode.
491      */

492     public void setShort(String JavaDoc name, short value) throws JMSException JavaDoc {
493         initializeWriting();
494         put(name, new Short JavaDoc(value));
495     }
496
497     /**
498      * Sets a Unicode character value with the specified name into the Map.
499      *
500      * @param name the name of the Unicode character
501      * @param value the Unicode character value to set in the Map
502      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
503      * @throws IllegalArgumentException if the name is null or if the name is an empty string.
504      * @throws MessageNotWriteableException if the message is in read-only mode.
505      */

506     public void setChar(String JavaDoc name, char value) throws JMSException JavaDoc {
507         initializeWriting();
508         put(name, new Character JavaDoc(value));
509     }
510
511     /**
512      * Sets an <CODE>int</CODE> value with the specified name into the Map.
513      *
514      * @param name the name of the <CODE>int</CODE>
515      * @param value the <CODE>int</CODE> value to set in the Map
516      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
517      * @throws IllegalArgumentException if the name is null or if the name is an empty string.
518      * @throws MessageNotWriteableException if the message is in read-only mode.
519      */

520     public void setInt(String JavaDoc name, int value) throws JMSException JavaDoc {
521         initializeWriting();
522         put(name, new Integer JavaDoc(value));
523     }
524
525     /**
526      * Sets a <CODE>long</CODE> value with the specified name into the Map.
527      *
528      * @param name the name of the <CODE>long</CODE>
529      * @param value the <CODE>long</CODE> value to set in the Map
530      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
531      * @throws IllegalArgumentException if the name is null or if the name is an empty string.
532      * @throws MessageNotWriteableException if the message is in read-only mode.
533      */

534     public void setLong(String JavaDoc name, long value) throws JMSException JavaDoc {
535         initializeWriting();
536         put(name, new Long JavaDoc(value));
537     }
538
539     /**
540      * Sets a <CODE>float</CODE> value with the specified name into the Map.
541      *
542      * @param name the name of the <CODE>float</CODE>
543      * @param value the <CODE>float</CODE> value to set in the Map
544      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
545      * @throws IllegalArgumentException if the name is null or if the name is an empty string.
546      * @throws MessageNotWriteableException if the message is in read-only mode.
547      */

548     public void setFloat(String JavaDoc name, float value) throws JMSException JavaDoc {
549         initializeWriting();
550         put(name, new Float JavaDoc(value));
551     }
552
553     /**
554      * Sets a <CODE>double</CODE> value with the specified name into the Map.
555      *
556      * @param name the name of the <CODE>double</CODE>
557      * @param value the <CODE>double</CODE> value to set in the Map
558      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
559      * @throws IllegalArgumentException if the name is null or if the name is an empty string.
560      * @throws MessageNotWriteableException if the message is in read-only mode.
561      */

562     public void setDouble(String JavaDoc name, double value) throws JMSException JavaDoc {
563         initializeWriting();
564         put(name, new Double JavaDoc(value));
565     }
566
567     /**
568      * Sets a <CODE>String</CODE> value with the specified name into the Map.
569      *
570      * @param name the name of the <CODE>String</CODE>
571      * @param value the <CODE>String</CODE> value to set in the Map
572      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
573      * @throws IllegalArgumentException if the name is null or if the name is an empty string.
574      * @throws MessageNotWriteableException if the message is in read-only mode.
575      */

576     public void setString(String JavaDoc name, String JavaDoc value) throws JMSException JavaDoc {
577         initializeWriting();
578         put(name, value);
579     }
580
581     /**
582      * Sets a byte array value with the specified name into the Map.
583      *
584      * @param name the name of the byte array
585      * @param value the byte array value to set in the Map; the array is copied so that the value for <CODE>name </CODE>
586      * will not be altered by future modifications
587      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
588      * @throws NullPointerException if the name is null, or if the name is an empty string.
589      * @throws MessageNotWriteableException if the message is in read-only mode.
590      */

591     public void setBytes(String JavaDoc name, byte[] value) throws JMSException JavaDoc {
592         initializeWriting();
593         if (value != null) {
594             put(name, value);
595         } else {
596             map.remove(name);
597         }
598     }
599
600     /**
601      * Sets a portion of the byte array value with the specified name into the Map.
602      *
603      * @param name the name of the byte array
604      * @param value the byte array value to set in the Map
605      * @param offset the initial offset within the byte array
606      * @param length the number of bytes to use
607      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
608      * @throws IllegalArgumentException if the name is null or if the name is an empty string.
609      * @throws MessageNotWriteableException if the message is in read-only mode.
610      */

611     public void setBytes(String JavaDoc name, byte[] value, int offset, int length) throws JMSException JavaDoc {
612         initializeWriting();
613         byte[] data = new byte[length];
614         System.arraycopy(value, offset, data, 0, length);
615         put(name, data);
616     }
617
618     /**
619      * Sets an object value with the specified name into the Map. <P> This method works only for the objectified
620      * primitive object types (<code>Integer</code>,<code>Double</code>, <code>Long</code> &nbsp;...),
621      * <code>String</code> objects, and byte arrays.
622      *
623      * @param name the name of the Java object
624      * @param value the Java object value to set in the Map
625      * @throws JMSException if the JMS provider fails to write the message due to some internal error.
626      * @throws IllegalArgumentException if the name is null or if the name is an empty string.
627      * @throws MessageFormatException if the object is invalid.
628      * @throws MessageNotWriteableException if the message is in read-only mode.
629      */

630     public void setObject(String JavaDoc name, Object JavaDoc value) throws JMSException JavaDoc {
631         initializeWriting();
632         if (value != null) {
633             // byte[] not allowed on properties
634
if (!(value instanceof byte[])) {
635                 checkValidObject(value);
636             }
637             put(name, value);
638         } else {
639             put(name, null);
640         }
641     }
642
643     /**
644      * Indicates whether an item exists in this <CODE>MapMessage</CODE> object.
645      *
646      * @param name the name of the item to test
647      * @return true if the item exists
648      * @throws JMSException if the JMS provider fails to determine if the item exists due to some internal error.
649      */

650     public boolean itemExists(String JavaDoc name) throws JMSException JavaDoc {
651         initializeReading();
652         return map.containsKey(name);
653     }
654     
655     private void initializeReading() throws JMSException JavaDoc {
656         loadContent();
657     }
658     
659     private void initializeWriting() throws MessageNotWriteableException JavaDoc {
660         checkReadOnlyBody();
661         setContent(null);
662     }
663
664     public String JavaDoc toString() {
665         return super.toString() + " ActiveMQMapMessage{ " +
666                 "theTable = " + map +
667                 " }";
668     }
669     
670     public Map JavaDoc getContentMap() throws JMSException JavaDoc {
671         initializeReading();
672         return map;
673     }
674 }
675
Popular Tags