KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)POAFactory.java 1.21 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
8 package com.sun.corba.se.impl.oa.poa ;
9
10 import java.util.Set JavaDoc ;
11 import java.util.HashSet JavaDoc ;
12 import java.util.Collections JavaDoc ;
13 import java.util.Iterator JavaDoc ;
14 import java.util.Map JavaDoc ;
15 import java.util.WeakHashMap JavaDoc ;
16
17 import org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ;
18 import org.omg.CORBA.TRANSIENT JavaDoc ;
19
20 import org.omg.CORBA.ORBPackage.InvalidName JavaDoc ;
21
22 import org.omg.PortableServer.Servant JavaDoc ;
23 import org.omg.PortableServer.POA JavaDoc ;
24 import org.omg.PortableServer.POAManager JavaDoc ;
25
26 import com.sun.corba.se.spi.oa.ObjectAdapter ;
27 import com.sun.corba.se.spi.oa.ObjectAdapterFactory ;
28
29 import com.sun.corba.se.spi.ior.ObjectAdapterId ;
30
31 import com.sun.corba.se.spi.orb.ORB ;
32
33 import com.sun.corba.se.spi.orbutil.closure.Closure ;
34 import com.sun.corba.se.spi.orbutil.closure.ClosureFactory ;
35
36 import com.sun.corba.se.spi.protocol.PIHandler ;
37
38 import com.sun.corba.se.spi.logging.CORBALogDomains ;
39
40 import com.sun.corba.se.impl.logging.POASystemException ;
41 import com.sun.corba.se.impl.logging.OMGSystemException ;
42
43 import com.sun.corba.se.impl.orbutil.ORBConstants ;
44
45 import com.sun.corba.se.impl.oa.poa.POAManagerImpl ;
46
47 public class POAFactory implements ObjectAdapterFactory
48 {
49     // Maps servants to POAs for deactivating servants when unexportObject is called.
50
// Maintained by POAs activate_object and deactivate_object.
51
private Map JavaDoc exportedServantsToPOA = new WeakHashMap JavaDoc();
52
53     private Set JavaDoc poaManagers ;
54     private int poaManagerId ;
55     private int poaId ;
56     private POAImpl rootPOA ;
57     private DelegateImpl delegateImpl;
58     private ORB orb ;
59     private POASystemException wrapper ;
60     private OMGSystemException omgWrapper ;
61
62     public POASystemException getWrapper()
63     {
64     return wrapper ;
65     }
66
67     /** All object adapter factories must have a no-arg constructor.
68     */

69     public POAFactory()
70     {
71     poaManagers = Collections.synchronizedSet(new HashSet JavaDoc(4));
72     poaManagerId = 0 ;
73     poaId = 0 ;
74     rootPOA = null ;
75     delegateImpl = null ;
76     orb = null ;
77     }
78
79     public synchronized POA JavaDoc lookupPOA (Servant JavaDoc servant)
80     {
81         return (POA JavaDoc)exportedServantsToPOA.get(servant);
82     }
83
84     public synchronized void registerPOAForServant(POA JavaDoc poa, Servant JavaDoc servant)
85     {
86         exportedServantsToPOA.put(servant, poa);
87     }
88
89     public synchronized void unregisterPOAForServant(POA JavaDoc poa, Servant JavaDoc servant)
90     {
91         exportedServantsToPOA.remove(servant);
92     }
93
94 // Implementation of ObjectAdapterFactory interface
95

96     public void init( ORB orb )
97     {
98     this.orb = orb ;
99     wrapper = POASystemException.get( orb,
100         CORBALogDomains.OA_LIFECYCLE ) ;
101     omgWrapper = OMGSystemException.get( orb,
102         CORBALogDomains.OA_LIFECYCLE ) ;
103     delegateImpl = new DelegateImpl( orb, this ) ;
104     registerRootPOA() ;
105
106     POACurrent poaCurrent = new POACurrent(orb);
107     orb.getLocalResolver().register( ORBConstants.POA_CURRENT_NAME,
108         ClosureFactory.makeConstant( poaCurrent ) ) ;
109     }
110
111     public ObjectAdapter find( ObjectAdapterId oaid )
112     {
113     POA JavaDoc poa=null;
114     try {
115         boolean first = true ;
116         Iterator JavaDoc iter = oaid.iterator() ;
117         poa = getRootPOA();
118         while (iter.hasNext()) {
119         String JavaDoc name = (String JavaDoc)(iter.next()) ;
120
121         if (first) {
122             if (!name.equals( ORBConstants.ROOT_POA_NAME ))
123             throw wrapper.makeFactoryNotPoa( name ) ;
124             first = false ;
125         } else {
126             poa = poa.find_POA( name, true ) ;
127         }
128         }
129     } catch ( org.omg.PortableServer.POAPackage.AdapterNonExistent JavaDoc ex ){
130         throw omgWrapper.noObjectAdaptor( ex ) ;
131     } catch ( OBJECT_NOT_EXIST JavaDoc ex ) {
132         throw ex;
133     } catch ( TRANSIENT JavaDoc ex ) {
134         throw ex;
135     } catch ( Exception JavaDoc ex ) {
136         throw wrapper.poaLookupError( ex ) ;
137     }
138
139     if ( poa == null )
140         throw wrapper.poaLookupError() ;
141
142     return (ObjectAdapter)poa;
143     }
144
145     public void shutdown( boolean waitForCompletion )
146     {
147         // It is important to copy the list of POAManagers first because
148
// pm.deactivate removes itself from poaManagers!
149
Iterator JavaDoc managers = null ;
150     synchronized (this) {
151         managers = (new HashSet JavaDoc(poaManagers)).iterator();
152     }
153
154     while ( managers.hasNext() ) {
155         try {
156             ((POAManager JavaDoc)managers.next()).deactivate(true, waitForCompletion);
157         } catch ( org.omg.PortableServer.POAManagerPackage.AdapterInactive JavaDoc e ) {}
158     }
159     }
160
161 // Special methods used to manipulate global POA related state
162

163     public synchronized void removePoaManager( POAManager JavaDoc manager )
164     {
165         poaManagers.remove(manager);
166     }
167
168     public synchronized void addPoaManager( POAManager JavaDoc manager )
169     {
170         poaManagers.add(manager);
171     }
172
173     synchronized public int newPOAManagerId()
174     {
175     return poaManagerId++ ;
176     }
177
178     public void registerRootPOA()
179     {
180     // We delay the evaluation of makeRootPOA until
181
// a call to resolve_initial_references( "RootPOA" ).
182
// The Future guarantees that makeRootPOA is only called once.
183
Closure rpClosure = new Closure() {
184         public Object JavaDoc evaluate() {
185         return POAImpl.makeRootPOA( orb ) ;
186         }
187     } ;
188
189     orb.getLocalResolver().register( ORBConstants.ROOT_POA_NAME,
190         ClosureFactory.makeFuture( rpClosure ) ) ;
191     }
192
193     public synchronized POA JavaDoc getRootPOA()
194     {
195     if (rootPOA == null) {
196         try {
197         Object JavaDoc obj = orb.resolve_initial_references(
198             ORBConstants.ROOT_POA_NAME ) ;
199         rootPOA = (POAImpl)obj ;
200         } catch (InvalidName JavaDoc inv) {
201         throw wrapper.cantResolveRootPoa( inv ) ;
202         }
203     }
204
205     return rootPOA;
206     }
207
208     public org.omg.PortableServer.portable.Delegate JavaDoc getDelegateImpl()
209     {
210     return delegateImpl ;
211     }
212
213     synchronized public int newPOAId()
214     {
215     return poaId++ ;
216     }
217
218     public ORB getORB()
219     {
220     return orb ;
221     }
222 }
223
Popular Tags