001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jms.message;
031:
032: import javax.jms.JMSException;
033: import javax.jms.MapMessage;
034: import javax.jms.MessageFormatException;
035: import java.util.*;
036: import java.io.*;
037: import com.caucho.vfs.*;
038: import com.caucho.hessian.io.*;
039:
040: /**
041: * A stream message.
042: */
043: public class MapMessageImpl extends MessageImpl implements MapMessage {
044: private HashMap<String, Object> _map = new HashMap<String, Object>();
045:
046: public MapMessageImpl() {
047: }
048:
049: MapMessageImpl(MapMessage map) throws JMSException {
050: super (map);
051:
052: Enumeration e = map.getMapNames();
053: while (e.hasMoreElements()) {
054: String name = (String) e.nextElement();
055:
056: _map.put(name, map.getObject(name));
057: }
058: }
059:
060: MapMessageImpl(MapMessageImpl map) {
061: super (map);
062:
063: _map.putAll(map._map);
064: }
065:
066: /**
067: * Returns the type enumeration.
068: */
069: @Override
070: public MessageType getType() {
071: return MessageType.MAP;
072: }
073:
074: /**
075: * Returns true if the object exists.
076: */
077: public boolean itemExists(String name) throws JMSException {
078: return _map.containsKey(name);
079: }
080:
081: /**
082: * Returns an enumeration of the map names.
083: */
084: public Enumeration getMapNames() throws JMSException {
085: return Collections.enumeration(_map.keySet());
086: }
087:
088: /**
089: * Get a boolean from the stream.
090: */
091: public boolean getBoolean(String name) throws JMSException {
092: return ObjectConverter.toBoolean(getObject(name));
093: }
094:
095: /**
096: * Get a byte from the stream.
097: */
098: public byte getByte(String name) throws JMSException {
099: return ObjectConverter.toByte(getObject(name));
100: }
101:
102: /**
103: * Get a short from the stream.
104: */
105: public short getShort(String name) throws JMSException {
106: return ObjectConverter.toShort(getObject(name));
107: }
108:
109: /**
110: * Get an integer from the stream.
111: */
112: public int getInt(String name) throws JMSException {
113: return ObjectConverter.toInt(getObject(name));
114: }
115:
116: /**
117: * Get a long from the stream.
118: */
119: public long getLong(String name) throws JMSException {
120: return ObjectConverter.toLong(getObject(name));
121: }
122:
123: /**
124: * Get a float from the stream.
125: */
126: public float getFloat(String name) throws JMSException {
127: return ObjectConverter.toFloat(getObject(name));
128: }
129:
130: /**
131: * Get a double from the stream.
132: */
133: public double getDouble(String name) throws JMSException {
134: return ObjectConverter.toDouble(getObject(name));
135: }
136:
137: /**
138: * Get a character object from the stream.
139: */
140: public char getChar(String name) throws JMSException {
141: return ObjectConverter.toChar(getObject(name));
142: }
143:
144: /**
145: * Get a string from the stream.
146: */
147: public String getString(String name) throws JMSException {
148: return ObjectConverter.toString(getObject(name));
149: }
150:
151: /**
152: * Get a byte array object from the stream.
153: */
154: public byte[] getBytes(String name) throws JMSException {
155: return ObjectConverter.toBytes(getObject(name));
156: }
157:
158: /**
159: * Get a byte array object from the stream.
160: */
161: public int getBytes(String name, byte[] value) throws JMSException {
162: byte[] bytes = ObjectConverter.toBytes(getObject(name));
163:
164: if (bytes == null)
165: return 0;
166:
167: int sublen = bytes.length;
168: if (value.length < sublen)
169: sublen = value.length;
170:
171: for (int i = 0; i < sublen; i++)
172: value[i] = bytes[i];
173:
174: return sublen;
175: }
176:
177: /**
178: * Gets the next object.
179: */
180: public Object getObject(String name) throws JMSException {
181: return _map.get(name);
182: }
183:
184: /**
185: * Clears the message and puts it into write mode.
186: */
187: public void clearBody() throws JMSException {
188: super .clearBody();
189:
190: _map.clear();
191: }
192:
193: /**
194: * Sets a boolean to the stream.
195: */
196: public void setBoolean(String name, boolean b) throws JMSException {
197: setObject(name, new Boolean(b));
198: }
199:
200: /**
201: * Sets a byte to the stream.
202: */
203: public void setByte(String name, byte b) throws JMSException {
204: setObject(name, new Byte(b));
205: }
206:
207: /**
208: * Sets a short to the stream.
209: */
210: public void setShort(String name, short s) throws JMSException {
211: setObject(name, new Short(s));
212: }
213:
214: /**
215: * Sets an integer to the stream.
216: */
217: public void setInt(String name, int i) throws JMSException {
218: setObject(name, new Integer(i));
219: }
220:
221: /**
222: * Sets a long to the stream.
223: */
224: public void setLong(String name, long l) throws JMSException {
225: setObject(name, new Long(l));
226: }
227:
228: /**
229: * Sets a float to the stream.
230: */
231: public void setFloat(String name, float f) throws JMSException {
232: setObject(name, new Float(f));
233: }
234:
235: /**
236: * Sets a double to the stream.
237: */
238: public void setDouble(String name, double d) throws JMSException {
239: setObject(name, new Double(d));
240: }
241:
242: /**
243: * Sets a string to the stream.
244: */
245: public void setString(String name, String s) throws JMSException {
246: setObject(name, s);
247: }
248:
249: /**
250: * Sets a character to the stream.
251: */
252: public void setChar(String name, char ch) throws JMSException {
253: setObject(name, new Character(ch));
254: }
255:
256: /**
257: * Sets a byte array to the stream.
258: */
259: public void setBytes(String name, byte[] buf) throws JMSException {
260: setBytes(name, buf, 0, buf.length);
261: }
262:
263: /**
264: * Sets a byte array to the stream.
265: */
266: public void setBytes(String name, byte[] buf, int offset, int length)
267: throws JMSException {
268: byte[] newBuf = new byte[length];
269:
270: System.arraycopy(buf, offset, newBuf, 0, length);
271:
272: setObject(name, newBuf);
273: }
274:
275: /**
276: * Sets the next object.
277: */
278: public void setObject(String name, Object obj) throws JMSException {
279: checkBodyWriteable();
280:
281: if (obj == null) {
282: } else if (obj instanceof byte[]) {
283: } else if (!obj.getClass().getName().startsWith("java.lang."))
284: throw new MessageFormatException(L.l(
285: "'{0}' is an invalid value for a map message.", obj
286: .getClass().getName()));
287:
288: if (name == null || "".equals(name))
289: throw new IllegalArgumentException(L
290: .l("MapMessage.setXXX name may not be empty."));
291:
292: _map.put(name, obj);
293: }
294:
295: public MessageImpl copy() {
296: return new MapMessageImpl(this );
297: }
298:
299: protected void copy(MapMessageImpl newMsg) {
300: super .copy(newMsg);
301:
302: newMsg._map = new HashMap<String, Object>(_map);
303: }
304:
305: /**
306: * Serialize the body to an input stream.
307: */
308: @Override
309: public InputStream bodyToInputStream() throws IOException {
310: if (_map == null)
311: return null;
312:
313: TempStream body = new TempStream();
314: body.openWrite();
315:
316: StreamImplOutputStream ws = new StreamImplOutputStream(body);
317:
318: Hessian2Output out = new Hessian2Output(ws);
319:
320: out.writeObject(_map);
321:
322: out.close();
323:
324: ws.close();
325:
326: return body.openRead();
327: }
328:
329: /**
330: * Read the body from an input stream.
331: */
332: @Override
333: public void readBody(InputStream is) throws IOException,
334: JMSException {
335: if (is != null) {
336: Hessian2Input in = new Hessian2Input(is);
337:
338: _map = (HashMap<String, Object>) in.readObject();
339:
340: in.close();
341: }
342:
343: setBodyReadOnly();
344: }
345:
346: public String toString() {
347: return "MapMessageImpl[]";
348: }
349: }
|