1 48 49 package org.exolab.jms.message; 50 51 import java.io.IOException ; 52 import java.io.ObjectInput ; 53 import java.io.ObjectOutput ; 54 import java.util.Collections ; 55 import java.util.Enumeration ; 56 import java.util.HashMap ; 57 58 import javax.jms.JMSException ; 59 import javax.jms.MapMessage ; 60 import javax.jms.MessageFormatException ; 61 import javax.jms.MessageNotWriteableException ; 62 63 64 120 public class MapMessageImpl extends MessageImpl implements MapMessage { 121 122 125 static final long serialVersionUID = 2; 126 127 130 private static final int INITIAL_SIZE = 20; 131 132 135 private HashMap _map = new HashMap (INITIAL_SIZE); 136 137 142 public MapMessageImpl() throws JMSException { 143 setJMSType("MapMessage"); 144 } 145 146 153 public final Object clone() throws CloneNotSupportedException { 154 MapMessageImpl result = (MapMessageImpl) super.clone(); 155 result._map = (HashMap ) _map.clone(); 156 return result; 157 } 158 159 165 public final void writeExternal(ObjectOutput out) throws IOException { 166 super.writeExternal(out); 167 out.writeLong(serialVersionUID); 168 out.writeObject(_map); 169 } 170 171 179 public final void readExternal(ObjectInput in) 180 throws ClassNotFoundException , IOException { 181 super.readExternal(in); 182 long version = in.readLong(); 183 if (version == serialVersionUID) { 184 _map = (HashMap ) in.readObject(); 185 } else { 186 throw new IOException ("Incorrect version enountered: " + version + 187 ". This version = " + serialVersionUID); 188 } 189 } 190 191 200 public final boolean getBoolean(String name) 201 throws JMSException , MessageFormatException { 202 return FormatConverter.getBoolean(_map.get(name)); 203 } 204 205 214 public final byte getByte(String name) 215 throws JMSException , MessageFormatException { 216 return FormatConverter.getByte(_map.get(name)); 217 } 218 219 228 public final short getShort(String name) 229 throws JMSException , MessageFormatException { 230 return FormatConverter.getShort(_map.get(name)); 231 } 232 233 242 public final char getChar(String name) 243 throws JMSException , MessageFormatException { 244 return FormatConverter.getChar(_map.get(name)); 245 } 246 247 256 public final int getInt(String name) 257 throws JMSException , MessageFormatException { 258 return FormatConverter.getInt(_map.get(name)); 259 } 260 261 270 public final long getLong(String name) 271 throws JMSException , MessageFormatException { 272 return FormatConverter.getLong(_map.get(name)); 273 } 274 275 284 public final float getFloat(String name) 285 throws JMSException , MessageFormatException { 286 return FormatConverter.getFloat(_map.get(name)); 287 } 288 289 298 public final double getDouble(String name) 299 throws JMSException , MessageFormatException { 300 return FormatConverter.getDouble(_map.get(name)); 301 } 302 303 313 public final String getString(String name) 314 throws JMSException , MessageFormatException { 315 return FormatConverter.getString(_map.get(name)); 316 } 317 318 328 public final byte[] getBytes(String name) 329 throws JMSException , MessageFormatException { 330 return FormatConverter.getBytes(_map.get(name)); 331 } 332 333 350 public final Object getObject(String name) throws JMSException { 351 Object result = null; 352 Object value = _map.get(name); 353 if (value != null) { 354 if (value instanceof Boolean ) { 355 result = new Boolean (((Boolean ) value).booleanValue()); 356 } else if (value instanceof Byte ) { 357 result = new Byte (((Byte ) value).byteValue()); 358 } else if (value instanceof Short ) { 359 result = new Short (((Short ) value).shortValue()); 360 } else if (value instanceof Character ) { 361 result = new Character (((Character ) value).charValue()); 362 } else if (value instanceof Integer ) { 363 result = new Integer (((Integer ) value).intValue()); 364 } else if (value instanceof Long ) { 365 result = new Long (((Long ) value).longValue()); 366 } else if (value instanceof Float ) { 367 result = new Float (((Float ) value).floatValue()); 368 } else if (value instanceof Double ) { 369 result = new Double (((Double ) value).doubleValue()); 370 } else if (value instanceof String ) { 371 result = (String ) value; 372 } else if (value instanceof byte[]) { 373 result = getBytes(name); 374 } else { 375 throw new MessageFormatException ( 376 "MapMessage contains an unsupported object of type=" + 377 value.getClass().getName()); 378 } 379 } 380 return result; 381 } 382 383 388 public final Enumeration getMapNames() { 389 return Collections.enumeration(_map.keySet()); 390 } 391 392 399 public final void setBoolean(String name, boolean value) 400 throws MessageNotWriteableException { 401 checkWrite(); 402 _map.put(name, new Boolean (value)); 403 } 404 405 412 public final void setByte(String name, byte value) 413 throws MessageNotWriteableException { 414 checkWrite(); 415 _map.put(name, new Byte (value)); 416 } 417 418 425 public final void setShort(String name, short value) 426 throws MessageNotWriteableException { 427 checkWrite(); 428 _map.put(name, new Short (value)); 429 } 430 431 438 public final void setChar(String name, char value) 439 throws MessageNotWriteableException { 440 checkWrite(); 441 _map.put(name, new Character (value)); 442 } 443 444 451 public final void setInt(String name, int value) 452 throws MessageNotWriteableException { 453 checkWrite(); 454 _map.put(name, new Integer (value)); 455 } 456 457 464 public final void setLong(String name, long value) 465 throws MessageNotWriteableException { 466 checkWrite(); 467 _map.put(name, new Long (value)); 468 } 469 470 477 public final void setFloat(String name, float value) 478 throws MessageNotWriteableException { 479 checkWrite(); 480 _map.put(name, new Float (value)); 481 } 482 483 490 public final void setDouble(String name, double value) 491 throws MessageNotWriteableException { 492 checkWrite(); 493 _map.put(name, new Double (value)); 494 } 495 496 503 public final void setString(String name, String value) 504 throws MessageNotWriteableException { 505 checkWrite(); 506 _map.put(name, value); 507 } 508 509 518 public final void setBytes(String name, byte[] value) 519 throws MessageNotWriteableException { 520 checkWrite(); 521 byte[] bytes = null; 522 if (value != null) { 523 bytes = new byte[value.length]; 524 System.arraycopy(value, 0, bytes, 0, bytes.length); 525 } 526 _map.put(name, bytes); 527 } 528 529 538 public final void setBytes(String name, byte[] value, 539 int offset, int length) 540 throws MessageNotWriteableException { 541 checkWrite(); 542 byte[] bytes = null; 543 if (value != null) { 544 bytes = new byte[length]; 545 System.arraycopy(value, offset, bytes, 0, length); 546 } 547 _map.put(name, bytes); 548 } 549 550 561 public final void setObject(String name, Object value) 562 throws MessageFormatException , MessageNotWriteableException { 563 checkWrite(); 564 if (value == null) { 565 _map.put(name, null); 566 } else if (value instanceof Boolean ) { 567 setBoolean(name, ((Boolean ) value).booleanValue()); 568 } else if (value instanceof Byte ) { 569 setByte(name, ((Byte ) value).byteValue()); 570 } else if (value instanceof Short ) { 571 setShort(name, ((Short ) value).shortValue()); 572 } else if (value instanceof Character ) { 573 setChar(name, ((Character ) value).charValue()); 574 } else if (value instanceof Integer ) { 575 setInt(name, ((Integer ) value).intValue()); 576 } else if (value instanceof Long ) { 577 setLong(name, ((Long ) value).longValue()); 578 } else if (value instanceof Float ) { 579 setFloat(name, ((Float ) value).floatValue()); 580 } else if (value instanceof Double ) { 581 setDouble(name, ((Double ) value).doubleValue()); 582 } else if (value instanceof String ) { 583 setString(name, (String ) value); 584 } else if (value instanceof byte[]) { 585 setBytes(name, (byte[]) value); 586 } else { 587 throw new MessageFormatException ( 588 "MapMessage does not support objects of type=" + 589 value.getClass().getName()); 590 } 591 } 592 593 599 public final boolean itemExists(String name) { 600 return _map.containsKey(name); 601 } 602 603 610 public final void clearBody() throws JMSException { 611 super.clearBody(); 612 _map = new HashMap (INITIAL_SIZE); 613 } 614 615 } | Popular Tags |