1 46 package org.mr.api.jms; 47 84 85 import java.io.ByteArrayInputStream ; 86 import java.io.ByteArrayOutputStream ; 87 import java.io.DataInputStream ; 88 import java.io.DataOutputStream ; 89 import java.io.IOException ; 90 91 import javax.jms.JMSException ; 92 import javax.jms.MessageEOFException ; 93 import javax.jms.MessageFormatException ; 94 import javax.jms.MessageNotReadableException ; 95 import javax.jms.MessageNotWriteableException ; 96 import javax.jms.StreamMessage ; 97 98 import org.mr.core.util.byteable.Byteable; 99 import org.mr.core.util.byteable.ByteableInputStream; 100 import org.mr.core.util.byteable.ByteableOutputStream; 101 import org.mr.core.util.byteable.ByteableRegistry; 102 103 104 public class MantaStreamMessage extends MantaMessage implements StreamMessage { 105 106 private static final byte[] EMPTY = new byte[]{}; 108 private byte[] message = EMPTY; 110 private DataOutputStream outputStream = null; 113 private ByteArrayOutputStream byteOutputStream = null; 114 private DataInputStream inputStream = null; 115 private ByteArrayInputStream byteInputStream = null; 116 private int internalOffset = 0; 118 private boolean readingBytes = false; 119 120 private byte[] keepForReadBytes; 121 124 MantaStreamMessage(){ } 125 126 132 public MantaStreamMessage(MantaSession sess) throws JMSException { 133 flags = (flags & ONLY_MSG_TYPES) | STREAM_MESSAGE; 134 creatingSession = sess; 135 } 137 146 public boolean readBoolean() throws JMSException { 147 148 checkBytes(); 149 boolean result = false; 150 changeInput(); 152 Object o = readNextValue(); 153 if (o instanceof Boolean ) 154 result = ((Boolean )o).booleanValue(); 155 else if (o==null || o instanceof String ) 156 result = Boolean.valueOf((String )o).booleanValue(); 157 else 158 throw new MessageFormatException ("MNJMS0008C : FAILED ON METHOD readBoolean(). CAN NOT CONVERT FROM "+o.getClass().getName()); 159 return result; 160 } 162 163 172 public final byte readByte() throws JMSException { 173 174 checkBytes(); 175 176 byte result = 0; 177 changeInput(); 179 Object o = readNextValue(); 180 if (o instanceof Byte ) 181 result = ((Byte ) o).byteValue(); 182 else if (o instanceof String ) 183 try { 184 result = Byte.parseByte((String ) o); 185 } 186 catch (NumberFormatException nfe) { 187 throw new MessageFormatException ("MNJMS0008D : FAILED ON METHOD readByte(). CAN NOT CONVERT TO BYTE FROM "+o); 188 } 189 else if (o == null) 190 result = Byte.valueOf((String ) o).byteValue(); 191 else 192 throw new MessageFormatException ("MNJMS0008E : FAILED ON METHOD readByte(). CAN NOT CONVERT TO BYTE FROM OBJECT OF TYPE "+o.getClass().getName()); 193 194 return result; 195 } 197 198 207 public Object readObject() throws JMSException { 208 209 checkBytes(); 210 changeInput(); 211 Object o = readNextValue(); 212 213 return o; 214 } 215 216 225 public final short readShort() throws JMSException { 226 227 checkBytes(); 228 229 short result = 0; 230 changeInput(); 232 Object o = readNextValue(); 233 if (o instanceof Short ) 234 result = ((Short ) o).shortValue(); 235 else if (o instanceof Byte ) 236 result = ((Byte ) o).shortValue(); 237 else if (o instanceof String ) 238 try { 239 result = Short.parseShort((String ) o); 240 } 241 catch (NumberFormatException nfe) { 242 throw new MessageFormatException ("MNJMS0008F : FAILED ON METHOD readShort(). CAN NOT CONVERT TO SHORT FROM "+o); 243 } 244 else if (o==null) 245 result = Short.valueOf((String ) o).shortValue(); 246 else 247 throw new MessageFormatException ("MNJMS00090 : FAILED ON METHOD readShort(). CAN NOT CONVERT TO SHORT FROM OBJECT OF TYPE " +o.getClass().getName()); 248 249 return result; 250 } 252 253 262 263 public final char readChar() throws JMSException { 264 265 checkBytes(); 266 267 char result = 0; 268 changeInput(); 270 Object o = readNextValue(); 271 if (o == null) 272 throw new NullPointerException ("MNJMS00091 : FAILED ON METHOD readChar(). A NULL VALUE WAS READ."); 273 else if (o instanceof Character ) 274 result = ((Character ) o).charValue(); 275 else 276 throw new MessageFormatException ("MNJMS00092 : FAILED ON METHOD readChar(). CAN NOT CONVERT TO CHAR FROM OBJECT OF TYPE "+o.getClass().getName()); 277 278 return result; 279 } 281 282 291 public final int readInt() throws JMSException { 292 293 checkBytes(); 294 295 int result = 0; 296 changeInput(); 298 Object o = readNextValue(); 299 if (o instanceof Integer ) 300 result = ((Integer ) o).intValue(); 301 else if (o instanceof Short ) 302 result = ((Short ) o).intValue(); 303 else if (o instanceof Byte ) 304 result = ((Byte ) o).intValue(); 305 else if (o instanceof String ) 306 try { 307 result = Integer.parseInt((String )o); 308 } 309 catch (NumberFormatException nfe) { 310 throw new MessageFormatException ("MNJMS00093 : FAILED ON METHOD readInt(). CAN NOT CONVERT TO INTEGER FROM "+o); 311 } 312 else if (o==null) 313 result = Integer.valueOf((String ) o).intValue(); 314 else 315 throw new MessageFormatException ("MNJMS00094 : FAILED ON METHOD readInt(). CAN NOT CONVERT TO INTEGER FROM OBJECT OF TYPE "+o.getClass().getName()); 316 317 return result; 318 } 320 321 330 public final long readLong() throws JMSException { 331 332 checkBytes(); 333 334 long result = 0; 335 changeInput(); 337 Object o = readNextValue(); 338 if (o instanceof Long ) 339 result = ((Long ) o).longValue(); 340 else if (o instanceof Integer ) 341 result = ((Integer ) o).longValue(); 342 else if (o instanceof Short ) 343 result = ((Short ) o).longValue(); 344 else if (o instanceof Byte ) 345 result = ((Byte ) o).longValue(); 346 else if (o instanceof String ) 347 try { 348 result = Long.valueOf((String ) o).longValue(); 349 } 350 catch (NumberFormatException nfe) { 351 throw new MessageFormatException ("MNJMS00095 : FAILED ON METHOD readLong(). CAN NOT CONVERT TO LONG FROM "+o); 352 } 353 else 354 throw new MessageFormatException ("MNJMS00096 : FAILED ON METHOD readLong(). CAN NOT CONVERT TO LONG FROM OBJECT OF TYPE "+o.getClass().getName()); 355 356 return result; 357 } 359 360 368 public final float readFloat() throws JMSException { 369 370 checkBytes(); 371 372 float result = 0; 373 changeInput(); 375 Object o = readNextValue(); 376 if (o instanceof Float ) 377 result = ((Float ) o).floatValue(); 378 else if (o instanceof String ) 379 try { 380 result = Float.parseFloat((String ) o); 381 } 382 catch(NumberFormatException nfe) { 383 throw new MessageFormatException ("MNJMS00097 : FAILED ON METHOD readFloat(). CAN NOT CONVERT TO FLOAT FROM "+o); 384 } 385 else if (o == null) 386 result = Float.valueOf((String ) o).floatValue(); 387 else 388 throw new MessageFormatException ("MNJMS00098 : FAILED ON METHOD readFloat(). CAN NOT CONVERT TO FLOAT FROM OBJECT OF TYPE "+o.getClass().getName()); 389 390 return result; 391 } 393 394 403 public final double readDouble() throws JMSException { 404 405 checkBytes(); 406 407 double result = 0; 408 changeInput(); 410 Object o = readNextValue(); 411 if (o instanceof Double ) 412 result = ((Double ) o).doubleValue(); 413 else if (o instanceof Float ) 414 result = ((Float ) o).doubleValue(); 415 else if (o instanceof String ) 416 try { 417 result = Double.parseDouble((String ) o); 418 } 419 catch (NumberFormatException nfe) { 420 421 throw new MessageFormatException ("MNJMS00099 : FAILED ON METHOD readDouble(). CAN NOT CONVERT TO DOUBLE FROM "+o); 422 } 423 else if (o == null) 424 result = Double.valueOf((String ) o).doubleValue(); 425 else 426 throw new MessageFormatException ("MNJMS0009A : FAILED ON METHOD readDouble(). CAN NOT CONVERT TO DOUBLE FROM OBJECT OF TYPE "+o.getClass().getName()); 427 428 return result; 429 } 431 432 444 public final String readString() throws JMSException { 445 446 checkBytes(); 447 448 String result = null; 449 changeInput(); 451 Object o = readNextValue(); 452 if (o instanceof byte[]) 453 throw new MessageFormatException ("MNJMS0009B : FAILED ON METHOD readString(). CAN NOT CONVERT A BYTE ARRAY TO STRING."); 454 else if (o!=null) 455 result=String.valueOf(o); 456 return result; 457 } 459 460 481 public final int readBytes(byte[] value) throws JMSException { 482 483 if (!readingBytes) { 485 486 changeInput(); 487 Object o = readNextValue(); 488 if (o==null) 489 throw new MessageFormatException ("MNJMS0009C : FAILED ON METHOD readBytes(). A NULL VALUE WAS SPECIFIED."); 490 else if (o instanceof byte[]) 491 keepForReadBytes = (byte[])o; else throw new MessageFormatException ("MNJMS0009D : FAILED ON METHOD readBytes(). CAN NOT CONVERT TO BYTE ARRAY FROM "+o.getClass().getName()); 494 495 } 496 497 if (keepForReadBytes.length<=value.length) { 498 System.arraycopy(keepForReadBytes,0,value,0,keepForReadBytes.length); 499 readingBytes = false; 500 return keepForReadBytes.length; 502 } 504 else { 505 System.arraycopy(keepForReadBytes,0,value,0,value.length); 508 readingBytes=true; 509 int remainder =keepForReadBytes.length - value.length; 510 byte[] temp = new byte[remainder]; 511 System.arraycopy(keepForReadBytes,value.length,temp,0,remainder); 512 keepForReadBytes = temp; 513 return value.length; 514 } 515 516 } 518 528 public final void writeBoolean(boolean value) throws JMSException { 529 checkWriteableState(false); 531 try { 532 getOutputStream().writeByte(BOOLEAN); 533 getOutputStream().writeBoolean(value); 534 } catch (IOException exception) { 536 throw new JMSException ("MNJMS0009E : FAILED ON METHOD writeBoolean(). ERROR TEXT : "+exception.getMessage()); 537 } } 540 541 549 public final void writeByte(byte value) throws JMSException { 550 checkWriteableState(false); 552 try { 553 getOutputStream().writeByte(BYTE); 554 getOutputStream().writeByte(value); 555 556 } catch (IOException exception) { 558 throw new JMSException ("MNJMS0009F : FAILED ON METHOD writeByte(). ERROR TEXT : "+exception.getMessage()); 559 } } 562 563 571 public final void writeShort(short value) throws JMSException { 572 checkWriteableState(false); 574 try { 575 getOutputStream().writeByte(SHORT); 576 getOutputStream().writeShort(value); 577 } catch (IOException exception) { 579 throw new JMSException ("MNJMS000A0 : FAILED ON METHOD writeByte(). ERROR TEXT : "+exception.getMessage()); 580 581 } } 584 585 593 public final void writeChar(char value) throws JMSException { 594 checkWriteableState(false); 596 try { 597 getOutputStream().writeByte(CHAR); 598 getOutputStream().writeChar(value); 599 } catch (IOException exception) { 601 throw new JMSException ("MNJMS000A1 : FAILED ON METHOD writeChar(). ERROR TEXT : "+exception.getMessage()); 602 } } 605 606 614 public final void writeInt(int value) throws JMSException { 615 checkWriteableState(false); 617 try { 618 getOutputStream().writeByte(MantaMessage.INTEGER); 619 getOutputStream().writeInt(value); 620 } catch (IOException exception) { 622 throw new JMSException ("MNJMS000A2 : FAILED ON METHOD writeInt(). ERROR TEXT : "+exception.getMessage()); 623 } } 626 627 634 public final void writeLong(long value) throws JMSException { 635 checkWriteableState(false); 637 try { 638 getOutputStream().writeByte(LONG); 639 getOutputStream().writeLong(value); 640 } catch (IOException exception) { 642 throw new JMSException ("MNJMS000A3 : FAILED ON METHOD writeLong(). ERROR TEXT : "+exception.getMessage()); 643 } } 646 647 657 public final void writeFloat(float value) throws JMSException { 658 checkWriteableState(false); 660 try { 661 getOutputStream().writeByte(FLOAT); 662 getOutputStream().writeFloat(value); 663 } catch (IOException exception) { 665 throw new JMSException ("MNJMS000A4 : FAILED ON METHOD writeFloat(). ERROR TEXT : "+exception.getMessage()); 666 } } 669 670 681 public final void writeDouble(double value) throws JMSException { 682 checkWriteableState(false); 684 try { 685 getOutputStream().writeByte(DOUBLE); 686 getOutputStream().writeDouble(value); 687 } catch (IOException exception) { 689 throw new JMSException ("MNJMS000A5 : FAILED ON METHOD writeDouble(). ERROR TEXT : "+exception.getMessage()); 690 } } 693 694 703 public final void writeString(String value) throws JMSException { 704 checkWriteableState(false); 706 try { 707 getOutputStream().writeByte(STRING); 708 getOutputStream().writeUTF(value); 709 } catch (IOException exception) { 711 throw new JMSException ("MNJMS000A6 : FAILED ON METHOD writeString(). ERROR TEXT : "+exception.getMessage()); 712 } } 715 716 723 public final void writeBytes(byte[] value) throws JMSException { 724 checkWriteableState(false); 726 try { 727 getOutputStream().writeByte(BYTEARR); 728 getOutputStream().writeInt(value.length); 729 getOutputStream().write(value); 730 } catch (IOException exception) { 732 throw new JMSException ("MNJMS000A7 : FAILED ON METHOD writeBytes(). ERROR TEXT : "+exception.getMessage()); 733 } } 736 737 747 public final void writeBytes(byte[] value, int offset, int length) throws JMSException { 748 checkWriteableState(false); 750 try { 751 getOutputStream().writeByte(BYTEARR); 752 getOutputStream().writeInt(length); 753 getOutputStream().write(value, offset, length); 754 } catch (IOException exception) { 756 throw new JMSException ("MNJMS000A8 : FAILED ON METHOD writeBytes(). ERROR TEXT : "+exception.getMessage()); 757 } } 760 761 774 public final void writeObject(Object value) throws JMSException { 775 checkWriteableState(false); 777 778 if (value == null) { 780 throw new NullPointerException ("MNJMS000A9 : FAILED ON METHOD writeObject(). NULL VALUE SPECIFIED."); 781 } 782 else if (value instanceof Boolean ) { 783 writeBoolean(((Boolean ) value).booleanValue()); 784 } else if (value instanceof Byte ) { 786 writeByte(((Byte ) value).byteValue()); 787 } else if (value instanceof Short ) { 789 writeShort(((Short ) value).shortValue()); 790 } else if (value instanceof Character ) { 792 writeChar(((Character ) value).charValue()); 793 } else if (value instanceof Integer ) { 795 writeInt(((Integer ) value).intValue()); 796 } else if (value instanceof Long ) { 798 writeLong(((Long ) value).longValue()); 799 } else if (value instanceof Float ) { 801 writeFloat(((Float ) value).floatValue()); 802 } else if (value instanceof Double ) { 804 writeDouble(((Double ) value).doubleValue()); 805 } else if (value instanceof String ) { 807 writeString((String ) value); 808 } else if (value instanceof byte[]) { 810 writeBytes((byte[]) value); 811 } else { 813 throw new MessageFormatException ("MNJMS000AA : FAILED ON METHOD writeObject(). TYPE "+value.getClass().getName()+" IS NOT ALLOWED."); 814 } } 817 818 824 public final void reset() throws JMSException { 825 826 try { 827 if (this.isWriteable()) 828 this.setWriteableState(false); 829 if (outputStream != null) { 830 outputStream.flush(); 831 message = this.byteOutputStream.toByteArray(); 832 byteOutputStream = null; 833 outputStream.close(); 834 outputStream=null; 835 } 836 837 else 838 if (this.inputStream!=null) { 839 this.byteInputStream=null; 840 this.inputStream.close(); 841 this.inputStream = null; 842 } 843 844 } catch (IOException exception) { 846 throw new JMSException ("MNJMS000AB : FAILED ON METHOD reset(). ERROR TEXT : "+exception.getMessage()); 847 } } 850 851 866 public final void clearBody() throws JMSException { 867 try { 868 if (!this.isWriteable()) { 869 this.setWriteableState(true); 871 if (inputStream != null) { 872 byteInputStream = null; 873 inputStream.close(); 874 inputStream = null; 875 this.internalOffset = 0; 876 } } else if (outputStream != null) { 879 this.byteOutputStream = null; 880 this.outputStream.close(); 881 this.outputStream = null; 882 } flags=flags|ORIG_BODY_SAVED; 888 this.message = EMPTY; 890 this.byteOutputStream = null; 891 this.outputStream = null; 892 } catch (IOException exception) { 894 throw new JMSException ("MNJMS000AC : FAILED ON METHOD clearBody(). ERROR TEXT : "+exception.getMessage()); 895 } } 898 899 900 903 private DataOutputStream getOutputStream() throws IOException { 904 if (this.outputStream==null) { 905 this.byteOutputStream = new ByteArrayOutputStream (); 906 this.outputStream = new DataOutputStream (this.byteOutputStream); 907 this.outputStream.write(this.message); 908 } 909 910 return this.outputStream; 911 } 912 913 914 public void toBytes(ByteableOutputStream out) throws IOException { 916 super.toBytes(out); 917 if(message == null) 918 message = this.byteOutputStream.toByteArray(); 919 out.writeInt(message.length); 920 out.write(message); 921 out.flush(); 922 } 923 924 public Byteable createInstance(ByteableInputStream in) throws IOException { 925 MantaStreamMessage msm; 926 try { 927 msm = new MantaStreamMessage(null); 928 msm.byteOutputStream = new ByteArrayOutputStream (); 929 createHeadersAndProperties(msm,in); 930 byte[] cbmInputStream = new byte[in.readInt()]; 931 in.read(cbmInputStream); 932 msm.inputStream = new DataInputStream (new ByteArrayInputStream (cbmInputStream)); 933 msm.message =cbmInputStream; 934 } 935 catch (Exception ee) { 936 throw new IOException ("MNJMS00FFB : METHOD createInstance() FAILED ON MantaStreamMessage. EXCEPTION TEXT : "+ee.getMessage()); 937 } 938 939 return msm; 940 } 941 942 public void registerToByteableRegistry() { 961 ByteableRegistry.registerByteableFactory(getByteableName() , this); 962 } 963 964 965 public final static String BYTEABLENAME = "MantaStreamMsg"; 966 967 public String getByteableName() { 968 969 return BYTEABLENAME; 970 } 971 972 public static void register() throws JMSException { 973 MantaStreamMessage instance = new MantaStreamMessage(null); 974 instance.registerToByteableRegistry(); 975 } 976 977 979 980 private void checkBytes() throws JMSException { 981 if (readingBytes) 982 throw new MessageFormatException ("MNJMS000AD : FAILED ON METHOD checkBytes(). STREAM MESSAGE IS IN A readBytes() SEQUENCE."); 983 984 } 985 986 989 990 private void changeInput() throws JMSException { 991 if ((flags&IS_WRITEABLE)>0) 992 throw new MessageNotReadableException ("MNJMS000AE : FAILED ON METHOD changeInput(). MESSAGE IS IN WRITEABLE STATE."); 993 994 if (this.inputStream==null) { 995 this.byteInputStream = new ByteArrayInputStream (message,this.internalOffset, 996 message.length-internalOffset); 997 inputStream = new DataInputStream (byteInputStream); 998 } 999 try { 1000 inputStream.mark(this.message.length - inputStream.available()); 1001 } 1002 catch (IOException ioe) { 1003 throw new JMSException ("MNJMS000AF : FAILED ON METHOD changeInput(). ERROR TEXT : "+ioe.getMessage()); 1004 } 1005 } 1007 1008 1015 public long getBodyLength() throws JMSException { 1016 1017 return this.message.length; 1018 } 1019 1020 1021 1023 1024 1029 private Object readNextValue() throws JMSException { 1030 1031 Object ret = null; 1032 byte objectType=0; 1033 try { 1034 objectType = inputStream.readByte(); 1035 1036 switch (objectType) { 1037 case STRING: 1038 ret = inputStream.readUTF(); 1039 break; 1040 case LONG: 1041 ret = new Long (inputStream.readLong()); 1042 break; 1043 case FLOAT: 1044 ret = new Float (inputStream.readFloat()); 1045 break; 1046 case DOUBLE: 1047 ret = new Double (inputStream.readDouble()); 1048 break; 1049 case CHAR: 1050 ret = new Character (inputStream.readChar()); 1051 break; 1052 case BYTE: 1053 ret = new Byte (inputStream.readByte()); 1054 break; 1055 case BYTEARR: 1056 int len = inputStream.readInt(); 1057 byte[] retBytes = new byte[len]; 1058 inputStream.readFully(retBytes); 1059 ret = retBytes; 1060 break; 1061 case INTEGER: 1062 ret = new Integer (inputStream.readInt()); 1063 break; 1064 case SHORT: 1065 ret = new Short (inputStream.readShort()); 1066 break; 1067 case BOOLEAN: 1068 ret = new Boolean (inputStream.readBoolean()); 1069 break; 1070 } 1071 } 1072 catch (Exception ig) { 1073 throw new MessageEOFException ("MNJMS000B0 : FAILED ON METHOD readNextValue(). STREAM END REACHED."); 1074 } 1075 1076 return ret; 1077 1078 } 1079 1080 public MantaMessage makeCopy() throws JMSException { 1081 MantaStreamMessage copy = new MantaStreamMessage(); 1082 initCopy(copy); 1083 if(message == null ||message == EMPTY ){ 1086 if (byteOutputStream==null) 1087 copy.message = EMPTY; 1088 else 1089 copy.message=byteOutputStream.toByteArray(); 1090 }else{ 1091 copy.message = message; 1092 } 1093 1094 copy.internalOffset = internalOffset; 1095 1096 return copy; 1097 } 1099} 1100 1101 | Popular Tags |