1 28 29 package com.caucho.iiop; 30 31 import com.caucho.util.Alarm; 32 import com.caucho.util.IdentityIntMap; 33 34 import org.omg.CORBA.Any ; 35 import org.omg.CORBA.ORB ; 36 import org.omg.CORBA.SystemException ; 37 import org.omg.CORBA.TCKind ; 38 import org.omg.CORBA.TypeCode ; 39 import org.omg.SendingContext.RunTime ; 40 41 import javax.rmi.CORBA.ClassDesc ; 42 import javax.rmi.CORBA.Util ; 43 import javax.rmi.CORBA.ValueHandler ; 44 import java.io.IOException ; 45 import java.io.Serializable ; 46 47 abstract public class IiopWriter extends org.omg.CORBA_2_3.portable.OutputStream { 48 static protected final int VALUE_TAG = 0x7fffff00; 49 static protected final int VALUE_HAS_CODESET = 0x1; 50 static protected final int VALUE_NO_TYPE = 0x0; 51 static protected final int VALUE_ONE_REP_ID = 0x2; 52 static protected final int VALUE_MANY_REP_IDS = 0x6; 53 54 protected MessageWriter _out; 55 protected int _type; 56 57 protected SystemException _nullSystemException; 58 59 protected IiopReader _reader; 60 61 protected String _host; 62 protected int _port; 63 64 private IdentityIntMap _refMap = new IdentityIntMap(); 65 66 private ValueHandler valueHandler = Util.createValueHandler(); 67 private RunTime runTime = valueHandler.getRunTimeCodeBase(); 68 69 74 public void init(MessageWriter out) 75 { 76 _out = out; 77 78 _refMap.clear(); 79 } 80 81 87 public void init(MessageWriter out, IiopReader reader) 88 { 89 init(out); 90 91 _reader = reader; 92 } 93 94 97 public void setHost(String host) 98 { 99 _host = host; 100 } 101 102 105 public String getHost() 106 { 107 return _host; 108 } 109 110 113 public void setPort(int port) 114 { 115 _port = port; 116 } 117 118 121 public int getPort() 122 { 123 return _port; 124 } 125 126 131 public void startRequest(IOR ior, String operation) 132 throws IOException 133 { 134 byte []bytes = ior.getOid(); 135 136 startRequest(bytes, 0, bytes.length, operation); 137 } 138 139 144 public void startRequest(byte []oid, int off, int len, String operation) 145 throws IOException 146 { 147 startRequest(oid, off, len, operation, (int) Alarm.getCurrentTime()); 148 } 149 150 155 abstract public void startRequest(byte []oid, int off, int len, 156 String operation, int requestId) 157 throws IOException ; 158 159 public void writeRequestServiceControlList() 160 throws IOException 161 { 162 write_long(1); 163 writeCodeSetService(); 164 } 165 166 public void writeCodeSetService() 167 throws IOException 168 { 169 write_long(IiopReader.SERVICE_CODE_SET); 170 write_long(12); write_long(0); 172 write_long(0x10001); write_long(0x10100); } 175 176 179 abstract public void startReplyOk(int requestId) 180 throws IOException ; 181 182 185 abstract public void startReplySystemException(int requestId, 186 String exceptionId, 187 int minorStatus, 188 int completionStatus) 189 throws IOException ; 190 191 194 public void startReplyUserException(int requestId, 195 String exceptionId) 196 throws IOException 197 { 198 startReplyUserException(requestId); 199 200 writeString(exceptionId); 201 } 202 203 206 abstract public void startReplyUserException(int requestId) 207 throws IOException ; 208 209 212 public void writeIOR(IOR ior) 213 { 214 byte []bytes = ior.getByteArray(); 215 216 _out.align(4); 217 _out.write(bytes, 0, bytes.length); 218 } 219 220 223 public void writeNullIOR() 224 { 225 write_long(1); 226 write_long(0); 227 write_long(0); 228 } 229 230 233 public void writeNull() 234 { 235 write_long(0); 236 } 237 238 239 public org.omg.CORBA.ORB orb() 240 { 241 ORB orb = ORB.init(); 243 244 return orb; 245 } 246 247 public org.omg.CORBA.portable.InputStream create_input_stream() 248 { 249 throw new UnsupportedOperationException ("no input stream"); 250 } 251 252 255 public void write_boolean_array(boolean []value, int offset, int length) 256 { 257 for (int i = 0; i < length; i++) 258 _out.write(value[i + offset] ? 1 : 0); 259 } 260 261 264 public void write_char_array(char []value, int offset, int length) 265 { 266 for (int i = 0; i < length; i++) 267 _out.write((int) value[i + offset]); 268 } 269 270 273 public void write_wchar_array(char []value, int offset, int length) 274 { 275 for (int i = 0; i < length; i++) 276 write_wchar(value[i + offset]); 277 } 278 279 282 public void write_octet_array(byte []value, int offset, int length) 283 { 284 for (int i = 0; i < length; i++) 285 _out.write((int) value[i + offset]); 286 } 287 288 291 public void write_short_array(short []value, int offset, int length) 292 { 293 for (int i = 0; i < length; i++) 294 _out.writeShort((int) value[i + offset]); 295 } 296 297 300 public void write_ushort_array(short []value, int offset, int length) 301 { 302 _out.align(2); 303 for (int i = 0; i < length; i++) 304 _out.writeShort((int) value[i + offset]); 305 } 306 307 310 public void write_long_array(int []value, int offset, int length) 311 { 312 _out.align(4); 313 for (int i = 0; i < length; i++) 314 _out.writeInt(value[i + offset]); 315 } 316 317 320 public void write_ulong_array(int []value, int offset, int length) 321 { 322 _out.align(4); 323 for (int i = 0; i < length; i++) 324 _out.writeInt(value[i + offset]); 325 } 326 327 330 public void write_longlong_array(long []value, int offset, int length) 331 { 332 _out.align(8); 333 for (int i = 0; i < length; i++) 334 _out.writeLong(value[i + offset]); 335 } 336 337 340 public void write_ulonglong_array(long []value, int offset, int length) 341 { 342 _out.align(8); 343 for (int i = 0; i < length; i++) 344 _out.writeLong(value[i + offset]); 345 } 346 347 350 public void write_float_array(float []value, int offset, int length) 351 { 352 _out.align(4); 353 for (int i = 0; i < length; i++) { 354 float v = value[i + offset]; 355 int bits = Float.floatToIntBits(v); 356 357 _out.writeInt(bits); 358 } 359 } 360 361 364 public void write_double_array(double []value, int offset, int length) 365 { 366 _out.align(8); 367 for (int i = 0; i < length; i++) { 368 double v = value[i + offset]; 369 long bits = Double.doubleToLongBits(v); 370 371 _out.writeLong(bits); 372 } 373 } 374 375 378 public void write_string(String a) 379 { 380 if (a == null) { 381 write_long(0); 382 return; 383 } 384 385 int length = a.length(); 386 write_long(length + 1); 387 for (int i = 0; i < length; i++) 388 _out.write((int) a.charAt(i)); 389 _out.write(0); 390 } 391 392 395 public void write_wstring(String a) 396 { 397 if (a == null) { 398 write_long(0); 399 return; 400 } 401 402 int length = a.length(); 403 write_long(length + 1); 404 for (int i = 0; i < length; i++) 405 _out.writeShort((int) a.charAt(i)); 406 _out.writeShort(0); 407 } 408 409 412 public void write_Object(org.omg.CORBA.Object obj) 413 { 414 if (obj == null) { 415 write_long(1); 416 write(0); 417 write_long(0); 418 } 419 else { 420 writeIOR(((DummyObjectImpl) obj).getIOR()); 421 } 422 } 423 424 427 public void write_TypeCode(TypeCode tc) 428 { 429 if (tc == null) { 431 write_long(TCKind._tk_null); 432 return; 433 } 434 435 try { 436 switch (tc.kind().value()) { 437 case TCKind._tk_null: 438 case TCKind._tk_void: 439 case TCKind._tk_short: 440 case TCKind._tk_ushort: 441 case TCKind._tk_long: 442 case TCKind._tk_ulong: 443 case TCKind._tk_longlong: 444 case TCKind._tk_ulonglong: 445 case TCKind._tk_float: 446 case TCKind._tk_double: 447 case TCKind._tk_longdouble: 448 case TCKind._tk_boolean: 449 case TCKind._tk_char: 450 case TCKind._tk_wchar: 451 case TCKind._tk_octet: 452 case TCKind._tk_any: 453 case TCKind._tk_TypeCode: 454 write_long(tc.kind().value()); 455 break; 456 457 case TCKind._tk_string: 458 write_long(tc.kind().value()); 459 write_long(tc.length()); 460 break; 461 462 case TCKind._tk_sequence: 463 write_long(tc.kind().value()); 464 write_TypeCode(tc.content_type()); 465 write_long(tc.length()); 466 break; 467 468 case TCKind._tk_value: 469 write_long(tc.kind().value()); 471 write_string(tc.id()); 472 write_string(tc.name()); 473 write_short(tc.type_modifier()); 474 write_TypeCode(tc.concrete_base_type()); 475 write_ulong(tc.member_count()); 476 for (int i = 0; i < tc.member_count(); i++) { 477 write_string(tc.member_name(i)); 478 write_TypeCode(tc.member_type(i)); 479 write_short(tc.member_visibility(i)); 480 } 481 break; 482 483 case TCKind._tk_value_box: 484 write_long(tc.kind().value()); 485 write_string(tc.id()); 486 write_string(tc.name()); 487 write_TypeCode(tc.content_type()); 488 break; 489 490 case TCKind._tk_abstract_interface: 491 write_long(tc.kind().value()); 493 write_string(tc.id()); 494 write_string(tc.name()); 495 break; 496 497 default: 498 System.out.println("UNKNOWN TC: " + tc + " " + tc.kind() + " " + tc.kind().value()); 499 throw new UnsupportedOperationException (String.valueOf(tc)); 500 } 501 } catch (Exception e) { 502 throw new RuntimeException (e); 503 } 504 } 505 506 509 public void write_abstract_interface(java.lang.Object obj) 510 { 511 write_boolean(false); 513 514 write_value((Serializable ) obj); 515 } 516 517 public void write_any(Any any) 518 { 519 write_TypeCode(any.type()); 520 any.write_value(this); 521 } 522 523 public void write_Principal(org.omg.CORBA.Principal principal) 524 { 525 throw new UnsupportedOperationException (); 526 } 527 528 public void write_value(Serializable obj, Class javaType) 529 { 530 write_value(obj); 531 } 532 533 public void write_value(Serializable obj) 534 { 535 if (obj == null) { 536 write_long(0); 537 return; 538 } 539 else if (obj instanceof String ) { 540 write_long(VALUE_TAG | VALUE_ONE_REP_ID); 541 write_string("IDL:omg.org/CORBA/WStringValue:1.0"); 542 write_wstring((String ) obj); 543 } 544 else if (obj instanceof Class ) { 545 write_long(VALUE_TAG | VALUE_ONE_REP_ID); 546 write_string(valueHandler.getRMIRepositoryID(ClassDesc .class)); 547 write_value(valueHandler.getRMIRepositoryID((Class ) obj)); 548 write_value(valueHandler.getRMIRepositoryID((Class ) obj)); 549 } 550 else { 551 int oldValue = _refMap.get(obj); 552 553 if (oldValue == _refMap.NULL) { 554 _out.align(4); 555 556 _refMap.put(obj, _out.getOffset()); 557 558 write_long(VALUE_TAG | VALUE_ONE_REP_ID); 559 write_string(valueHandler.getRMIRepositoryID(obj.getClass())); 560 valueHandler.writeValue(this, obj); 561 } 562 else { 563 _out.align(4); 564 565 write_long(0xffffffff); 566 int delta = oldValue - _out.getOffset(); 567 568 write_long(delta); 569 } 570 } 571 } 572 573 576 public void write(int b) 577 { 578 write_long(b); 579 } 580 581 584 public void write_boolean(boolean v) 585 { 586 _out.write(v ? 1 : 0); 587 } 588 589 592 public void write_octet(byte v) 593 { 594 _out.write(v); 595 } 596 597 600 public void write_short(short v) 601 { 602 _out.align(2); 603 _out.writeShort(v); 604 } 605 606 609 public void write_ushort(short v) 610 { 611 _out.align(2); 612 _out.writeShort(v); 613 } 614 615 618 public void write_char(char v) 619 { 620 _out.write(v); 621 } 622 623 626 public void write_wchar(char v) 627 { 628 _out.align(2); 629 _out.writeShort(v); 630 } 631 632 635 public void write_long(int v) 636 { 637 _out.align(4); 638 _out.writeInt(v); 639 } 640 641 644 public void write_ulong(int v) 645 { 646 _out.align(4); 647 _out.writeInt(v); 648 } 649 650 653 public void write_longlong(long v) 654 { 655 _out.align(8); 656 _out.writeLong(v); 657 } 658 659 662 public void write_ulonglong(long v) 663 { 664 _out.align(8); 665 _out.writeLong(v); 666 } 667 668 671 public void write_float(float v) 672 { 673 int bits = Float.floatToIntBits(v); 674 675 _out.align(4); 676 _out.writeInt(bits); 677 } 678 679 682 public void write_double(double v) 683 { 684 long bits = Double.doubleToLongBits(v); 685 686 _out.align(8); 687 _out.writeLong(bits); 688 } 689 690 695 public void writeString(String v) 696 { 697 if (v == null) { 698 write_long(0); 699 return; 700 } 701 702 int len = v.length(); 703 write_long(len + 1); 704 for (int i = 0; i < len; i++) 705 _out.write(v.charAt(i)); 706 _out.write(0); 707 } 708 709 714 public void writeBytes(byte []b, int off, int len) 715 { 716 write_long(len); 717 _out.write(b, off, len); 718 } 719 720 725 public void write(byte []b, int off, int len) 726 { 727 _out.write(b, off, len); 728 } 729 730 public IiopReader _call() 731 throws IOException 732 { 733 _out.close(); 734 735 _reader.readRequest(); 736 737 return _reader; 738 } 739 } 740 | Popular Tags |