1 23 package org.objectweb.joram.client.jms; 24 25 import java.io.Serializable ; 26 import java.io.InputStream ; 27 import java.io.ObjectInputStream ; 28 import java.io.OutputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.io.IOException ; 31 32 import java.util.*; 33 34 import javax.jms.JMSException ; 35 import javax.jms.IllegalStateException ; 36 import javax.jms.MessageEOFException ; 37 import javax.jms.MessageFormatException ; 38 import javax.jms.MessageNotWriteableException ; 39 40 import org.objectweb.joram.shared.stream.StreamUtil; 41 import org.objectweb.joram.shared.messages.ConversionHelper; 42 import org.objectweb.joram.shared.excepts.MessageValueException; 43 44 import org.objectweb.util.monolog.api.BasicLevel; 45 import org.objectweb.joram.shared.JoramTracing; 46 47 53 public class Message implements javax.jms.Message { 54 org.objectweb.joram.shared.messages.Message momMsg; 55 56 59 Message() { 60 momMsg = new org.objectweb.joram.shared.messages.Message(); 61 } 62 63 public static Message wrapMomMessage(Session session, 64 org.objectweb.joram.shared.messages.Message momMsg) throws JMSException { 65 switch (momMsg.type) { 66 case org.objectweb.joram.shared.messages.Message.SIMPLE: 67 return new Message(session, momMsg); 68 case org.objectweb.joram.shared.messages.Message.TEXT: 69 return new TextMessage(session, momMsg); 70 case org.objectweb.joram.shared.messages.Message.OBJECT: 71 return new ObjectMessage(session, momMsg); 72 case org.objectweb.joram.shared.messages.Message.MAP: 73 return new MapMessage(session, momMsg); 74 case org.objectweb.joram.shared.messages.Message.STREAM: 75 return new StreamMessage(session, momMsg); 76 case org.objectweb.joram.shared.messages.Message.BYTES: 77 return new BytesMessage(session, momMsg); 78 default: 79 throw new JMSException ("Unknow message type: " + momMsg.type); 80 } 81 } 82 83 87 protected transient Session session = null; 88 89 93 protected transient javax.jms.Destination jmsDest = null; 94 95 102 Message(Session session, 103 org.objectweb.joram.shared.messages.Message momMsg) { 104 this.session = session; 105 this.momMsg = momMsg; 106 setReadOnly(); 107 } 108 109 116 public void acknowledge() throws JMSException { 117 if ((session == null) || 118 session.getTransacted() || 119 (session.getAcknowledgeMode() != javax.jms.Session.CLIENT_ACKNOWLEDGE)) 120 return; 121 session.acknowledge(); 122 } 123 124 125 protected boolean RObody = false; 126 127 132 public void clearBody() throws JMSException { 133 momMsg.body = null; 134 RObody = false; 135 } 136 137 138 public void setReadOnly() { 139 propertiesRO = true; 140 RObody = true; 141 } 142 143 149 public final String getJMSMessageID() throws JMSException { 150 return momMsg.id; 151 } 152 153 158 public final void setJMSMessageID(String id) throws JMSException { 159 momMsg.id = id; 160 } 161 162 168 public final int getJMSPriority() throws JMSException { 169 return momMsg.priority; 170 } 171 172 177 public final void setJMSPriority(int priority) throws JMSException { 178 if (priority >= 0 && priority <= 9) 179 momMsg.priority = priority; 180 else 181 throw new JMSException ("Priority of "+ priority +" is not valid" 182 + " (should be an integer between 0 and 9)."); 183 } 184 185 193 public final javax.jms.Destination getJMSDestination() throws JMSException { 194 if (jmsDest != null) return jmsDest; 195 196 if (momMsg.toId != null) { 197 try { 198 return Destination.newInstance(momMsg.toId, null, momMsg.toType); 199 } catch (Exception exc) { 200 throw new JMSException (exc.getMessage()); 202 } 203 } 204 return null; 205 } 206 207 215 public final void setJMSDestination(javax.jms.Destination dest) throws JMSException { 216 jmsDest = dest; 217 if (dest == null) { 218 momMsg.setDestination(null, null); 219 } else if (dest instanceof org.objectweb.joram.client.jms.Destination) { 220 Destination d = (org.objectweb.joram.client.jms.Destination) dest; 221 momMsg.toId = d.getName(); 222 momMsg.toType = d.getType(); 223 } 224 } 225 226 232 public final long getJMSExpiration() throws JMSException { 233 return momMsg.expiration; 234 } 235 236 241 public final void setJMSExpiration(long expiration) throws JMSException { 242 if (expiration >= 0) 243 momMsg.expiration = expiration; 244 } 245 246 252 public final boolean getJMSRedelivered() throws JMSException { 253 return momMsg.redelivered; 254 } 255 256 261 public final void setJMSRedelivered(boolean redelivered) throws JMSException { 262 momMsg.redelivered = redelivered; 263 } 264 265 272 public final javax.jms.Destination getJMSReplyTo() throws JMSException { 273 if (momMsg.replyToId != null) { 274 try { 276 return Destination.newInstance(momMsg.replyToId, null, momMsg.replyToType); 277 } catch (Exception exc) { 278 throw new JMSException (exc.getMessage()); 279 } 280 } 281 return null; 282 } 283 284 289 public final void setJMSReplyTo(javax.jms.Destination replyTo) throws JMSException { 290 try { 291 Destination d = (org.objectweb.joram.client.jms.Destination) replyTo; 292 momMsg.replyToId = d.getName(); 293 momMsg.replyToType = d.getType(); 294 } catch (NullPointerException npe) { 295 momMsg.replyToId = null; 296 momMsg.replyToType = null; 297 } catch (ClassCastException cce) { 298 throw new JMSException ("Destination is not Joram compatible."); 299 } 300 } 301 302 308 public final long getJMSTimestamp() throws JMSException { 309 return momMsg.timestamp; 310 } 311 312 317 public final void setJMSTimestamp(long timestamp) throws JMSException { 318 momMsg.timestamp = timestamp; 319 } 320 321 327 public final String getJMSCorrelationID() throws JMSException { 328 return momMsg.correlationId; 329 } 330 331 336 public final void setJMSCorrelationID(String correlationID) throws JMSException { 337 momMsg.correlationId = correlationID; 338 } 339 340 346 public final byte[] getJMSCorrelationIDAsBytes() throws JMSException { 347 try { 348 return ConversionHelper.toBytes(momMsg.correlationId); 349 } catch (MessageValueException mE) { 350 throw new MessageFormatException (mE.getMessage()); 351 } 352 } 353 354 359 public final void setJMSCorrelationIDAsBytes(byte[] correlationID) { 360 momMsg.correlationId = ConversionHelper.toString(correlationID); 361 } 362 363 369 public final int getJMSDeliveryMode() throws JMSException { 370 if (momMsg.persistent) 371 return javax.jms.DeliveryMode.PERSISTENT; 372 else 373 return javax.jms.DeliveryMode.NON_PERSISTENT; 374 } 375 376 381 public final void setJMSDeliveryMode(int deliveryMode) throws JMSException { 382 if (deliveryMode != javax.jms.DeliveryMode.PERSISTENT && 383 deliveryMode != javax.jms.DeliveryMode.NON_PERSISTENT) 384 throw new JMSException ("Invalid delivery mode."); 385 386 momMsg.persistent = (deliveryMode == javax.jms.DeliveryMode.PERSISTENT); 387 } 388 389 394 public final String getJMSType() throws JMSException { 395 return ConversionHelper.toString(momMsg.getOptionalHeader("JMSType")); 396 } 397 398 403 public final void setJMSType(String type) throws JMSException { 404 momMsg.setOptionalHeader("JMSType", type); 405 } 406 407 412 public void getOptionalHeader(Hashtable h) { 413 if (momMsg.optionalHeader == null) return; 414 momMsg.optionalHeader.copyInto(h); 415 } 416 417 421 422 public boolean propertiesRO = false; 423 424 429 public final void clearProperties() throws JMSException { 430 propertiesRO = false; 431 if (momMsg.properties == null) return; 432 433 momMsg.properties.clear(); 434 momMsg.properties = null; 435 } 436 437 443 public final void resetPropertiesRO() throws JMSException { 444 propertiesRO = false; 445 } 446 447 454 public final boolean propertyExists(String name) throws JMSException { 455 if (momMsg.properties == null) 456 return false; 457 458 return momMsg.properties.containsKey(name); 459 } 460 461 466 public void getProperties(Hashtable h) { 467 if (momMsg.properties == null) return; 468 momMsg.properties.copyInto(h); 469 } 470 471 476 public final Enumeration getPropertyNames() throws JMSException { 477 if (momMsg.properties == null) 478 return (new Hashtable()).keys(); 479 480 return momMsg.properties.keys(); 481 } 482 483 492 public final void setBooleanProperty(String name, boolean value) throws JMSException { 493 doSetProperty(name, new Boolean (value)); 494 } 495 496 505 public final void setByteProperty(String name, byte value) throws JMSException { 506 doSetProperty(name, new Byte (value)); 507 } 508 509 518 public final void setShortProperty(String name, short value) throws JMSException { 519 doSetProperty(name, new Short (value)); 520 } 521 522 531 public final void setIntProperty(String name, int value) throws JMSException { 532 doSetProperty(name, new Integer (value)); 533 } 534 535 544 public final void setLongProperty(String name, long value) throws JMSException { 545 doSetProperty(name, new Long (value)); 546 } 547 548 557 public final void setFloatProperty(String name, float value) throws JMSException { 558 doSetProperty(name, new Float (value)); 559 } 560 561 570 public final void setDoubleProperty(String name, double value) throws JMSException { 571 doSetProperty(name, new Double (value)); 572 } 573 574 583 public final void setStringProperty(String name, String value) throws JMSException { 584 doSetProperty(name, value); 585 } 586 587 605 public final void setObjectProperty(String name, Object value) throws JMSException { 606 if (value instanceof Boolean || 607 value instanceof Number || 608 value instanceof String ) { 609 doSetProperty(name, value); 610 } else { 611 throw new MessageFormatException ("Can't set non primitive Java object as a property value."); 612 } 613 } 614 615 626 private final void doSetProperty(String name, Object value) throws JMSException { 627 if (name == null || name.equals("")) 628 throw new IllegalArgumentException ("Invalid property name: " + name); 629 630 if (name.startsWith("JMSX")) { 631 if (name.equals("JMSXGroupID")) { 632 momMsg.setOptionalHeader(name, ConversionHelper.toString(value)); 633 } else if (name.equals("JMSXGroupSeq")) { 634 try { 635 momMsg.setOptionalHeader(name, 636 new Integer (ConversionHelper.toInt(value))); 637 } catch (MessageValueException mE) { 638 throw new MessageFormatException (mE.getMessage()); 639 } 640 } else { 641 throw new JMSException ("Property names with prefix 'JMSX' are reserved."); 642 } 643 } else if (name.startsWith("JMS")) { 644 throw new JMSException ("Property names with prefix 'JMS' are reserved."); 645 } else if (name.equalsIgnoreCase("NULL") || 646 name.equalsIgnoreCase("TRUE") || 647 name.equalsIgnoreCase("FALSE") || 648 name.equalsIgnoreCase("NOT") || 649 name.equalsIgnoreCase("AND") || 650 name.equalsIgnoreCase("OR") || 651 name.equalsIgnoreCase("BETWEEN") || 652 name.equalsIgnoreCase("LIKE") || 653 name.equalsIgnoreCase("IN") || 654 name.equalsIgnoreCase("IS") || 655 name.equalsIgnoreCase("ESCAPE")) { 656 throw new JMSException ("Invalid property name cannot use SQL terminal: " + name); 657 } else { 658 if (propertiesRO) 659 throw new MessageNotWriteableException ("Can't set property as the message properties are READ-ONLY."); 660 661 momMsg.setProperty(name, value); 662 } 663 } 664 665 673 public final boolean getBooleanProperty(String name) throws JMSException { 674 try { 675 return ConversionHelper.toBoolean(doGetProperty(name)); 676 } catch (MessageValueException mE) { 677 throw new MessageFormatException (mE.getMessage()); 678 } 679 } 680 681 689 public final byte getByteProperty(String name) throws JMSException { 690 try { 691 return ConversionHelper.toByte(doGetProperty(name)); 692 } catch (MessageValueException mE) { 693 throw new MessageFormatException (mE.getMessage()); 694 } 695 } 696 697 705 public final short getShortProperty(String name) throws JMSException { 706 try { 707 return ConversionHelper.toShort(doGetProperty(name)); 708 } catch (MessageValueException mE) { 709 throw new MessageFormatException (mE.getMessage()); 710 } 711 } 712 713 721 public final int getIntProperty(String name) throws JMSException { 722 try { 723 return ConversionHelper.toInt(doGetProperty(name)); 724 } catch (MessageValueException mE) { 725 throw new MessageFormatException (mE.getMessage()); 726 } 727 } 728 729 737 public final long getLongProperty(String name) throws JMSException { 738 try { 739 return ConversionHelper.toLong(doGetProperty(name)); 740 } catch (MessageValueException mE) { 741 throw new MessageFormatException (mE.getMessage()); 742 } 743 } 744 745 753 public final float getFloatProperty(String name) throws JMSException { 754 try { 755 return ConversionHelper.toFloat(doGetProperty(name)); 756 } catch (MessageValueException mE) { 757 throw new MessageFormatException (mE.getMessage()); 758 } 759 } 760 761 769 public final double getDoubleProperty(String name) throws JMSException { 770 try { 771 return ConversionHelper.toDouble(doGetProperty(name)); 772 } catch (MessageValueException mE) { 773 throw new MessageFormatException (mE.getMessage()); 774 } 775 } 776 777 785 public final String getStringProperty(String name) throws JMSException { 786 return ConversionHelper.toString(doGetProperty(name)); 787 } 788 789 796 public final Object getObjectProperty(String name) throws JMSException { 797 return doGetProperty(name); 798 } 799 800 805 private final Object doGetProperty(String name) { 806 if (name == null || name.equals("")) 807 throw new IllegalArgumentException ("Invalid property name: " + name); 808 809 if (name.startsWith("JMSX")) { 810 if (name.equals("JMSXDeliveryCount")) 811 return new Integer (momMsg.deliveryCount); 812 else 813 return momMsg.getOptionalHeader(name); 814 } else if (name.startsWith("JMS_JORAM")) { 815 if (name.equals("JMS_JORAM_DELETEDDEST")) 816 return new Boolean (momMsg.deletedDest); 817 else if (name.equals("JMS_JORAM_NOTWRITABLE")) 818 return new Boolean (momMsg.notWriteable); 819 else if (name.equals("JMS_JORAM_EXPIRED")) 820 return new Boolean (momMsg.expired); 821 else if (name.equals("JMS_JORAM_UNDELIVERABLE")) 822 return new Boolean (momMsg.undeliverable); 823 } else { 824 return momMsg.getProperty(name); 825 } 826 return null; 827 } 828 829 834 static public Message convertJMSMessage(javax.jms.Message jmsMsg) throws JMSException { 835 Message joramMsg = null; 836 if (jmsMsg instanceof javax.jms.TextMessage ) { 837 joramMsg = new TextMessage(); 838 ((javax.jms.TextMessage ) joramMsg).setText(((javax.jms.TextMessage ) jmsMsg).getText()); 839 } else if (jmsMsg instanceof javax.jms.ObjectMessage ) { 840 joramMsg = new ObjectMessage(); 841 ((javax.jms.ObjectMessage ) joramMsg).setObject(((javax.jms.ObjectMessage ) jmsMsg).getObject()); 842 } else if (jmsMsg instanceof javax.jms.StreamMessage ) { 843 joramMsg = new StreamMessage(); 844 try { 845 ((javax.jms.StreamMessage ) jmsMsg).reset(); 846 while (true) 847 ((StreamMessage) joramMsg).writeObject(((javax.jms.StreamMessage ) jmsMsg).readObject()); 848 } catch (Exception mE) {} 849 } else if (jmsMsg instanceof javax.jms.BytesMessage ) { 850 joramMsg = new BytesMessage(); 851 try { 852 ((javax.jms.BytesMessage ) jmsMsg).reset(); 853 while (true) 854 ((BytesMessage) joramMsg).writeByte(((javax.jms.BytesMessage ) jmsMsg).readByte()); 855 } catch (Exception mE) {} 856 } else if (jmsMsg instanceof javax.jms.MapMessage ) { 857 joramMsg = new MapMessage(); 858 Enumeration mapNames = ((javax.jms.MapMessage ) jmsMsg).getMapNames(); 859 String mapName; 860 while (mapNames.hasMoreElements()) { 861 mapName = (String ) mapNames.nextElement(); 862 ((javax.jms.MapMessage ) joramMsg).setObject(mapName, 863 ((javax.jms.MapMessage ) 864 jmsMsg).getObject(mapName)); 865 } 866 } else { 867 joramMsg = new Message(); 868 } 869 870 joramMsg.setJMSDestination(jmsMsg.getJMSDestination()); 871 joramMsg.setJMSCorrelationID(jmsMsg.getJMSCorrelationID()); 872 joramMsg.setJMSReplyTo(jmsMsg.getJMSReplyTo()); 873 joramMsg.setJMSType(jmsMsg.getJMSType()); 874 joramMsg.setJMSMessageID(jmsMsg.getJMSMessageID()); 875 876 Enumeration names = jmsMsg.getPropertyNames(); 877 if (names != null) { 878 String name; 879 while (names.hasMoreElements()) { 880 name = (String ) names.nextElement(); 881 try { 882 joramMsg.setObjectProperty(name, jmsMsg.getObjectProperty(name)); 883 } catch (JMSException e) { 884 if (! name.startsWith("JMSX") && ! name.startsWith("JMS_")) 886 throw e; 887 } 888 } 889 } 890 891 return joramMsg; 892 } 893 894 900 protected void prepare() throws JMSException { 901 momMsg.redelivered = false; 902 momMsg.deletedDest = false; 903 momMsg.expired = false; 904 momMsg.notWriteable = false; 905 momMsg.undeliverable = false; 906 } 907 908 909 912 public org.objectweb.joram.shared.messages.Message getMomMsg() { 913 return momMsg; 914 } 915 916 public void toString(StringBuffer strbuf) { 917 try { 918 strbuf.append('('); 919 strbuf.append(super.toString()); 920 strbuf.append(",JMSMessageID=").append(getJMSMessageID()); 921 try { 922 strbuf.append(",JMSDestination=").append(getJMSDestination()); 923 } catch (JMSException exc) { 924 JoramTracing.dbgClient.log(BasicLevel.ERROR, "Message.toString()", exc); 925 } 926 strbuf.append(",JMSCorrelationID=").append(getJMSCorrelationID()); 927 strbuf.append(",JMSDeliveryMode=").append(getJMSDeliveryMode()); 928 strbuf.append(",JMSExpiration=").append(getJMSExpiration()); 929 strbuf.append(",JMSPriority=").append(getJMSPriority()); 930 strbuf.append(",JMSRedelivered=").append(getJMSRedelivered()); 931 try { 932 strbuf.append(",JMSReplyTo=").append(getJMSReplyTo()); 933 } catch (JMSException exc) { 934 JoramTracing.dbgClient.log(BasicLevel.ERROR, "Message.toString()", exc); 935 } 936 strbuf.append(",JMSTimestamp=").append(getJMSTimestamp()); 937 strbuf.append(",JMSType=").append(getJMSType()); 938 strbuf.append(')'); 939 } catch (JMSException exc) { 940 } 942 } 943 } 944 | Popular Tags |