1 46 package org.mr.api.jms; 47 48 import java.util.Enumeration ; 49 import java.util.HashMap ; 50 import java.util.Hashtable ; 51 import java.util.Iterator ; 52 import java.util.Map ; 53 54 55 import javax.jms.Destination ; 56 import javax.jms.JMSException ; 57 import javax.jms.Message ; 58 import javax.jms.MessageFormatException ; 59 import javax.jms.MessageNotWriteableException ; 60 61 import java.io.IOException ; 62 import java.io.Serializable ; 63 64 import org.mr.core.util.byteable.Byteable; 65 import org.mr.core.util.byteable.ByteableInputStream; 66 import org.mr.core.util.byteable.ByteableOutputStream; 67 import org.mr.core.util.byteable.ByteableRegistry; 68 69 70 71 102 public class MantaMessage implements Serializable , Byteable, Message { 103 104 String JMSMessageId; 105 long JMSTimeStamp; 106 long JMSExpiration; 107 String JMSCorrelationId=null; 108 Destination JMSReplyTo, JMSDestination; 109 protected String connId; 110 111 112 protected MantaSession creatingSession = null; 113 protected Map properties = null; 114 protected long timeAccepted; 115 116 117 public static final int NON_PERSISTENT = 1; 119 public static final int PERSISTENT = 2; 120 public static final int MIN_PRIORITY = 0; 121 public static final int MAX_PRIORITY = 9; 122 protected String originalClient; 123 124 126 139 int flags; 140 final static int IS_PERSISTENT = 1<<4; final static int IS_REDELIVERED = 1<<5; final static int IS_WRITEABLE = 1<<6; final static int MANTA_MESSAGE = 0x00000000; final static int BYTES_MESSAGE = 0x00000100; final static int OBJECT_MESSAGE = 0x00000200; final static int MAP_MESSAGE = 0x00000300; final static int STREAM_MESSAGE = 0x00000400; final static int TEXT_MESSAGE = 0x00000500; 151 final static int ONLY_MSG_TYPES = 0x0FFFF0FF; 153 final static int PROPERTIES_WRITEABLE = 0x00010000; final static int PROPERTIES_NOT_WRITEABLE = 0x0FFEFFFF; 155 final static int ORIG_PROPERTIES_SAVED = 0x00020000; 156 final static int ORIG_BODY_SAVED = 0x00040000; 157 158 final static int REVERSE_WRITEABLE = 0xFFFFFFBF; final static int PRIORITY_GETTER = 0x0000000F; final static String BYT_M = "BytesMessage"; 161 final static String MAP_M = "MapMessage"; 162 final static String OBJ_M = "ObjectMessage"; 163 final static String REG_M = "Message"; 164 final static String STR_M = "StreamMessage"; 165 final static String TXT_M = "TextMessage"; 166 final static String [] MESSAGE_TYPES = new String [] { 167 REG_M, BYT_M, OBJ_M, MAP_M, STR_M,TXT_M 168 }; 169 170 final static byte BYTE = 1; 172 final static byte BOOLEAN = 2; 173 final static byte SHORT = 3; 174 final static byte INTEGER = 4; 175 final static byte DOUBLE = 5; 176 final static byte FLOAT = 6; 177 final static byte STRING = 7; 178 final static byte LONG = 8; 179 final static byte BYTEARR = 9; 180 final static byte CHAR = 10; 181 182 187 MantaMessage(){ 188 properties = new Hashtable (); 189 flags = 0 | IS_WRITEABLE | PROPERTIES_WRITEABLE | 190 DEFAULT_PRIORITY | IS_PERSISTENT; 192 } 194 MantaMessage(MantaSession sess) { 196 this(); 197 creatingSession = sess; 198 } 199 200 204 public final String getClientId() { 205 return originalClient; 206 } 208 209 210 215 public final void setClientId(String clientId) { 216 originalClient = clientId; 217 } 219 220 223 public final String getJMSMessageID() throws JMSException { 224 return JMSMessageId; 225 } 227 230 public final void setJMSMessageID(String id) throws JMSException { 231 232 JMSMessageId = id; 233 } 235 238 public final long getJMSTimestamp() throws JMSException { 239 return JMSTimeStamp; 240 } 242 243 246 public final void setJMSTimestamp(long timestamp) throws JMSException { 247 JMSTimeStamp = timestamp; 248 } 250 251 256 257 public final void setTimeAccepted(long acTime) { 258 timeAccepted = acTime; 259 } 261 266 public final long getTimeAccepted() { 267 return timeAccepted; 268 } 270 271 276 public final byte[] getJMSCorrelationIDAsBytes() throws JMSException { 277 278 if (JMSCorrelationId!=null) 279 return JMSCorrelationId.getBytes(); 280 else 281 return null; 282 } 284 285 288 public final void setJMSCorrelationIDAsBytes(byte[] correlationIDAsBytes) throws JMSException { 289 291 JMSCorrelationId = new String (correlationIDAsBytes); 292 } 294 297 298 public final void setJMSCorrelationID(String correlationID) throws JMSException { 299 JMSCorrelationId = correlationID; 300 } 302 303 306 public final String getJMSCorrelationID() throws JMSException { 307 308 return JMSCorrelationId; 309 } 311 312 316 public final Destination getJMSReplyTo() throws JMSException { 317 318 return JMSReplyTo; 319 } 321 322 326 public final void setJMSReplyTo(Destination replyTo) throws JMSException { 327 329 JMSReplyTo = replyTo; 330 331 } 333 334 337 public final Destination getJMSDestination() throws JMSException { 338 return JMSDestination; 339 } 341 342 345 public final void setJMSDestination(Destination destination) throws JMSException { 346 JMSDestination = destination; 348 349 } 351 352 355 public final int getJMSDeliveryMode() throws JMSException { 356 357 return ((flags & IS_PERSISTENT) >>4) + 1; 358 } 360 363 public final void setJMSDeliveryMode(int deliveryMode) throws JMSException { 364 flags = (flags & 0x0FFFFFEF) | ((deliveryMode-1)<<4); 366 367 } 369 370 373 public final boolean getJMSRedelivered() throws JMSException { 374 return ((flags & IS_REDELIVERED) > 0); 375 } 377 380 public final void setJMSRedelivered(boolean redelivered) throws JMSException { 381 checkWriteableState(false); 384 } 386 387 391 public final String getJMSType() throws JMSException { 392 int fl = (flags&0x00000F00) >> 8; 393 return MESSAGE_TYPES[fl]; 394 } 396 400 public final void setJMSType(String type) throws JMSException { 401 checkWriteableState(false); 402 } 405 406 409 public final long getJMSExpiration() throws JMSException { 410 411 return JMSExpiration; 412 413 } 415 418 public final void setJMSExpiration(long expiration) throws JMSException { 419 this.JMSExpiration = expiration; 421 } 423 426 public final int getJMSPriority() throws JMSException { 427 428 return PRIORITY_GETTER & flags; 429 430 } 432 435 public final void setJMSPriority(int priority) throws JMSException { 436 if (priority>MAX_PRIORITY||priority<0) 437 438 throw new JMSException ("MNJMS0004E : FAILED IN METHOD setJMSPriority(). PRIORITY "+priority+" OUT OF RANGE."); 439 440 flags = flags>>4<<4 | priority; 441 } 443 446 447 public void clearProperties() throws JMSException { 448 449 properties.clear(); 461 462 flags=flags|PROPERTIES_WRITEABLE; 463 } 465 467 473 478 public final boolean propertyExists(String name) throws JMSException { 479 480 if (name!=null && name.length()>0) 481 return properties.containsKey(name); 482 else 483 return false; 484 } 486 487 490 public final boolean getBooleanProperty(String name) throws JMSException { 491 checkPropertyName(name); 492 Object o = properties.get(name); 493 if (o instanceof Boolean ) 494 return ((Boolean )o).booleanValue(); 495 else if (o==null ||o instanceof String ) 496 return Boolean.valueOf((String )o).booleanValue(); 497 else 498 throw new MessageFormatException ("MNJMS0004F : FAILED ON METHOD getBooleanProperty(). CONVERSION TO BOOLEAN FROM "+o.getClass().getName()+" IS INVALID."); 499 } 501 504 public final byte getByteProperty(String name) throws JMSException { 505 506 checkPropertyName(name); 507 Object o = properties.get(name); 508 if (o instanceof Byte ) 509 return ((Number )o).byteValue(); 510 else if ( o == null || o instanceof String ) 511 try { 512 return Byte.valueOf((String )o).byteValue(); 513 } 514 catch (NumberFormatException nfe) { 515 throw new MessageFormatException ("MNJMS00050 : FAILED ON METHOD getByteProperty(). STRING "+o+" IS INVALID FOR CONVERSION TO BYTE."); 516 } 517 else 518 throw new MessageFormatException ("MNJMS00051 : FAILED ON METHOD getByteProperty(). CONVERSION TO BYTE FROM "+o.getClass().getName()+" IS INVALID."); 519 } 521 524 public final short getShortProperty(String name) throws JMSException { 525 526 checkPropertyName(name); 527 Object o = properties.get(name); 528 if (o instanceof Byte || o instanceof Short ) 529 return ((Number )o).shortValue(); 530 else if (o == null || o instanceof String ) 531 try { 532 return Short.valueOf((String )o).shortValue(); 533 } 534 catch (NumberFormatException nfe) { 535 throw new MessageFormatException ("MNJMS00052 : FAILED ON METHOD getShortProperty(). STRING "+o+" IS INVALID FOR CONVERSION TO SHORT."); 536 } 537 else 538 throw new MessageFormatException ("MNJMS00053 : FAILED ON METHOD getShortProperty(). CONVERSION TO SHORT FROM "+o.getClass().getName()+" IS INVALID."); 539 } 541 544 public final int getIntProperty(String name) throws JMSException { 545 546 checkPropertyName(name); 547 Object o = properties.get(name); 548 if (o instanceof Byte || o instanceof Short || o instanceof Integer ) 549 return ((Number )o).intValue(); 550 else if (o == null ||o instanceof String ) 551 try { 552 return Integer.valueOf((String )o).intValue(); 553 } 554 catch (NumberFormatException nfe) { 555 throw new MessageFormatException ("MNJMS00054 : FAILED ON METHOD getIntProperty(). STRING "+o+" IS INVALID FOR CONVERSION TO INTEGER."); 556 } 557 else 558 throw new MessageFormatException ("MNJMS00055 : FAILED ON METHOD getIntProperty(). CONVERSION TO INTEGER FROM "+o.getClass().getName()+" IS INVALID."); 559 } 561 564 public final long getLongProperty(String name) throws JMSException { 565 566 checkPropertyName(name); 567 Object o = properties.get(name); 568 if (o instanceof Byte || o instanceof Short || o instanceof Integer || o instanceof Long ) 569 return ((Number )o).longValue(); 570 else if ( o == null ||o instanceof String ) 571 try { 572 return Long.valueOf((String )o).longValue(); 573 } 574 catch (NumberFormatException nfe) { 575 throw new MessageFormatException ("MNJMS00056 : FAILED ON METHOD getLongProperty(). STRING "+o+" IS INVALID FOR CONVERSION TO LONG."); 576 } 577 else 578 throw new MessageFormatException ("MNJMS00057 : FAILED ON METHOD getLongProperty(). CONVERSION TO LONG FROM "+o.getClass().getName()+" IS INVALID."); 579 } 581 584 public final float getFloatProperty(String name) throws JMSException { 585 586 checkPropertyName(name); 587 Object o = properties.get(name); 588 if (o instanceof Float ) 589 return ((Number )o).floatValue(); 590 else if ( o == null || o instanceof String ) 591 try { 592 return Float.valueOf((String )o).floatValue(); 593 } 594 catch (NumberFormatException nfe) { 595 throw new MessageFormatException ("MNJMS00058 : FAILED ON METHOD getFloatProperty(). STRING "+o+" IS INVALID FOR CONVERSION TO FLOAT."); 596 } 597 else 598 throw new MessageFormatException ("MNJMS00059 : FAILED ON METHOD getFloatProperty(). CONVERSION TO FLOAT FROM "+o.getClass().getName()+" IS INVALID."); 599 } 601 604 public final double getDoubleProperty(String name) throws JMSException { 605 606 checkPropertyName(name); 607 Object o = properties.get(name); 608 if (o instanceof Float || o instanceof Double ) 609 return ((Number )o).doubleValue(); 610 else if ( o == null |o instanceof String ) 611 try { 612 return Double.valueOf((String )o).doubleValue(); 613 } 614 catch (NumberFormatException nfe) { 615 throw new MessageFormatException ("MNJMS0005A : FAILED ON METHOD getDoubleProperty(). STRING "+o+" IS INVALID FOR CONVERSION TO DOUBLE."); 616 } 617 else 618 throw new MessageFormatException ("MNJMS0005B : FAILED ON METHOD getDoubleProperty(). CONVERSION TO DOUBLE FROM "+o.getClass().getName()+" IS INVALID."); 619 620 } 622 625 public final String getStringProperty(String name) throws JMSException { 626 627 checkPropertyName(name); 628 Object o = properties.get(name); 629 630 if (o != null) 631 return o.toString(); 632 return null; 633 } 635 638 public final Object getObjectProperty(String name) throws JMSException { 639 checkPropertyName(name); 640 return properties.get(name); 641 } 643 646 public final Enumeration getPropertyNames() throws JMSException { 647 648 final Iterator i = properties.keySet().iterator(); 650 651 return new Enumeration () { 653 public boolean hasMoreElements() { 654 return i.hasNext(); 655 } 657 public Object nextElement() { 658 return i.next(); 659 } }; 661 662 } 664 665 protected void checkPropertyName(String name) throws JMSException { 666 if (name==null || name.length()==0) 667 throw new IllegalArgumentException ("MNJMS0005C : ILLEGAL PROPERTY NAME. MAY NOT BE NULL OR EMPTY."); 668 } 669 672 public final void setBooleanProperty(String name, boolean value) throws JMSException { 673 checkWriteableState(true); 674 checkPropertyName(name); 675 properties.put(name, new Boolean (value)); 676 677 } 679 682 public final void setByteProperty(String name,byte value) throws JMSException { 683 checkWriteableState(true); 684 checkPropertyName(name); 685 properties.put(name, new Byte (value)); 686 687 } 689 692 public final void setShortProperty(String name, short value) throws JMSException { 693 checkWriteableState(true); 694 checkPropertyName(name); 695 properties.put(name, new Short (value)); 696 697 } 699 702 public final void setIntProperty(String name,int value) throws JMSException { 703 checkWriteableState(true); 704 checkPropertyName(name); 705 properties.put(name, new Integer (value)); 706 707 } 709 712 public final void setLongProperty(String name, long value) throws JMSException { 713 checkWriteableState(true); 714 checkPropertyName(name); 715 properties.put(name, new Long (value)); 716 717 } 719 722 public final void setFloatProperty(String name,float value) throws JMSException { 723 checkWriteableState(true); 724 checkPropertyName(name); 725 properties.put(name, new Float (value)); 726 727 } 729 732 public final void setDoubleProperty(String name, double value) throws JMSException { 733 checkWriteableState(true); 734 checkPropertyName(name); 735 properties.put(name, new Double (value)); 736 737 } 739 742 public final void setStringProperty(String name, String value) throws JMSException { 743 checkWriteableState(true); 744 checkPropertyName(name); 745 properties.put(name, value); 746 747 } 749 752 public final void setObjectProperty(String name, Object value) throws JMSException { 753 checkWriteableState(true); 754 checkPropertyName(name); 755 if (value instanceof Boolean || value instanceof Number || value instanceof String ) 756 properties.put(name, value); 757 else 758 throw new MessageFormatException ("MNJMS0005D : INVALID OBJECT TYPE "+value.getClass().getName()+" FOR METHOD setObjectProperty()."); 759 760 } 762 765 public final void acknowledge() throws JMSException { 766 if (creatingSession==null) 767 throw new IllegalStateException ("MNJMS0005E : METHOD acknowledge() FAILED. PARENT SESSION IS NULL."); 768 if (creatingSession.getAcknowledgeMode()==javax.jms.Session.CLIENT_ACKNOWLEDGE) 769 creatingSession.ackMessage(null); 770 } 775 778 public void clearBody() throws JMSException { 779 780 setWriteableState(true); 781 782 } 784 788 protected final boolean isWriteable(){ 789 return ((flags & IS_WRITEABLE) >0); 790 } 792 793 public final void setWriteableState(boolean writeable) { 794 if (writeable) 795 flags = flags | IS_WRITEABLE | PROPERTIES_WRITEABLE; 796 797 else 798 flags = flags & REVERSE_WRITEABLE & PROPERTIES_NOT_WRITEABLE; 799 800 } 802 protected final void checkWriteableState(boolean prop) throws JMSException { 803 if ((prop && ( (flags & PROPERTIES_WRITEABLE)==0) )|| 804 (!prop && ( (flags & IS_WRITEABLE)==0) )) 805 806 throw new MessageNotWriteableException ("MNJMS0005F : FAILED ON METHOD checkWriteableState(). MESSAGE IS NOT WRITEABLE."); 807 } 809 810 public final static String BYTEABLENAME ="MantaMsg"; 811 public String getByteableName() { 812 813 return BYTEABLENAME; 814 } 815 816 817 820 public void toBytes(ByteableOutputStream out) throws IOException { 821 822 823 out.writeASCIIString(JMSMessageId); 825 out.writeInt(flags); 826 out.writeLong(JMSExpiration); 827 out.writeLong(JMSTimeStamp); 828 out.writeASCIIString(this.JMSCorrelationId); 829 out.writeByteable((Byteable)this.JMSReplyTo); 830 out.writeByteable((Byteable)this.JMSDestination); 831 832 out.writeInt(properties.size()); 834 Iterator propIt = properties.keySet().iterator(); 835 836 while (propIt.hasNext()) { 837 838 String key = (String )propIt.next(); 840 Object val = properties.get(key); 841 842 out.writeUTF(key); 844 845 if (val instanceof Boolean ) { 847 out.write(BOOLEAN); 848 out.writeBoolean( ((Boolean )val).booleanValue()); 849 } 850 else if (val instanceof Byte ) { 851 out.write(BYTE); 852 out.writeByte( ((Byte )val).byteValue()); 853 } 854 else if (val instanceof Short ) { 855 out.write(SHORT); 856 out.writeShort( ((Short )val).shortValue()); 857 } 858 else if (val instanceof Integer ) { 859 out.write(INTEGER); 860 out.writeInt( ((Integer )val).intValue()); 861 } 862 else if (val instanceof Long ) { 863 out.write(LONG); 864 out.writeLong( ((Long )val).longValue()); 865 } 866 else if (val instanceof String ) { 867 out.write(STRING); 868 out.writeUTF((String )val); 869 } 870 else if (val instanceof Float ) { 871 out.write(FLOAT); 872 out.writeFloat( ((Float )val).floatValue()); 873 } 874 else if (val instanceof Double ) { 875 out.write(DOUBLE); 876 out.writeDouble( ((Double )val).doubleValue()); 877 878 } 879 880 } 881 882 out.writeASCIIString(this.originalClient); 883 } 884 885 886 887 public Byteable createInstance(ByteableInputStream in) throws IOException { 888 889 MantaMessage result = new MantaMessage(); 890 return createHeadersAndProperties(result,in); 891 892 } 893 894 MantaMessage createHeadersAndProperties(MantaMessage result, ByteableInputStream in) throws IOException { 895 896 result.JMSMessageId = in.readASCIIString(); 897 result.flags = in.readInt(); 898 result.JMSExpiration = in.readLong(); 899 result.JMSTimeStamp = in.readLong(); 900 result.JMSCorrelationId = in.readASCIIString(); 901 902 result.JMSReplyTo = (MantaDestination)in.readByteable(); result.JMSDestination = (MantaDestination)in.readByteable(); result.connId = null; 905 906 int size = in.readInt(); 908 for (int i = 0; i<size ; i++) { 909 String key = in.readUTF(); 911 byte type = in.readByte(); 912 switch(type) { 913 case BYTE : result.properties.put(key, new Byte (in.readByte())); break; 914 case BOOLEAN : result.properties.put(key, new Boolean (in.readBoolean()));break; 915 case SHORT : result.properties.put(key,new Short (in.readShort())); break; 916 case INTEGER : result.properties.put(key,new Integer (in.readInt())); break; 917 case DOUBLE : result.properties.put(key,new Double (in.readDouble()));break; 918 case FLOAT : result.properties.put(key,new Float (in.readFloat()));break; 919 case STRING : result.properties.put(key,in.readUTF());break; 920 case LONG : result.properties.put(key,new Long (in.readLong()));break; 921 default : break; } 923 924 } 925 result.originalClient = in.readASCIIString(); 926 result.flags = result.flags & PROPERTIES_NOT_WRITEABLE; 927 return result; 928 } 929 930 931 934 public void registerToByteableRegistry() { 935 ByteableRegistry.registerByteableFactory(getByteableName() , this); 936 937 } 938 939 public static void register() throws JMSException { 940 MantaMessage instance = new MantaMessage(); 941 instance.registerToByteableRegistry(); 942 } 943 944 945 public MantaMessage makeCopy() throws JMSException { 946 MantaMessage copy = new MantaMessage(); 947 initCopy(copy); 948 return copy; 949 950 } 951 952 protected void setConnId(String id){ 953 connId=id; 954 } 955 956 public String getConnId(){ 957 return connId; 958 } 959 960 public void repSession(MantaSession session) { 961 this.creatingSession=session; 962 } 963 public void initCopy(MantaMessage copy){ 964 965 copy.JMSMessageId=JMSMessageId; 966 copy.JMSTimeStamp=JMSTimeStamp; 967 copy.JMSExpiration=JMSExpiration; 968 copy.JMSCorrelationId=JMSCorrelationId; 969 copy.JMSReplyTo=JMSReplyTo; 970 copy.JMSDestination=JMSDestination; 971 copy.flags = flags; 972 copy.connId=connId; 973 copy.creatingSession = creatingSession; 974 copy.properties = new HashMap (properties); 975 copy.timeAccepted = timeAccepted; 976 copy.originalClient= originalClient; 977 } 978 979 980 982 } | Popular Tags |