001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mq;
023:
024: import java.io.Externalizable;
025: import java.io.IOException;
026: import java.io.ObjectInput;
027: import java.io.ObjectOutput;
028: import java.util.Collections;
029: import java.util.Enumeration;
030: import java.util.HashMap;
031: import java.util.Hashtable;
032:
033: import javax.jms.JMSException;
034: import javax.jms.MapMessage;
035: import javax.jms.MessageFormatException;
036: import javax.jms.MessageNotWriteableException;
037:
038: import org.jboss.util.Primitives;
039:
040: /**
041: * This class implements javax.jms.MapMessage
042: *
043: * @author Norbert Lataille (Norbert.Lataille@m4x.org)
044: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
045: * @version $Revision: 57198 $
046: */
047: public class SpyMapMessage extends SpyMessage implements MapMessage,
048: Externalizable {
049: // Constants -----------------------------------------------------
050:
051: /** The serialVersionUID */
052: private final static long serialVersionUID = -4917633165373197269L;
053:
054: // Attributes ----------------------------------------------------
055:
056: /** The content */
057: HashMap content;
058:
059: // Static --------------------------------------------------------
060:
061: // Constructors --------------------------------------------------
062:
063: /**
064: * Create a new SpyMapMessage
065: */
066: public SpyMapMessage() {
067: content = new HashMap();
068: }
069:
070: // Public --------------------------------------------------------
071:
072: // MapMessage implementation -------------------------------------
073:
074: public void setBoolean(String name, boolean value)
075: throws JMSException {
076: checkName(name);
077: if (header.msgReadOnly)
078: throw new MessageNotWriteableException(
079: "Message is ReadOnly !");
080: synchronized (content) {
081: content.put(name, Primitives.valueOf(value));
082: }
083: }
084:
085: public void setByte(String name, byte value) throws JMSException {
086: checkName(name);
087: if (header.msgReadOnly)
088: throw new MessageNotWriteableException(
089: "Message is ReadOnly !");
090: synchronized (content) {
091: content.put(name, new Byte(value));
092: }
093: }
094:
095: public void setShort(String name, short value) throws JMSException {
096: checkName(name);
097: if (header.msgReadOnly)
098: throw new MessageNotWriteableException(
099: "Message is ReadOnly !");
100: synchronized (content) {
101: content.put(name, new Short(value));
102: }
103: }
104:
105: public void setChar(String name, char value) throws JMSException {
106: checkName(name);
107: if (header.msgReadOnly)
108: throw new MessageNotWriteableException(
109: "Message is ReadOnly !");
110: synchronized (content) {
111: content.put(name, new Character(value));
112: }
113: }
114:
115: public void setInt(String name, int value) throws JMSException {
116: checkName(name);
117: if (header.msgReadOnly)
118: throw new MessageNotWriteableException(
119: "Message is ReadOnly !");
120: synchronized (content) {
121: content.put(name, new Integer(value));
122: }
123: }
124:
125: public void setLong(String name, long value) throws JMSException {
126: checkName(name);
127: if (header.msgReadOnly)
128: throw new MessageNotWriteableException(
129: "Message is ReadOnly !");
130: synchronized (content) {
131: content.put(name, new Long(value));
132: }
133: }
134:
135: public void setFloat(String name, float value) throws JMSException {
136: checkName(name);
137: if (header.msgReadOnly)
138: throw new MessageNotWriteableException(
139: "Message is ReadOnly !");
140: synchronized (content) {
141: content.put(name, new Float(value));
142: }
143: }
144:
145: public void setDouble(String name, double value)
146: throws JMSException {
147: checkName(name);
148: if (header.msgReadOnly)
149: throw new MessageNotWriteableException(
150: "Message is ReadOnly !");
151: synchronized (content) {
152: content.put(name, new Double(value));
153: }
154: }
155:
156: public void setString(String name, String value)
157: throws JMSException {
158: checkName(name);
159: if (header.msgReadOnly)
160: throw new MessageNotWriteableException(
161: "Message is ReadOnly !");
162: synchronized (content) {
163: content.put(name, value);
164: }
165: }
166:
167: public void setBytes(String name, byte[] value) throws JMSException {
168: checkName(name);
169: if (header.msgReadOnly)
170: throw new MessageNotWriteableException(
171: "Message is ReadOnly !");
172: synchronized (content) {
173: content.put(name, value.clone());
174: }
175: }
176:
177: public void setBytes(String name, byte[] value, int offset,
178: int length) throws JMSException {
179: checkName(name);
180: if (header.msgReadOnly)
181: throw new MessageNotWriteableException(
182: "Message is ReadOnly !");
183:
184: if (offset + length > value.length)
185: throw new JMSException("Array is too small");
186: byte[] temp = new byte[length];
187: for (int i = 0; i < length; i++)
188: temp[i] = value[i + offset];
189:
190: synchronized (content) {
191: content.put(name, temp);
192: }
193: }
194:
195: public void setObject(String name, Object value)
196: throws JMSException {
197: checkName(name);
198: if (header.msgReadOnly)
199: throw new MessageNotWriteableException(
200: "Message is ReadOnly !");
201:
202: synchronized (content) {
203: if (value instanceof Boolean)
204: content.put(name, value);
205: else if (value instanceof Byte)
206: content.put(name, value);
207: else if (value instanceof Short)
208: content.put(name, value);
209: else if (value instanceof Character)
210: content.put(name, value);
211: else if (value instanceof Integer)
212: content.put(name, value);
213: else if (value instanceof Long)
214: content.put(name, value);
215: else if (value instanceof Float)
216: content.put(name, value);
217: else if (value instanceof Double)
218: content.put(name, value);
219: else if (value instanceof String)
220: content.put(name, value);
221: else if (value instanceof byte[])
222: content.put(name, ((byte[]) value).clone());
223: else
224: throw new MessageFormatException("Invalid object type.");
225: }
226: }
227:
228: public boolean getBoolean(String name) throws JMSException {
229: Object value;
230: synchronized (content) {
231: value = content.get(name);
232: }
233: if (value == null)
234: return Boolean.valueOf(null).booleanValue();
235:
236: if (value instanceof Boolean)
237: return ((Boolean) value).booleanValue();
238: else if (value instanceof String)
239: return Boolean.valueOf((String) value).booleanValue();
240: else
241: throw new MessageFormatException("Invalid conversion");
242: }
243:
244: public byte getByte(String name) throws JMSException {
245: Object value;
246: synchronized (content) {
247: value = content.get(name);
248: }
249: if (value == null)
250: return Byte.parseByte(null);
251:
252: if (value instanceof Byte)
253: return ((Byte) value).byteValue();
254: else if (value instanceof String)
255: return Byte.parseByte((String) value);
256: else
257: throw new MessageFormatException("Invalid conversion");
258: }
259:
260: public short getShort(String name) throws JMSException {
261: Object value;
262: synchronized (content) {
263: value = content.get(name);
264: }
265: if (value == null)
266: return Short.parseShort(null);
267:
268: if (value instanceof Byte)
269: return ((Byte) value).shortValue();
270: else if (value instanceof Short)
271: return ((Short) value).shortValue();
272: else if (value instanceof String)
273: return Short.parseShort((String) value);
274: else
275: throw new MessageFormatException("Invalid conversion");
276: }
277:
278: public char getChar(String name) throws JMSException {
279: Object value;
280: synchronized (content) {
281: value = content.get(name);
282: }
283: if (value == null)
284: throw new NullPointerException("Invalid conversion");
285:
286: if (value instanceof Character)
287: return ((Character) value).charValue();
288: else
289: throw new MessageFormatException("Invalid conversion");
290: }
291:
292: public int getInt(String name) throws JMSException {
293: Object value;
294: synchronized (content) {
295: value = content.get(name);
296: }
297: if (value == null)
298: return Integer.parseInt(null);
299:
300: if (value instanceof Byte)
301: return ((Byte) value).intValue();
302: else if (value instanceof Short)
303: return ((Short) value).intValue();
304: else if (value instanceof Integer)
305: return ((Integer) value).intValue();
306: else if (value instanceof String)
307: return Integer.parseInt((String) value);
308: else
309: throw new MessageFormatException("Invalid conversion");
310: }
311:
312: public long getLong(String name) throws JMSException {
313: Object value;
314: synchronized (content) {
315: value = content.get(name);
316: }
317: if (value == null)
318: return Long.parseLong(null);
319:
320: if (value instanceof Byte)
321: return ((Byte) value).longValue();
322: else if (value instanceof Short)
323: return ((Short) value).longValue();
324: else if (value instanceof Integer)
325: return ((Integer) value).longValue();
326: else if (value instanceof Long)
327: return ((Long) value).longValue();
328: else if (value instanceof String)
329: return Long.parseLong((String) value);
330: else
331: throw new MessageFormatException("Invalid conversion");
332: }
333:
334: public float getFloat(String name) throws JMSException {
335: Object value;
336: synchronized (content) {
337: value = content.get(name);
338: }
339: if (value == null)
340: return Float.parseFloat(null);
341:
342: if (value instanceof Float)
343: return ((Float) value).floatValue();
344: else if (value instanceof String)
345: return Float.parseFloat((String) value);
346: else
347: throw new MessageFormatException("Invalid conversion");
348: }
349:
350: public double getDouble(String name) throws JMSException {
351: Object value;
352: synchronized (content) {
353: value = content.get(name);
354: }
355: if (value == null)
356: return Double.parseDouble(null);
357:
358: if (value instanceof Float)
359: return ((Float) value).doubleValue();
360: else if (value instanceof Double)
361: return ((Double) value).doubleValue();
362: else if (value instanceof String)
363: return Double.parseDouble((String) value);
364: else
365: throw new MessageFormatException("Invalid conversion");
366: }
367:
368: public String getString(String name) throws JMSException {
369: Object value;
370: synchronized (content) {
371: value = content.get(name);
372: }
373: if (value == null)
374: return null;
375:
376: if (value instanceof Boolean)
377: return ((Boolean) value).toString();
378: else if (value instanceof Byte)
379: return ((Byte) value).toString();
380: else if (value instanceof Short)
381: return ((Short) value).toString();
382: else if (value instanceof Character)
383: return ((Character) value).toString();
384: else if (value instanceof Integer)
385: return ((Integer) value).toString();
386: else if (value instanceof Long)
387: return ((Long) value).toString();
388: else if (value instanceof Float)
389: return ((Float) value).toString();
390: else if (value instanceof Double)
391: return ((Double) value).toString();
392: else if (value instanceof String)
393: return (String) value;
394: else
395: throw new MessageFormatException("Invalid conversion");
396: }
397:
398: public byte[] getBytes(String name) throws JMSException {
399: Object value;
400: synchronized (content) {
401: value = content.get(name);
402: }
403: if (value == null)
404: return null;
405: if (value instanceof byte[])
406: return (byte[]) value;
407: else
408: throw new MessageFormatException("Invalid conversion");
409: }
410:
411: public Object getObject(String name) throws JMSException {
412: synchronized (content) {
413: return content.get(name);
414: }
415: }
416:
417: public Enumeration getMapNames() throws JMSException {
418: synchronized (content) {
419: return Collections.enumeration(new HashMap(content)
420: .keySet());
421: }
422: }
423:
424: public boolean itemExists(String name) throws JMSException {
425: synchronized (content) {
426: return content.containsKey(name);
427: }
428: }
429:
430: // SpyMessage overrides ------------------------------------------
431:
432: public void clearBody() throws JMSException {
433: content = new HashMap();
434: super .clearBody();
435: }
436:
437: public SpyMessage myClone() throws JMSException {
438: SpyMapMessage result = MessagePool.getMapMessage();
439: result.copyProps(this );
440: synchronized (content) {
441: result.content = (HashMap) this .content.clone();
442: }
443: return result;
444: }
445:
446: // Externalizable implementation ---------------------------------
447:
448: public void writeExternal(ObjectOutput out) throws IOException {
449: super .writeExternal(out);
450: // TODO: For backwards compatbility this should really
451: // write out a hashtable. But if the content contained nulls
452: // the old client wouldn't be able to deserialize it anyway.
453: out.writeObject(content);
454: }
455:
456: public void readExternal(ObjectInput in) throws IOException,
457: ClassNotFoundException {
458: super .readExternal(in);
459: Object object = in.readObject();
460: // This is a hack for backwards compatibility
461: if (object instanceof Hashtable) {
462: Hashtable ht = (Hashtable) object;
463: content = new HashMap(ht.size());
464: content.putAll(ht);
465: } else
466: content = (HashMap) object;
467: }
468:
469: // Package protected ---------------------------------------------
470:
471: // Protected -----------------------------------------------------
472:
473: // Private -------------------------------------------------------
474:
475: /**
476: * Check the name
477: *
478: * @param name the name
479: */
480: private void checkName(String name) {
481: if (name == null)
482: throw new IllegalArgumentException("Name must not be null.");
483:
484: if (name.equals(""))
485: throw new IllegalArgumentException(
486: "Name must not be an empty String.");
487: }
488:
489: // Inner classes -------------------------------------------------
490:
491: }
492: /*
493: * vim:ts=3:sw=3:et
494: */
|