1 24 package com.scalagent.kjoram; 25 26 import com.scalagent.kjoram.excepts.IllegalStateException; 27 import com.scalagent.kjoram.excepts.*; 28 import com.scalagent.kjoram.messages.*; 29 30 import java.util.*; 31 32 36 public class Message 37 { 38 39 protected com.scalagent.kjoram.messages.Message momMsg; 40 44 protected Session sess = null; 45 46 47 50 Message() 51 { 52 momMsg = new com.scalagent.kjoram.messages.Message(); 53 } 54 55 62 Message(Session sess, com.scalagent.kjoram.messages.Message momMsg) 63 { 64 this.sess = sess; 65 this.momMsg = momMsg; 66 } 67 68 69 76 public void acknowledge() throws JMSException 77 { 78 if (sess == null 79 || sess.transacted 80 || sess.acknowledgeMode != Session.CLIENT_ACKNOWLEDGE) 81 return; 82 83 if (sess.closed) 84 throw new IllegalStateException ("Forbidden call on a closed session."); 85 86 sess.acknowledge(); 87 } 88 89 94 public void clearBody() throws JMSException 95 { 96 momMsg.clearBody(); 97 } 98 99 104 public void clearProperties() throws JMSException 105 { 106 momMsg.clearProperties(); 107 } 108 109 114 public boolean propertyExists(String name) throws JMSException 115 { 116 return momMsg.propertyExists(name); 117 } 118 119 124 public Enumeration getPropertyNames() throws JMSException 125 { 126 return momMsg.getPropertyNames(); 127 } 128 129 130 135 public void setJMSMessageID(String id) throws JMSException 136 { 137 momMsg.setIdentifier(id); 138 } 139 140 141 146 public void setJMSPriority(int priority) throws JMSException 147 { 148 if (0 <= priority && priority <= 9) 149 momMsg.setPriority(priority); 150 else 151 throw new JMSException("Priority of "+ priority +" is not valid" 152 + " (should be an integer between 0 and 9)."); 153 } 154 155 160 public void setJMSDestination(Destination dest) throws JMSException 161 { 162 if (dest == null) 163 momMsg.setDestination(null, true); 164 165 if (dest instanceof Queue) 166 momMsg.setDestination(((Queue) dest).getQueueName(), true); 167 else 168 momMsg.setDestination(((Topic) dest).getTopicName(), false); 169 170 if (dest instanceof TemporaryQueue || dest instanceof TemporaryTopic) 171 momMsg.setOptionalHeader("JMSTempDestination", new Boolean (true)); 172 else 173 momMsg.setOptionalHeader("JMSTempDestination", new Boolean (false)); 174 } 175 176 181 public void setJMSExpiration(long expiration) throws JMSException 182 { 183 momMsg.setExpiration(expiration); 184 } 185 186 191 public void setJMSRedelivered(boolean redelivered) throws JMSException 192 { 193 momMsg.denied = redelivered; 194 } 195 196 201 public void setJMSReplyTo(Destination replyTo) throws JMSException 202 { 203 if (replyTo == null) 204 momMsg.setReplyTo(null, true); 205 206 if (replyTo instanceof Queue) 207 momMsg.setReplyTo(((Queue) replyTo).getQueueName(), true); 208 else 209 momMsg.setReplyTo(((Topic) replyTo).getTopicName(), false); 210 211 if (replyTo instanceof TemporaryQueue || replyTo instanceof TemporaryTopic) 212 momMsg.setOptionalHeader("JMSTempReplyTo", new Boolean (true)); 213 else 214 momMsg.setOptionalHeader("JMSTempReplyTo", new Boolean (false)); 215 } 216 217 222 public void setJMSTimestamp(long timestamp) throws JMSException 223 { 224 momMsg.setTimestamp(timestamp); 225 } 226 227 232 public void setJMSCorrelationID(String correlationID) throws JMSException 233 { 234 momMsg.setCorrelationId(correlationID); 235 } 236 237 242 public void setJMSCorrelationIDAsBytes(byte[] correlationID) 243 throws JMSException 244 { 245 momMsg.setCorrelationId(ConversionHelper.toString(correlationID)); 246 } 247 248 253 public void setJMSType(String type) throws JMSException 254 { 255 momMsg.setOptionalHeader("JMSType", type); 256 } 257 258 263 public void setJMSDeliveryMode(int deliveryMode) throws JMSException 264 { 265 if (deliveryMode != DeliveryMode.PERSISTENT 266 && deliveryMode != DeliveryMode.NON_PERSISTENT) 267 throw new JMSException("Invalid delivery mode."); 268 269 momMsg.setPersistent(deliveryMode == DeliveryMode.PERSISTENT); 270 } 271 272 277 public String getJMSMessageID() throws JMSException 278 { 279 return momMsg.getIdentifier(); 280 } 281 282 287 public int getJMSPriority() throws JMSException 288 { 289 return momMsg.getPriority(); 290 } 291 292 297 public int getJMSDeliveryMode() throws JMSException 298 { 299 if (momMsg.getPersistent()) 300 return DeliveryMode.PERSISTENT; 301 else 302 return DeliveryMode.NON_PERSISTENT; 303 } 304 305 310 public Destination getJMSDestination() throws JMSException 311 { 312 String id = momMsg.getDestinationId(); 313 boolean queue = momMsg.toQueue(); 314 Object temporaryValue = null; 315 boolean temporary = false; 316 317 try { 318 temporaryValue = momMsg.getOptionalHeader("JMSTempDestination"); 319 temporary = ConversionHelper.toBoolean(temporaryValue); 320 } 321 catch (Exception exc) {} 324 325 if (queue) { 326 if (temporary) 327 return new TemporaryQueue(id, null); 328 else 329 return new Queue(id); 330 } 331 else { 332 if (temporary) 333 return new TemporaryTopic(id, null); 334 else 335 return new Topic(id); 336 } 337 } 338 339 344 public long getJMSExpiration() throws JMSException 345 { 346 return momMsg.getExpiration(); 347 } 348 349 354 public boolean getJMSRedelivered() throws JMSException 355 { 356 return momMsg.denied; 357 } 358 359 364 public Destination getJMSReplyTo() throws JMSException 365 { 366 String id = momMsg.getReplyToId(); 367 boolean queue = momMsg.replyToQueue(); 368 Object temporaryValue = null; 369 boolean temporary = false; 370 371 if (id == null) 372 return null; 373 374 try { 375 temporaryValue = momMsg.getOptionalHeader("JMSTempReplyTo"); 376 temporary = ConversionHelper.toBoolean(temporaryValue); 377 } 378 catch (Exception exc) {} 381 382 if (queue) { 383 if (temporary) 384 return new TemporaryQueue(id, null); 385 else 386 return new Queue(id); 387 } 388 else { 389 if (temporary) 390 return new TemporaryTopic(id, null); 391 else 392 return new Topic(id); 393 } 394 } 395 396 401 public long getJMSTimestamp() throws JMSException 402 { 403 return momMsg.getTimestamp(); 404 } 405 406 411 public String getJMSType() throws JMSException 412 { 413 Object value = momMsg.getOptionalHeader("JMSType"); 414 return ConversionHelper.toString(value); 415 } 416 417 422 public String getJMSCorrelationID() throws JMSException 423 { 424 return momMsg.getCorrelationId(); 425 } 426 427 433 public byte[] getJMSCorrelationIDAsBytes() throws JMSException 434 { 435 try { 436 return ConversionHelper.toBytes(momMsg.getCorrelationId()); 437 } 438 catch (MessageValueException mE) { 439 throw new MessageFormatException(mE.getMessage()); 440 } 441 } 442 443 449 public void setBooleanProperty(String name, boolean value) 450 throws JMSException 451 { 452 doSetProperty(name, new Boolean (value)); 453 } 454 455 461 public void setByteProperty(String name, byte value) throws JMSException 462 { 463 doSetProperty(name, new Byte (value)); 464 } 465 466 472 477 483 488 494 public void setIntProperty(String name, int value) throws JMSException 495 { 496 doSetProperty(name, new Integer (value)); 497 } 498 499 505 public void setLongProperty(String name, long value) throws JMSException 506 { 507 doSetProperty(name, new Long (value)); 508 } 509 510 517 public void setObjectProperty(String name, Object value) throws JMSException 518 { 519 doSetProperty(name, value); 520 } 521 522 528 public void setShortProperty(String name, short value) throws JMSException 529 { 530 doSetProperty(name, new Short (value)); 531 } 532 533 539 public void setStringProperty(String name, String value) throws JMSException 540 { 541 doSetProperty(name, value); 542 } 543 544 545 551 public boolean getBooleanProperty(String name) throws JMSException 552 { 553 try { 554 return ConversionHelper.toBoolean(doGetProperty(name)); 555 } 556 catch (MessageValueException mE) { 557 throw new MessageFormatException(mE.getMessage()); 558 } 559 } 560 561 567 public byte getByteProperty(String name) throws JMSException 568 { 569 try { 570 return ConversionHelper.toByte(doGetProperty(name)); 571 } 572 catch (MessageValueException mE) { 573 throw new MessageFormatException(mE.getMessage()); 574 } 575 } 576 577 583 593 599 609 615 public int getIntProperty(String name) throws JMSException 616 { 617 try { 618 return ConversionHelper.toInt(doGetProperty(name)); 619 } 620 catch (MessageValueException mE) { 621 throw new MessageFormatException(mE.getMessage()); 622 } 623 } 624 625 631 public long getLongProperty(String name) throws JMSException 632 { 633 try { 634 return ConversionHelper.toLong(doGetProperty(name)); 635 } 636 catch (MessageValueException mE) { 637 throw new MessageFormatException(mE.getMessage()); 638 } 639 } 640 641 646 public Object getObjectProperty(String name) throws JMSException 647 { 648 return doGetProperty(name); 649 } 650 651 657 public short getShortProperty(String name) throws JMSException 658 { 659 try { 660 return ConversionHelper.toShort(doGetProperty(name)); 661 } 662 catch (MessageValueException mE) { 663 throw new MessageFormatException(mE.getMessage()); 664 } 665 } 666 667 672 public String getStringProperty(String name) throws JMSException 673 { 674 return ConversionHelper.toString(doGetProperty(name)); 675 } 676 677 688 private void doSetProperty(String name, Object value) throws JMSException 689 { 690 if (name == null || name.equals("")) 691 throw new IllegalArgumentException ("Invalid property name: " + name); 692 693 String upName = name.toUpperCase(); 694 695 try { 696 if (name.startsWith("JMSX")) { 697 if (name.equals("JMSXGroupID")) 698 momMsg.setOptionalHeader(name, ConversionHelper.toString(value)); 699 else if (name.equals("JMSXGroupSeq")) 700 momMsg.setOptionalHeader(name, 701 new Integer (ConversionHelper.toInt(value))); 702 else 703 throw new JMSException("Property names with prefix 'JMSX' are" 704 + " reserved."); 705 } 706 else if (name.startsWith("JMS_")) 707 throw new JMSException("Property names with prefix 'JMS_' are" 708 + " reserved."); 709 else if (name.startsWith("JMS")) 710 throw new JMSException("Property names with prefix 'JMS' are" 711 + " reserved."); 712 else if (upName.equals("NULL") 713 || upName.equals("TRUE") 714 || upName.equals("FALSE") 715 || upName.equals("NOT") 716 || upName.equals("AND") 717 || upName.equals("OR") 718 || upName.equals("BETWEEN") 719 || upName.equals("LIKE") 720 || upName.equals("IN") 721 || upName.equals("IS") 722 || upName.equals("ESCAPE")) 723 throw new JMSException("Invalid property name: " + name + " is a" 724 + " SQL terminal."); 725 else 726 momMsg.setObjectProperty(name, value); 727 } 728 catch (MessageException mE) { 729 if (mE instanceof MessageValueException) 730 throw new MessageFormatException(mE.getMessage()); 731 if (mE instanceof MessageROException) 732 throw new MessageNotWriteableException(mE.getMessage()); 733 } 734 } 735 736 741 private Object doGetProperty(String name) 742 { 743 if (name == null || name.equals("")) 744 throw new IllegalArgumentException ("Invalid property name: " + name); 745 746 Object value = null; 747 748 if (name.startsWith("JMSX")) { 749 if (name.equals("JMSXDeliveryCount")) 750 value = new Integer (momMsg.deliveryCount); 751 else 752 value = momMsg.getOptionalHeader(name); 753 } 754 else if (name.startsWith("JMS_JORAM")) { 755 if (name.equals("JMS_JORAM_DELETEDDEST")) 756 value = new Boolean (momMsg.deletedDest); 757 else if (name.equals("JMS_JORAM_NOTWRITABLE")) 758 value = new Boolean (momMsg.notWriteable); 759 else if (name.equals("JMS_JORAM_EXPIRED")) 760 value = new Boolean (momMsg.expired); 761 else if (name.equals("JMS_JORAM_UNDELIVERABLE")) 762 value = new Boolean (momMsg.undeliverable); 763 } 764 else 765 value = momMsg.getObjectProperty(name); 766 767 return value; 768 } 769 770 771 777 com.scalagent.kjoram.messages.Message getMomMessage() 778 throws MessageFormatException 779 { 780 try { 781 prepare(); 782 return momMsg; 783 } 784 catch (Exception e) { 785 MessageFormatException jE = 786 new MessageFormatException("The message body could not be" 787 + " serialized."); 788 jE.setLinkedException(e); 789 throw jE; 790 } 791 } 792 793 801 static Message 802 wrapMomMessage(Session sess, com.scalagent.kjoram.messages.Message momMsg) 803 throws JMSException 804 { 805 Message msg = null; 806 807 if (momMsg.getType() == MessageType.SIMPLE) 808 msg = new Message(sess, momMsg); 809 else if (momMsg.getType() == MessageType.TEXT) 810 msg = new TextMessage(sess, momMsg); 811 else if (momMsg.getType() == MessageType.MAP) 812 msg = new MapMessage(sess, momMsg); 813 else if (momMsg.getType() == MessageType.BYTES) 814 msg = new BytesMessage(sess, momMsg); 815 816 return msg; 817 } 818 819 824 static Message convertJMSMessage(Message jmsMsg) 825 throws JMSException 826 { 827 Message msg = null; 828 if (jmsMsg instanceof TextMessage) { 829 msg = new TextMessage(); 830 ((TextMessage) msg).setText(((TextMessage)jmsMsg).getText()); 831 } 832 else if (jmsMsg instanceof BytesMessage) { 833 msg = new BytesMessage(); 834 try { 835 while (true) 836 ((BytesMessage) msg).writeByte(((BytesMessage)jmsMsg).readByte()); 837 } 838 catch (MessageEOFException mE) {} 839 } 840 else if (jmsMsg instanceof MapMessage) { 841 msg = new MapMessage(); 842 Enumeration mapNames = ((MapMessage) jmsMsg).getMapNames(); 843 } 844 else 845 msg = new Message(); 846 847 msg.setJMSCorrelationID(jmsMsg.getJMSCorrelationID()); 848 msg.setJMSReplyTo(jmsMsg.getJMSReplyTo()); 849 msg.setJMSType(jmsMsg.getJMSType()); 850 851 return msg; 852 } 853 854 860 protected void prepare() throws Exception 861 { 862 momMsg.denied = false; 863 momMsg.deletedDest = false; 864 momMsg.expired = false; 865 momMsg.notWriteable = false; 866 momMsg.undeliverable = false; 867 } 868 } 869 | Popular Tags |