KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > interceptors > PIHandlerImpl


1 /*
2  * @(#)PIHandlerImpl.java 1.35 04/06/21
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.corba.se.impl.interceptors;
8
9 import java.util.*;
10 import java.io.IOException JavaDoc;
11              
12 import org.omg.CORBA.Any JavaDoc;
13 import org.omg.CORBA.BAD_PARAM JavaDoc;
14 import org.omg.CORBA.BAD_POLICY JavaDoc;
15 import org.omg.CORBA.BAD_INV_ORDER JavaDoc;
16 import org.omg.CORBA.COMM_FAILURE JavaDoc;
17 import org.omg.CORBA.CompletionStatus JavaDoc;
18 import org.omg.CORBA.INTERNAL JavaDoc;
19 import org.omg.CORBA.NVList JavaDoc;
20 import org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc;
21 import org.omg.CORBA.ORBPackage.InvalidName JavaDoc;
22 import org.omg.CORBA.SystemException JavaDoc;
23 import org.omg.CORBA.UserException JavaDoc;
24 import org.omg.CORBA.UNKNOWN JavaDoc;
25
26 import org.omg.CORBA.portable.ApplicationException JavaDoc;
27 import org.omg.CORBA.portable.RemarshalException JavaDoc;
28
29 import org.omg.IOP.CodecFactory JavaDoc;
30
31 import org.omg.PortableInterceptor.ForwardRequest JavaDoc;
32 import org.omg.PortableInterceptor.Current JavaDoc;
33 import org.omg.PortableInterceptor.Interceptor JavaDoc;
34 import org.omg.PortableInterceptor.LOCATION_FORWARD JavaDoc;
35 import org.omg.PortableInterceptor.ORBInitializer JavaDoc;
36 import org.omg.PortableInterceptor.ORBInitInfo JavaDoc;
37 import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName JavaDoc;
38 import org.omg.PortableInterceptor.SUCCESSFUL JavaDoc;
39 import org.omg.PortableInterceptor.SYSTEM_EXCEPTION JavaDoc;
40 import org.omg.PortableInterceptor.TRANSPORT_RETRY JavaDoc;
41 import org.omg.PortableInterceptor.USER_EXCEPTION JavaDoc;
42 import org.omg.PortableInterceptor.PolicyFactory JavaDoc;
43 import org.omg.PortableInterceptor.ObjectReferenceTemplate JavaDoc ;
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 /**
68  * Provides portable interceptor functionality.
69  */

70 public class PIHandlerImpl implements PIHandler
71 {
72     // REVISIT - delete these after framework merging.
73
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     // A unique id used in ServerRequestInfo.
102
// This does not correspond to the GIOP request id.
103
private int serverRequestIdCounter = 0;
104
105     // Stores the codec factory for producing codecs
106
CodecFactory JavaDoc codecFactory = null;
107
108     // The arguments passed to the application's main method. May be null.
109
// This is used for ORBInitializers and set from set_parameters.
110
String JavaDoc[] arguments = null;
111
112     // The list of portable interceptors, organized by type:
113
private InterceptorList interceptorList;
114
115     // Cached information for optimization - do we have any interceptors
116
// registered of the given types? Set during ORB initialization.
117
private boolean hasIORInterceptors;
118     private boolean hasClientInterceptors; // temp always true
119
private boolean hasServerInterceptors;
120
121     // The class responsible for invoking interceptors
122
private InterceptorInvoker interceptorInvoker;
123
124     // There will be one PICurrent instantiated for every ORB.
125
private PICurrent current;
126
127     // This table contains a list of PolicyFactories registered using
128
// ORBInitInfo.registerPolicyFactory() method.
129
// Key for the table is PolicyType which is an Integer
130
// Value is PolicyFactory.
131
private HashMap policyFactoryTable;
132     
133     // Table to convert from a ReplyMessage.? to a PI replyStatus short.
134
// Note that this table relies on the order and constants of
135
// ReplyMessage not to change.
136
private final static short REPLY_MESSAGE_TO_PI_REPLY_STATUS[] = {
137         SUCCESSFUL.value, // = ReplyMessage.NO_EXCEPTION
138
USER_EXCEPTION.value, // = ReplyMessage.USER_EXCEPTION
139
SYSTEM_EXCEPTION.value, // = ReplyMessage.SYSTEM_EXCEPTION
140
LOCATION_FORWARD.value, // = ReplyMessage.LOCATION_FORWARD
141
LOCATION_FORWARD.value, // = ReplyMessage.LOCATION_FORWARD_PERM
142
TRANSPORT_RETRY.value // = ReplyMessage.NEEDS_ADDRESSING_MODE
143
};
144             
145     // ThreadLocal containing a stack to store client request info objects
146
// and a disable count.
147
private ThreadLocal JavaDoc threadLocalClientRequestInfoStack =
148         new ThreadLocal JavaDoc() {
149             protected Object JavaDoc initialValue() {
150                 return new RequestInfoStack();
151             }
152         };
153
154     // ThreadLocal containing the current server request info object.
155
private ThreadLocal JavaDoc threadLocalServerRequestInfoStack =
156     new ThreadLocal JavaDoc() {
157         protected Object JavaDoc initialValue() {
158         return new RequestInfoStack();
159         }
160     };
161      
162     // Class to contain all ThreadLocal data for ClientRequestInfo
163
// maintenance.
164
//
165
// We use an ArrayList instead since it is not thread-safe.
166
// RequestInfoStack is used quite frequently.
167
private final class RequestInfoStack extends Stack {
168         // Number of times a request has been made to disable interceptors.
169
// When this reaches 0, interception hooks are disabled. Any higher
170
// value indicates they are enabled.
171
// NOTE: The is only currently used on the client side.
172
public int disableCount = 0;
173     }
174         
175     public PIHandlerImpl( ORB orb, String JavaDoc[] 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     // Create codec factory:
186
codecFactory = new CodecFactoryImpl( orb );
187
188     // Create new interceptor list:
189
interceptorList = new InterceptorList( wrapper );
190
191         // Create a new PICurrent.
192
current = new PICurrent( orb );
193
194     // Create new interceptor invoker, initially disabled:
195
interceptorInvoker = new InterceptorInvoker( orb, interceptorList,
196                                                      current );
197
198     // Register the PI current and Codec factory objects
199
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 we have any orb initializers, make use of them:
207
if( orb.getORBData().getORBInitializers() != null ) {
208         // Create the ORBInitInfo object to pass to ORB intializers:
209
ORBInitInfoImpl orbInitInfo = createORBInitInfo();
210
211         // Make sure get_slot and set_slot are not called from within
212
// ORB initializers:
213
current.setORBInitializing( true );
214
215         // Call pre_init on all ORB initializers:
216
preInitORBInitializers( orbInitInfo );
217
218         // Call post_init on all ORB initializers:
219
postInitORBInitializers( orbInitInfo );
220
221         // Proprietary: sort interceptors:
222
interceptorList.sortInterceptors();
223
224         // Re-enable get_slot and set_slot to be called from within
225
// ORB initializers:
226
current.setORBInitializing( false );
227
228         // Ensure nobody makes any more calls on this object.
229
orbInitInfo.setStage( ORBInitInfoImpl.STAGE_CLOSED );
230
231         // Set cached flags indicating whether we have interceptors
232
// registered of a given type.
233
hasIORInterceptors = interceptorList.hasInterceptorsOfType(
234         InterceptorList.INTERCEPTOR_TYPE_IOR );
235         // XXX This must always be true, so that using the new generic
236
// RPC framework can pass info between the PI stack and the
237
// framework invocation stack. Temporary until Harold fixes
238
// this. Note that this must never be true until after the
239
// ORBInitializer instances complete executing.
240
//hasClientInterceptors = interceptorList.hasInterceptorsOfType(
241
//InterceptorList.INTERCEPTOR_TYPE_CLIENT );
242
hasClientInterceptors = true;
243         hasServerInterceptors = interceptorList.hasInterceptorsOfType(
244         InterceptorList.INTERCEPTOR_TYPE_SERVER );
245
246         // Enable interceptor invoker (not necessary if no interceptors
247
// are registered). This should be the last stage of ORB
248
// initialization.
249
interceptorInvoker.setEnabled( true );
250     }
251     }
252
253     /**
254      * ptc/00-08-06 p 205: "When an application calls ORB::destroy, the ORB
255      * 1) waits for all requests in progress to complete
256      * 2) calls the Interceptor::destroy operation for each interceptor
257      * 3) completes destruction of the ORB"
258      *
259      * This must be called at the end of ORB.destroy. Note that this is not
260      * part of the PIHandler interface, since ORBImpl implements the ORB interface.
261      */

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 JavaDoc[]
284     templates, short newState )
285     {
286     if (!hasIORInterceptors)
287         return ;
288
289     interceptorInvoker.adapterStateChanged( templates, newState ) ;
290     }
291
292     /*
293      *****************
294      * Client PI hooks
295      *****************/

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 JavaDoc
315     {
316     if( !hasClientInterceptors ) return;
317         if( !isClientPIEnabledForThisThread() ) return;
318
319         // Invoke the starting interception points and record exception
320
// and reply status info in the info object:
321
ClientRequestInfoImpl info = peekClientRequestInfoImplStack();
322         interceptorInvoker.invokeClientInterceptorStartingPoint( info );
323         
324         // Check reply status. If we will not have another chance later
325
// to invoke the client ending points, do it now.
326
short replyStatus = info.getReplyStatus();
327         if( (replyStatus == SYSTEM_EXCEPTION.value) ||
328             (replyStatus == LOCATION_FORWARD.value) )
329         {
330         // Note: Transport retry cannot happen here since this happens
331
// before the request hits the wire.
332

333             Exception JavaDoc exception = invokeClientPIEndingPoint(
334                 convertPIReplyStatusToReplyMessage( replyStatus ),
335                 info.getException() );
336             if( exception == null ) {
337                 // Do not throw anything. Otherwise, it must be a
338
// SystemException, UserException or RemarshalException.
339
} if( exception instanceof SystemException JavaDoc ) {
340                 throw (SystemException JavaDoc)exception;
341             } else if( exception instanceof RemarshalException JavaDoc ) {
342                 throw (RemarshalException JavaDoc)exception;
343             } else if( (exception instanceof UserException JavaDoc) ||
344              (exception instanceof ApplicationException JavaDoc) ) {
345                 // It should not be possible for an interceptor to throw
346
// a UserException. By asserting instead of throwing the
347
// UserException, we need not declare anything but
348
// RemarshalException in the throws clause.
349
throw wrapper.exceptionInvalid() ;
350             }
351         }
352     else if( replyStatus != ClientRequestInfoImpl.UNINITIALIZED ) {
353         throw wrapper.replyStatusNotInit() ;
354     }
355     }
356     
357     public Exception JavaDoc invokeClientPIEndingPoint(
358         int replyStatus, Exception JavaDoc exception )
359     {
360     if( !hasClientInterceptors ) return exception;
361         if( !isClientPIEnabledForThisThread() ) return exception;
362
363         // Translate ReplyMessage.replyStatus into PI replyStatus:
364
// Note: this is also an assertion to make sure a valid replyStatus
365
// is passed in (IndexOutOfBoundsException will be thrown otherwise)
366
short piReplyStatus = REPLY_MESSAGE_TO_PI_REPLY_STATUS[replyStatus];
367         
368         // Invoke the ending interception points and record exception
369
// and reply status info in the info object:
370
ClientRequestInfoImpl info = peekClientRequestInfoImplStack();
371         info.setReplyStatus( piReplyStatus );
372         info.setException( exception );
373         interceptorInvoker.invokeClientInterceptorEndingPoint( info );
374     piReplyStatus = info.getReplyStatus();
375
376         // Check reply status:
377
if( (piReplyStatus == LOCATION_FORWARD.value) ||
378             (piReplyStatus == TRANSPORT_RETRY.value) )
379         {
380             // If this is a forward or a retry, reset and reuse
381
// info object:
382
info.reset();
383             info.setRetryRequest( true );
384
385             // ... and return a RemarshalException so the orb internals know
386
exception = new RemarshalException JavaDoc();
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     // Get the most recent info object from the thread local
402
// ClientRequestInfoImpl stack:
403
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         // In RequestImpl.doInvocation we already called
411
// initiateClientPIRequest( true ), so ignore this initiate.
412
info.setDIIInitiate( false );
413     }
414     else {
415         // If there is no info object or if we are not retrying a request,
416
// push a new ClientRequestInfoImpl on the stack:
417
if( (info == null) || !info.getRetryRequest() ) {
418         info = new ClientRequestInfoImpl( orb );
419         infoStack.push( info );
420         printPush();
421         // Note: the entry count is automatically initialized to 0.
422
}
423         
424         // Reset the retry request flag so that recursive calls will
425
// push a new info object, and bump up entry count so we know
426
// when to pop this info object:
427
info.setRetryRequest( false );
428         info.incrementEntryCount();
429
430         // If this is a DII request, make sure we ignore the next initiate.
431
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         // If the replyStatus has not yet been set, this is an indication
444
// that the ORB threw an exception before we had a chance to
445
// invoke the client interceptor ending points.
446
//
447
// _REVISIT_ We cannot handle any exceptions or ForwardRequests
448
// flagged by the ending points here because there is no way
449
// to gracefully handle this in any of the calling code.
450
// This is a rare corner case, so we will ignore this for now.
451
short replyStatus = info.getReplyStatus();
452         if( replyStatus == info.UNINITIALIZED ) {
453         invokeClientPIEndingPoint( ReplyMessage.SYSTEM_EXCEPTION,
454         wrapper.unknownRequestInvoke(
455             CompletionStatus.COMPLETED_MAYBE ) ) ;
456         }
457         
458         // Decrement entry count, and if it is zero, pop it from the stack.
459
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     /*
484      *****************
485      * Server PI hooks
486      *****************/

487     
488     public void invokeServerPIStartingPoint()
489     {
490     if( !hasServerInterceptors ) return;
491
492         ServerRequestInfoImpl info = peekServerRequestInfoImplStack();
493         interceptorInvoker.invokeServerInterceptorStartingPoint( info );
494
495     // Handle SystemException or ForwardRequest:
496
serverPIHandleExceptions( info );
497     }
498
499     public void invokeServerPIIntermediatePoint()
500     {
501     if( !hasServerInterceptors ) return;
502
503         ServerRequestInfoImpl info = peekServerRequestInfoImplStack();
504         interceptorInvoker.invokeServerInterceptorIntermediatePoint( info );
505         
506         // Clear servant from info object so that the user has control over
507
// its lifetime:
508
info.releaseServant();
509
510     // Handle SystemException or ForwardRequest:
511
serverPIHandleExceptions( info );
512     }
513     
514     public void invokeServerPIEndingPoint( ReplyMessage replyMessage )
515     {
516     if( !hasServerInterceptors ) return;
517         ServerRequestInfoImpl info = peekServerRequestInfoImplStack();
518
519     // REVISIT: This needs to be done "early" for the following workaround.
520
info.setReplyMessage( replyMessage );
521
522     // REVISIT: This was done inside of invokeServerInterceptorEndingPoint
523
// but needs to be here for now. See comment in that method for why.
524
info.setCurrentExecutionPoint( info.EXECUTION_POINT_ENDING );
525
526         // It is possible we might have entered this method more than
527
// once (e.g. if an ending point threw a SystemException, then
528
// a new ServerResponseImpl is created).
529
if( !info.getAlreadyExecuted() ) {
530         int replyStatus = replyMessage.getReplyStatus();
531
532             // Translate ReplyMessage.replyStatus into PI replyStatus:
533
// Note: this is also an assertion to make sure a valid
534
// replyStatus is passed in (IndexOutOfBoundsException will be
535
// thrown otherwise)
536
short piReplyStatus =
537                 REPLY_MESSAGE_TO_PI_REPLY_STATUS[replyStatus];
538
539         // Make forwarded IOR available to interceptors, if applicable:
540
if( ( piReplyStatus == LOCATION_FORWARD.value ) ||
541         ( piReplyStatus == TRANSPORT_RETRY.value ) )
542         {
543         info.setForwardRequest( replyMessage.getIOR() );
544         }
545
546         // REVISIT: Do early above for now.
547
// Make reply message available to interceptors:
548
//info.setReplyMessage( replyMessage );
549

550             // Remember exception so we can tell if an interceptor changed it.
551
Exception JavaDoc prevException = info.getException();
552
553         // _REVISIT_ We do not have access to the User Exception at
554
// this point, so treat it as an UNKNOWN for now.
555
// Note that if this is a DSI call, we do have the user exception.
556
if( !info.isDynamic() &&
557         (piReplyStatus == USER_EXCEPTION.value) )
558         {
559         info.setException( omgWrapper.unknownUserException(
560             CompletionStatus.COMPLETED_MAYBE ) ) ;
561         }
562             
563             // Invoke the ending interception points:
564
info.setReplyStatus( piReplyStatus );
565             interceptorInvoker.invokeServerInterceptorEndingPoint( info );
566             short newPIReplyStatus = info.getReplyStatus();
567             Exception JavaDoc newException = info.getException();
568             
569             // Check reply status. If an interceptor threw a SystemException
570
// and it is different than the one that we came in with,
571
// rethrow it so the proper response can be constructed:
572
if( ( newPIReplyStatus == SYSTEM_EXCEPTION.value ) &&
573                 ( newException != prevException ) )
574             {
575                 throw (SystemException JavaDoc)newException;
576             }
577
578         // If we are to forward the location:
579
if( newPIReplyStatus == LOCATION_FORWARD.value ) {
580             if( piReplyStatus != LOCATION_FORWARD.value ) {
581                 // Treat a ForwardRequest as a ForwardException.
582
IOR ior = info.getForwardRequestIOR();
583             throw new ForwardException( orb, ior ) ;
584         }
585         else if( info.isForwardRequestRaisedInEnding() ) {
586                 // Treat a ForwardRequest by changing the IOR.
587
replyMessage.setIOR( info.getForwardRequestIOR() );
588         }
589         }
590         }
591     }
592     
593     public void setServerPIInfo( Exception JavaDoc exception ) {
594     if( !hasServerInterceptors ) return;
595
596         ServerRequestInfoImpl info = peekServerRequestInfoImplStack();
597         info.setException( exception );
598     }
599
600     public void setServerPIInfo( NVList JavaDoc arguments )
601     {
602     if( !hasServerInterceptors ) return;
603
604         ServerRequestInfoImpl info = peekServerRequestInfoImplStack();
605         info.setDSIArguments( arguments );
606     }
607
608     public void setServerPIExceptionInfo( Any JavaDoc exception )
609     {
610     if( !hasServerInterceptors ) return;
611
612         ServerRequestInfoImpl info = peekServerRequestInfoImplStack();
613         info.setDSIException( exception );
614     }
615
616     public void setServerPIInfo( Any JavaDoc 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     // Notify request object that once response is constructed, make
636
// sure we execute ending points.
637
request.setExecutePIInResponseConstructor( true );
638
639         info.setInfo( request, oa, objectId, oktemp );
640     }
641     
642     public void setServerPIInfo( java.lang.Object JavaDoc servant,
643                           String JavaDoc 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     /*
661      **********************************************************************
662      * The following methods are private utility methods.
663      ************************************************************************/

664
665     /**
666      * Handles exceptions for the starting and intermediate points for
667      * server request interceptors. This is common code that has been
668      * factored out into this utility method.
669      * <p>
670      * This method will NOT work for ending points.
671      */

672     private void serverPIHandleExceptions( ServerRequestInfoImpl info )
673     {
674         int endingPointCall = info.getEndingPointCall();
675         if(endingPointCall == ServerRequestInfoImpl.CALL_SEND_EXCEPTION) {
676             // If a system exception was thrown, throw it to caller:
677
throw (SystemException JavaDoc)info.getException();
678         }
679         else if( (endingPointCall == ServerRequestInfoImpl.CALL_SEND_OTHER) &&
680                  (info.getForwardRequestException() != null) )
681         {
682             // If an interceptor throws a forward request, convert it
683
// into a ForwardException for easier handling:
684
IOR ior = info.getForwardRequestIOR();
685         throw new ForwardException( orb, ior );
686         }
687     }
688
689     /**
690      * Utility method to convert a PI reply status short to a ReplyMessage
691      * constant. This is a reverse lookup on the table defined in
692      * REPLY_MESSAGE_TO_PI_REPLY_STATUS. The reverse lookup need not be
693      * performed as quickly since it is only executed in exception
694      * conditions.
695      */

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     /**
708      * Convenience method to get the ClientRequestInfoImpl object off the
709      * top of the ThreadLocal stack. Throws an INTERNAL exception if
710      * the Info stack is empty.
711      */

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     /**
726      * Convenience method to get the ServerRequestInfoImpl object off the
727      * top of the ThreadLocal stack. Returns null if there are none.
728      */

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     /**
744      * Convenience method to determine whether Client PI is enabled
745      * for requests on this thread.
746      */

747     private boolean isClientPIEnabledForThisThread() {
748         RequestInfoStack infoStack =
749             (RequestInfoStack)threadLocalClientRequestInfoStack.get();
750         return (infoStack.disableCount == 0);
751     }
752     
753     /**
754      * Call pre_init on all ORB initializers
755      */

756     private void preInitORBInitializers( ORBInitInfoImpl info ) {
757
758     // Inform ORBInitInfo we are in pre_init stage
759
info.setStage( ORBInitInfoImpl.STAGE_PRE_INIT );
760
761     // Step through each initializer instantiation and call its
762
// pre_init. Ignore any exceptions.
763
for( int i = 0; i < orb.getORBData().getORBInitializers().length;
764         i++ ) {
765         ORBInitializer JavaDoc init = orb.getORBData().getORBInitializers()[i];
766         if( init != null ) {
767         try {
768             init.pre_init( info );
769         }
770         catch( Exception JavaDoc e ) {
771             // As per orbos/99-12-02, section 9.3.1.2, "If there are
772
// any exceptions, the ORB shall ignore them and proceed."
773
}
774         }
775     }
776     }
777
778     /**
779      * Call post_init on all ORB initializers
780      */

781     private void postInitORBInitializers( ORBInitInfoImpl info ) {
782
783     // Inform ORBInitInfo we are in post_init stage
784
info.setStage( ORBInitInfoImpl.STAGE_POST_INIT );
785
786     // Step through each initializer instantiation and call its post_init.
787
// Ignore any exceptions.
788
for( int i = 0; i < orb.getORBData().getORBInitializers().length;
789         i++ ) {
790         ORBInitializer JavaDoc init = orb.getORBData().getORBInitializers()[i];
791         if( init != null ) {
792         try {
793             init.post_init( info );
794         }
795         catch( Exception JavaDoc e ) {
796             // As per orbos/99-12-02, section 9.3.1.2, "If there are
797
// any exceptions, the ORB shall ignore them and proceed."
798
}
799         }
800     }
801     }
802
803     /**
804      * Creates the ORBInitInfo object to be passed to ORB intializers'
805      * pre_init and post_init methods
806      */

807     private ORBInitInfoImpl createORBInitInfo() {
808     ORBInitInfoImpl result = null;
809     
810     // arguments comes from set_parameters. May be null.
811

812     // _REVISIT_ The spec does not specify which ID this is to be.
813
// We currently get this from the corba.ORB, which reads it from
814
// the ORB_ID_PROPERTY property.
815
String JavaDoc orbId = orb.getORBData().getORBId() ;
816
817     result = new ORBInitInfoImpl( orb, arguments, orbId, codecFactory );
818
819     return result;
820     }
821
822     /**
823      * Called by ORBInitInfo when an interceptor needs to be registered.
824      * The type is one of:
825      * <ul>
826      * <li>INTERCEPTOR_TYPE_CLIENT - ClientRequestInterceptor
827      * <li>INTERCEPTOR_TYPE_SERVER - ServerRequestInterceptor
828      * <li>INTERCEPTOR_TYPE_IOR - IORInterceptor
829      * </ul>
830      *
831      * @exception DuplicateName Thrown if an interceptor of the given
832      * name already exists for the given type.
833      */

834     public void register_interceptor( Interceptor JavaDoc interceptor, int type )
835     throws DuplicateName JavaDoc
836     {
837     // We will assume interceptor is not null, since it is called
838
// internally.
839
if( (type >= InterceptorList.NUM_INTERCEPTOR_TYPES) || (type < 0) ) {
840         throw wrapper.typeOutOfRange( new Integer JavaDoc( type ) ) ;
841     }
842
843         String JavaDoc interceptorName = interceptor.name();
844
845         if( interceptorName == null ) {
846         throw wrapper.nameNull() ;
847         }
848
849     // Register with interceptor list:
850
interceptorList.register_interceptor( interceptor, type );
851     }
852
853     public Current JavaDoc getPICurrent( ) {
854         return current;
855     }
856
857     /**
858      * Called when an invalid null parameter was passed. Throws a
859      * BAD_PARAM with a minor code of 1
860      */

861     private void nullParam()
862         throws BAD_PARAM JavaDoc
863     {
864     throw orbutilWrapper.nullParam() ;
865     }
866
867     /** This is the implementation of standard API defined in org.omg.CORBA.ORB
868      * class. This method finds the Policy Factory for the given Policy Type
869      * and instantiates the Policy object from the Factory. It will throw
870      * PolicyError exception, If the PolicyFactory for the given type is
871      * not registered.
872      * _REVISIT_, Once Policy Framework work is completed, Reorganize
873      * this method to com.sun.corba.se.spi.orb.ORB.
874      */

875     public org.omg.CORBA.Policy JavaDoc create_policy(int type, org.omg.CORBA.Any JavaDoc val)
876         throws org.omg.CORBA.PolicyError JavaDoc
877     {
878         if( val == null ) {
879             nullParam( );
880         }
881         if( policyFactoryTable == null ) {
882             throw new org.omg.CORBA.PolicyError JavaDoc(
883                 "There is no PolicyFactory Registered for type " + type,
884         BAD_POLICY.value );
885         }
886         PolicyFactory JavaDoc factory = (PolicyFactory JavaDoc)policyFactoryTable.get(
887             new Integer JavaDoc(type) );
888         if( factory == null ) {
889             throw new org.omg.CORBA.PolicyError JavaDoc(
890                 " Could Not Find PolicyFactory for the Type " + type,
891                 BAD_POLICY.value);
892         }
893         org.omg.CORBA.Policy JavaDoc policy = factory.create_policy( type, val );
894         return policy;
895     }
896
897     /** This method registers the Policy Factory in the policyFactoryTable,
898      * which is a HashMap. This method is made package private, because
899      * it is used internally by the Interceptors.
900      */

901     public void registerPolicyFactory( int type, PolicyFactory JavaDoc factory ) {
902         if( policyFactoryTable == null ) {
903             policyFactoryTable = new HashMap();
904         }
905         Integer JavaDoc key = new Integer JavaDoc( type );
906         java.lang.Object JavaDoc val = policyFactoryTable.get( key );
907         if( val == null ) {
908             policyFactoryTable.put( key, factory );
909         }
910         else {
911         throw omgWrapper.policyFactoryRegFailed( new Integer JavaDoc( type ) ) ;
912         }
913     }
914     
915     public synchronized int allocateServerRequestId ()
916     {
917     return serverRequestIdCounter++;
918     }
919 }
920
Popular Tags