1 6 7 package org.jfox.jms.message; 8 9 import java.io.Serializable ; 10 import java.util.Collections ; 11 import java.util.Enumeration ; 12 import java.util.HashMap ; 13 import javax.jms.JMSException ; 14 import javax.jms.MapMessage ; 15 import javax.jms.MessageFormatException ; 16 import javax.jms.MessageNotWriteableException ; 17 18 31 32 public class MapMessageImpl 33 extends JMSMessage 34 implements MapMessage , Serializable { 35 36 private HashMap map = new HashMap (); 37 38 41 public MapMessageImpl() { 42 super(); 43 } 44 45 56 public boolean getBoolean(String name) throws JMSException { 57 return getBoolean(map.get(name)); 58 } 59 60 71 public byte getByte(String name) throws JMSException { 72 return getByte(map.get(name)); 73 } 74 75 86 public short getShort(String name) throws JMSException { 87 return getShort(map.get(name)); 88 } 89 90 101 public char getChar(String name) throws JMSException { 102 return getChar(map.get(name)); 103 } 104 105 116 public int getInt(String name) throws JMSException { 117 return getInt(map.get(name)); 118 } 119 120 131 public long getLong(String name) throws JMSException { 132 return getLong(map.get(name)); 133 } 134 135 146 public float getFloat(String name) throws JMSException { 147 return getFloat(map.get(name)); 148 } 149 150 161 public double getDouble(String name) throws JMSException { 162 return getDouble(map.get(name)); 163 } 164 165 177 public String getString(String name) throws JMSException { 178 return getString(map.get(name)); 179 } 180 181 193 public byte[] getBytes(String name) throws JMSException { 194 return getBytes(map.get(name)); 195 } 196 197 219 public Object getObject(String name) throws JMSException { 220 return map.get(name); 221 } 222 223 233 public Enumeration getMapNames() throws JMSException { 234 return Collections.enumeration(map.keySet()); 235 } 236 237 249 public void setBoolean(String name, boolean value) throws JMSException { 250 if (isBodyModifiable()) { 251 try { 252 map.put(name, new Boolean (value)); 253 } catch (NullPointerException e) { 254 throw new JMSException (e.getMessage()); 255 } 256 } else { 257 throw new MessageNotWriteableException ("MapMesage read_only"); 258 } 259 } 260 261 272 public void setByte(String name, byte value) throws JMSException { 273 if (isBodyModifiable()) { 274 try { 275 map.put(name, new Byte (value)); 276 } catch (NullPointerException e) { 277 throw new JMSException (e.getMessage()); 278 } 279 } else { 280 throw new MessageNotWriteableException ("MapMessage read_only"); 281 } 282 } 283 284 295 public void setShort(String name, short value) throws JMSException { 296 if (isBodyModifiable()) { 297 try { 298 map.put(name, new Short (value)); 299 } catch (NullPointerException e) { 300 throw new JMSException (e.getMessage()); 301 } 302 } else { 303 throw new MessageNotWriteableException ("MapMessage read_only"); 304 } 305 } 306 307 318 public void setChar(String name, char value) throws JMSException { 319 if (isBodyModifiable()) { 320 try { 321 map.put(name, new Character (value)); 322 } catch (NullPointerException e) { 323 throw new JMSException (e.getMessage()); 324 } 325 } else { 326 throw new MessageNotWriteableException ("MapMessage read_only"); 327 } 328 } 329 330 341 public void setInt(String name, int value) throws JMSException { 342 if (isBodyModifiable()) { 343 try { 344 map.put(name, new Integer (value)); 345 } catch (NullPointerException e) { 346 throw new JMSException (e.getMessage()); 347 } 348 } else { 349 throw new MessageNotWriteableException ("MapMessage read_only"); 350 } 351 } 352 353 364 public void setLong(String name, long value) throws JMSException { 365 if (isBodyModifiable()) { 366 try { 367 map.put(name, new Long (value)); 368 } catch (NullPointerException e) { 369 throw new JMSException (e.getMessage()); 370 } 371 } else { 372 throw new MessageNotWriteableException ("MapMessage read_only"); 373 } 374 } 375 376 387 public void setFloat(String name, float value) throws JMSException { 388 if (isBodyModifiable()) { 389 try { 390 map.put(name, new Float (value)); 391 } catch (NullPointerException e) { 392 throw new JMSException (e.getMessage()); 393 } 394 } else { 395 throw new MessageNotWriteableException ("MapMessage read_only"); 396 } 397 } 398 399 410 public void setDouble(String name, double value) throws JMSException { 411 if (isBodyModifiable()) { 412 try { 413 map.put(name, new Double (value)); 414 } catch (NullPointerException e) { 415 throw new JMSException (e.getMessage()); 416 } 417 } else { 418 throw new MessageNotWriteableException ("MapMessage read_only"); 419 } 420 } 421 422 433 public void setString(String name, String value) throws JMSException { 434 if (isBodyModifiable()) { 435 try { 436 map.put(name, value); 437 } catch (NullPointerException e) { 438 throw new JMSException (e.getMessage()); 439 } 440 } else { 441 throw new MessageNotWriteableException ("MapMessage read_only"); 442 } 443 } 444 445 458 public void setBytes(String name, byte[] value) throws JMSException { 459 if (isBodyModifiable()) { 460 try { 461 int len = value.length; 462 byte[] valueCopy = new byte[len]; 463 System.arraycopy(value, 0, valueCopy, 0, len); 464 map.put(name, valueCopy); 465 } catch (NullPointerException e) { 466 throw new JMSException (e.getMessage()); 467 } 468 } else { 469 throw new MessageNotWriteableException ("MapMmessage read_only"); 470 } 471 472 } 473 474 488 public void setBytes(String name, byte[] value, int offset, int length) 489 throws JMSException { 490 if (isBodyModifiable()) { 491 try { 492 byte[] newValue = new byte[length]; 493 System.arraycopy(value, offset, newValue, 0, length); 494 map.put(name, newValue); 495 } catch (NullPointerException e) { 496 throw new JMSException (e.getMessage()); 497 } 498 } else { 499 throw new MessageNotWriteableException ("MapMessageimpl read_only"); 500 } 501 } 502 503 521 public void setObject(String name, Object value) throws JMSException { 522 if (isBodyModifiable()) { 523 try { 524 if ((value instanceof Boolean ) 525 || (value instanceof Byte ) 526 || (value instanceof Short ) 527 || (value instanceof Character ) 528 || (value instanceof Integer ) 529 || (value instanceof Long ) 530 || (value instanceof Float ) 531 || (value instanceof Double ) 532 || (value instanceof String ) 533 || (value instanceof byte[])) { 534 map.put(name, value); 535 } else { 536 throw new MessageFormatException ("MapMessagei invalid_type"); 537 } 538 } catch (NullPointerException e) { 539 throw new JMSException (e.getMessage()); 540 } 541 } else { 542 throw new MessageNotWriteableException ("MapMessage read_only"); 543 } 544 } 545 546 556 public boolean itemExists(String name) throws JMSException { 557 return map.containsKey(name); 558 } 559 560 565 public void clearBody() throws JMSException { 566 super.clearBody(); 567 map = new HashMap (); 568 } 569 570 578 private boolean getBoolean(Object value) throws MessageFormatException { 579 if (value instanceof Boolean ) { 580 return ((Boolean ) value).booleanValue(); 581 } else if (value instanceof String ) { 582 return Boolean.valueOf((String ) value).booleanValue(); 583 } else if (value == null) { 584 return Boolean.valueOf((String ) value).booleanValue(); 585 } else { 586 throw new MessageFormatException ("Can't convert value of type " 587 + value.getClass().getName() 588 + " to Boolean"); 589 } 590 } 591 592 600 private byte getByte(Object value) throws MessageFormatException { 601 if (value instanceof Byte ) { 602 return ((Byte ) value).byteValue(); 603 } else if (value instanceof String ) { 604 return Byte.parseByte((String ) value); 605 } else if (value == null) { 606 return Byte.valueOf((String ) value).byteValue(); 607 } else { 608 throw new MessageFormatException ("Can't convert value of type " 609 + value.getClass().getName() 610 + " to Byte"); 611 } 612 } 613 614 622 private short getShort(Object value) throws MessageFormatException { 623 if (value instanceof Short ) { 624 return ((Short ) value).shortValue(); 625 } else if (value instanceof Byte ) { 626 return ((Byte ) value).shortValue(); 627 } else if (value instanceof String ) { 628 return Short.parseShort((String ) value); 629 } else if (value == null) { 630 return Short.valueOf((String ) value).shortValue(); 631 } else { 632 throw new MessageFormatException ("Can't convert value of type " 633 + value.getClass().getName() 634 + " to Short"); 635 } 636 } 637 638 647 private char getChar(Object value) throws MessageFormatException { 648 if (value == null) { 649 return ((Character ) null).charValue(); 650 } else if (value instanceof Character ) { 651 return ((Character ) value).charValue(); 652 } else { 653 throw new MessageFormatException ("Can't convert value of type " 654 + value.getClass().getName() 655 + " to Char"); 656 } 657 } 658 659 668 private int getInt(Object value) throws MessageFormatException { 669 if (value instanceof Integer ) { 670 return ((Integer ) value).intValue(); 671 } else if (value instanceof Byte ) { 672 return ((Byte ) value).intValue(); 673 } else if (value instanceof Short ) { 674 return ((Short ) value).intValue(); 675 } else if (value instanceof String ) { 676 return Integer.parseInt((String ) value); 677 } else if (value == null) { 678 return Integer.valueOf((String ) value).intValue(); 679 } else { 680 throw new MessageFormatException ("Can't convert value of type " 681 + value.getClass().getName() 682 + " to Int"); 683 } 684 } 685 686 695 private long getLong(Object value) throws MessageFormatException { 696 if (value instanceof Long ) { 697 return ((Long ) value).longValue(); 698 } else if (value instanceof Byte ) { 699 return ((Byte ) value).longValue(); 700 } else if (value instanceof Short ) { 701 return ((Short ) value).longValue(); 702 } else if (value instanceof Integer ) { 703 return ((Integer ) value).longValue(); 704 } else if (value instanceof String ) { 705 return Long.parseLong((String ) value); 706 } else if (value == null) { 707 return Long.valueOf((String ) value).longValue(); 708 } else { 709 throw new MessageFormatException ("Can't convert value of type " 710 + value.getClass().getName() 711 + " to Long"); 712 } 713 } 714 715 724 private float getFloat(Object value) throws MessageFormatException { 725 if (value instanceof Float ) { 726 return ((Float ) value).floatValue(); 727 } else if (value instanceof String ) { 728 return Float.parseFloat((String ) value); 729 } else if (value == null) { 730 return Float.valueOf((String ) value).floatValue(); 731 } else { 732 throw new MessageFormatException ("Can't convert value of type " 733 + value.getClass().getName() 734 + " to Float"); 735 } 736 } 737 738 747 private double getDouble(Object value) throws MessageFormatException { 748 if (value instanceof Double ) { 749 return ((Double ) value).doubleValue(); 750 } else if (value instanceof Float ) { 751 return ((Float ) value).doubleValue(); 752 } else if (value instanceof String ) { 753 return Double.parseDouble((String ) value); 754 } else if (value == null) { 755 return Double.valueOf((String ) value).doubleValue(); 756 } else { 757 throw new MessageFormatException ("Can't convert value of type " 758 + value.getClass().getName() 759 + " to Double"); 760 } 761 } 762 763 770 private String getString(Object value) { 771 return (value == null) ? null : String.valueOf(value); 772 } 773 774 782 private byte[] getBytes(Object value) throws MessageFormatException { 783 if (value == null) { 784 return (byte[]) value; 785 } else if (value instanceof byte[]) { 786 return (byte[]) value; 787 } else { 788 throw new MessageFormatException ("Can't convert value of type " 789 + value.getClass().getName() 790 + " to byte[]."); 791 } 792 } 793 } 794 | Popular Tags |