KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > message > MapMessageImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.message;
30
31 import javax.jms.JMSException JavaDoc;
32 import javax.jms.MapMessage JavaDoc;
33 import javax.jms.MessageFormatException JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.HashMap JavaDoc;
37
38 /**
39  * A stream message.
40  */

41 public class MapMessageImpl extends MessageImpl implements MapMessage JavaDoc {
42   private HashMap JavaDoc<String JavaDoc,Object JavaDoc> _map = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
43
44   /**
45    * Returns true if the object exists.
46    */

47   public boolean itemExists(String JavaDoc name)
48     throws JMSException JavaDoc
49   {
50     return getObject(name) != null;
51   }
52
53   /**
54    * Returns an enumeration of the map names.
55    */

56   public Enumeration JavaDoc getMapNames()
57     throws JMSException JavaDoc
58   {
59     return Collections.enumeration(_map.keySet());
60   }
61
62   /**
63    * Get a boolean from the stream.
64    */

65   public boolean getBoolean(String JavaDoc name)
66     throws JMSException JavaDoc
67   {
68     return ObjectConverter.toBoolean(getObject(name));
69   }
70
71   /**
72    * Get a byte from the stream.
73    */

74   public byte getByte(String JavaDoc name)
75     throws JMSException JavaDoc
76   {
77     return ObjectConverter.toByte(getObject(name));
78   }
79
80   /**
81    * Get a short from the stream.
82    */

83   public short getShort(String JavaDoc name)
84     throws JMSException JavaDoc
85   {
86     return ObjectConverter.toShort(getObject(name));
87   }
88
89   /**
90    * Get an integer from the stream.
91    */

92   public int getInt(String JavaDoc name)
93     throws JMSException JavaDoc
94   {
95     return ObjectConverter.toInt(getObject(name));
96   }
97
98   /**
99    * Get a long from the stream.
100    */

101   public long getLong(String JavaDoc name)
102     throws JMSException JavaDoc
103   {
104     return ObjectConverter.toLong(getObject(name));
105   }
106
107   /**
108    * Get a float from the stream.
109    */

110   public float getFloat(String JavaDoc name)
111     throws JMSException JavaDoc
112   {
113     return ObjectConverter.toFloat(getObject(name));
114   }
115
116   /**
117    * Get a double from the stream.
118    */

119   public double getDouble(String JavaDoc name)
120     throws JMSException JavaDoc
121   {
122     return ObjectConverter.toDouble(getObject(name));
123   }
124
125   /**
126    * Get a character object from the stream.
127    */

128   public char getChar(String JavaDoc name)
129     throws JMSException JavaDoc
130   {
131     return ObjectConverter.toChar(getObject(name));
132   }
133
134   /**
135    * Get a string from the stream.
136    */

137   public String JavaDoc getString(String JavaDoc name)
138     throws JMSException JavaDoc
139   {
140     return ObjectConverter.toString(getObject(name));
141   }
142
143   /**
144    * Get a byte array object from the stream.
145    */

146   public byte []getBytes(String JavaDoc name)
147     throws JMSException JavaDoc
148   {
149     return ObjectConverter.toBytes(getObject(name));
150   }
151
152   /**
153    * Get a byte array object from the stream.
154    */

155   public int getBytes(String JavaDoc name, byte []value)
156     throws JMSException JavaDoc
157   {
158     byte []bytes = ObjectConverter.toBytes(getObject(name));
159
160     if (bytes == null)
161       return 0;
162
163     int sublen = bytes.length;
164     if (value.length < sublen)
165       sublen = value.length;
166
167     for (int i = 0; i < sublen; i++)
168       value[i] = bytes[i];
169
170     return sublen;
171   }
172
173   /**
174    * Gets the next object.
175    */

176   public Object JavaDoc getObject(String JavaDoc name)
177     throws JMSException JavaDoc
178   {
179     return _map.get(name);
180   }
181
182   /**
183    * Clears the message and puts it into write mode.
184    */

185   public void clearBody()
186     throws JMSException JavaDoc
187   {
188     super.clearBody();
189     
190     _map.clear();
191   }
192
193   /**
194    * Sets a boolean to the stream.
195    */

196   public void setBoolean(String JavaDoc name, boolean b)
197     throws JMSException JavaDoc
198   {
199     setObject(name, new Boolean JavaDoc(b));
200   }
201
202   /**
203    * Sets a byte to the stream.
204    */

205   public void setByte(String JavaDoc name, byte b)
206     throws JMSException JavaDoc
207   {
208     setObject(name, new Byte JavaDoc(b));
209   }
210
211   /**
212    * Sets a short to the stream.
213    */

214   public void setShort(String JavaDoc name, short s)
215     throws JMSException JavaDoc
216   {
217     setObject(name, new Short JavaDoc(s));
218   }
219
220   /**
221    * Sets an integer to the stream.
222    */

223   public void setInt(String JavaDoc name, int i)
224     throws JMSException JavaDoc
225   {
226     setObject(name, new Integer JavaDoc(i));
227   }
228
229   /**
230    * Sets a long to the stream.
231    */

232   public void setLong(String JavaDoc name, long l)
233     throws JMSException JavaDoc
234   {
235     setObject(name, new Long JavaDoc(l));
236   }
237
238   /**
239    * Sets a float to the stream.
240    */

241   public void setFloat(String JavaDoc name, float f)
242     throws JMSException JavaDoc
243   {
244     setObject(name, new Float JavaDoc(f));
245   }
246
247   /**
248    * Sets a double to the stream.
249    */

250   public void setDouble(String JavaDoc name, double d)
251     throws JMSException JavaDoc
252   {
253     setObject(name, new Double JavaDoc(d));
254   }
255
256   /**
257    * Sets a string to the stream.
258    */

259   public void setString(String JavaDoc name, String JavaDoc s)
260     throws JMSException JavaDoc
261   {
262     setObject(name, s);
263   }
264
265   /**
266    * Sets a character to the stream.
267    */

268   public void setChar(String JavaDoc name, char ch)
269     throws JMSException JavaDoc
270   {
271     setObject(name, new Character JavaDoc(ch));
272   }
273
274   /**
275    * Sets a byte array to the stream.
276    */

277   public void setBytes(String JavaDoc name, byte []buf)
278     throws JMSException JavaDoc
279   {
280     setBytes(name, buf, 0, buf.length);
281   }
282
283   /**
284    * Sets a byte array to the stream.
285    */

286   public void setBytes(String JavaDoc name, byte []buf, int offset, int length)
287     throws JMSException JavaDoc
288   {
289     byte []newBuf = new byte[length];
290
291     System.arraycopy(buf, offset, newBuf, 0, length);
292     
293     setObject(name, newBuf);
294   }
295
296   /**
297    * Sets the next object.
298    */

299   public void setObject(String JavaDoc name, Object JavaDoc obj)
300     throws JMSException JavaDoc
301   {
302     checkBodyWriteable();
303     
304     if (obj == null) {
305     }
306     else if (obj instanceof byte[]) {
307     }
308     else if (! obj.getClass().getName().startsWith("java.lang."))
309       throw new MessageFormatException JavaDoc(L.l("'{0}' is an invalid value for a map message.",
310                  obj.getClass().getName()));
311     _map.put(name, obj);
312   }
313
314   public MessageImpl copy()
315   {
316     MapMessageImpl msg = new MapMessageImpl();
317
318     copy(msg);
319
320     return msg;
321   }
322
323   protected void copy(MapMessageImpl newMsg)
324   {
325     super.copy(newMsg);
326
327     newMsg._map = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>(_map);
328   }
329 }
330
331
Popular Tags