1 7 package com.sun.corba.se.impl.interceptors; 8 9 import java.util.*; 10 import java.io.IOException ; 11 12 import org.omg.CORBA.Any ; 13 import org.omg.CORBA.BAD_PARAM ; 14 import org.omg.CORBA.BAD_POLICY ; 15 import org.omg.CORBA.BAD_INV_ORDER ; 16 import org.omg.CORBA.COMM_FAILURE ; 17 import org.omg.CORBA.CompletionStatus ; 18 import org.omg.CORBA.INTERNAL ; 19 import org.omg.CORBA.NVList ; 20 import org.omg.CORBA.OBJECT_NOT_EXIST ; 21 import org.omg.CORBA.ORBPackage.InvalidName ; 22 import org.omg.CORBA.SystemException ; 23 import org.omg.CORBA.UserException ; 24 import org.omg.CORBA.UNKNOWN ; 25 26 import org.omg.CORBA.portable.ApplicationException ; 27 import org.omg.CORBA.portable.RemarshalException ; 28 29 import org.omg.IOP.CodecFactory ; 30 31 import org.omg.PortableInterceptor.ForwardRequest ; 32 import org.omg.PortableInterceptor.Current ; 33 import org.omg.PortableInterceptor.Interceptor ; 34 import org.omg.PortableInterceptor.LOCATION_FORWARD ; 35 import org.omg.PortableInterceptor.ORBInitializer ; 36 import org.omg.PortableInterceptor.ORBInitInfo ; 37 import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName ; 38 import org.omg.PortableInterceptor.SUCCESSFUL ; 39 import org.omg.PortableInterceptor.SYSTEM_EXCEPTION ; 40 import org.omg.PortableInterceptor.TRANSPORT_RETRY ; 41 import org.omg.PortableInterceptor.USER_EXCEPTION ; 42 import org.omg.PortableInterceptor.PolicyFactory ; 43 import org.omg.PortableInterceptor.ObjectReferenceTemplate ; 44 45 import com.sun.corba.se.pept.encoding.OutputObject; 46 47 import com.sun.corba.se.spi.ior.IOR; 48 import com.sun.corba.se.spi.ior.ObjectKeyTemplate; 49 import com.sun.corba.se.spi.oa.ObjectAdapter; 50 import com.sun.corba.se.spi.orb.ORB; 51 import com.sun.corba.se.spi.orbutil.closure.ClosureFactory; 52 import com.sun.corba.se.spi.protocol.CorbaMessageMediator; 53 import com.sun.corba.se.spi.protocol.ForwardException; 54 import com.sun.corba.se.spi.protocol.PIHandler; 55 import com.sun.corba.se.spi.logging.CORBALogDomains; 56 57 import com.sun.corba.se.impl.logging.InterceptorsSystemException; 58 import com.sun.corba.se.impl.logging.ORBUtilSystemException; 59 import com.sun.corba.se.impl.logging.OMGSystemException; 60 import com.sun.corba.se.impl.corba.RequestImpl; 61 import com.sun.corba.se.impl.orbutil.ORBClassLoader; 62 import com.sun.corba.se.impl.orbutil.ORBConstants; 63 import com.sun.corba.se.impl.orbutil.ORBUtility; 64 import com.sun.corba.se.impl.orbutil.StackImpl; 65 import com.sun.corba.se.impl.protocol.giopmsgheaders.ReplyMessage; 66 67 70 public class PIHandlerImpl implements PIHandler 71 { 72 boolean printPushPopEnabled = false; 74 int pushLevel = 0; 75 private void printPush() 76 { 77 if (! printPushPopEnabled) return; 78 printSpaces(pushLevel); 79 pushLevel++; 80 System.out.println("PUSH"); 81 } 82 private void printPop() 83 { 84 if (! printPushPopEnabled) return; 85 pushLevel--; 86 printSpaces(pushLevel); 87 System.out.println("POP"); 88 } 89 private void printSpaces(int n) 90 { 91 for (int i = 0; i < n; i++) { 92 System.out.print(" "); 93 } 94 } 95 96 private ORB orb ; 97 InterceptorsSystemException wrapper ; 98 ORBUtilSystemException orbutilWrapper ; 99 OMGSystemException omgWrapper ; 100 101 private int serverRequestIdCounter = 0; 104 105 CodecFactory codecFactory = null; 107 108 String [] arguments = null; 111 112 private InterceptorList interceptorList; 114 115 private boolean hasIORInterceptors; 118 private boolean hasClientInterceptors; private boolean hasServerInterceptors; 120 121 private InterceptorInvoker interceptorInvoker; 123 124 private PICurrent current; 126 127 private HashMap policyFactoryTable; 132 133 private final static short REPLY_MESSAGE_TO_PI_REPLY_STATUS[] = { 137 SUCCESSFUL.value, USER_EXCEPTION.value, SYSTEM_EXCEPTION.value, LOCATION_FORWARD.value, LOCATION_FORWARD.value, TRANSPORT_RETRY.value }; 144 145 private ThreadLocal threadLocalClientRequestInfoStack = 148 new ThreadLocal () { 149 protected Object initialValue() { 150 return new RequestInfoStack(); 151 } 152 }; 153 154 private ThreadLocal threadLocalServerRequestInfoStack = 156 new ThreadLocal () { 157 protected Object initialValue() { 158 return new RequestInfoStack(); 159 } 160 }; 161 162 private final class RequestInfoStack extends Stack { 168 public int disableCount = 0; 173 } 174 175 public PIHandlerImpl( ORB orb, String [] args ) { 176 this.orb = orb ; 177 wrapper = InterceptorsSystemException.get( orb, 178 CORBALogDomains.RPC_PROTOCOL ) ; 179 orbutilWrapper = ORBUtilSystemException.get( orb, 180 CORBALogDomains.RPC_PROTOCOL ) ; 181 omgWrapper = OMGSystemException.get( orb, 182 CORBALogDomains.RPC_PROTOCOL ) ; 183 arguments = args ; 184 185 codecFactory = new CodecFactoryImpl( orb ); 187 188 interceptorList = new InterceptorList( wrapper ); 190 191 current = new PICurrent( orb ); 193 194 interceptorInvoker = new InterceptorInvoker( orb, interceptorList, 196 current ); 197 198 orb.getLocalResolver().register( ORBConstants.PI_CURRENT_NAME, 200 ClosureFactory.makeConstant( current ) ) ; 201 orb.getLocalResolver().register( ORBConstants.CODEC_FACTORY_NAME, 202 ClosureFactory.makeConstant( codecFactory ) ) ; 203 } 204 205 public void initialize() { 206 if( orb.getORBData().getORBInitializers() != null ) { 208 ORBInitInfoImpl orbInitInfo = createORBInitInfo(); 210 211 current.setORBInitializing( true ); 214 215 preInitORBInitializers( orbInitInfo ); 217 218 postInitORBInitializers( orbInitInfo ); 220 221 interceptorList.sortInterceptors(); 223 224 current.setORBInitializing( false ); 227 228 orbInitInfo.setStage( ORBInitInfoImpl.STAGE_CLOSED ); 230 231 hasIORInterceptors = interceptorList.hasInterceptorsOfType( 234 InterceptorList.INTERCEPTOR_TYPE_IOR ); 235 hasClientInterceptors = true; 243 hasServerInterceptors = interceptorList.hasInterceptorsOfType( 244 InterceptorList.INTERCEPTOR_TYPE_SERVER ); 245 246 interceptorInvoker.setEnabled( true ); 250 } 251 } 252 253 262 public void destroyInterceptors() { 263 interceptorList.destroyAll(); 264 } 265 266 public void objectAdapterCreated( ObjectAdapter oa ) 267 { 268 if (!hasIORInterceptors) 269 return ; 270 271 interceptorInvoker.objectAdapterCreated( oa ) ; 272 } 273 274 public void adapterManagerStateChanged( int managerId, 275 short newState ) 276 { 277 if (!hasIORInterceptors) 278 return ; 279 280 interceptorInvoker.adapterManagerStateChanged( managerId, newState ) ; 281 } 282 283 public void adapterStateChanged( ObjectReferenceTemplate [] 284 templates, short newState ) 285 { 286 if (!hasIORInterceptors) 287 return ; 288 289 interceptorInvoker.adapterStateChanged( templates, newState ) ; 290 } 291 292 296 297 public void disableInterceptorsThisThread() { 298 if( !hasClientInterceptors ) return; 299 300 RequestInfoStack infoStack = 301 (RequestInfoStack)threadLocalClientRequestInfoStack.get(); 302 infoStack.disableCount++; 303 } 304 305 public void enableInterceptorsThisThread() { 306 if( !hasClientInterceptors ) return; 307 308 RequestInfoStack infoStack = 309 (RequestInfoStack)threadLocalClientRequestInfoStack.get(); 310 infoStack.disableCount--; 311 } 312 313 public void invokeClientPIStartingPoint() 314 throws RemarshalException 315 { 316 if( !hasClientInterceptors ) return; 317 if( !isClientPIEnabledForThisThread() ) return; 318 319 ClientRequestInfoImpl info = peekClientRequestInfoImplStack(); 322 interceptorInvoker.invokeClientInterceptorStartingPoint( info ); 323 324 short replyStatus = info.getReplyStatus(); 327 if( (replyStatus == SYSTEM_EXCEPTION.value) || 328 (replyStatus == LOCATION_FORWARD.value) ) 329 { 330 333 Exception exception = invokeClientPIEndingPoint( 334 convertPIReplyStatusToReplyMessage( replyStatus ), 335 info.getException() ); 336 if( exception == null ) { 337 } if( exception instanceof SystemException ) { 340 throw (SystemException )exception; 341 } else if( exception instanceof RemarshalException ) { 342 throw (RemarshalException )exception; 343 } else if( (exception instanceof UserException ) || 344 (exception instanceof ApplicationException ) ) { 345 throw wrapper.exceptionInvalid() ; 350 } 351 } 352 else if( replyStatus != ClientRequestInfoImpl.UNINITIALIZED ) { 353 throw wrapper.replyStatusNotInit() ; 354 } 355 } 356 357 public Exception invokeClientPIEndingPoint( 358 int replyStatus, Exception exception ) 359 { 360 if( !hasClientInterceptors ) return exception; 361 if( !isClientPIEnabledForThisThread() ) return exception; 362 363 short piReplyStatus = REPLY_MESSAGE_TO_PI_REPLY_STATUS[replyStatus]; 367 368 ClientRequestInfoImpl info = peekClientRequestInfoImplStack(); 371 info.setReplyStatus( piReplyStatus ); 372 info.setException( exception ); 373 interceptorInvoker.invokeClientInterceptorEndingPoint( info ); 374 piReplyStatus = info.getReplyStatus(); 375 376 if( (piReplyStatus == LOCATION_FORWARD.value) || 378 (piReplyStatus == TRANSPORT_RETRY.value) ) 379 { 380 info.reset(); 383 info.setRetryRequest( true ); 384 385 exception = new RemarshalException (); 387 } 388 else if( (piReplyStatus == SYSTEM_EXCEPTION.value) || 389 (piReplyStatus == USER_EXCEPTION.value) ) 390 { 391 exception = info.getException(); 392 } 393 394 return exception; 395 } 396 397 public void initiateClientPIRequest( boolean diiRequest ) { 398 if( !hasClientInterceptors ) return; 399 if( !isClientPIEnabledForThisThread() ) return; 400 401 RequestInfoStack infoStack = 404 (RequestInfoStack)threadLocalClientRequestInfoStack.get(); 405 ClientRequestInfoImpl info = null; 406 if( !infoStack.empty() ) info = 407 (ClientRequestInfoImpl)infoStack.peek(); 408 409 if( !diiRequest && (info != null) && info.isDIIInitiate() ) { 410 info.setDIIInitiate( false ); 413 } 414 else { 415 if( (info == null) || !info.getRetryRequest() ) { 418 info = new ClientRequestInfoImpl( orb ); 419 infoStack.push( info ); 420 printPush(); 421 } 423 424 info.setRetryRequest( false ); 428 info.incrementEntryCount(); 429 430 if( diiRequest ) { 432 info.setDIIInitiate( true ); 433 } 434 } 435 } 436 437 public void cleanupClientPIRequest() { 438 if( !hasClientInterceptors ) return; 439 if( !isClientPIEnabledForThisThread() ) return; 440 441 ClientRequestInfoImpl info = peekClientRequestInfoImplStack(); 442 443 short replyStatus = info.getReplyStatus(); 452 if( replyStatus == info.UNINITIALIZED ) { 453 invokeClientPIEndingPoint( ReplyMessage.SYSTEM_EXCEPTION, 454 wrapper.unknownRequestInvoke( 455 CompletionStatus.COMPLETED_MAYBE ) ) ; 456 } 457 458 info.decrementEntryCount(); 460 if( info.getEntryCount() == 0 ) { 461 RequestInfoStack infoStack = 462 (RequestInfoStack)threadLocalClientRequestInfoStack.get(); 463 infoStack.pop(); 464 printPop(); 465 } 466 } 467 468 public void setClientPIInfo(CorbaMessageMediator messageMediator) 469 { 470 if( !hasClientInterceptors ) return; 471 if( !isClientPIEnabledForThisThread() ) return; 472 473 peekClientRequestInfoImplStack().setInfo(messageMediator); 474 } 475 476 public void setClientPIInfo( RequestImpl requestImpl ) { 477 if( !hasClientInterceptors ) return; 478 if( !isClientPIEnabledForThisThread() ) return; 479 480 peekClientRequestInfoImplStack().setDIIRequest( requestImpl ); 481 } 482 483 487 488 public void invokeServerPIStartingPoint() 489 { 490 if( !hasServerInterceptors ) return; 491 492 ServerRequestInfoImpl info = peekServerRequestInfoImplStack(); 493 interceptorInvoker.invokeServerInterceptorStartingPoint( info ); 494 495 serverPIHandleExceptions( info ); 497 } 498 499 public void invokeServerPIIntermediatePoint() 500 { 501 if( !hasServerInterceptors ) return; 502 503 ServerRequestInfoImpl info = peekServerRequestInfoImplStack(); 504 interceptorInvoker.invokeServerInterceptorIntermediatePoint( info ); 505 506 info.releaseServant(); 509 510 serverPIHandleExceptions( info ); 512 } 513 514 public void invokeServerPIEndingPoint( ReplyMessage replyMessage ) 515 { 516 if( !hasServerInterceptors ) return; 517 ServerRequestInfoImpl info = peekServerRequestInfoImplStack(); 518 519 info.setReplyMessage( replyMessage ); 521 522 info.setCurrentExecutionPoint( info.EXECUTION_POINT_ENDING ); 525 526 if( !info.getAlreadyExecuted() ) { 530 int replyStatus = replyMessage.getReplyStatus(); 531 532 short piReplyStatus = 537 REPLY_MESSAGE_TO_PI_REPLY_STATUS[replyStatus]; 538 539 if( ( piReplyStatus == LOCATION_FORWARD.value ) || 541 ( piReplyStatus == TRANSPORT_RETRY.value ) ) 542 { 543 info.setForwardRequest( replyMessage.getIOR() ); 544 } 545 546 550 Exception prevException = info.getException(); 552 553 if( !info.isDynamic() && 557 (piReplyStatus == USER_EXCEPTION.value) ) 558 { 559 info.setException( omgWrapper.unknownUserException( 560 CompletionStatus.COMPLETED_MAYBE ) ) ; 561 } 562 563 info.setReplyStatus( piReplyStatus ); 565 interceptorInvoker.invokeServerInterceptorEndingPoint( info ); 566 short newPIReplyStatus = info.getReplyStatus(); 567 Exception newException = info.getException(); 568 569 if( ( newPIReplyStatus == SYSTEM_EXCEPTION.value ) && 573 ( newException != prevException ) ) 574 { 575 throw (SystemException )newException; 576 } 577 578 if( newPIReplyStatus == LOCATION_FORWARD.value ) { 580 if( piReplyStatus != LOCATION_FORWARD.value ) { 581 IOR ior = info.getForwardRequestIOR(); 583 throw new ForwardException( orb, ior ) ; 584 } 585 else if( info.isForwardRequestRaisedInEnding() ) { 586 replyMessage.setIOR( info.getForwardRequestIOR() ); 588 } 589 } 590 } 591 } 592 593 public void setServerPIInfo( Exception exception ) { 594 if( !hasServerInterceptors ) return; 595 596 ServerRequestInfoImpl info = peekServerRequestInfoImplStack(); 597 info.setException( exception ); 598 } 599 600 public void setServerPIInfo( NVList arguments ) 601 { 602 if( !hasServerInterceptors ) return; 603 604 ServerRequestInfoImpl info = peekServerRequestInfoImplStack(); 605 info.setDSIArguments( arguments ); 606 } 607 608 public void setServerPIExceptionInfo( Any exception ) 609 { 610 if( !hasServerInterceptors ) return; 611 612 ServerRequestInfoImpl info = peekServerRequestInfoImplStack(); 613 info.setDSIException( exception ); 614 } 615 616 public void setServerPIInfo( Any result ) 617 { 618 if( !hasServerInterceptors ) return; 619 620 ServerRequestInfoImpl info = peekServerRequestInfoImplStack(); 621 info.setDSIResult( result ); 622 } 623 624 public void initializeServerPIInfo( CorbaMessageMediator request, 625 ObjectAdapter oa, byte[] objectId, ObjectKeyTemplate oktemp ) 626 { 627 if( !hasServerInterceptors ) return; 628 629 RequestInfoStack infoStack = 630 (RequestInfoStack)threadLocalServerRequestInfoStack.get(); 631 ServerRequestInfoImpl info = new ServerRequestInfoImpl( orb ); 632 infoStack.push( info ); 633 printPush(); 634 635 request.setExecutePIInResponseConstructor( true ); 638 639 info.setInfo( request, oa, objectId, oktemp ); 640 } 641 642 public void setServerPIInfo( java.lang.Object servant, 643 String targetMostDerivedInterface ) 644 { 645 if( !hasServerInterceptors ) return; 646 647 ServerRequestInfoImpl info = peekServerRequestInfoImplStack(); 648 info.setInfo( servant, targetMostDerivedInterface ); 649 } 650 651 public void cleanupServerPIRequest() { 652 if( !hasServerInterceptors ) return; 653 654 RequestInfoStack infoStack = 655 (RequestInfoStack)threadLocalServerRequestInfoStack.get(); 656 infoStack.pop(); 657 printPop(); 658 } 659 660 664 665 672 private void serverPIHandleExceptions( ServerRequestInfoImpl info ) 673 { 674 int endingPointCall = info.getEndingPointCall(); 675 if(endingPointCall == ServerRequestInfoImpl.CALL_SEND_EXCEPTION) { 676 throw (SystemException )info.getException(); 678 } 679 else if( (endingPointCall == ServerRequestInfoImpl.CALL_SEND_OTHER) && 680 (info.getForwardRequestException() != null) ) 681 { 682 IOR ior = info.getForwardRequestIOR(); 685 throw new ForwardException( orb, ior ); 686 } 687 } 688 689 696 private int convertPIReplyStatusToReplyMessage( short replyStatus ) { 697 int result = 0; 698 for( int i = 0; i < REPLY_MESSAGE_TO_PI_REPLY_STATUS.length; i++ ) { 699 if( REPLY_MESSAGE_TO_PI_REPLY_STATUS[i] == replyStatus ) { 700 result = i; 701 break; 702 } 703 } 704 return result; 705 } 706 707 712 private ClientRequestInfoImpl peekClientRequestInfoImplStack() { 713 RequestInfoStack infoStack = 714 (RequestInfoStack)threadLocalClientRequestInfoStack.get(); 715 ClientRequestInfoImpl info = null; 716 if( !infoStack.empty() ) { 717 info = (ClientRequestInfoImpl)infoStack.peek(); 718 } else { 719 throw wrapper.clientInfoStackNull() ; 720 } 721 722 return info; 723 } 724 725 729 private ServerRequestInfoImpl peekServerRequestInfoImplStack() { 730 RequestInfoStack infoStack = 731 (RequestInfoStack)threadLocalServerRequestInfoStack.get(); 732 ServerRequestInfoImpl info = null; 733 734 if( !infoStack.empty() ) { 735 info = (ServerRequestInfoImpl)infoStack.peek(); 736 } else { 737 throw wrapper.serverInfoStackNull() ; 738 } 739 740 return info; 741 } 742 743 747 private boolean isClientPIEnabledForThisThread() { 748 RequestInfoStack infoStack = 749 (RequestInfoStack)threadLocalClientRequestInfoStack.get(); 750 return (infoStack.disableCount == 0); 751 } 752 753 756 private void preInitORBInitializers( ORBInitInfoImpl info ) { 757 758 info.setStage( ORBInitInfoImpl.STAGE_PRE_INIT ); 760 761 for( int i = 0; i < orb.getORBData().getORBInitializers().length; 764 i++ ) { 765 ORBInitializer init = orb.getORBData().getORBInitializers()[i]; 766 if( init != null ) { 767 try { 768 init.pre_init( info ); 769 } 770 catch( Exception e ) { 771 } 774 } 775 } 776 } 777 778 781 private void postInitORBInitializers( ORBInitInfoImpl info ) { 782 783 info.setStage( ORBInitInfoImpl.STAGE_POST_INIT ); 785 786 for( int i = 0; i < orb.getORBData().getORBInitializers().length; 789 i++ ) { 790 ORBInitializer init = orb.getORBData().getORBInitializers()[i]; 791 if( init != null ) { 792 try { 793 init.post_init( info ); 794 } 795 catch( Exception e ) { 796 } 799 } 800 } 801 } 802 803 807 private ORBInitInfoImpl createORBInitInfo() { 808 ORBInitInfoImpl result = null; 809 810 812 String orbId = orb.getORBData().getORBId() ; 816 817 result = new ORBInitInfoImpl( orb, arguments, orbId, codecFactory ); 818 819 return result; 820 } 821 822 834 public void register_interceptor( Interceptor interceptor, int type ) 835 throws DuplicateName 836 { 837 if( (type >= InterceptorList.NUM_INTERCEPTOR_TYPES) || (type < 0) ) { 840 throw wrapper.typeOutOfRange( new Integer ( type ) ) ; 841 } 842 843 String interceptorName = interceptor.name(); 844 845 if( interceptorName == null ) { 846 throw wrapper.nameNull() ; 847 } 848 849 interceptorList.register_interceptor( interceptor, type ); 851 } 852 853 public Current getPICurrent( ) { 854 return current; 855 } 856 857 861 private void nullParam() 862 throws BAD_PARAM 863 { 864 throw orbutilWrapper.nullParam() ; 865 } 866 867 875 public org.omg.CORBA.Policy create_policy(int type, org.omg.CORBA.Any val) 876 throws org.omg.CORBA.PolicyError 877 { 878 if( val == null ) { 879 nullParam( ); 880 } 881 if( policyFactoryTable == null ) { 882 throw new org.omg.CORBA.PolicyError ( 883 "There is no PolicyFactory Registered for type " + type, 884 BAD_POLICY.value ); 885 } 886 PolicyFactory factory = (PolicyFactory )policyFactoryTable.get( 887 new Integer (type) ); 888 if( factory == null ) { 889 throw new org.omg.CORBA.PolicyError ( 890 " Could Not Find PolicyFactory for the Type " + type, 891 BAD_POLICY.value); 892 } 893 org.omg.CORBA.Policy policy = factory.create_policy( type, val ); 894 return policy; 895 } 896 897 901 public void registerPolicyFactory( int type, PolicyFactory factory ) { 902 if( policyFactoryTable == null ) { 903 policyFactoryTable = new HashMap(); 904 } 905 Integer key = new Integer ( type ); 906 java.lang.Object val = policyFactoryTable.get( key ); 907 if( val == null ) { 908 policyFactoryTable.put( key, factory ); 909 } 910 else { 911 throw omgWrapper.policyFactoryRegFailed( new Integer ( type ) ) ; 912 } 913 } 914 915 public synchronized int allocateServerRequestId () 916 { 917 return serverRequestIdCounter++; 918 } 919 } 920 | Popular Tags |