1 22 package org.jboss.iiop.rmi.marshal; 23 24 import java.io.Externalizable ; 25 import java.io.Serializable ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 import javax.rmi.CORBA.Util ; 29 import javax.rmi.PortableRemoteObject ; 30 import org.omg.CORBA.portable.IDLEntity ; 31 import org.omg.CORBA_2_3.portable.InputStream ; 32 import org.omg.CORBA_2_3.portable.OutputStream ; 33 34 import org.jboss.iiop.rmi.RmiIdlUtil; 35 36 50 public class CDRStream 51 { 52 89 public static String abbrevFor(Class clz) 90 { 91 if (clz == Boolean.TYPE) { 92 return "Z"; 93 } 94 else if (clz == Byte.TYPE) { 95 return "B"; 96 } 97 else if (clz == Character.TYPE) { 98 return "C"; 99 } 100 else if (clz == Double.TYPE) { 101 return "D"; 102 } 103 else if (clz == Float.TYPE) { 104 return "F"; 105 } 106 else if (clz == Integer.TYPE) { 107 return "I"; 108 } 109 else if (clz == Long.TYPE) { 110 return "J"; 111 } 112 else if (clz == Short.TYPE) { 113 return "S"; 114 } 115 else if (clz == Void.TYPE) { 116 return "V"; 117 } 118 else if (clz == String .class) { 119 return "G"; } 121 else if (RmiIdlUtil.isRMIIDLRemoteInterface(clz)) { 122 return "R" + clz.getName(); } 124 else if (clz == org.omg.CORBA.Object .class) { 125 return "M"; } 127 else if (org.omg.CORBA.Object .class.isAssignableFrom(clz)) { 128 return "N" + clz.getName(); } 130 else if (IDLEntity .class.isAssignableFrom(clz)) { 131 return "L" + clz.getName(); } 133 else if (clz == Serializable .class) { 134 return "E"; } 136 else if (RmiIdlUtil.isAbstractInterface(clz)) { 137 return "A"; } 139 else if (Serializable .class.isAssignableFrom(clz)) { 140 return "L" + clz.getName(); } 142 else if (Externalizable .class.isAssignableFrom(clz)) { 143 return "X"; } 145 else if (clz == Object .class) { 146 return "O"; } 148 else { 149 return "L" + clz.getName(); } 151 } 152 153 157 public static CDRStreamReader readerFor(String s, ClassLoader cl) 158 { 159 switch (s.charAt(0)) { 160 161 case 'A': 162 return AbstractInterfaceReader.instance; 163 case 'B': 164 return ByteReader.instance; 165 case 'C': 166 return CharReader.instance; 167 case 'D': 168 return DoubleReader.instance; 169 case 'E': 170 return SerializableReader.instance; 171 case 'F': 172 return FloatReader.instance; 173 case 'G': 174 return StringReader.instance; 175 case 'I': 176 return IntReader.instance; 177 case 'J': 178 return LongReader.instance; 179 case 'L': 180 try { 181 return new ValuetypeReader(Class.forName(s.substring(1), 184 true, 185 cl)); 186 } 187 catch (ClassNotFoundException e) { 188 throw new RuntimeException ("Error loading class " 189 + s.substring(1) + ": " + e); 190 } 191 case 'M': 192 return CorbaObjectReader.instance; 193 case 'N': 194 try { 195 return new IdlInterfaceReader(cl.loadClass(s.substring(1))); 196 } 197 catch (ClassNotFoundException e) { 198 throw new RuntimeException ("Error loading class " 199 + s.substring(1) + ": " + e); 200 } 201 case 'O': 202 return ObjectReader.instance; 203 case 'R': 204 try { 205 return new RemoteReader(cl.loadClass(s.substring(1))); 206 } 207 catch (ClassNotFoundException e) { 208 throw new RuntimeException ("Error loading class " 209 + s.substring(1) + ": " + e); 210 } 211 case 'S': 212 return ShortReader.instance; 213 case 'V': 214 return null; 215 case 'X': 216 return ExternalizableReader.instance; 217 case 'Z': 218 return BooleanReader.instance; 219 default: 220 return null; 221 } 222 } 223 224 228 public static CDRStreamWriter writerFor(String s, ClassLoader cl) 229 { 230 switch (s.charAt(0)) { 231 232 case 'A': 233 return AbstractInterfaceWriter.instance; 234 case 'B': 235 return ByteWriter.instance; 236 case 'C': 237 return CharWriter.instance; 238 case 'D': 239 return DoubleWriter.instance; 240 case 'E': 241 return SerializableWriter.instance; 242 case 'F': 243 return FloatWriter.instance; 244 case 'G': 245 return StringWriter.instance; 246 case 'I': 247 return IntWriter.instance; 248 case 'J': 249 return LongWriter.instance; 250 case 'L': 251 try { 252 return new ValuetypeWriter(Class.forName(s.substring(1), 255 true, 256 cl)); 257 } 258 catch (ClassNotFoundException e) { 259 throw new RuntimeException ("Error loading class " 260 + s.substring(1) + ": " + e); 261 } 262 case 'M': 263 return CorbaObjectWriter.instance; 264 case 'N': 265 try { 266 return new IdlInterfaceWriter(cl.loadClass(s.substring(1))); 267 } 268 catch (ClassNotFoundException e) { 269 throw new RuntimeException ("Error loading class " 270 + s.substring(1) + ": " + e); 271 } 272 case 'O': 273 return ObjectWriter.instance; 274 case 'R': 275 return RemoteWriter.instance; 276 case 'S': 277 return ShortWriter.instance; 278 case 'V': 279 return null; 280 case 'X': 281 return ExternalizableWriter.instance; 282 case 'Z': 283 return BooleanWriter.instance; 284 default: 285 return null; 286 } 287 } 288 289 292 public static CDRStreamReader readerFor(Class clz) 293 { 294 if (clz == Boolean.TYPE) { 295 return BooleanReader.instance; 296 } 297 else if (clz == Byte.TYPE) { 298 return ByteReader.instance; 299 } 300 else if (clz == Character.TYPE) { 301 return CharReader.instance; 302 } 303 else if (clz == Double.TYPE) { 304 return DoubleReader.instance; 305 } 306 else if (clz == Float.TYPE) { 307 return FloatReader.instance; 308 } 309 else if (clz == Integer.TYPE) { 310 return IntReader.instance; 311 } 312 else if (clz == Long.TYPE) { 313 return LongReader.instance; 314 } 315 else if (clz == Short.TYPE) { 316 return ShortReader.instance; 317 } 318 else if (clz == Void.TYPE) { 319 return null; 320 } 321 else if (clz == String .class) { 322 return StringReader.instance; 323 } 324 else if (RmiIdlUtil.isRMIIDLRemoteInterface(clz)) { 325 return new RemoteReader(clz); 326 } 327 else if (clz == org.omg.CORBA.Object .class) { 328 return CorbaObjectReader.instance; 329 } 330 else if (org.omg.CORBA.Object .class.isAssignableFrom(clz)) { 331 return new IdlInterfaceReader(clz); 332 } 333 else if (IDLEntity .class.isAssignableFrom(clz)) { 334 return new ValuetypeReader(clz); 335 } 336 else if (clz == Serializable .class) { 337 return SerializableReader.instance; 338 } 339 else if (RmiIdlUtil.isAbstractInterface(clz)) { 340 return AbstractInterfaceReader.instance; 341 } 342 else if (Serializable .class.isAssignableFrom(clz)) { 343 return new ValuetypeReader(clz); 344 } 345 else if (Externalizable .class.isAssignableFrom(clz)) { 346 return ExternalizableReader.instance; 347 } 348 else if (clz == Object .class) { 349 return ObjectReader.instance; 350 } 351 else { 352 return new ValuetypeReader(clz); 353 } 354 } 355 356 359 public static CDRStreamWriter writerFor(Class clz) 360 { 361 if (clz == Boolean.TYPE) { 362 return BooleanWriter.instance; 363 } 364 else if (clz == Byte.TYPE) { 365 return ByteWriter.instance; 366 } 367 else if (clz == Character.TYPE) { 368 return CharWriter.instance; 369 } 370 else if (clz == Double.TYPE) { 371 return DoubleWriter.instance; 372 } 373 else if (clz == Float.TYPE) { 374 return FloatWriter.instance; 375 } 376 else if (clz == Integer.TYPE) { 377 return IntWriter.instance; 378 } 379 else if (clz == Long.TYPE) { 380 return LongWriter.instance; 381 } 382 else if (clz == Short.TYPE) { 383 return ShortWriter.instance; 384 } 385 else if (clz == String .class) { 386 return StringWriter.instance; 387 } 388 else if (clz == Void.TYPE) { 389 return null; 390 } 391 else if (RmiIdlUtil.isRMIIDLRemoteInterface(clz)) { 392 return RemoteWriter.instance; 393 } 394 else if (clz == org.omg.CORBA.Object .class) { 395 return CorbaObjectWriter.instance; 396 } 397 else if (org.omg.CORBA.Object .class.isAssignableFrom(clz)) { 398 return new IdlInterfaceWriter(clz); 399 } 400 else if (IDLEntity .class.isAssignableFrom(clz)) { 401 return new ValuetypeWriter(clz); 402 } 403 else if (clz == Serializable .class) { 404 return SerializableWriter.instance; 405 } 406 else if (RmiIdlUtil.isAbstractInterface(clz)) { 407 return AbstractInterfaceWriter.instance; 408 } 409 else if (Serializable .class.isAssignableFrom(clz)) { 410 return new ValuetypeWriter(clz); 411 } 412 else if (Externalizable .class.isAssignableFrom(clz)) { 413 return ExternalizableWriter.instance; 414 } 415 else if (clz == Object .class) { 416 return ObjectWriter.instance; 417 } 418 else { 419 return new ValuetypeWriter(clz); 420 } 421 } 422 423 425 427 431 private static final class BooleanReader 432 implements CDRStreamReader 433 { 434 static final CDRStreamReader instance = new BooleanReader(); 435 436 private BooleanReader() { } 437 438 public Object read(InputStream in) 439 { 440 return new Boolean (in.read_boolean()); 441 } 442 } 443 444 448 private static final class ByteReader 449 implements CDRStreamReader 450 { 451 static final CDRStreamReader instance = new ByteReader(); 452 453 private ByteReader() { } 454 455 public Object read(InputStream in) 456 { 457 return new Byte (in.read_octet()); 458 } 459 } 460 461 465 private static final class CharReader 466 implements CDRStreamReader 467 { 468 static final CDRStreamReader instance = new CharReader(); 469 470 private CharReader() { } 471 472 public Object read(InputStream in) 473 { 474 return new Character (in.read_wchar()); 475 } 476 } 477 478 482 private static final class DoubleReader 483 implements CDRStreamReader 484 { 485 static final CDRStreamReader instance = new DoubleReader(); 486 487 private DoubleReader() { } 488 489 public Object read(InputStream in) 490 { 491 return new Double (in.read_double()); 492 } 493 } 494 495 499 private static final class FloatReader 500 implements CDRStreamReader 501 { 502 static final CDRStreamReader instance = new FloatReader(); 503 504 private FloatReader() { } 505 506 public Object read(InputStream in) 507 { 508 return new Float (in.read_float()); 509 } 510 } 511 512 516 private static final class IntReader 517 implements CDRStreamReader 518 { 519 static final CDRStreamReader instance = new IntReader(); 520 521 private IntReader() { } 522 523 public Object read(InputStream in) 524 { 525 return new Integer (in.read_long()); 526 } 527 } 528 529 533 private static final class LongReader 534 implements CDRStreamReader 535 { 536 static final CDRStreamReader instance = new LongReader(); 537 538 private LongReader() { } 539 540 public Object read(InputStream in) 541 { 542 return new Long (in.read_longlong()); 543 } 544 } 545 546 550 private static final class ShortReader 551 implements CDRStreamReader 552 { 553 static final CDRStreamReader instance = new ShortReader(); 554 555 private ShortReader() { } 556 557 public Object read(InputStream in) 558 { 559 return new Short (in.read_short()); 560 } 561 } 562 563 567 private static final class StringReader 568 implements CDRStreamReader 569 { 570 static final CDRStreamReader instance = new StringReader(); 571 572 private StringReader() { } 573 574 public Object read(InputStream in) 575 { 576 return in.read_value(String .class); 577 } 578 } 579 580 586 private static final class RemoteReader 587 implements CDRStreamReader 588 { 589 private Class clz; 590 591 RemoteReader(Class clz) { 592 this.clz = clz; 593 } 594 595 public Object read(InputStream in) 596 { 597 return PortableRemoteObject.narrow(in.read_Object(), clz); 600 } 601 } 602 603 607 private static final class ObjectReader 608 implements CDRStreamReader 609 { 610 static final CDRStreamReader instance = new ObjectReader(); 611 612 private ObjectReader() { } 613 614 public Object read(InputStream in) 615 { 616 return Util.readAny(in); 617 } 618 } 619 620 624 private static final class SerializableReader 625 implements CDRStreamReader 626 { 627 static final CDRStreamReader instance = new SerializableReader(); 628 629 private SerializableReader() { } 630 631 public Object read(InputStream in) 632 { 633 return (Serializable )Util.readAny(in); 634 } 635 } 636 637 641 private static final class ExternalizableReader 642 implements CDRStreamReader 643 { 644 static final CDRStreamReader instance = new ExternalizableReader(); 645 646 private ExternalizableReader() { } 647 648 public Object read(InputStream in) 649 { 650 return (Externalizable )Util.readAny(in); 651 } 652 } 653 654 658 private static final class CorbaObjectReader 659 implements CDRStreamReader 660 { 661 static final CDRStreamReader instance = new CorbaObjectReader(); 662 663 private CorbaObjectReader() { } 664 665 public Object read(InputStream in) 666 { 667 return (org.omg.CORBA.Object )in.read_Object(); 668 } 669 } 670 671 678 private static final class IdlInterfaceReader 679 implements CDRStreamReader 680 { 681 static private Class [] paramTypes = 682 { org.omg.CORBA.portable.InputStream .class }; 683 684 private Method readMethod = null; 686 687 IdlInterfaceReader(Class clz) 688 { 689 String helperClassName = clz.getName() + "Helper"; 690 691 try { 692 Class helperClass = 693 clz.getClassLoader().loadClass(helperClassName); 694 readMethod = helperClass.getMethod("read", paramTypes); 695 } 696 catch (ClassNotFoundException e) { 697 throw new RuntimeException ("Error loading class " 698 + helperClassName + ": " + e); 699 } 700 catch (NoSuchMethodException e) { 701 throw new RuntimeException ("No read method in helper class " 702 + helperClassName + ": " + e); 703 } 704 } 705 706 public Object read(InputStream in) 707 { 708 try { 709 return readMethod.invoke(null, new Object [] { in }); 710 } 711 catch (IllegalAccessException e) { 712 throw new RuntimeException ("Internal error: " + e); 713 } 714 catch (InvocationTargetException e) { 715 throw new RuntimeException ("Exception unmarshaling CORBA object: " 716 + e.getTargetException()); 717 } 718 } 719 } 720 721 728 private static final class AbstractInterfaceReader 729 implements CDRStreamReader 730 { 731 static final CDRStreamReader instance = new AbstractInterfaceReader(); 732 733 private AbstractInterfaceReader() { } 734 735 public Object read(InputStream in) 736 { 737 return in.read_abstract_interface(); 738 } 739 } 740 741 747 private static final class ValuetypeReader 748 implements CDRStreamReader 749 { 750 private Class clz; 751 752 ValuetypeReader(Class clz) { 753 this.clz = clz; 754 } 755 756 public Object read(InputStream in) 757 { 758 return in.read_value(clz); 759 } 760 } 761 762 766 private static final class BooleanWriter 767 implements CDRStreamWriter 768 { 769 static final CDRStreamWriter instance = new BooleanWriter(); 770 771 private BooleanWriter() { } 772 773 public void write(OutputStream out, Object obj) 774 { 775 out.write_boolean(((Boolean )obj).booleanValue()); 776 } 777 } 778 779 783 private static final class ByteWriter 784 implements CDRStreamWriter 785 { 786 static final CDRStreamWriter instance = new ByteWriter(); 787 788 private ByteWriter() { } 789 790 public void write(OutputStream out, Object obj) 791 { 792 out.write_octet(((Byte )obj).byteValue()); 793 } 794 } 795 796 800 private static final class CharWriter 801 implements CDRStreamWriter 802 { 803 804 static final CDRStreamWriter instance = new CharWriter(); 805 806 private CharWriter() { } 807 808 public void write(OutputStream out, Object obj) 809 { 810 out.write_wchar(((Character )obj).charValue()); 811 } 812 } 813 814 818 private static final class DoubleWriter 819 implements CDRStreamWriter 820 { 821 static final CDRStreamWriter instance = new DoubleWriter(); 822 823 private DoubleWriter() { } 824 825 public void write(OutputStream out, Object obj) 826 { 827 out.write_double(((Double )obj).doubleValue()); 828 } 829 } 830 831 835 private static final class FloatWriter 836 implements CDRStreamWriter 837 { 838 static final CDRStreamWriter instance = new FloatWriter(); 839 840 private FloatWriter() { } 841 842 public void write(OutputStream out, Object obj) 843 { 844 out.write_float(((Float )obj).floatValue()); 845 } 846 } 847 848 852 private static final class IntWriter 853 implements CDRStreamWriter 854 { 855 static final CDRStreamWriter instance = new IntWriter(); 856 857 private IntWriter() { } 858 859 public void write(OutputStream out, Object obj) 860 { 861 out.write_long(((Integer )obj).intValue()); 862 } 863 } 864 865 869 private static final class LongWriter 870 implements CDRStreamWriter 871 { 872 static final CDRStreamWriter instance = new LongWriter(); 873 874 private LongWriter() { } 875 876 public void write(OutputStream out, Object obj) 877 { 878 out.write_longlong(((Long )obj).longValue()); 879 } 880 } 881 882 886 private static final class ShortWriter 887 implements CDRStreamWriter 888 { 889 static final CDRStreamWriter instance = new ShortWriter(); 890 891 private ShortWriter() { } 892 893 public void write(OutputStream out, Object obj) 894 { 895 out.write_short(((Short )obj).shortValue()); 896 } 897 } 898 899 903 private static final class StringWriter 904 implements CDRStreamWriter 905 { 906 static final CDRStreamWriter instance = new StringWriter(); 907 908 private StringWriter() { } 909 910 public void write(OutputStream out, Object obj) 911 { 912 out.write_value((String )obj, String .class); 913 } 914 } 915 916 920 private static final class RemoteWriter 921 implements CDRStreamWriter 922 { 923 static final CDRStreamWriter instance = new RemoteWriter(); 924 925 private RemoteWriter() { } 926 927 public void write(OutputStream out, Object obj) 928 { 929 out.write_Object((org.omg.CORBA.Object )obj); 930 } 931 } 932 933 937 private static final class ObjectWriter 938 implements CDRStreamWriter 939 { 940 static final CDRStreamWriter instance = new ObjectWriter(); 941 942 private ObjectWriter() { } 943 944 public void write(OutputStream out, Object obj) 945 { 946 Util.writeAny(out, obj); 947 } 948 } 949 950 954 private static final class SerializableWriter 955 implements CDRStreamWriter 956 { 957 static final CDRStreamWriter instance = new SerializableWriter(); 958 959 private SerializableWriter() { } 960 961 public void write(OutputStream out, Object obj) 962 { 963 Util.writeAny(out, (Serializable )obj); 964 } 965 } 966 967 971 private static final class ExternalizableWriter 972 implements CDRStreamWriter 973 { 974 static final CDRStreamWriter instance = new ExternalizableWriter(); 975 976 private ExternalizableWriter() { } 977 978 public void write(OutputStream out, Object obj) 979 { 980 Util.writeAny(out, (Externalizable )obj); 981 } 982 } 983 984 988 private static final class CorbaObjectWriter 989 implements CDRStreamWriter 990 { 991 static final CDRStreamWriter instance = new CorbaObjectWriter(); 992 993 private CorbaObjectWriter() { } 994 995 public void write(OutputStream out, Object obj) 996 { 997 out.write_Object((org.omg.CORBA.Object )obj); 998 } 999 } 1000 1001 1008 private static final class IdlInterfaceWriter 1009 implements CDRStreamWriter 1010 { 1011 private Method writeMethod = null; 1013 1014 IdlInterfaceWriter(Class clz) 1015 { 1016 String helperClassName = clz.getName() + "Helper"; 1017 1018 try { 1019 Class helperClass = 1020 clz.getClassLoader().loadClass(helperClassName); 1021 Class [] paramTypes = { 1022 org.omg.CORBA.portable.OutputStream .class, 1023 clz 1024 }; 1025 writeMethod = helperClass.getMethod("write", paramTypes); 1026 } 1027 catch (ClassNotFoundException e) { 1028 throw new RuntimeException ("Error loading class " 1029 + helperClassName + ": " + e); 1030 } 1031 catch (NoSuchMethodException e) { 1032 throw new RuntimeException ("No write method in helper class " 1033 + helperClassName + ": " + e); 1034 } 1035 } 1036 1037 public void write(OutputStream out, Object obj) 1038 { 1039 try { 1040 writeMethod.invoke(null, new Object [] { out, obj }); 1041 } 1042 catch (IllegalAccessException e) { 1043 throw new RuntimeException ("Internal error: " + e); 1044 } 1045 catch (InvocationTargetException e) { 1046 throw new RuntimeException ("Exception marshaling CORBA object: " 1047 + e.getTargetException()); 1048 } 1049 } 1050 } 1051 1052 1059 private static final class AbstractInterfaceWriter 1060 implements CDRStreamWriter 1061 { 1062 static final CDRStreamWriter instance = new AbstractInterfaceWriter(); 1063 1064 private AbstractInterfaceWriter() { } 1065 1066 public void write(OutputStream out, Object obj) 1067 { 1068 out.write_abstract_interface(obj); 1069 } 1070 } 1071 1072 1078 private static final class ValuetypeWriter 1079 implements CDRStreamWriter 1080 { 1081 private Class clz; 1082 1083 ValuetypeWriter(Class clz) 1084 { 1085 this.clz = clz; 1086 } 1087 1088 public void write(OutputStream out, Object obj) 1089 { 1090 out.write_value((java.io.Serializable )obj, clz); 1091 } 1092 } 1093 1094} 1095 | Popular Tags |