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.Hashtable ; 13 import javax.jms.JMSException ; 14 import javax.jms.MessageFormatException ; 15 16 116 117 public class MessageProperties implements Serializable { 118 119 private static final String JMSX_USERID = "JMSXUserID"; 121 private static final String JMSX_APPID = "JMSXAppID"; 122 private static final String JMSX_GROUPID = "JMSXGroupID"; 123 private static final String JMSX_GROUPSEQ = "JMSXGroupSeq"; 124 125 private Hashtable properties = new Hashtable (); 126 127 128 137 public void setBooleanProperty(String name, boolean value) throws JMSException { 138 setProperty(name, new Boolean (value)); 139 } 140 141 152 public boolean getBooleanProperty(String name) throws JMSException { 153 return getBoolean(properties.get(name)); 154 } 155 156 165 public void setByteProperty(String name, byte value) throws JMSException { 166 setProperty(name, new Byte (value)); 167 } 168 169 180 public byte getByteProperty(String name) throws JMSException { 181 return getByte(properties.get(name)); 182 } 183 184 193 public void setShortProperty(String name, short value) throws JMSException { 194 setProperty(name, new Short (value)); 195 } 196 197 208 public short getShortProperty(String name) throws JMSException { 209 return getShort(properties.get(name)); 210 } 211 212 221 public void setIntProperty(String name, int value) throws JMSException { 222 setProperty(name, new Integer (value)); 223 } 224 225 236 public int getIntProperty(String name) throws JMSException { 237 return getInt(properties.get(name)); 238 } 239 240 249 public void setLongProperty(String name, long value) throws JMSException { 250 setProperty(name, new Long (value)); 251 } 252 253 264 public long getLongProperty(String name) throws JMSException { 265 return getLong(properties.get(name)); 266 } 267 268 277 public void setFloatProperty(String name, float value) throws JMSException { 278 setProperty(name, new Float (value)); 279 } 280 281 292 public float getFloatProperty(String name) throws JMSException { 293 return getFloat(properties.get(name)); 294 } 295 296 305 public void setDoubleProperty(String name, double value) throws JMSException { 306 setProperty(name, new Double (value)); 307 } 308 309 320 public double getDoubleProperty(String name) throws JMSException { 321 return getDouble(properties.get(name)); 322 } 323 324 333 public void setStringProperty(String name, String value) throws JMSException { 334 setProperty(name, new String (value)); 335 } 336 337 349 public String getStringProperty(String name) throws JMSException { 350 return getString(properties.get(name)); 351 } 352 353 368 public void setObjectProperty(String name, Object value) throws JMSException { 369 if (value instanceof Boolean ) { 370 setBooleanProperty(name, ((Boolean ) value).booleanValue()); 371 } else if (value instanceof Byte ) { 372 setByteProperty(name, ((Byte ) value).byteValue()); 373 } else if (value instanceof Short ) { 374 setShortProperty(name, ((Short ) value).shortValue()); 375 } else if (value instanceof Integer ) { 376 setIntProperty(name, ((Integer ) value).intValue()); 377 } else if (value instanceof Long ) { 378 setLongProperty(name, ((Long ) value).longValue()); 379 } else if (value instanceof Float ) { 380 setFloatProperty(name, ((Float ) value).floatValue()); 381 } else if (value instanceof Double ) { 382 setDoubleProperty(name, ((Double ) value).doubleValue()); 383 } else if (value instanceof String ) { 384 setStringProperty(name, (String ) value); 385 } else if (value == null) { 386 setProperty(name, null); 387 } else { 388 throw new MessageFormatException ("Does not support objects of " + "type=" + value.getClass().getName()); 389 } 390 } 391 392 409 public Object getObjectProperty(String name) throws JMSException { 410 Object value = properties.get(name); 411 if (value != null) { 412 if (value instanceof Boolean ) { 413 return new Boolean (((Boolean ) value).booleanValue()); 414 } else if (value instanceof Byte ) { 415 return new Byte (((Byte ) value).byteValue()); 416 } else if (value instanceof Short ) { 417 return new Short (((Short ) value).shortValue()); 418 } else if (value instanceof Integer ) { 419 return new Integer (((Integer ) value).intValue()); 420 } else if (value instanceof Long ) { 421 return new Long (((Long ) value).longValue()); 422 } else if (value instanceof Float ) { 423 return new Float (((Float ) value).floatValue()); 424 } else if (value instanceof Double ) { 425 return new Double (((Double ) value).doubleValue()); 426 } else { 427 return new String ((String ) value); 428 } 429 } else { 430 return null; 431 } 432 } 433 434 435 443 private void setProperty(String name, Object value) throws JMSException { 444 if (name == null) { 445 throw new JMSException ("It's invalid property name"); 446 } else { 447 if (name.equalsIgnoreCase("NULL") || 448 name.equalsIgnoreCase("TRUE") || 449 name.equalsIgnoreCase("FALSE") || 450 name.equals("NOT") || 451 name.equals("AND") || 452 name.equals("OR") || 453 name.equals("BETWEEN") || 454 name.equals("LIKE") || 455 name.equals("IN") || 456 name.equals("IS")) { 457 throw new JMSException ("Invalid property name"); 458 } else { 459 if (name.startsWith("JMS") && !name.startsWith("JMS_")) { 460 throw new JMSException ("Properties cannot begin with JMS"); 461 } else { 462 properties.put(name, value); 463 } 464 } 465 } 466 } 467 468 476 private boolean getBoolean(Object value) throws MessageFormatException { 477 if (value instanceof Boolean ) { 478 return ((Boolean ) value).booleanValue(); 479 } else if (value instanceof String ) { 480 return Boolean.valueOf((String ) value).booleanValue(); 481 } else if (value == null) { 482 return Boolean.valueOf((String ) value).booleanValue(); 483 } else { 484 throw new MessageFormatException ("Can't convert value of type " 485 + value.getClass().getName() + " to Boolean"); 486 } 487 } 488 489 497 private byte getByte(Object value) throws MessageFormatException { 498 if (value instanceof Byte ) { 499 return ((Byte ) value).byteValue(); 500 } else if (value instanceof String ) { 501 return Byte.parseByte((String ) value); 502 } else if (value == null) { 503 return Byte.valueOf((String ) value).byteValue(); 504 } else { 505 throw new MessageFormatException ("Can't convert value of type " 506 + value.getClass().getName() + " to Byte"); 507 } 508 } 509 510 518 private short getShort(Object value) throws MessageFormatException { 519 if (value instanceof Short ) { 520 return ((Short ) value).shortValue(); 521 } else if (value instanceof Byte ) { 522 return ((Byte ) value).shortValue(); 523 } else if (value instanceof String ) { 524 return Short.parseShort((String ) value); 525 } else if (value == null) { 526 return Short.valueOf((String ) value).shortValue(); 527 } else { 528 throw new MessageFormatException ("Can't convert value of type " 529 + value.getClass().getName() + " to Short"); 530 } 531 } 532 533 543 private int getInt(Object value) throws MessageFormatException { 544 if (value instanceof Integer ) { 545 return ((Integer ) value).intValue(); 546 } else if (value instanceof Byte ) { 547 return ((Byte ) value).intValue(); 548 } else if (value instanceof Short ) { 549 return ((Short ) value).intValue(); 550 } else if (value instanceof String ) { 551 return Integer.parseInt((String ) value); 552 } else if (value == null) { 553 return Integer.valueOf((String ) value).intValue(); 554 } else { 555 throw new MessageFormatException ("Can't convert value of type " 556 + value.getClass().getName() + " to Int"); 557 } 558 } 559 560 561 571 private long getLong(Object value) throws MessageFormatException { 572 if (value instanceof Long ) { 573 return ((Long ) value).longValue(); 574 } else if (value instanceof Byte ) { 575 return ((Byte ) value).longValue(); 576 } else if (value instanceof Short ) { 577 return ((Short ) value).longValue(); 578 } else if (value instanceof Integer ) { 579 return ((Integer ) value).longValue(); 580 } else if (value instanceof String ) { 581 return Long.parseLong((String ) value); 582 } else if (value == null) { 583 return Long.valueOf((String ) value).longValue(); 584 } else { 585 throw new MessageFormatException ("Can't convert value of type " 586 + value.getClass().getName() + " to Long"); 587 } 588 } 589 590 600 private float getFloat(Object value) throws MessageFormatException { 601 if (value instanceof Float ) { 602 return ((Float ) value).floatValue(); 603 } else if (value instanceof String ) { 604 return Float.parseFloat((String ) value); 605 } else if (value == null) { 606 return Float.valueOf((String ) value).floatValue(); 607 } else { 608 throw new MessageFormatException ("Can't convert value of type " 609 + value.getClass().getName() + " to Float"); 610 } 611 } 612 613 623 private double getDouble(Object value) throws MessageFormatException { 624 if (value instanceof Double ) { 625 return ((Double ) value).doubleValue(); 626 } else if (value instanceof Float ) { 627 return ((Float ) value).doubleValue(); 628 } else if (value instanceof String ) { 629 return Double.parseDouble((String ) value); 630 } else if (value == null) { 631 return Double.valueOf((String ) value).doubleValue(); 632 } else { 633 throw new MessageFormatException ("Can't convert value of type " 634 + value.getClass().getName() + " to Double"); 635 } 636 } 637 638 646 private String getString(Object value) throws MessageFormatException { 647 return (value == null) ? null : String.valueOf(value); 648 } 649 650 651 656 public void clearProperties() { 657 properties.clear(); 658 } 659 660 665 public boolean propertyExists(String name) { 666 return properties.containsKey(name); 667 } 668 669 674 public Enumeration getPropertyNames() { 675 return Collections.enumeration(properties.keySet()); 676 } 677 } 678 | Popular Tags |