1 package org.jacorb.orb; 2 3 22 23 import java.lang.reflect.Method ; 24 import java.util.*; 25 26 import org.apache.avalon.framework.logger.Logger; 27 import org.apache.avalon.framework.configuration.Configurable; 28 import org.apache.avalon.framework.configuration.DefaultConfiguration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 31 import org.jacorb.ir.RepositoryID; 32 import org.jacorb.imr.ImRAccessImpl; 33 import org.jacorb.orb.giop.*; 34 import org.jacorb.orb.iiop.*; 35 import org.jacorb.orb.portableInterceptor.*; 36 import org.jacorb.orb.util.*; 37 import org.jacorb.poa.util.POAUtil; 38 import org.jacorb.util.Time; 39 import org.jacorb.util.ObjectUtil; 40 import org.jacorb.config.Configuration; 41 42 import org.omg.CORBA.*; 43 import org.omg.CORBA.Object ; 44 import org.omg.CORBA.portable.*; 45 import org.omg.GIOP.LocateStatusType_1_2; 46 import org.omg.IOP.ServiceContext ; 47 import org.omg.Messaging.*; 48 import org.omg.PortableInterceptor.SUCCESSFUL ; 49 import org.omg.PortableServer.POAPackage.*; 50 import org.omg.TimeBase.UtcT; 51 import org.omg.PortableServer.ServantLocatorPackage.CookieHolder ; 52 import org.omg.PortableServer.Servant ; 53 54 61 62 public final class Delegate 63 extends org.omg.CORBA_2_3.portable.Delegate 64 implements Configurable 65 { 66 private ParsedIOR _pior = null; 70 private org.omg.IOP.IOR ior = null; 71 private ClientConnection connection = null; 72 private String objectReference = null; 73 74 75 private ParsedIOR piorOriginal = null; 76 77 78 private ParsedIOR piorLastFailed = null; 79 80 81 private boolean isImR = false; 82 83 private boolean bound = false; 84 private org.jacorb.poa.POA poa; 85 86 private org.jacorb.orb.ORB orb = null; 87 private Logger logger = null; 88 89 91 private boolean resolved_locality = false; 92 93 private Set pending_replies = new HashSet(); 94 private Barrier pending_replies_sync = new Barrier(); 95 96 private java.lang.Object bind_sync = new java.lang.Object (); 97 98 private boolean locate_on_bind_performed = false; 99 100 private ClientConnectionManager conn_mg = null; 101 102 private Map policy_overrides; 103 104 private boolean doNotCheckExceptions = false; 106 private CookieHolder cookie = null; 107 108 private String invokedOperation = null; 109 110 111 private org.apache.avalon.framework.configuration.Configuration configuration = null; 112 113 114 private boolean useIMR; 115 private boolean locateOnBind; 116 117 132 133 134 135 136 private Delegate() 137 { 138 } 139 140 public Delegate ( org.jacorb.orb.ORB orb, ParsedIOR pior ) 141 { 142 this.orb = orb; 143 _pior = pior; 144 145 checkIfImR( _pior.getTypeId() ); 146 conn_mg = orb.getClientConnectionManager(); 147 } 148 149 public Delegate( org.jacorb.orb.ORB orb, String object_reference ) 150 { 151 if ( object_reference.indexOf( "IOR:" ) != 0 ) 152 { 153 throw new org.omg.CORBA.INV_OBJREF ( "Not an IOR: " + 154 object_reference ); 155 } 156 157 this.orb = orb; 158 this.objectReference = object_reference; 159 conn_mg = orb.getClientConnectionManager(); 160 } 161 162 public Delegate(org.jacorb.orb.ORB orb, org.omg.IOP.IOR ior) 163 { 164 this.orb = orb; 165 this.ior = ior; 166 conn_mg = orb.getClientConnectionManager(); 167 } 168 169 172 public Delegate( org.jacorb.orb.ORB orb, 173 String object_reference, 174 boolean _donotcheckexceptions ) 175 { 176 this( orb, object_reference ); 177 doNotCheckExceptions = _donotcheckexceptions; 178 } 179 180 181 public void configure(org.apache.avalon.framework.configuration.Configuration configuration) 182 throws org.apache.avalon.framework.configuration.ConfigurationException 183 { 184 this.configuration = configuration; 185 logger = 186 ((Configuration)configuration).getNamedLogger("jacorb.orb.delegate"); 187 useIMR = 188 configuration.getAttribute("jacorb.use_imr","off").equals("on"); 189 locateOnBind = 190 configuration.getAttribute("jacorb.locate_on_bind","off").equals("on"); 191 192 if (objectReference != null) 193 _pior = new ParsedIOR( objectReference, orb, logger); 194 else if (ior!=null) 195 _pior = new ParsedIOR( ior, orb, logger); 196 else if (_pior == null ) 197 throw new ConfigurationException("Neither objectReference nor IOR set!"); 198 checkIfImR( _pior.getTypeId() ); 199 } 200 201 202 public boolean doNotCheckExceptions() 203 { 204 return doNotCheckExceptions; 205 } 206 207 216 private void checkIfImR( String typeId ) 217 { 218 if ( typeId.equals( "IDL:org/jacorb/imr/ImplementationRepository:1.0" ) ) 219 { 220 isImR = true; 221 } 222 } 223 224 225 public int _get_TCKind() 226 { 227 return org.omg.CORBA.TCKind._tk_objref; 228 } 229 230 231 243 private void bind() 244 { 245 if (bound) 247 return ; 248 249 synchronized (bind_sync) 250 { 251 if ( bound ) 252 return ; 253 254 org.omg.ETF.Profile p = _pior.getEffectiveProfile(); 255 if (p == null) 256 throw new org.omg.CORBA.COMM_FAILURE ("no effective profile"); 257 258 connection = conn_mg.getConnection(p); 259 bound = true; 260 261 266 if (( ! locate_on_bind_performed ) && 267 locateOnBind ) 268 { 269 locate_on_bind_performed = true; 272 273 try 274 { 275 LocateRequestOutputStream lros = 276 new LocateRequestOutputStream 277 ( _pior.get_object_key(), 278 connection.getId(), 279 ( int ) _pior.getEffectiveProfile().version().minor ); 280 281 LocateReplyReceiver receiver = 282 new LocateReplyReceiver(orb); 283 284 connection.sendRequest( lros, 285 receiver, 286 lros.getRequestId(), 287 true ); 289 LocateReplyInputStream lris = receiver.getReply(); 290 291 switch ( lris.rep_hdr.locate_status.value() ) 292 { 293 294 case LocateStatusType_1_2._UNKNOWN_OBJECT : 295 { 296 throw new org.omg.CORBA.UNKNOWN ( "Could not bind to object, server does not know it!" ); 297 } 298 299 case LocateStatusType_1_2._OBJECT_HERE : 300 { 301 break; 302 } 303 304 case LocateStatusType_1_2._OBJECT_FORWARD : 305 { 306 } 308 309 case LocateStatusType_1_2._OBJECT_FORWARD_PERM : 310 { 311 rebind( orb.object_to_string( lris.read_Object() ) ); 314 break; 315 } 316 317 case LocateStatusType_1_2._LOC_SYSTEM_EXCEPTION : 318 { 319 throw SystemExceptionHelper.read( lris ); 320 321 } 323 324 case LocateStatusType_1_2._LOC_NEEDS_ADDRESSING_MODE : 325 { 326 throw new org.omg.CORBA.NO_IMPLEMENT ( "Server responded to LocateRequest with a status of LOC_NEEDS_ADDRESSING_MODE, but this isn't yet implemented by JacORB" ); 327 328 } 330 331 default : 332 { 333 throw new RuntimeException ( "Unknown reply status for LOCATE_REQUEST: " + lris.rep_hdr.locate_status.value() ); 334 } 335 336 } 337 338 } 339 catch ( org.omg.CORBA.SystemException se ) 340 { 341 throw se; 343 } 344 catch ( Exception e ) 345 { 346 if (logger.isWarnEnabled()) 347 logger.warn( e.getMessage() ); 348 } 349 350 } 351 352 bind_sync.notifyAll(); 354 } 355 } 356 357 public void rebind(String object_reference) 358 { 359 if (object_reference.indexOf( "IOR:" ) == 0) 362 { 363 rebind( new ParsedIOR( object_reference, orb, logger ) ); 364 } 365 else 366 { 367 throw new org.omg.CORBA.INV_OBJREF ( "Not an IOR: " + 368 object_reference ); 369 } 370 } 372 373 public void rebind( org.omg.CORBA.Object o ) 374 { 375 rebind(orb.object_to_string(o)); 376 } 377 378 public void rebind( ParsedIOR p ) 379 { 380 synchronized ( bind_sync ) 381 { 382 if ( p.equals( _pior ) ) 383 { 384 return ; 386 } 387 388 if ( piorLastFailed != null && piorLastFailed.equals( p ) ) 389 { 390 throw new org.omg.CORBA.TRANSIENT (); 392 } 393 394 if ( piorOriginal == null ) 395 { 396 piorOriginal = _pior; 398 } 399 400 _pior = p; 401 402 if ( connection != null ) 403 { 404 conn_mg.releaseConnection( connection ); 405 connection = null; 406 } 407 408 bound = false; 410 411 bind(); 412 } 413 } 414 415 public org.omg.CORBA.Request create_request( org.omg.CORBA.Object self, 416 org.omg.CORBA.Context ctx, 417 java.lang.String operation , 418 org.omg.CORBA.NVList args, 419 org.omg.CORBA.NamedValue result ) 420 { 421 bind(); 422 423 return new org.jacorb.orb.dii.Request( self, 424 orb, 425 connection, 426 getParsedIOR().get_object_key(), 427 operation, 428 args, 429 ctx, 430 result ); 431 } 432 433 public org.omg.CORBA.Request create_request( org.omg.CORBA.Object self, 434 org.omg.CORBA.Context ctx, 435 String operation, 436 org.omg.CORBA.NVList arg_list, 437 org.omg.CORBA.NamedValue result, 438 org.omg.CORBA.ExceptionList exceptions, 439 org.omg.CORBA.ContextList contexts ) 440 { 441 throw new org.omg.CORBA.NO_IMPLEMENT (); 442 } 443 444 public synchronized org.omg.CORBA.Object duplicate( org.omg.CORBA.Object self ) 445 { 446 return self; 447 } 448 449 public boolean equals(java.lang.Object obj) 450 { 451 return ( obj instanceof org.omg.CORBA.Object && 452 toString().equals( obj.toString() ) ); 453 } 454 455 public boolean equals( org.omg.CORBA.Object self, java.lang.Object obj ) 456 { 457 return equals( obj ); 458 } 459 460 protected void finalize() throws Throwable 461 { 462 try 463 { 464 if ( connection != null ) 465 { 466 conn_mg.releaseConnection( connection ); 469 } 470 471 orb._release( getParsedIOR().getIORString() ); 473 474 if ( logger.isDebugEnabled() ) 475 { 476 logger.debug("Delegate gc'ed!"); 477 } 478 } 479 finally 480 { 481 super.finalize(); 482 } 483 } 484 485 public org.omg.CORBA.DomainManager [] get_domain_managers 486 ( org.omg.CORBA.Object self ) 487 { 488 return null; 489 } 490 491 507 508 public org.omg.CORBA.Policy get_policy( org.omg.CORBA.Object self, 509 int policy_type ) 510 { 511 Policy result = get_client_policy(policy_type); 512 if (result != null) 513 { 514 return result; 516 } 517 else 518 { 519 return get_policy( self, 521 policy_type, 522 request( self, "_get_policy", true ) ); 523 } 524 } 525 526 533 534 public org.omg.CORBA.Policy get_client_policy(int policy_type) 535 { 536 Integer key = new Integer (policy_type); 537 Policy result = null; 538 539 if (policy_overrides != null) 540 { 541 result = (Policy)policy_overrides.get(key); 542 } 543 544 if ( result == null ) 545 { 546 550 org.omg.CORBA.Policy [] orbPolicies = 552 orb.getPolicyManager().get_policy_overrides(new int[]{policy_type}); 553 if ( orbPolicies!= null && orbPolicies.length == 1) 554 result = orbPolicies[0]; 555 } 556 557 return result; 558 } 559 560 561 public org.omg.CORBA.Policy get_policy( org.omg.CORBA.Object self, 562 int policy_type, 563 org.omg.CORBA.portable.OutputStream os ) 564 { 565 while ( true ) 567 { 568 try 569 { 570 os.write_Object( self ); 571 os.write_long( policy_type ); 572 org.omg.CORBA.portable.InputStream is = invoke( self, os ); 573 return org.omg.CORBA.PolicyHelper.narrow( is.read_Object() ); 574 } 575 catch ( RemarshalException r ) 576 { 577 } 578 catch ( ApplicationException _ax ) 579 { 580 String _id = _ax.getId(); 581 throw new RuntimeException ( "Unexpected exception " + _id ); 582 } 583 584 } 585 } 587 public UtcT getRequestEndTime() 588 { 589 Policy p = get_client_policy (REQUEST_END_TIME_POLICY_TYPE.value); 590 if (p != null) 591 return ((org.omg.Messaging.RequestEndTimePolicy)p).end_time(); 592 else 593 return null; 594 } 595 596 public UtcT getReplyEndTime() 597 { 598 Policy p = get_client_policy (REPLY_END_TIME_POLICY_TYPE.value); 599 if (p != null) 600 return ((org.omg.Messaging.ReplyEndTimePolicy)p).end_time(); 601 else 602 return null; 603 } 604 605 public UtcT getRequestStartTime() 606 { 607 Policy p = get_client_policy (REQUEST_START_TIME_POLICY_TYPE.value); 608 if (p != null) 609 return ((org.omg.Messaging.RequestStartTimePolicy)p).start_time(); 610 else 611 return null; 612 } 613 614 public UtcT getReplyStartTime() 615 { 616 Policy p = get_client_policy (REPLY_START_TIME_POLICY_TYPE.value); 617 if (p != null) 618 return ((org.omg.Messaging.ReplyStartTimePolicy)p).start_time(); 619 else 620 return null; 621 } 622 623 public long getRelativeRoundtripTimeout() 624 { 625 Policy p = get_client_policy (RELATIVE_RT_TIMEOUT_POLICY_TYPE.value); 626 if (p != null) 627 return ((org.omg.Messaging.RelativeRoundtripTimeoutPolicy)p) 628 .relative_expiry(); 629 else 630 return -1; 631 } 632 633 public long getRelativeRequestTimeout() 634 { 635 Policy p = get_client_policy (RELATIVE_REQ_TIMEOUT_POLICY_TYPE.value); 636 if (p != null) 637 return ((org.omg.Messaging.RelativeRequestTimeoutPolicy)p) 638 .relative_expiry(); 639 else 640 return -1; 641 } 642 643 public short getSyncScope() 644 { 645 Policy p = get_client_policy (SYNC_SCOPE_POLICY_TYPE.value); 646 if (p != null) 647 return ((org.omg.Messaging.SyncScopePolicy)p).synchronization(); 648 else 649 return ((org.omg.Messaging.SYNC_WITH_TRANSPORT.value)); 650 } 651 652 655 656 public org.omg.CORBA.InterfaceDef get_interface( org.omg.CORBA.Object self ) 657 { 658 return org.omg.CORBA.InterfaceDefHelper.narrow( get_interface_def( self ) ) ; 659 } 660 661 public org.omg.CORBA.Object get_interface_def (org.omg.CORBA.Object self) 662 { 663 665 if (is_really_local (self)) 666 { 667 org.omg.PortableServer.Servant servant; 668 org.omg.CORBA.portable.ServantObject so; 669 670 so = servant_preinvoke (self, "_interface", java.lang.Object .class); 671 672 if (so == null ) 675 { 676 throw new OBJ_ADAPTER ( "Servant from pre_invoke was null" ); 677 } 678 try 679 { 680 servant = (org.omg.PortableServer.Servant ) so.servant; 681 orb.set_delegate (servant); 682 return (servant._get_interface_def()); 683 } 684 finally 685 { 686 servant_postinvoke (self, so); 687 } 688 } 689 else 690 { 691 org.omg.CORBA.portable.OutputStream os; 692 org.omg.CORBA.portable.InputStream is; 693 694 while (true) 695 { 696 try 697 { 698 os = request (self, "_interface", true); 699 is = invoke (self, os); 700 return is.read_Object(); 701 } 702 catch (RemarshalException re) 703 { 704 } 705 catch (Exception ex) 706 { 707 return null; 708 } 709 } 710 } 711 } 712 713 ClientConnection getConnection() 714 { 715 synchronized ( bind_sync ) 716 { 717 bind(); 718 return connection; 719 } 720 } 721 722 public org.omg.IOP.IOR getIOR() 723 { 724 synchronized ( bind_sync ) 725 { 726 if ( piorOriginal != null ) 727 { 728 return piorOriginal.getIOR(); 729 } 730 else 731 { 732 return getParsedIOR().getIOR(); 733 } 734 } 735 } 736 737 public byte[] getObjectId() 738 { 739 synchronized ( bind_sync ) 740 { 741 bind(); 742 743 return POAUtil.extractOID( getParsedIOR().get_object_key() ); 744 } 745 } 746 747 public byte[] getObjectKey() 748 { 749 synchronized ( bind_sync ) 750 { 751 bind(); 752 753 return getParsedIOR().get_object_key(); 754 } 755 } 756 757 public ParsedIOR getParsedIOR() 758 { 759 synchronized ( bind_sync ) 760 { 761 while ( _pior == null ) 762 { 763 try 764 { 765 bind_sync.wait(); 766 } 767 catch ( InterruptedException ie ) 768 { 769 } 770 } 771 772 return _pior; 773 } 774 } 775 776 public void resolvePOA (org.omg.CORBA.Object self) 777 { 778 if (! resolved_locality) 779 { 780 resolved_locality = true; 781 org.jacorb.poa.POA local_poa = orb.findPOA (this, self); 782 783 if (local_poa != null) 784 { 785 poa = local_poa; 786 } 787 } 788 } 789 790 public org.jacorb.poa.POA getPOA() 791 { 792 return ( org.jacorb.poa.POA ) poa; 793 } 794 795 797 798 public org.omg.CORBA.portable.ObjectImpl getReference( org.jacorb.poa.POA _poa ) 799 { 800 if ( logger.isDebugEnabled() ) 801 { 802 logger.debug("Delegate.getReference with POA <" + 803 ( _poa != null ? _poa._getQualifiedName() : " empty" ) + ">" ); 804 } 805 806 if ( _poa != null ) poa = _poa; 808 809 org.omg.CORBA.portable.ObjectImpl o = 810 new org.jacorb.orb.Reference( typeId() ); 811 812 o._set_delegate( this ); 813 814 return o; 815 } 816 817 public int hash( org.omg.CORBA.Object self, int x ) 818 { 819 return hashCode(); 820 } 821 822 public int hashCode() 823 { 824 return getIDString().hashCode(); 825 } 826 827 public int hashCode( org.omg.CORBA.Object self ) 828 { 829 return hashCode(); 830 } 831 832 837 public void invoke( org.omg.CORBA.Object self, 838 org.omg.CORBA.portable.OutputStream os, 839 org.omg.Messaging.ReplyHandler replyHandler ) 840 throws ApplicationException, RemarshalException 841 { 842 invoke_internal (self, os, replyHandler, true); 844 } 845 846 852 public org.omg.CORBA.portable.InputStream invoke 853 ( org.omg.CORBA.Object self, 854 org.omg.CORBA.portable.OutputStream os ) 855 throws ApplicationException, RemarshalException 856 { 857 return invoke_internal (self, os, null, false); 858 } 859 860 866 private org.omg.CORBA.portable.InputStream invoke_internal 867 ( org.omg.CORBA.Object self, 868 org.omg.CORBA.portable.OutputStream os, 869 org.omg.Messaging.ReplyHandler replyHandler, 870 boolean async ) 871 throws ApplicationException, RemarshalException 872 { 873 RequestOutputStream ros = (RequestOutputStream)os; 874 ReplyReceiver receiver = null; 875 876 ClientInterceptorHandler interceptors = 877 new ClientInterceptorHandler( orb, ros, self, this, 878 piorOriginal, connection ); 879 880 interceptors.handle_send_request(); 881 882 try 883 { 884 if ( !ros.response_expected() ) { 886 invoke_oneway (ros, interceptors); 887 return null; 888 } 889 else { 891 receiver = new ReplyReceiver(this, 892 ros.operation(), 893 ros.getReplyEndTime(), 894 interceptors, 895 replyHandler ); 896 897 901 synchronized ( pending_replies ) 902 { 903 pending_replies.add ( receiver ); 904 } 905 906 synchronized ( bind_sync ) 907 { 908 if ( ros.getConnection() == connection ) 909 { 910 connection.sendRequest( ros, 913 receiver, 914 ros.requestId(), 915 true ); } 917 else 918 { 919 if (logger.isDebugEnabled()) 920 logger.debug("invoke: RemarshalException"); 921 922 throw new RemarshalException(); 925 } 926 } 927 } 928 } 929 catch ( org.omg.CORBA.SystemException cfe ) 930 { 931 if (logger.isDebugEnabled()) 932 logger.debug("invoke: SystemException"); 933 934 if( !async ) 935 { 936 synchronized (pending_replies) 940 { 941 pending_replies.remove (receiver); 942 } 943 } 944 945 interceptors.handle_receive_exception ( cfe ); 946 947 if ( cfe instanceof org.omg.CORBA.TRANSIENT ) 948 { 949 if ( try_rebind() ) 951 { 952 throw new RemarshalException(); 953 } 954 } 955 956 throw cfe; 957 } 958 959 if ( !async && receiver != null ) 960 { 961 org.omg.CORBA.portable.InputStream is = receiver.getReply(); 964 965 return is; 966 } 967 else 968 { 969 return null; 970 } 971 } 972 973 private void invoke_oneway (RequestOutputStream ros, 974 ClientInterceptorHandler interceptors) 975 throws RemarshalException, ApplicationException 976 { 977 switch (ros.syncScope()) 978 { 979 case SYNC_NONE.value: 980 passToTransport (ros); 981 interceptors.handle_receive_other (SUCCESSFUL.value); 982 break; 983 984 case SYNC_WITH_TRANSPORT.value: 985 connection.sendRequest (ros, false); 986 interceptors.handle_receive_other (SUCCESSFUL.value); 987 break; 988 989 case SYNC_WITH_SERVER.value: 990 case SYNC_WITH_TARGET.value: 991 ReplyReceiver rcv = new ReplyReceiver (this, 992 ros.operation(), 993 ros.getReplyEndTime(), 994 interceptors, 995 null); 996 connection.sendRequest (ros, rcv, ros.requestId(), true); 997 ReplyInputStream in = rcv.getReply(); 998 interceptors.handle_receive_reply (in); 999 break; 1000 1001 default: 1002 throw new org.omg.CORBA.MARSHAL 1003 ("Illegal SYNC_SCOPE: " + ros.syncScope(), 1004 0, CompletionStatus.COMPLETED_MAYBE); 1005 } 1006 } 1007 1008 private void passToTransport (final RequestOutputStream ros) 1009 { 1010 new Thread (new Runnable () 1011 { 1012 public void run() 1013 { 1014 connection.sendRequest (ros, false); 1015 } 1016 }).start(); 1017 } 1018 1019 private boolean try_rebind() 1020 { 1021 synchronized ( bind_sync ) 1022 { 1023 if( logger.isDebugEnabled()) 1024 { 1025 logger.debug("Delegate.try_rebind" ); 1026 } 1027 1028 if ( piorOriginal != null ) 1029 { 1030 if( logger.isDebugEnabled()) 1031 { 1032 logger.debug("Delegate: falling back to original IOR"); 1033 } 1034 1035 piorLastFailed = getParsedIOR(); 1037 1038 rebind( piorOriginal ); 1040 1041 piorOriginal = null; 1043 piorLastFailed = null; 1045 return true; 1046 } 1047 else if ( useIMR && ! isImR ) 1048 { 1049 Integer orbTypeId = getParsedIOR().getORBTypeId(); 1050 1051 if ( orbTypeId == null || 1053 orbTypeId.intValue() != ORBConstants.JACORB_ORB_ID ) 1054 { 1055 if( logger.isDebugEnabled()) 1056 { 1057 logger.debug("Delegate: foreign IOR detected" ); 1058 } 1059 return false; 1060 } 1061 1062 if( logger.isDebugEnabled()) 1063 { 1064 logger.debug("Delegate: JacORB IOR detected" ); 1065 } 1066 1067 byte[] object_key = getParsedIOR().get_object_key(); 1068 1069 1073 if( logger.isDebugEnabled()) 1074 { 1075 logger.debug("Delegate: attempting to contact ImR" ); 1076 } 1077 1078 ImRAccess imr = null; 1079 1080 try 1081 { 1082 imr = ImRAccessImpl.connect(orb); 1083 } 1084 catch ( Exception e ) 1085 { 1086 if( logger.isDebugEnabled()) 1087 { 1088 logger.debug("Delegate: failed to contact ImR" ); 1089 } 1090 return false; 1091 } 1092 1093 StringBuffer corbaloc = new StringBuffer ( "corbaloc:iiop:" ); 1095 1096 corbaloc.append( imr.getImRHost() ); 1097 corbaloc.append( ":" ); 1098 corbaloc.append( imr.getImRPort() ); 1099 corbaloc.append( "/" ); 1100 corbaloc.append( CorbaLoc.parseKey( object_key ) ); 1101 1102 rebind( new ParsedIOR( corbaloc.toString(), orb, logger)); 1104 1105 piorOriginal = null; 1107 piorLastFailed = null; 1109 return true; 1110 } 1111 else 1112 { 1113 return false; 1114 } 1115 } 1116 } 1117 1118 public void invokeInterceptors( ClientRequestInfoImpl info, short op ) 1119 throws RemarshalException 1120 { 1121 ClientInterceptorIterator intercept_iter = 1122 orb.getInterceptorManager().getClientIterator(); 1123 1124 try 1125 { 1126 intercept_iter.iterate( info, op ); 1127 } 1128 catch ( org.omg.PortableInterceptor.ForwardRequest fwd ) 1129 { 1130 rebind( orb.object_to_string( fwd.forward ) ); 1131 throw new RemarshalException(); 1132 } 1133 catch ( org.omg.CORBA.UserException ue ) 1134 { 1135 if (logger.isErrorEnabled()) 1136 logger.error( ue.getMessage() ); 1137 } 1138 } 1139 1140 1144 1145 public boolean is_a( org.omg.CORBA.Object self, String logical_type_id ) 1146 { 1147 1148 1149 1155 1156 ParsedIOR pior = getParsedIOR(); 1157 1158 if ( pior.getTypeId().equals( logical_type_id ) ) 1159 return true; 1160 1161 1164 String [] ids = ( ( org.omg.CORBA.portable.ObjectImpl ) self )._ids(); 1165 1166 1167 for ( int i = 0; i < ids.length - 1; i++ ) 1168 { 1169 if ( ids[ i ].equals( logical_type_id ) ) 1170 return true; 1171 } 1172 1173 1175 1176 if (is_really_local (self)) 1178 { 1179 org.omg.PortableServer.Servant servant; 1180 org.omg.CORBA.portable.ServantObject so; 1181 1182 so = servant_preinvoke (self, "_is_a", java.lang.Object .class); 1183 1184 if (so == null ) 1187 { 1188 throw new OBJ_ADAPTER( "Servant from pre_invoke was null" ); 1189 } 1190 try 1191 { 1192 servant = (org.omg.PortableServer.Servant )so.servant; 1193 orb.set_delegate(servant); 1194 return servant._is_a(logical_type_id); 1195 } 1196 finally 1197 { 1198 servant_postinvoke(self, so); 1199 } 1200 } 1201 else 1202 { 1203 try 1205 { 1206 1209 String classname = RepositoryID.className( ids[0], "_Stub", null ); 1210 1211 int lastDot = classname.lastIndexOf( '.' ); 1212 StringBuffer scn = new StringBuffer ( classname.substring( 0, lastDot + 1) ); 1213 scn.append( '_' ); 1214 scn.append( classname.substring( lastDot + 1 ) ); 1215 1216 1220 Class stub=null; 1224 try { 1225 stub = ObjectUtil.classForName( scn.toString()); 1226 } catch (ClassNotFoundException e) { 1227 stub = ObjectUtil.classForName("org.omg.stub."+scn.toString()); 1228 } 1229 1230 Method idm = stub.getMethod ( "_ids", (Class []) null ); 1231 String newids[] = (String [] )idm.invoke( stub.newInstance(), (java.lang.Object []) null ); 1232 1233 for ( int i = 0; i < newids.length ; i++ ) 1234 { 1235 if (newids[i].equals( logical_type_id ) ) 1236 { 1237 return true; 1238 } 1239 } 1240 } 1241 catch (Throwable e) 1243 { 1244 if (logger.isDebugEnabled()) 1245 logger.debug("trying is_a remotely"); 1246 } 1247 1248 org.omg.CORBA.portable.OutputStream os; 1249 org.omg.CORBA.portable.InputStream is; 1250 1251 while (true) 1252 { 1253 try 1254 { 1255 os = request(self, "_is_a", true); 1256 os.write_string(logical_type_id); 1257 is = invoke(self, os); 1258 return is.read_boolean(); 1259 } 1260 catch (RemarshalException re) 1261 { 1262 } 1263 catch (ApplicationException ax) 1264 { 1265 throw new RuntimeException ("Unexpected exception " + ax.getId()); 1266 } 1267 } 1268 } 1269 } 1270 1271 public boolean is_equivalent(org.omg.CORBA.Object self, 1272 org.omg.CORBA.Object obj) 1273 { 1274 boolean result = true; 1275 1276 if (self != obj) 1277 { 1278 ParsedIOR pior1 = new ParsedIOR( obj.toString(), orb, logger ); 1279 ParsedIOR pior2 = new ParsedIOR( self.toString(), orb, logger ); 1280 result = pior2.getIDString().equals( pior1.getIDString() ); 1281 } 1282 1283 return result; 1284 } 1285 1286 public String getIDString() 1287 { 1288 return getParsedIOR().getIDString(); 1289 } 1290 1291 1297 1298 public boolean is_local(org.omg.CORBA.Object self) 1299 { 1300 if (orb.hasRequestInterceptors()) 1301 { 1302 return false; 1303 } 1304 return is_really_local(self); 1305 } 1306 1307 1310 1311 private boolean is_really_local(org.omg.CORBA.Object self) 1312 { 1313 if (poa == null) 1314 { 1315 resolvePOA(self); 1316 } 1317 1318 return poa != null; 1319 } 1320 1321 public boolean is_nil() 1322 { 1323 ParsedIOR pior = getParsedIOR(); 1324 1325 return ( pior.getIOR().type_id.equals( "" ) && 1326 pior.getIOR().profiles.length == 0 ); 1327 } 1328 1329 public boolean non_existent (org.omg.CORBA.Object self) 1330 { 1331 1333 if (is_really_local(self)) 1334 { 1335 org.omg.PortableServer.Servant servant; 1336 org.omg.CORBA.portable.ServantObject so; 1337 1338 so = servant_preinvoke(self, "_non_existent", java.lang.Object .class); 1339 1340 try 1341 { 1342 servant = (org.omg.PortableServer.Servant )so.servant; 1343 orb.set_delegate(servant); 1344 return servant._non_existent(); 1345 } 1346 finally 1347 { 1348 servant_postinvoke(self, so); 1349 } 1350 } 1351 else 1352 { 1353 org.omg.CORBA.portable.OutputStream os; 1354 org.omg.CORBA.portable.InputStream is; 1355 1356 while (true) 1357 { 1358 try 1359 { 1360 os = request(self, "_non_existent", true); 1361 is = invoke(self, os); 1362 return is.read_boolean(); 1363 } 1364 catch (RemarshalException re) 1365 { 1366 } 1367 catch (ApplicationException e) 1368 { 1369 throw new RuntimeException ( "Unexpected exception " + e.getId() ); 1370 } 1371 } 1372 } 1373 } 1374 1375 public org.omg.CORBA.ORB orb( org.omg.CORBA.Object self ) 1376 { 1377 return orb; 1378 } 1379 1380 public synchronized void release( org.omg.CORBA.Object self ) 1381 { 1382 } 1383 1384 1387 public void releaseReply( org.omg.CORBA.Object self, 1388 org.omg.CORBA.portable.InputStream is ) 1389 { 1390 if ( is != null ) 1391 { 1392 try 1393 { 1394 is.close(); 1395 } 1396 catch ( java.io.IOException io ) 1397 { 1398 } 1399 } 1400 Time.waitFor (getReplyStartTime()); 1401 } 1402 1403 public synchronized org.omg.CORBA.Request request( org.omg.CORBA.Object self, 1404 String operation ) 1405 { 1406 synchronized ( bind_sync ) 1407 { 1408 bind(); 1409 return new org.jacorb.orb.dii.Request( self, 1410 orb, 1411 connection, 1412 getParsedIOR().get_object_key(), 1413 operation ); 1414 } 1415 1416 } 1417 1418 1420 1421 public synchronized org.omg.CORBA.portable.OutputStream request 1422 ( org.omg.CORBA.Object self, 1423 String operation, 1424 boolean responseExpected ) 1425 { 1426 1431 UtcT requestEndTime = getRequestEndTime(); 1432 long requestTimeout = getRelativeRequestTimeout(); 1433 1434 if ((requestTimeout != 0) || (requestEndTime != null)) { 1435 requestEndTime = Time.earliest(Time.corbaFuture (requestTimeout), 1436 requestEndTime); 1437 if (Time.hasPassed(requestEndTime)) 1438 throw new TIMEOUT("Request End Time exceeded prior to invocation", 1439 0, CompletionStatus.COMPLETED_NO); 1440 } 1441 1442 UtcT replyEndTime = getReplyEndTime(); 1443 long roundtripTimeout = getRelativeRoundtripTimeout(); 1444 1445 if ((roundtripTimeout != 0) || (replyEndTime != null)) { 1446 replyEndTime = Time.earliest(Time.corbaFuture (roundtripTimeout), 1447 replyEndTime); 1448 if (Time.hasPassed(replyEndTime)) 1449 throw new TIMEOUT("Reply End Time exceeded prior to invocation", 1450 0, CompletionStatus.COMPLETED_NO); 1451 } 1452 1453 synchronized ( bind_sync ) 1454 { 1455 bind(); 1456 1457 ParsedIOR p = getParsedIOR(); 1458 1459 RequestOutputStream ros = 1460 new RequestOutputStream( connection, 1461 connection.getId(), 1462 operation, 1463 responseExpected, 1464 getSyncScope(), 1465 getRequestStartTime(), 1466 requestEndTime, 1467 replyEndTime, 1468 p.get_object_key(), 1469 ( int ) p.getEffectiveProfile().version().minor ); 1470 1471 if (!connection.isTCSNegotiated()) 1475 { 1476 connection.setCodeSet(p); 1477 } 1478 1479 ros.setCodeSet( connection.getTCS(), connection.getTCSW() ); 1484 1485 return ros; 1486 } 1487 1488 } 1489 1490 1494 1495 1496 public void servant_postinvoke( org.omg.CORBA.Object self, ServantObject servant ) 1497 { 1498 if (poa != null) 1499 { 1500 if ( poa.isUseServantManager() ) 1501 { 1502 if (! poa.isRetain() && 1503 cookie != null && 1504 invokedOperation != null ) 1505 { 1506 try 1509 { 1510 byte [] oid = 1511 POAUtil.extractOID( getParsedIOR().get_object_key() ); 1512 org.omg.PortableServer.ServantLocator sl = 1513 ( org.omg.PortableServer.ServantLocator ) poa.get_servant_manager(); 1514 1515 sl.postinvoke( oid, poa, invokedOperation, cookie.value, (Servant )servant.servant ); 1516 1517 cookie = null; 1519 invokedOperation = null; 1520 } 1521 catch ( Throwable e ) 1522 { 1523 if (logger.isWarnEnabled()) 1524 logger.warn( e.getMessage() ); 1525 } 1526 } 1527 } 1528 poa.removeLocalRequest(); 1529 } 1530 orb.getPOACurrent()._removeContext( Thread.currentThread() ); 1531 } 1532 1533 1537 1538 public ServantObject servant_preinvoke( org.omg.CORBA.Object self, 1539 String operation, 1540 Class expectedType ) 1541 { 1542 if (poa == null) 1543 { 1544 resolvePOA(self); 1545 } 1546 1547 if (poa != null) 1548 { 1549 poa.addLocalRequest(); 1553 1554 ServantObject so = new ServantObject(); 1555 1556 try 1557 { 1558 if ( ( poa.isRetain() && !poa.isUseServantManager() ) || 1559 poa.useDefaultServant() ) 1560 { 1561 try 1563 { 1564 so.servant = poa.reference_to_servant( self ); 1565 } 1566 catch( WrongAdapter e ) 1567 { 1568 poa.removeLocalRequest(); 1570 throw new OBJ_ADAPTER( "WrongAdapter caught when converting servant to reference. " + e ); 1571 } 1572 catch( WrongPolicy e ) 1573 { 1574 poa.removeLocalRequest(); 1576 throw new OBJ_ADAPTER("WrongPolicy caught" + e ); 1577 } 1578 catch( ObjectNotActive e ) 1579 { 1580 poa.removeLocalRequest(); 1582 throw new org.omg.CORBA.OBJECT_NOT_EXIST (); 1583 } 1584 } 1585 else if ( poa.isUseServantManager() ) 1586 { 1587 byte [] oid = 1588 POAUtil.extractOID( getParsedIOR().get_object_key() ); 1589 org.omg.PortableServer.ServantManager sm = 1590 poa.get_servant_manager(); 1591 1592 if ( poa.isRetain() ) 1593 { 1594 try 1597 { 1598 so.servant = poa.id_to_servant( oid ); 1599 } 1600 catch( ObjectNotActive ona ) 1601 { 1602 org.omg.PortableServer.ServantActivator sa = 1604 ( org.omg.PortableServer.ServantActivator ) sm ; 1605 org.omg.PortableServer.ServantActivatorHelper.narrow( sm ); 1606 so.servant = sa.incarnate( oid, poa ); 1607 orb.set_delegate(so.servant); 1608 } 1609 } 1610 else 1611 { 1612 1615 org.omg.PortableServer.ServantLocator sl = 1616 ( org.omg.PortableServer.ServantLocator ) sm; 1617 1618 1620 cookie = 1621 new org.omg.PortableServer.ServantLocatorPackage.CookieHolder (); 1622 1623 invokedOperation = operation; 1624 1625 so.servant = 1626 sl.preinvoke( oid, poa, operation, cookie ); 1627 } 1628 } 1629 else 1630 { 1631 throw new INTERNAL("Internal error: we should not have gotten to this piece of code!"); 1632 } 1633 } 1634 catch( WrongPolicy e ) 1635 { 1636 poa.removeLocalRequest(); 1638 throw new OBJ_ADAPTER( "WrongPolicy caught" + e ); 1639 } 1640 catch( org.omg.PortableServer.ForwardRequest e ) 1641 { 1642 if( logger.isDebugEnabled() ) 1643 { 1644 logger.debug("Caught forwardrequest to " + e.forward_reference + " from " + self ); 1645 } 1646 return servant_preinvoke(e.forward_reference, operation, expectedType); 1647 } 1648 1649 if ( !expectedType.isInstance( so.servant ) ) 1650 { 1651 if( logger.isWarnEnabled() ) 1652 { 1653 logger.warn("Expected " + expectedType + 1654 " got " + so.servant.getClass() ); 1655 } 1656 1657 poa.removeLocalRequest(); 1658 return null; 1659 } 1660 else 1661 { 1662 orb.getPOACurrent()._addContext( 1663 Thread.currentThread(), 1664 new org.jacorb.poa.LocalInvocationContext( 1665 orb, 1666 poa, 1667 getObjectId(), 1668 ( org.omg.PortableServer.Servant ) so.servant 1669 ) 1670 ); 1671 } 1672 return so; 1673 } 1674 if (logger.isWarnEnabled()) 1675 { 1676 logger.warn("No POA! servant_preinvoke returns null"); 1677 } 1678 return null; 1679 } 1680 1681 public String toString() 1682 { 1683 synchronized ( bind_sync ) 1684 { 1685 if ( piorOriginal != null ) 1686 return piorOriginal.getIORString(); 1687 else 1688 return getParsedIOR().getIORString(); 1689 } 1690 1691 } 1692 1693 public String toString( org.omg.CORBA.Object self ) 1694 { 1695 return toString(); 1696 } 1697 1698 public String typeId() 1699 { 1700 return getParsedIOR().getIOR().type_id; 1701 } 1702 1703 public org.omg.CORBA.Object set_policy_override( org.omg.CORBA.Object self, 1704 org.omg.CORBA.Policy [] policies, 1705 org.omg.CORBA.SetOverrideType set_add ) 1706 { 1707 if (policy_overrides == null) 1708 { 1709 policy_overrides = new HashMap(); 1710 } 1711 if ( set_add == org.omg.CORBA.SetOverrideType.SET_OVERRIDE ) 1712 { 1713 policy_overrides.clear(); 1714 } 1715 1716 for ( int i = 0; i < policies.length; i++ ) 1717 { 1718 policy_overrides.put( new Integer ( policies[ i ].policy_type() ), policies[ i ] ); 1719 } 1720 1721 return self; 1722 } 1723 1724 public String get_codebase( org.omg.CORBA.Object self ) 1725 { 1726 return getParsedIOR().getCodebaseComponent(); 1727 } 1728 1729 public Set get_pending_replies() 1730 { 1731 return pending_replies; 1732 } 1733 1734 public void replyDone (ReplyPlaceholder placeholder) 1735 { 1736 synchronized (pending_replies) 1737 { 1738 pending_replies.remove (placeholder); 1739 } 1740 } 1741 1742 public void lockBarrier() 1743 { 1744 pending_replies_sync.lockBarrier(); 1745 } 1746 1747 public void waitOnBarrier() 1748 { 1749 pending_replies_sync.waitOnBarrier(); 1750 } 1751 1752 public void openBarrier() 1753 { 1754 pending_replies_sync.openBarrier(); 1755 } 1756 1757 private static class Barrier 1758 { 1759 private boolean is_open = true; 1760 1761 public synchronized void waitOnBarrier() 1762 { 1763 while (! is_open) 1764 { 1765 try 1766 { 1767 this.wait(); 1768 } 1769 catch ( InterruptedException e ) 1770 { 1771 } 1773 } 1774 } 1775 1776 public synchronized void lockBarrier() 1777 { 1778 is_open = false; 1779 } 1780 1781 public synchronized void openBarrier() 1782 { 1783 is_open = true; 1784 1785 this.notifyAll(); 1786 } 1787 } 1788} 1789 | Popular Tags |