KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > oa > poa > POAImpl


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

7
8 package com.sun.corba.se.impl.oa.poa;
9
10 import java.util.Collection JavaDoc ;
11 import java.util.Set JavaDoc ;
12 import java.util.HashSet JavaDoc ;
13 import java.util.Map JavaDoc ;
14 import java.util.HashMap JavaDoc ;
15 import java.util.Iterator JavaDoc ;
16
17 import org.omg.CORBA.Policy JavaDoc ;
18 import org.omg.CORBA.SystemException JavaDoc ;
19
20 import org.omg.PortableServer.POA JavaDoc ;
21 import org.omg.PortableServer.Servant JavaDoc ;
22 import org.omg.PortableServer.POAManager JavaDoc ;
23 import org.omg.PortableServer.AdapterActivator JavaDoc ;
24 import org.omg.PortableServer.ServantManager JavaDoc ;
25 import org.omg.PortableServer.ForwardRequest JavaDoc ;
26 import org.omg.PortableServer.ThreadPolicy JavaDoc;
27 import org.omg.PortableServer.LifespanPolicy JavaDoc;
28 import org.omg.PortableServer.IdUniquenessPolicy JavaDoc;
29 import org.omg.PortableServer.IdAssignmentPolicy JavaDoc;
30 import org.omg.PortableServer.ImplicitActivationPolicy JavaDoc;
31 import org.omg.PortableServer.ServantRetentionPolicy JavaDoc;
32 import org.omg.PortableServer.RequestProcessingPolicy JavaDoc;
33 import org.omg.PortableServer.ThreadPolicyValue JavaDoc ;
34 import org.omg.PortableServer.LifespanPolicyValue JavaDoc ;
35 import org.omg.PortableServer.IdUniquenessPolicyValue JavaDoc ;
36 import org.omg.PortableServer.IdAssignmentPolicyValue JavaDoc ;
37 import org.omg.PortableServer.ImplicitActivationPolicyValue JavaDoc ;
38 import org.omg.PortableServer.ServantRetentionPolicyValue JavaDoc ;
39 import org.omg.PortableServer.RequestProcessingPolicyValue JavaDoc ;
40 import org.omg.PortableServer.POAPackage.AdapterAlreadyExists JavaDoc ;
41 import org.omg.PortableServer.POAPackage.AdapterNonExistent JavaDoc ;
42 import org.omg.PortableServer.POAPackage.InvalidPolicy JavaDoc ;
43 import org.omg.PortableServer.POAPackage.WrongPolicy JavaDoc ;
44 import org.omg.PortableServer.POAPackage.WrongAdapter JavaDoc ;
45 import org.omg.PortableServer.POAPackage.NoServant JavaDoc ;
46 import org.omg.PortableServer.POAPackage.ServantAlreadyActive JavaDoc ;
47 import org.omg.PortableServer.POAPackage.ObjectAlreadyActive JavaDoc ;
48 import org.omg.PortableServer.POAPackage.ServantNotActive JavaDoc ;
49 import org.omg.PortableServer.POAPackage.ObjectNotActive JavaDoc ;
50
51 import org.omg.PortableInterceptor.ObjectReferenceFactory JavaDoc ;
52 import org.omg.PortableInterceptor.ObjectReferenceTemplate JavaDoc ;
53 import org.omg.PortableInterceptor.NON_EXISTENT JavaDoc ;
54
55 import org.omg.IOP.TAG_INTERNET_IOP JavaDoc ;
56
57 import com.sun.corba.se.spi.copyobject.CopierManager ;
58 import com.sun.corba.se.spi.copyobject.ObjectCopier ;
59 import com.sun.corba.se.spi.copyobject.ObjectCopierFactory ;
60 import com.sun.corba.se.spi.oa.OADestroyed ;
61 import com.sun.corba.se.spi.oa.OAInvocationInfo ;
62 import com.sun.corba.se.spi.oa.ObjectAdapter ;
63 import com.sun.corba.se.spi.oa.ObjectAdapterBase ;
64 import com.sun.corba.se.spi.oa.ObjectAdapterFactory ;
65 import com.sun.corba.se.spi.ior.ObjectKeyTemplate ;
66 import com.sun.corba.se.spi.ior.ObjectId ;
67 import com.sun.corba.se.spi.ior.ObjectAdapterId ;
68 import com.sun.corba.se.spi.ior.IOR ;
69 import com.sun.corba.se.spi.ior.IORFactories ;
70 import com.sun.corba.se.spi.ior.IORTemplate ;
71 import com.sun.corba.se.spi.ior.IORTemplateList ;
72 import com.sun.corba.se.spi.ior.TaggedProfile ;
73 import com.sun.corba.se.spi.ior.iiop.IIOPProfile ;
74 import com.sun.corba.se.spi.ior.iiop.IIOPAddress ;
75 import com.sun.corba.se.spi.ior.iiop.IIOPFactories ;
76 import com.sun.corba.se.spi.orb.ORB ;
77 import com.sun.corba.se.spi.protocol.ForwardException ;
78 import com.sun.corba.se.spi.transport.SocketOrChannelAcceptor;
79
80 import com.sun.corba.se.impl.ior.POAObjectKeyTemplate ;
81 import com.sun.corba.se.impl.ior.ObjectAdapterIdArray ;
82 import com.sun.corba.se.impl.orbutil.ORBUtility;
83 import com.sun.corba.se.impl.orbutil.ORBConstants;
84 import com.sun.corba.se.impl.orbutil.concurrent.Sync ;
85 import com.sun.corba.se.impl.orbutil.concurrent.SyncUtil ;
86 import com.sun.corba.se.impl.orbutil.concurrent.ReentrantMutex ;
87 import com.sun.corba.se.impl.orbutil.concurrent.CondVar ;
88
89 /**
90  * POAImpl is the implementation of the Portable Object Adapter. It
91  * contains an implementation of the POA interfaces specified in
92  * COBRA 2.3.1 chapter 11 (formal/99-10-07). This implementation
93  * is moving to comply with CORBA 3.0 due to the many clarifications
94  * that have been made to the POA semantics since CORBA 2.3.1.
95  * Specific comments have been added where 3.0 applies, but note that
96  * we do not have the new 3.0 APIs yet.
97  */

98 public class POAImpl extends ObjectAdapterBase implements POA JavaDoc
99 {
100     private boolean debug ;
101
102     /* POA creation takes place in 2 stages: first, the POAImpl constructor is
103        called, then the initialize method is called. This separation is
104        needed because an AdapterActivator does not know the POAManager or
105        the policies when
106        the unknown_adapter method is invoked. However, the POA must be created
107        before the unknown_adapter method is invoked, so that the parent knows
108        when concurrent attempts are made to create the same POA.
109        Calling the POAImpl constructor results in a new POA in state STATE_START.
110        Calling initialize( POAManager, Policies ) results in state STATE_RUN.
111        Calling destroy results in STATE_DESTROY, which marks the beginning of
112        POA destruction.
113     */

114
115     // Notes on concurrency.
116
// The POA requires careful design for concurrency management to correctly
117
// implement the specification and avoid deadlocks. The order of acquiring
118
// locks must respect the following locking hierarchy:
119
//
120
// 1. Lock POAs before POAManagers
121
// 2. Lock a POA before locking its child POA
122
//
123
// Also note that there are 3 separate conditions on which threads may wait
124
// in the POA, as defined by invokeCV, beingDestroyedCV, and
125
// adapterActivatorCV. This means that (for this reason as well as others)
126
// we cannot simply use the standard Java synchronized primitive.
127
// This implementation uses a modified version of Doug Lea's
128
// util.concurrent (version 1.3.0) that supports reentrant
129
// mutexes to handle the locking. This will all be replaced by the new JSR
130
// 166 concurrency primitives in J2SE 1.5 and later once the ORB moves to
131
// J2SE 1.5.
132

133     // POA state constants
134
//
135
// Note that ordering is important here: we must have the state defined in
136
// this order so that ordered comparison is possible.
137
// DO NOT CHANGE THE VALUES OF THE STATE CONSTANTS!!! In particular, the
138
// initialization related states must be lower than STATE_RUN.
139
//
140
// POA is created in STATE_START
141
//
142
// Valid state transitions:
143
//
144
// START to INIT after find_POA constructor call
145
// START to RUN after initialize completes
146
// INIT to INIT_DONE after initialize completes
147
// INIT to DESTROYED after failed unknown_adapter
148
// INIT_DONE to RUN after successful unknown_adapter
149
// STATE_RUN to STATE_DESTROYING after start of destruction
150
// STATE_DESTROYING to STATE_DESTROYED after destruction completes.
151

152     private static final int STATE_START = 0 ; // constructor complete
153
private static final int STATE_INIT = 1 ; // waiting for adapter activator
154
private static final int STATE_INIT_DONE = 2 ; // adapter activator called create_POA
155
private static final int STATE_RUN = 3 ; // initialized and running
156
private static final int STATE_DESTROYING = 4 ; // being destroyed
157
private static final int STATE_DESTROYED = 5 ; // destruction complete
158

159     private String JavaDoc stateToString()
160     {
161     switch (state) {
162         case STATE_START :
163         return "START" ;
164         case STATE_INIT :
165         return "INIT" ;
166         case STATE_INIT_DONE :
167         return "INIT_DONE" ;
168         case STATE_RUN :
169         return "RUN" ;
170         case STATE_DESTROYING :
171         return "DESTROYING" ;
172         case STATE_DESTROYED :
173         return "DESTROYED" ;
174         default :
175         return "UNKNOWN(" + state + ")" ;
176     }
177     }
178
179     // Current state of the POA
180
private int state ;
181
182     // The POA request handler that performs all policy specific operations
183
// Note that POAImpl handles all synchronization, so mediator is (mostly)
184
// unsynchronized.
185
private POAPolicyMediator mediator;
186     
187     // Representation of object adapter ID
188
private int numLevels; // counts depth of tree. Root = 1.
189
private ObjectAdapterId poaId ; // the actual object adapter ID for this POA
190
private String JavaDoc name; // the name of this POA
191

192     private POAManagerImpl manager; // This POA's POAManager
193
private int uniquePOAId ; // ID for this POA that is unique relative
194
// to the POAFactory, which has the same
195
// lifetime as the ORB.
196
private POAImpl parent; // The POA that created this POA.
197
private Map JavaDoc children; // Map from name to POA of POAs created by
198
// this POA.
199

200     private AdapterActivator JavaDoc activator;
201     private int invocationCount ; // pending invocations on this POA.
202

203     // Data used to control POA concurrency
204
// XXX revisit for JSR 166
205

206     // Master lock for all POA synchronization. See lock and unlock.
207
// package private for access by AOMEntry.
208
Sync poaMutex ;
209
210     // Wait on this CV for AdapterActivator upcalls to complete
211
private CondVar adapterActivatorCV ;
212
213     // Wait on this CV for all active invocations to complete
214
private CondVar invokeCV ;
215
216     // Wait on this CV for the destroy method to complete doing its work
217
private CondVar beingDestroyedCV ;
218
219     // thread local variable to store a boolean to detect deadlock in
220
// POA.destroy().
221
protected ThreadLocal JavaDoc isDestroying ;
222
223     // This includes the most important information for debugging
224
// POA problems.
225
public String JavaDoc toString()
226     {
227     return "POA[" + poaId.toString() +
228         ", uniquePOAId=" + uniquePOAId +
229         ", state=" + stateToString() +
230         ", invocationCount=" + invocationCount + "]" ;
231     }
232
233     // package private for mediator implementations.
234
boolean getDebug()
235     {
236     return debug ;
237     }
238
239     // package private for access to servant to POA map
240
static POAFactory getPOAFactory( ORB orb )
241     {
242     return (POAFactory)orb.getRequestDispatcherRegistry().
243         getObjectAdapterFactory( ORBConstants.TRANSIENT_SCID ) ;
244     }
245
246     // package private so that POAFactory can access it.
247
static POAImpl makeRootPOA( ORB orb )
248     {
249     POAManagerImpl poaManager = new POAManagerImpl( getPOAFactory( orb ),
250         orb.getPIHandler() ) ;
251
252     POAImpl result = new POAImpl( ORBConstants.ROOT_POA_NAME,
253         null, orb, STATE_START ) ;
254     result.initialize( poaManager, Policies.rootPOAPolicies ) ;
255
256     return result ;
257     }
258
259     // package private so that POAPolicyMediatorBase can access it.
260
int getPOAId()
261     {
262     return uniquePOAId ;
263     }
264
265
266     // package private so that POAPolicyMediator can access it.
267
void lock()
268     {
269     SyncUtil.acquire( poaMutex ) ;
270
271     if (debug) {
272         ORBUtility.dprint( this, "LOCKED poa " + this ) ;
273     }
274     }
275
276     // package private so that POAPolicyMediator can access it.
277
void unlock()
278     {
279     if (debug) {
280         ORBUtility.dprint( this, "UNLOCKED poa " + this ) ;
281     }
282
283     poaMutex.release() ;
284     }
285
286     // package private so that DelegateImpl can access it.
287
Policies getPolicies()
288     {
289     return mediator.getPolicies() ;
290     }
291
292     // Note that the parent POA must be locked when this constructor is called.
293
private POAImpl( String JavaDoc name, POAImpl parent, ORB orb, int initialState )
294     {
295     super( orb ) ;
296
297     debug = orb.poaDebugFlag ;
298
299     if (debug) {
300         ORBUtility.dprint( this, "Creating POA with name=" + name +
301         " parent=" + parent ) ;
302     }
303
304     this.state = initialState ;
305     this.name = name ;
306     this.parent = parent;
307     children = new HashMap JavaDoc();
308     activator = null ;
309
310     // This was done in initialize, but I moved it here
311
// to get better searchability when tracing.
312
uniquePOAId = getPOAFactory( orb ).newPOAId() ;
313
314     if (parent == null) {
315         // This is the root POA, which counts as 1 level
316
numLevels = 1 ;
317     } else {
318         // My level is one more than that of my parent
319
numLevels = parent.numLevels + 1 ;
320
321         parent.children.put(name, this);
322     }
323
324     // Get an array of all of the POA names in order to
325
// create the poaid.
326
String JavaDoc[] names = new String JavaDoc[ numLevels ] ;
327     POAImpl poaImpl = this ;
328     int ctr = numLevels - 1 ;
329     while (poaImpl != null) {
330         names[ctr--] = poaImpl.name ;
331         poaImpl = poaImpl.parent ;
332     }
333
334     poaId = new ObjectAdapterIdArray( names ) ;
335
336     invocationCount = 0;
337
338     poaMutex = new ReentrantMutex( orb.poaConcurrencyDebugFlag ) ;
339
340     adapterActivatorCV = new CondVar( poaMutex,
341         orb.poaConcurrencyDebugFlag ) ;
342     invokeCV = new CondVar( poaMutex,
343         orb.poaConcurrencyDebugFlag ) ;
344     beingDestroyedCV = new CondVar( poaMutex,
345         orb.poaConcurrencyDebugFlag ) ;
346
347     isDestroying = new ThreadLocal JavaDoc () {
348         protected java.lang.Object JavaDoc initialValue() {
349         return Boolean.FALSE;
350         }
351     };
352     }
353
354     // The POA lock must be held when this method is called.
355
private void initialize( POAManagerImpl manager, Policies policies )
356     {
357     if (debug) {
358         ORBUtility.dprint( this, "Initializing poa " + this +
359         " with POAManager=" + manager + " policies=" + policies ) ;
360     }
361     
362     this.manager = manager;
363     manager.addPOA(this);
364
365     mediator = POAPolicyMediatorFactory.create( policies, this ) ;
366
367     // Construct the object key template
368
int serverid = mediator.getServerId() ;
369     int scid = mediator.getScid() ;
370     String JavaDoc orbId = getORB().getORBData().getORBId();
371
372     ObjectKeyTemplate oktemp = new POAObjectKeyTemplate( getORB(),
373         scid, serverid, orbId, poaId ) ;
374
375     if (debug) {
376         ORBUtility.dprint( this, "Initializing poa: oktemp=" + oktemp ) ;
377     }
378
379     // Note that parent == null iff this is the root POA.
380
// This was used to avoid executing interceptors on the RootPOA.
381
// That is no longer necessary.
382
boolean objectAdapterCreated = true; // parent != null ;
383

384     // XXX extract codebase from policies and pass into initializeTemplate
385
// after the codebase policy change is finalized.
386
initializeTemplate( oktemp, objectAdapterCreated,
387                 policies,
388                 null, // codebase
389
null, // manager id
390
oktemp.getObjectAdapterId()
391                 ) ;
392
393     if (state == STATE_START)
394         state = STATE_RUN ;
395     else if (state == STATE_INIT)
396         state = STATE_INIT_DONE ;
397     else
398         throw lifecycleWrapper().illegalPoaStateTrans() ;
399     }
400
401     // The poaMutex must be held when this method is called
402
private boolean waitUntilRunning()
403     {
404     if (debug) {
405         ORBUtility.dprint( this,
406         "Calling waitUntilRunning on poa " + this ) ;
407     }
408
409     while (state < STATE_RUN) {
410         try {
411         adapterActivatorCV.await() ;
412         } catch (InterruptedException JavaDoc exc) {
413         // NO-OP
414
}
415     }
416
417     if (debug) {
418         ORBUtility.dprint( this,
419         "Exiting waitUntilRunning on poa " + this ) ;
420     }
421
422     // Note that a POA could be destroyed while in STATE_INIT due to a
423
// failure in the AdapterActivator upcall.
424
return (state == STATE_RUN) ;
425     }
426
427     // This method checks that the AdapterActivator finished the
428
// initialization of a POA activated in find_POA. This is
429
// determined by checking the state of the POA. If the state is
430
// STATE_INIT, the AdapterActivator did not complete the
431
// inialization. In this case, we destroy the POA that was
432
// partially created and return false. Otherwise, we return true.
433
// In any case, we must wake up all threads waiting for the adapter
434
// activator, either to continue their invocations, or to return
435
// errors to their client.
436
//
437
// The poaMutex must NOT be held when this method is called.
438
private boolean destroyIfNotInitDone()
439     {
440     try {
441         lock() ;
442
443         if (debug) {
444         ORBUtility.dprint( this,
445             "Calling destroyIfNotInitDone on poa " + this ) ;
446         }
447
448         boolean success = (state == STATE_INIT_DONE) ;
449
450         if (success)
451         state = STATE_RUN ;
452         else {
453         // Don't just use destroy, because the check for
454
// deadlock is too general, and can prevent this from
455
// functioning properly.
456
DestroyThread destroyer = new DestroyThread( false, debug );
457         destroyer.doIt( this, true ) ;
458         }
459
460         return success ;
461     } finally {
462         adapterActivatorCV.broadcast() ;
463
464         if (debug) {
465         ORBUtility.dprint( this,
466             "Exiting destroyIfNotInitDone on poa " + this ) ;
467         }
468
469         unlock() ;
470     }
471     }
472
473     private byte[] internalReferenceToId(
474     org.omg.CORBA.Object JavaDoc reference ) throws WrongAdapter JavaDoc
475     {
476     IOR ior = ORBUtility.getIOR( reference ) ;
477     IORTemplateList thisTemplate = ior.getIORTemplates() ;
478
479     ObjectReferenceFactory JavaDoc orf = getCurrentFactory() ;
480     IORTemplateList poaTemplate =
481         IORFactories.getIORTemplateList( orf ) ;
482
483     if (!poaTemplate.isEquivalent( thisTemplate ))
484         throw new WrongAdapter JavaDoc();
485         
486     // Extract the ObjectId from the first TaggedProfile in the IOR.
487
// If ior was created in this POA, the same ID was used for
488
// every profile through the profile templates in the currentFactory,
489
// so we will get the same result from any profile.
490
Iterator JavaDoc iter = ior.iterator() ;
491     if (!iter.hasNext())
492         throw iorWrapper().noProfilesInIor() ;
493     TaggedProfile prof = (TaggedProfile)(iter.next()) ;
494     ObjectId oid = prof.getObjectId() ;
495
496     return oid.getId();
497     }
498
499     // Converted from anonymous class to local class
500
// so that we can call performDestroy() directly.
501
static class DestroyThread extends Thread JavaDoc {
502     private boolean wait ;
503     private boolean etherealize ;
504     private boolean debug ;
505     private POAImpl thePoa ;
506
507     public DestroyThread( boolean etherealize, boolean debug )
508     {
509         this.etherealize = etherealize ;
510         this.debug = debug ;
511     }
512
513     public void doIt( POAImpl thePoa, boolean wait )
514     {
515         if (debug) {
516         ORBUtility.dprint( this,
517             "Calling DestroyThread.doIt(thePOA=" + thePoa +
518             " wait=" + wait + " etherealize=" + etherealize ) ;
519         }
520
521         this.thePoa = thePoa ;
522         this.wait = wait ;
523     
524         if (wait) {
525             run() ;
526         } else {
527                 // Catch exceptions since setDaemon can cause a
528
// security exception to be thrown under netscape
529
// in the Applet mode
530
try { setDaemon(true); } catch (Exception JavaDoc e) {}
531         start() ;
532         }
533     }
534
535     public void run()
536     {
537         Set JavaDoc destroyedPOATemplates = new HashSet JavaDoc() ;
538
539         performDestroy( thePoa, destroyedPOATemplates );
540
541         Iterator JavaDoc iter = destroyedPOATemplates.iterator() ;
542         ObjectReferenceTemplate JavaDoc[] orts = new ObjectReferenceTemplate JavaDoc[
543         destroyedPOATemplates.size() ] ;
544         int index = 0 ;
545         while (iter.hasNext())
546         orts[ index++ ] = (ObjectReferenceTemplate JavaDoc)iter.next();
547
548         thePoa.getORB().getPIHandler().adapterStateChanged( orts,
549         NON_EXISTENT.value ) ;
550         }
551     
552     // Returns true if destruction must be completed, false
553
// if not, which means that another thread is already
554
// destroying poa.
555
private boolean prepareForDestruction( POAImpl poa,
556         Set JavaDoc destroyedPOATemplates )
557     {
558         POAImpl[] childPoas = null ;
559
560         // Note that we do not synchronize on this, since this is
561
// the PerformDestroy instance, not the POA.
562
try {
563         poa.lock() ;
564
565         if (debug) {
566             ORBUtility.dprint( this,
567             "Calling performDestroy on poa " + poa ) ;
568         }
569         
570         if (poa.state <= STATE_RUN) {
571             poa.state = STATE_DESTROYING ;
572         } else {
573             // destroy may be called multiple times, and each call
574
// is allowed to proceed with its own setting of the wait
575
// flag, but the etherealize value is used from the first
576
// call to destroy. Also all children should be destroyed
577
// before the parent POA. If the poa is already destroyed,
578
// we can just return. If the poa has started destruction,
579
// but not completed, and wait is true, we need to wait
580
// until destruction is complete, then just return.
581
if (wait)
582             while (poa.state != STATE_DESTROYED) {
583                 try {
584                 poa.beingDestroyedCV.await() ;
585                 } catch (InterruptedException JavaDoc exc) {
586                 // NO-OP
587
}
588             }
589
590             return false ;
591         }
592
593         poa.isDestroying.set(Boolean.TRUE);
594
595         // Make a copy since we can't hold the lock while destroying
596
// the children, and an iterator is not deletion-safe.
597
childPoas = (POAImpl[])poa.children.values().toArray(
598             new POAImpl[0] );
599         } finally {
600         poa.unlock() ;
601         }
602
603         // We are not holding the POA mutex here to avoid holding it
604
// while destroying the POA's children, since this may involve
605
// upcalls to etherealize methods.
606

607         for (int ctr=0; ctr<childPoas.length; ctr++ ) {
608         performDestroy( childPoas[ctr], destroyedPOATemplates ) ;
609         }
610
611         return true ;
612     }
613
614         public void performDestroy( POAImpl poa, Set JavaDoc destroyedPOATemplates )
615     {
616         if (!prepareForDestruction( poa, destroyedPOATemplates ))
617         return ;
618
619         // NOTE: If we are here, poa is in STATE_DESTROYING state. All
620
// other state checks are taken care of in prepareForDestruction.
621
// No other threads may either be starting new invocations
622
// by calling enter or starting to destroy poa. There may
623
// still be pending invocations.
624

625         POAImpl parent = poa.parent ;
626         boolean isRoot = parent == null ;
627
628         try {
629         // Note that we must lock the parent before the child.
630
// The parent lock is required (if poa is not the root)
631
// to safely remove poa from parent's children Map.
632
if (!isRoot)
633             parent.lock() ;
634
635         try {
636             poa.lock() ;
637
638             completeDestruction( poa, parent,
639             destroyedPOATemplates ) ;
640         } finally {
641             poa.unlock() ;
642
643             if (isRoot)
644             // We have just destroyed the root POA, so we need to
645
// make sure that the next call to
646
// resolve_initial_reference( "RootPOA" )
647
// will recreate a valid root POA.
648
poa.manager.getFactory().registerRootPOA() ;
649         }
650         } finally {
651         if (!isRoot) {
652             parent.unlock() ;
653             poa.parent = null ;
654         }
655         }
656     }
657
658     private void completeDestruction( POAImpl poa, POAImpl parent,
659         Set JavaDoc destroyedPOATemplates )
660     {
661         if (debug) {
662         ORBUtility.dprint( this,
663             "Calling completeDestruction on poa " + poa ) ;
664         }
665
666         try {
667         while (poa.invocationCount != 0) {
668             try {
669             poa.invokeCV.await() ;
670             } catch (InterruptedException JavaDoc ex) {
671             // NO-OP
672
}
673         }
674
675         if (poa.mediator != null) {
676             if (etherealize)
677             poa.mediator.etherealizeAll();
678             
679             poa.mediator.clearAOM() ;
680         }
681
682         if (poa.manager != null)
683             poa.manager.removePOA(poa);
684
685         if (parent != null)
686             parent.children.remove( poa.name ) ;
687
688         destroyedPOATemplates.add( poa.getAdapterTemplate() ) ;
689         } catch (Throwable JavaDoc thr) {
690         if (thr instanceof ThreadDeath JavaDoc)
691             throw (ThreadDeath JavaDoc)thr ;
692
693         poa.lifecycleWrapper().unexpectedException( thr, poa.toString() ) ;
694         } finally {
695         poa.state = STATE_DESTROYED ;
696         poa.beingDestroyedCV.broadcast();
697         poa.isDestroying.set(Boolean.FALSE);
698             
699         if (debug) {
700             ORBUtility.dprint( this,
701             "Exiting completeDestruction on poa " + poa ) ;
702         }
703         }
704     }
705     }
706
707     void etherealizeAll()
708     {
709     try {
710         lock() ;
711
712         if (debug) {
713         ORBUtility.dprint( this,
714             "Calling etheralizeAll on poa " + this ) ;
715         }
716         
717         mediator.etherealizeAll() ;
718     } finally {
719         if (debug) {
720         ORBUtility.dprint( this,
721             "Exiting etheralizeAll on poa " + this ) ;
722         }
723
724         unlock() ;
725     }
726     }
727
728  //*******************************************************************
729
// Public POA API
730
//*******************************************************************
731

732     /**
733      * <code>create_POA</code>
734      * <b>Section 3.3.8.2</b>
735      */

736     public POA JavaDoc create_POA(String JavaDoc name, POAManager JavaDoc
737     theManager, Policy JavaDoc[] policies) throws AdapterAlreadyExists JavaDoc,
738     InvalidPolicy JavaDoc
739     {
740     try {
741         lock() ;
742
743         if (debug) {
744         ORBUtility.dprint( this, "Calling create_POA(name=" + name +
745             " theManager=" + theManager + " policies=" + policies +
746             ") on poa " + this ) ;
747         }
748         
749         // We cannot create children of a POA that is (being) destroyed.
750
// This has been added to the CORBA 3.0 spec.
751
if (state > STATE_RUN)
752         throw omgLifecycleWrapper().createPoaDestroy() ;
753         
754         POAImpl poa = (POAImpl)(children.get(name)) ;
755
756         if (poa == null) {
757         poa = new POAImpl( name, this, getORB(), STATE_START ) ;
758         }
759
760         try {
761         poa.lock() ;
762
763         if (debug) {
764             ORBUtility.dprint( this,
765             "Calling create_POA: new poa is " + poa ) ;
766         }
767
768         if ((poa.state != STATE_START) && (poa.state != STATE_INIT))
769             throw new AdapterAlreadyExists JavaDoc();
770
771         POAManagerImpl newManager = (POAManagerImpl)theManager ;
772         if (newManager == null)
773             newManager = new POAManagerImpl( manager.getFactory(),
774             manager.getPIHandler() );
775
776         int defaultCopierId =
777             getORB().getCopierManager().getDefaultId() ;
778         Policies POAPolicies =
779             new Policies( policies, defaultCopierId ) ;
780
781         poa.initialize( newManager, POAPolicies ) ;
782
783         return poa;
784         } finally {
785         poa.unlock() ;
786         }
787     } finally {
788         unlock() ;
789     }
790     }
791
792     /**
793      * <code>find_POA</code>
794      * <b>Section 3.3.8.3</b>
795      */

796     public POA JavaDoc find_POA(String JavaDoc name, boolean activate)
797     throws AdapterNonExistent JavaDoc
798     {
799     POAImpl found = null ;
800     AdapterActivator JavaDoc act = null ;
801
802     lock() ;
803
804     if (debug) {
805         ORBUtility.dprint( this, "Calling find_POA(name=" + name +
806         " activate=" + activate + ") on poa " + this ) ;
807     }
808
809     found = (POAImpl) children.get(name);
810
811     if (found != null) {
812         if (debug) {
813         ORBUtility.dprint( this,
814             "Calling find_POA: found poa " + found ) ;
815         }
816         
817         try {
818         found.lock() ;
819
820         // Do not hold the parent POA lock while
821
// waiting for child to complete initialization.
822
unlock() ;
823
824         // Make sure that the child has completed its initialization,
825
// if it was created by an AdapterActivator, otherwise throw
826
// a standard TRANSIENT exception with minor code 4 (see
827
// CORBA 3.0 11.3.9.3, in reference to unknown_adapter)
828
if (!found.waitUntilRunning())
829             throw omgLifecycleWrapper().poaDestroyed() ;
830
831         // Note that found may be in state DESTROYING or DESTROYED at
832
// this point. That's OK, since destruction could start at
833
// any time.
834
} finally {
835         found.unlock() ;
836         }
837     } else {
838         try {
839         if (debug) {
840             ORBUtility.dprint( this,
841             "Calling find_POA: no poa found" ) ;
842         }
843
844         if (activate && (activator != null)) {
845             // Create a child, but don't initialize it. The newly
846
// created POA will be in state STATE_START, which will
847
// cause other calls to find_POA that are creating the same
848
// POA to block on the waitUntilRunning call above.
849
// Initialization must be completed by a call to create_POA
850
// inside the unknown_adapter upcall. Note that
851
// this.poaMutex must be held here so that this.children
852
// can be safely updated. The state is set to STATE_INIT
853
// so that initialize can make the correct state transition
854
// when create_POA is called inside the AdapterActivator.
855
// This avoids activating the new POA too soon
856
// by transitioning to STATE_RUN after unknown_adapter
857
// returns.
858
found = new POAImpl( name, this, getORB(), STATE_INIT ) ;
859
860             if (debug) {
861             ORBUtility.dprint( this,
862                 "Calling find_POA: created poa " + found ) ;
863             }
864
865             act = activator ;
866         } else {
867             throw new AdapterNonExistent JavaDoc();
868         }
869         } finally {
870         unlock() ;
871         }
872     }
873
874     // assert (found != null)
875
// assert not holding this.poaMutex OR found.poaMutex
876

877     // We must not hold either this.poaMutex or found.poaMutex here while
878
// waiting for intialization of found to complete to prevent possible
879
// deadlocks.
880

881     if (act != null) {
882         boolean status = false ;
883         boolean adapterResult = false ;
884
885         if (debug) {
886         ORBUtility.dprint( this,
887             "Calling find_POA: calling AdapterActivator" ) ;
888         }
889
890         try {
891         // Prevent more than one thread at a time from executing in act
892
// in case act is shared between multiple POAs.
893
synchronized (act) {
894             status = act.unknown_adapter(this, name);
895         }
896         } catch (SystemException JavaDoc exc) {
897         throw omgLifecycleWrapper().adapterActivatorException( exc,
898             name, poaId.toString() ) ;
899         } catch (Throwable JavaDoc thr) {
900         // ignore most non-system exceptions, but log them for
901
// diagnostic purposes.
902
lifecycleWrapper().unexpectedException( thr, this.toString() ) ;
903
904         if (thr instanceof ThreadDeath JavaDoc)
905             throw (ThreadDeath JavaDoc)thr ;
906         } finally {
907         // At this point, we have completed adapter activation.
908
// Whether this was successful or not, we must call
909
// destroyIfNotInitDone so that calls to enter() and create_POA()
910
// that are waiting can execute again. Failing to do this
911
// will cause the system to hang in complex tests.
912
adapterResult = found.destroyIfNotInitDone() ;
913         }
914
915         if (status) {
916         if (!adapterResult)
917             throw omgLifecycleWrapper().adapterActivatorException( name,
918             poaId.toString() ) ;
919         } else {
920         if (debug) {
921             ORBUtility.dprint( this,
922             "Calling find_POA: AdapterActivator returned false" ) ;
923         }
924
925         // OMG Issue 3740 is resolved to throw AdapterNonExistent if
926
// unknown_adapter() returns false.
927
throw new AdapterNonExistent JavaDoc();
928         }
929     }
930
931     return found;
932     }
933
934     /**
935      * <code>destroy</code>
936      * <b>Section 3.3.8.4</b>
937      */

938     public void destroy(boolean etherealize, boolean wait_for_completion)
939     {
940         // This is to avoid deadlock
941
if (wait_for_completion && getORB().isDuringDispatch()) {
942         throw lifecycleWrapper().destroyDeadlock() ;
943         }
944
945         DestroyThread destroyer = new DestroyThread( etherealize, debug );
946     destroyer.doIt( this, wait_for_completion ) ;
947     }
948
949     /**
950      * <code>create_thread_policy</code>
951      * <b>Section 3.3.8.5</b>
952      */

953     public ThreadPolicy JavaDoc create_thread_policy(
954     ThreadPolicyValue JavaDoc value)
955     {
956     return new ThreadPolicyImpl(value);
957     }
958
959     /**
960      * <code>create_lifespan_policy</code>
961      * <b>Section 3.3.8.5</b>
962      */

963     public LifespanPolicy JavaDoc create_lifespan_policy(
964     LifespanPolicyValue JavaDoc value)
965     {
966     return new LifespanPolicyImpl(value);
967     }
968
969     /**
970      * <code>create_id_uniqueness_policy</code>
971      * <b>Section 3.3.8.5</b>
972      */

973     public IdUniquenessPolicy JavaDoc create_id_uniqueness_policy(
974     IdUniquenessPolicyValue JavaDoc value)
975     {
976     return new IdUniquenessPolicyImpl(value);
977     }
978
979     /**
980      * <code>create_id_assignment_policy</code>
981      * <b>Section 3.3.8.5</b>
982      */

983     public IdAssignmentPolicy JavaDoc create_id_assignment_policy(
984     IdAssignmentPolicyValue JavaDoc value)
985     {
986     return new IdAssignmentPolicyImpl(value);
987     }
988
989     /**
990      * <code>create_implicit_activation_policy</code>
991      * <b>Section 3.3.8.5</b>
992      */

993     public ImplicitActivationPolicy JavaDoc create_implicit_activation_policy(
994     ImplicitActivationPolicyValue JavaDoc value)
995     {
996     return new ImplicitActivationPolicyImpl(value);
997     }
998
999     /**
1000     * <code>create_servant_retention_policy</code>
1001     * <b>Section 3.3.8.5</b>
1002     */

1003    public ServantRetentionPolicy JavaDoc create_servant_retention_policy(
1004    ServantRetentionPolicyValue JavaDoc value)
1005    {
1006    return new ServantRetentionPolicyImpl(value);
1007    }
1008    
1009    /**
1010     * <code>create_request_processing_policy</code>
1011     * <b>Section 3.3.8.5</b>
1012     */

1013    public RequestProcessingPolicy JavaDoc create_request_processing_policy(
1014    RequestProcessingPolicyValue JavaDoc value)
1015    {
1016    return new RequestProcessingPolicyImpl(value);
1017    }
1018    
1019    /**
1020     * <code>the_name</code>
1021     * <b>Section 3.3.8.6</b>
1022     */

1023    public String JavaDoc the_name()
1024    {
1025    try {
1026        lock() ;
1027
1028        return name;
1029    } finally {
1030        unlock() ;
1031    }
1032    }
1033
1034    /**
1035     * <code>the_parent</code>
1036     * <b>Section 3.3.8.7</b>
1037     */

1038    public POA JavaDoc the_parent()
1039    {
1040    try {
1041        lock() ;
1042
1043        return parent;
1044    } finally {
1045        unlock() ;
1046    }
1047    }
1048
1049    /**
1050     * <code>the_children</code>
1051     */

1052    public org.omg.PortableServer.POA JavaDoc[] the_children()
1053    {
1054    try {
1055        lock() ;
1056
1057        Collection JavaDoc coll = children.values() ;
1058        int size = coll.size() ;
1059        POA JavaDoc[] result = new POA JavaDoc[ size ] ;
1060        int index = 0 ;
1061        Iterator JavaDoc iter = coll.iterator() ;
1062        while (iter.hasNext()) {
1063            POA JavaDoc poa = (POA JavaDoc)(iter.next()) ;
1064            result[ index++ ] = poa ;
1065        }
1066
1067        return result ;
1068    } finally {
1069        unlock() ;
1070    }
1071    }
1072
1073    /**
1074     * <code>the_POAManager</code>
1075     * <b>Section 3.3.8.8</b>
1076     */

1077    public POAManager JavaDoc the_POAManager()
1078    {
1079    try {
1080        lock() ;
1081
1082        return manager;
1083    } finally {
1084        unlock() ;
1085    }
1086    }
1087
1088    /**
1089     * <code>the_activator</code>
1090     * <b>Section 3.3.8.9</b>
1091     */

1092    public AdapterActivator JavaDoc the_activator()
1093    {
1094    try {
1095        lock() ;
1096
1097        return activator;
1098    } finally {
1099        unlock() ;
1100    }
1101    }
1102    
1103    /**
1104     * <code>the_activator</code>
1105     * <b>Section 3.3.8.9</b>
1106     */

1107    public void the_activator(AdapterActivator JavaDoc activator)
1108    {
1109    try {
1110        lock() ;
1111        
1112        if (debug) {
1113        ORBUtility.dprint( this, "Calling the_activator on poa " +
1114            this + " activator=" + activator ) ;
1115        }
1116
1117        this.activator = activator;
1118    } finally {
1119        unlock() ;
1120    }
1121    }
1122
1123    /**
1124     * <code>get_servant_manager</code>
1125     * <b>Section 3.3.8.10</b>
1126     */

1127    public ServantManager JavaDoc get_servant_manager() throws WrongPolicy JavaDoc
1128    {
1129    try {
1130        lock() ;
1131
1132        return mediator.getServantManager() ;
1133    } finally {
1134        unlock() ;
1135    }
1136    }
1137
1138    /**
1139     * <code>set_servant_manager</code>
1140     * <b>Section 3.3.8.10</b>
1141     */

1142    public void set_servant_manager(ServantManager JavaDoc servantManager)
1143    throws WrongPolicy JavaDoc
1144    {
1145    try {
1146        lock() ;
1147
1148        if (debug) {
1149        ORBUtility.dprint( this, "Calling set_servant_manager on poa " +
1150            this + " servantManager=" + servantManager ) ;
1151        }
1152
1153        mediator.setServantManager( servantManager ) ;
1154    } finally {
1155        unlock() ;
1156    }
1157    }
1158    
1159    /**
1160     * <code>get_servant</code>
1161     * <b>Section 3.3.8.12</b>
1162     */

1163    public Servant JavaDoc get_servant() throws NoServant JavaDoc, WrongPolicy JavaDoc
1164    {
1165    try {
1166        lock() ;
1167
1168        return mediator.getDefaultServant() ;
1169    } finally {
1170        unlock() ;
1171    }
1172    }
1173
1174    /**
1175     * <code>set_servant</code>
1176     * <b>Section 3.3.8.13</b>
1177     */

1178    public void set_servant(Servant JavaDoc defaultServant)
1179    throws WrongPolicy JavaDoc
1180    {
1181    try {
1182        lock() ;
1183
1184        if (debug) {
1185        ORBUtility.dprint( this, "Calling set_servant on poa " +
1186            this + " defaultServant=" + defaultServant ) ;
1187        }
1188
1189        mediator.setDefaultServant( defaultServant ) ;
1190    } finally {
1191        unlock() ;
1192    }
1193    }
1194
1195    /**
1196     * <code>activate_object</code>
1197     * <b>Section 3.3.8.14</b>
1198     */

1199    public byte[] activate_object(Servant JavaDoc servant)
1200    throws ServantAlreadyActive JavaDoc, WrongPolicy JavaDoc
1201    {
1202    try {
1203        lock() ;
1204
1205        if (debug) {
1206        ORBUtility.dprint( this,
1207            "Calling activate_object on poa " + this +
1208            " (servant=" + servant + ")" ) ;
1209        }
1210
1211        // Allocate a new system-generated object-id.
1212
// This will throw WrongPolicy if not SYSTEM_ID
1213
// policy.
1214
byte[] id = mediator.newSystemId();
1215
1216        try {
1217        mediator.activateObject( id, servant ) ;
1218        } catch (ObjectAlreadyActive JavaDoc oaa) {
1219        // This exception can not occur in this case,
1220
// since id is always brand new.
1221
//
1222
}
1223
1224        return id ;
1225    } finally {
1226        if (debug) {
1227        ORBUtility.dprint( this,
1228            "Exiting activate_object on poa " + this ) ;
1229        }
1230
1231        unlock() ;
1232    }
1233    }
1234
1235    /**
1236     * <code>activate_object_with_id</code>
1237     * <b>Section 3.3.8.15</b>
1238     */

1239    public void activate_object_with_id(byte[] id,
1240                             Servant JavaDoc servant)
1241    throws ObjectAlreadyActive JavaDoc, ServantAlreadyActive JavaDoc, WrongPolicy JavaDoc
1242    {
1243    try {
1244        lock() ;
1245
1246        if (debug) {
1247        ORBUtility.dprint( this,
1248            "Calling activate_object_with_id on poa " + this +
1249            " (servant=" + servant + " id=" + id + ")" ) ;
1250        }
1251
1252        // Clone the id to avoid possible errors due to aliasing
1253
// (e.g. the client passes the id in and then changes it later).
1254
byte[] idClone = (byte[])(id.clone()) ;
1255
1256        mediator.activateObject( idClone, servant ) ;
1257    } finally {
1258        if (debug) {
1259        ORBUtility.dprint( this,
1260            "Exiting activate_object_with_id on poa " + this ) ;
1261        }
1262
1263        unlock() ;
1264    }
1265    }
1266
1267    /**
1268     * <code>deactivate_object</code>
1269     * <b>3.3.8.16</b>
1270     */

1271    public void deactivate_object(byte[] id)
1272    throws ObjectNotActive JavaDoc, WrongPolicy JavaDoc
1273    {
1274    try {
1275        lock() ;
1276
1277        if (debug) {
1278        ORBUtility.dprint( this,
1279            "Calling deactivate_object on poa " + this +
1280            " (id=" + id + ")" ) ;
1281        }
1282
1283        mediator.deactivateObject( id ) ;
1284    } finally {
1285        if (debug) {
1286        ORBUtility.dprint( this,
1287            "Exiting deactivate_object on poa " + this ) ;
1288        }
1289
1290        unlock() ;
1291    }
1292    }
1293
1294    /**
1295     * <code>create_reference</code>
1296     * <b>3.3.8.17</b>
1297     */

1298    public org.omg.CORBA.Object JavaDoc create_reference(String JavaDoc repId)
1299    throws WrongPolicy JavaDoc
1300    {
1301    try {
1302        lock() ;
1303
1304        if (debug) {
1305        ORBUtility.dprint( this, "Calling create_reference(repId=" +
1306            repId + ") on poa " + this ) ;
1307        }
1308
1309        return makeObject( repId, mediator.newSystemId()) ;
1310    } finally {
1311        unlock() ;
1312    }
1313    }
1314
1315    /**
1316     * <code>create_reference_with_id</code>
1317     * <b>3.3.8.18</b>
1318     */

1319    public org.omg.CORBA.Object JavaDoc
1320    create_reference_with_id(byte[] oid, String JavaDoc repId)
1321    {
1322    try {
1323        lock() ;
1324
1325        if (debug) {
1326        ORBUtility.dprint( this,
1327            "Calling create_reference_with_id(oid=" +
1328            oid + " repId=" + repId + ") on poa " + this ) ;
1329        }
1330
1331        // Clone the id to avoid possible errors due to aliasing
1332
// (e.g. the client passes the id in and then changes it later).
1333
byte[] idClone = (byte[])(oid.clone()) ;
1334
1335        return makeObject( repId, idClone ) ;
1336    } finally {
1337        unlock() ;
1338    }
1339    }
1340
1341    /**
1342     * <code>servant_to_id</code>
1343     * <b>3.3.8.19</b>
1344     */

1345    public byte[] servant_to_id(Servant JavaDoc servant)
1346    throws ServantNotActive JavaDoc, WrongPolicy JavaDoc
1347    {
1348    try {
1349        lock() ;
1350
1351        if (debug) {
1352        ORBUtility.dprint( this, "Calling servant_to_id(servant=" +
1353            servant + ") on poa " + this ) ;
1354        }
1355
1356        return mediator.servantToId( servant ) ;
1357    } finally {
1358        unlock() ;
1359    }
1360    }
1361        
1362    /**
1363     * <code>servant_to_reference</code>
1364     * <b>3.3.8.20</b>
1365     */

1366    public org.omg.CORBA.Object JavaDoc servant_to_reference(Servant JavaDoc servant)
1367    throws ServantNotActive JavaDoc, WrongPolicy JavaDoc
1368    {
1369    try {
1370        lock() ;
1371
1372        if (debug) {
1373        ORBUtility.dprint( this,
1374            "Calling servant_to_reference(servant=" +
1375            servant + ") on poa " + this ) ;
1376        }
1377
1378        byte[] oid = mediator.servantToId(servant);
1379        String JavaDoc repId = servant._all_interfaces( this, oid )[0] ;
1380        return create_reference_with_id(oid, repId);
1381    } finally {
1382        unlock() ;
1383    }
1384    }
1385
1386    /**
1387     * <code>reference_to_servant</code>
1388     * <b>3.3.8.21</b>
1389     */

1390    public Servant JavaDoc reference_to_servant(org.omg.CORBA.Object JavaDoc reference)
1391    throws ObjectNotActive JavaDoc, WrongPolicy JavaDoc, WrongAdapter JavaDoc
1392    {
1393    try {
1394        lock() ;
1395
1396        if (debug) {
1397        ORBUtility.dprint( this,
1398            "Calling reference_to_servant(reference=" +
1399            reference + ") on poa " + this ) ;
1400        }
1401        
1402            if ( state >= STATE_DESTROYING ) {
1403        throw lifecycleWrapper().adapterDestroyed() ;
1404            }
1405
1406        // reference_to_id should throw WrongAdapter
1407
// if the objref was not created by this POA
1408
byte [] id = internalReferenceToId(reference);
1409        
1410        return mediator.idToServant( id ) ;
1411    } finally {
1412        unlock() ;
1413    }
1414    }
1415
1416    /**
1417     * <code>reference_to_id</code>
1418     * <b>3.3.8.22</b>
1419     */

1420    public byte[] reference_to_id(org.omg.CORBA.Object JavaDoc reference)
1421    throws WrongAdapter JavaDoc, WrongPolicy JavaDoc
1422    {
1423    try {
1424        lock() ;
1425
1426        if (debug) {
1427        ORBUtility.dprint( this, "Calling reference_to_id(reference=" +
1428            reference + ") on poa " + this ) ;
1429        }
1430        
1431            if( state >= STATE_DESTROYING ) {
1432        throw lifecycleWrapper().adapterDestroyed() ;
1433            }
1434        
1435        return internalReferenceToId( reference ) ;
1436    } finally {
1437        unlock() ;
1438    }
1439    }
1440
1441    /**
1442     * <code>id_to_servant</code>
1443     * <b>3.3.8.23</b>
1444     */

1445    public Servant JavaDoc id_to_servant(byte[] id)
1446    throws ObjectNotActive JavaDoc, WrongPolicy JavaDoc
1447    {
1448    try {
1449        lock() ;
1450
1451        if (debug) {
1452        ORBUtility.dprint( this, "Calling id_to_servant(id=" +
1453            id + ") on poa " + this ) ;
1454        }
1455        
1456            if( state >= STATE_DESTROYING ) {
1457        throw lifecycleWrapper().adapterDestroyed() ;
1458            }
1459        return mediator.idToServant( id ) ;
1460    } finally {
1461        unlock() ;
1462    }
1463    }
1464
1465    /**
1466     * <code>id_to_reference</code>
1467     * <b>3.3.8.24</b>
1468     */

1469    public org.omg.CORBA.Object JavaDoc id_to_reference(byte[] id)
1470    throws ObjectNotActive JavaDoc, WrongPolicy JavaDoc
1471
1472    {
1473    try {
1474        lock() ;
1475
1476        if (debug) {
1477        ORBUtility.dprint( this, "Calling id_to_reference(id=" +
1478            id + ") on poa " + this ) ;
1479        }
1480        
1481            if( state >= STATE_DESTROYING ) {
1482        throw lifecycleWrapper().adapterDestroyed() ;
1483            }
1484        
1485        Servant JavaDoc s = mediator.idToServant( id ) ;
1486        String JavaDoc repId = s._all_interfaces( this, id )[0] ;
1487        return makeObject(repId, id );
1488    } finally {
1489        unlock() ;
1490    }
1491    }
1492
1493    /**
1494     * <code>id</code>
1495     * <b>11.3.8.26 in ptc/00-08-06</b>
1496     */

1497    public byte[] id()
1498    {
1499    try {
1500        lock() ;
1501
1502        return getAdapterId() ;
1503    } finally {
1504        unlock() ;
1505    }
1506    }
1507
1508    //***************************************************************
1509
//Implementation of ObjectAdapter interface
1510
//***************************************************************
1511

1512    public Policy JavaDoc getEffectivePolicy( int type )
1513    {
1514    return mediator.getPolicies().get_effective_policy( type ) ;
1515    }
1516
1517    public int getManagerId()
1518    {
1519    return manager.getManagerId() ;
1520    }
1521
1522    public short getState()
1523    {
1524    return manager.getORTState() ;
1525    }
1526
1527    public String JavaDoc[] getInterfaces( java.lang.Object JavaDoc servant, byte[] objectId )
1528    {
1529    Servant JavaDoc serv = (Servant JavaDoc)servant ;
1530    return serv._all_interfaces( this, objectId ) ;
1531    }
1532
1533    protected ObjectCopierFactory getObjectCopierFactory()
1534    {
1535    int copierId = mediator.getPolicies().getCopierId() ;
1536    CopierManager cm = getORB().getCopierManager() ;
1537    return cm.getObjectCopierFactory( copierId ) ;
1538    }
1539
1540    public void enter() throws OADestroyed
1541    {
1542    try {
1543        lock() ;
1544
1545        if (debug) {
1546        ORBUtility.dprint( this, "Calling enter on poa " + this ) ;
1547        }
1548         
1549        // Avoid deadlock if this is the thread that is processing the
1550
// POA.destroy because this is the only thread that can notify
1551
// waiters on beingDestroyedCV. This can happen if an
1552
// etherealize upcall invokes a method on a colocated object
1553
// served by this POA.
1554
while ((state == STATE_DESTROYING) &&
1555        (isDestroying.get() == Boolean.FALSE)) {
1556        try {
1557            beingDestroyedCV.await();
1558        } catch (InterruptedException JavaDoc ex) {
1559            // NO-OP
1560
}
1561        }
1562
1563        if (!waitUntilRunning())
1564        throw new OADestroyed() ;
1565        
1566        invocationCount++;
1567    } finally {
1568        if (debug) {
1569        ORBUtility.dprint( this, "Exiting enter on poa " + this ) ;
1570        }
1571         
1572        unlock() ;
1573    }
1574
1575    manager.enter();
1576    }
1577
1578    public void exit()
1579    {
1580    try {
1581        lock() ;
1582
1583        if (debug) {
1584        ORBUtility.dprint( this, "Calling exit on poa " + this ) ;
1585        }
1586
1587        invocationCount--;
1588
1589        if ((invocationCount == 0) && (state == STATE_DESTROYING)) {
1590        invokeCV.broadcast();
1591        }
1592    } finally {
1593        if (debug) {
1594        ORBUtility.dprint( this, "Exiting exit on poa " + this ) ;
1595        }
1596
1597        unlock() ;
1598    }
1599
1600    manager.exit();
1601    }
1602    
1603    public void getInvocationServant( OAInvocationInfo info )
1604    {
1605    try {
1606        lock() ;
1607
1608        if (debug) {
1609        ORBUtility.dprint( this,
1610            "Calling getInvocationServant on poa " + this ) ;
1611        }
1612        
1613        java.lang.Object JavaDoc servant = null ;
1614
1615        try {
1616        servant = mediator.getInvocationServant( info.id(),
1617            info.getOperation() );
1618        } catch (ForwardRequest JavaDoc freq) {
1619        throw new ForwardException( getORB(), freq.forward_reference ) ;
1620        }
1621
1622        info.setServant( servant ) ;
1623    } finally {
1624        if (debug) {
1625        ORBUtility.dprint( this,
1626            "Exiting getInvocationServant on poa " + this ) ;
1627        }
1628
1629        unlock() ;
1630    }
1631    }
1632
1633    public org.omg.CORBA.Object JavaDoc getLocalServant( byte[] objectId )
1634    {
1635    return null ;
1636    }
1637
1638    /** Called from the subcontract to let this POA cleanup after an
1639     * invocation. Note: If getServant was called, then returnServant
1640     * MUST be called, even in the case of exceptions. This may be
1641     * called multiple times for a single request.
1642     */

1643    public void returnServant()
1644    {
1645    try {
1646        lock() ;
1647
1648        if (debug) {
1649        ORBUtility.dprint( this,
1650            "Calling returnServant on poa " + this ) ;
1651        }
1652        
1653        mediator.returnServant();
1654    } catch (Throwable JavaDoc thr) {
1655        if (debug) {
1656        ORBUtility.dprint( this,
1657            "Exception " + thr + " in returnServant on poa " + this ) ;
1658        }
1659
1660        if (thr instanceof Error JavaDoc)
1661        throw (Error JavaDoc)thr ;
1662        else if (thr instanceof RuntimeException JavaDoc)
1663        throw (RuntimeException JavaDoc)thr ;
1664
1665    } finally {
1666        if (debug) {
1667        ORBUtility.dprint( this,
1668            "Exiting returnServant on poa " + this ) ;
1669        }
1670        
1671        unlock() ;
1672    }
1673    }
1674}
1675
Popular Tags