View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact tma@netspace.net.au. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: BasicMapMessage.java,v 1.2 2004/02/02 03:49:55 tanderson Exp $ 44 */ 45 46 package org.exolab.jmscts.jms.message; 47 48 import java.util.Collections; 49 import java.util.Enumeration; 50 import java.util.HashMap; 51 52 import javax.jms.JMSException; 53 import javax.jms.MapMessage; 54 import javax.jms.MessageFormatException; 55 import javax.jms.MessageNotWriteableException; 56 57 58 /*** 59 * This class provides a basic implementation of the javax.jms.MapMessage 60 * interface. 61 * 62 * @version $Revision: 1.2 $ $Date: 2004/02/02 03:49:55 $ 63 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 64 * @see javax.jms.MapMessage 65 */ 66 public class BasicMapMessage extends BasicMessage implements MapMessage { 67 68 /*** 69 * The container for all message data 70 */ 71 private HashMap _map = new HashMap(); 72 73 /*** 74 * Construct a new <code>BasicMapMessage</code> 75 */ 76 public BasicMapMessage() { 77 } 78 79 /*** 80 * Return the boolean value with the given name 81 * 82 * @param name the name of the boolean 83 * @return the boolean value with the given name 84 * @throws MessageFormatException if the type conversion is invalid 85 */ 86 public boolean getBoolean(String name) 87 throws MessageFormatException { 88 return FormatConverter.getBoolean(_map.get(name)); 89 } 90 91 /*** 92 * Return the byte value with the given name 93 * 94 * @param name the name of the byte 95 * @return the byte value with the given name 96 * @throws MessageFormatException if the type conversion is invalid 97 */ 98 public byte getByte(String name) throws MessageFormatException { 99 return FormatConverter.getByte(_map.get(name)); 100 } 101 102 /*** 103 * Return the short value with the given name 104 * 105 * @param name the name of the short 106 * @return the short value with the given name 107 * @throws MessageFormatException if the type conversion is invalid 108 */ 109 public short getShort(String name) throws MessageFormatException { 110 return FormatConverter.getShort(_map.get(name)); 111 } 112 113 /*** 114 * Return the Unicode character value with the given name 115 * 116 * @param name the name of the Unicode character 117 * @return the Unicode character value with the given name 118 * @throws MessageFormatException if the type conversion is invalid 119 */ 120 public char getChar(String name) throws MessageFormatException { 121 return FormatConverter.getChar(_map.get(name)); 122 } 123 124 /*** 125 * Return the integer value with the given name 126 * 127 * @param name the name of the integer 128 * @return the integer value with the given name 129 * @throws MessageFormatException if the type conversion is invalid 130 */ 131 public int getInt(String name) throws MessageFormatException { 132 return FormatConverter.getInt(_map.get(name)); 133 } 134 135 /*** 136 * Return the long value with the given name 137 * 138 * @param name the name of the long 139 * @return the long value with the given name 140 * @throws MessageFormatException if the type conversion is invalid 141 */ 142 public long getLong(String name) throws MessageFormatException { 143 return FormatConverter.getLong(_map.get(name)); 144 } 145 146 /*** 147 * Return the float value with the given name 148 * 149 * @param name the name of the float 150 * @return the float value with the given name 151 * @throws MessageFormatException if the type conversion is invalid 152 */ 153 public float getFloat(String name) throws MessageFormatException { 154 return FormatConverter.getFloat(_map.get(name)); 155 } 156 157 /*** 158 * Return the double value with the given name 159 * 160 * @param name the name of the double 161 * @return the double value with the given name 162 * @throws MessageFormatException if the type conversion is invalid 163 */ 164 public double getDouble(String name) throws MessageFormatException { 165 return FormatConverter.getDouble(_map.get(name)); 166 } 167 168 /*** 169 * Return the String value with the given name 170 * 171 * @param name the name of the String 172 * @return the String value with the given name. If there is no item 173 * by this name, a null value is returned. 174 * @throws MessageFormatException if the type conversion is invalid 175 */ 176 public String getString(String name) throws MessageFormatException { 177 return FormatConverter.getString(_map.get(name)); 178 } 179 180 /*** 181 * Return the byte array value with the given name 182 * 183 * @param name the name of the byte array 184 * @return a copy of the byte array value with the given name. 185 * If there is no item by this name, a null value is returned. 186 * @throws MessageFormatException if the type conversion is invalid 187 */ 188 public byte[] getBytes(String name) throws MessageFormatException { 189 return FormatConverter.getBytes(_map.get(name)); 190 } 191 192 /*** 193 * Return the Java object value with the given name 194 * <p> 195 * Note that this method can be used to return in objectified format, 196 * an object that had been stored in the Map with the equivalent 197 * <code>setObject</code> method call, or it's equivalent primitive 198 * set<type> method. 199 * 200 * @param name the name of the Java object 201 * @return a copy of the Java object value with the given name, in 202 * objectified format (eg. if it set as an int, then an Integer is 203 * returned). 204 * Note that byte values are returned as byte[], not Byte[]. 205 * If there is no item by this name, a null value is returned. 206 * @throws JMSException if JMS fails to read the message due to some 207 * internal JMS error 208 */ 209 public Object getObject(String name) throws JMSException { 210 Object result = _map.get(name); 211 if (result instanceof byte[]) { 212 result = getBytes(name); 213 } 214 return result; 215 } 216 217 /*** 218 * Return an Enumeration of all the Map message's names. 219 * 220 * @return an enumeration of all the names in this Map message. 221 */ 222 public Enumeration getMapNames() { 223 return Collections.enumeration(_map.keySet()); 224 } 225 226 /*** 227 * Set a boolean value with the given name, into the Map 228 * 229 * @param name the name of the boolean 230 * @param value the boolean value to set in the Map 231 * @throws MessageNotWriteableException if the message is in read-only mode 232 */ 233 public void setBoolean(String name, boolean value) 234 throws MessageNotWriteableException { 235 checkWrite(); 236 _map.put(name, new Boolean(value)); 237 } 238 239 /*** 240 * Set a byte value with the given name, into the Map 241 * 242 * @param name the name of the byte 243 * @param value the byte value to set in the Map 244 * @throws MessageNotWriteableException if the message is in read-only mode 245 */ 246 public void setByte(String name, byte value) 247 throws MessageNotWriteableException { 248 checkWrite(); 249 _map.put(name, new Byte(value)); 250 } 251 252 /*** 253 * Set a short value with the given name, into the Map 254 * 255 * @param name the name of the short 256 * @param value the short value to set in the Map 257 * @throws MessageNotWriteableException if the message is in read-only mode 258 */ 259 public void setShort(String name, short value) 260 throws MessageNotWriteableException { 261 checkWrite(); 262 _map.put(name, new Short(value)); 263 } 264 265 /*** 266 * Set a Unicode character value with the given name, into the Map 267 * 268 * @param name the name of the Unicode character 269 * @param value the Unicode character value to set in the Map 270 * @throws MessageNotWriteableException if the message is in read-only mode 271 */ 272 public void setChar(String name, char value) 273 throws MessageNotWriteableException { 274 checkWrite(); 275 _map.put(name, new Character(value)); 276 } 277 278 /*** 279 * Set an integer value with the given name, into the Map 280 * 281 * @param name the name of the integer 282 * @param value the integer value to set in the Map 283 * @throws MessageNotWriteableException if the message is in read-only mode 284 */ 285 public void setInt(String name, int value) 286 throws MessageNotWriteableException { 287 checkWrite(); 288 _map.put(name, new Integer(value)); 289 } 290 291 /*** 292 * Set a long value with the given name, into the Map 293 * 294 * @param name the name of the long 295 * @param value the long value to set in the Map 296 * @throws MessageNotWriteableException if the message is in read-only mode 297 */ 298 public void setLong(String name, long value) 299 throws MessageNotWriteableException { 300 checkWrite(); 301 _map.put(name, new Long(value)); 302 } 303 304 /*** 305 * Set a float value with the given name, into the Map 306 * 307 * @param name the name of the float 308 * @param value the float value to set in the Map 309 * @throws MessageNotWriteableException if the message is in read-only mode 310 */ 311 public void setFloat(String name, float value) 312 throws MessageNotWriteableException { 313 checkWrite(); 314 _map.put(name, new Float(value)); 315 } 316 317 /*** 318 * Set a double value with the given name, into the Map 319 * 320 * @param name the name of the double 321 * @param value the double value to set in the Map 322 * @throws MessageNotWriteableException if the message is in read-only mode 323 */ 324 public void setDouble(String name, double value) 325 throws MessageNotWriteableException { 326 checkWrite(); 327 _map.put(name, new Double(value)); 328 } 329 330 /*** 331 * Set a String value with the given name, into the Map 332 * 333 * @param name the name of the String 334 * @param value the String value to set in the Map 335 * @throws MessageNotWriteableException if the message is in read-only mode 336 */ 337 public void setString(String name, String value) 338 throws MessageNotWriteableException { 339 checkWrite(); 340 _map.put(name, value); 341 } 342 343 /*** 344 * Set a byte array value with the given name, into the Map 345 * 346 * @param name the name of the byte array 347 * @param value the byte array value to set in the Map. The array is 348 * copied so the value for name will not be altered by future 349 * modifications. 350 * @throws MessageNotWriteableException if the message is in read-only mode 351 */ 352 public void setBytes(String name, byte[] value) 353 throws MessageNotWriteableException { 354 checkWrite(); 355 byte[] bytes = null; 356 if (value != null) { 357 bytes = new byte[value.length]; 358 System.arraycopy(value, 0, bytes, 0, bytes.length); 359 } 360 _map.put(name, bytes); 361 } 362 363 /*** 364 * Set a portion of the byte array value with the given name, into the Map 365 * 366 * @param name the name of the byte array 367 * @param value the byte array value to set in the Map. 368 * @param offset the initial offset within the byte array. 369 * @param length the number of bytes to use. 370 * @throws MessageNotWriteableException if the message is in read-only mode 371 */ 372 public void setBytes(String name, byte[] value, 373 int offset, int length) 374 throws MessageNotWriteableException { 375 checkWrite(); 376 byte[] bytes = null; 377 if (value != null) { 378 bytes = new byte[length]; 379 System.arraycopy(value, offset, bytes, 0, length); 380 } 381 _map.put(name, bytes); 382 } 383 384 /*** 385 * Set a Java object value with the given name, into the Map 386 * <p> 387 * Note that this method only works for the objectified primitive 388 * object types (Integer, Double, Long ...), String's and byte arrays. 389 * 390 * @param name the name of the Java object 391 * @param value the Java object value to set in the Map 392 * @throws MessageFormatException if object is invalid 393 * @throws MessageNotWriteableException if message in read-only mode. 394 */ 395 public void setObject(String name, Object value) 396 throws MessageFormatException, MessageNotWriteableException { 397 if (value == null) { 398 checkWrite(); 399 _map.put(name, null); 400 } else if (value instanceof Boolean) { 401 setBoolean(name, ((Boolean) value).booleanValue()); 402 } else if (value instanceof Byte) { 403 setByte(name, ((Byte) value).byteValue()); 404 } else if (value instanceof Short) { 405 setShort(name, ((Short) value).shortValue()); 406 } else if (value instanceof Character) { 407 setChar(name, ((Character) value).charValue()); 408 } else if (value instanceof Integer) { 409 setInt(name, ((Integer) value).intValue()); 410 } else if (value instanceof Long) { 411 setLong(name, ((Long) value).longValue()); 412 } else if (value instanceof Float) { 413 setFloat(name, ((Float) value).floatValue()); 414 } else if (value instanceof Double) { 415 setDouble(name, ((Double) value).doubleValue()); 416 } else if (value instanceof String) { 417 setString(name, (String) value); 418 } else if (value instanceof byte[]) { 419 setBytes(name, (byte[]) value); 420 } else { 421 throw new MessageFormatException( 422 "MapMessage does not support objects of type=" 423 + value.getClass().getName()); 424 } 425 } 426 427 /*** 428 * Check if an item exists in this MapMessage 429 * 430 * @param name the name of the item to test 431 * @return true if the item exists 432 */ 433 public boolean itemExists(String name) { 434 return _map.containsKey(name); 435 } 436 437 /*** 438 * Clear out the message body. Clearing a message's body does not clear 439 * its header values or property entries. 440 * If this message body was read-only, calling this method leaves the 441 * message body is in the same state as an empty body in a newly created 442 * message 443 * 444 * @throws JMSException if the body can't be cleared 445 */ 446 public void clearBody() throws JMSException { 447 super.clearBody(); 448 _map = new HashMap(); 449 } 450 451 }

This page was automatically generated by Maven