POAFactory.java revision 608:7e06bf1dcb09
1195534Sscottl/*
2195534Sscottl * Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved.
3195534Sscottl * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4195534Sscottl *
5195534Sscottl * This code is free software; you can redistribute it and/or modify it
6195534Sscottl * under the terms of the GNU General Public License version 2 only, as
7195534Sscottl * published by the Free Software Foundation.  Oracle designates this
8195534Sscottl * particular file as subject to the "Classpath" exception as provided
9195534Sscottl * by Oracle in the LICENSE file that accompanied this code.
10195534Sscottl *
11195534Sscottl * This code is distributed in the hope that it will be useful, but WITHOUT
12195534Sscottl * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13195534Sscottl * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14195534Sscottl * version 2 for more details (a copy is included in the LICENSE file that
15195534Sscottl * accompanied this code).
16195534Sscottl *
17195534Sscottl * You should have received a copy of the GNU General Public License version
18195534Sscottl * 2 along with this work; if not, write to the Free Software Foundation,
19195534Sscottl * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20195534Sscottl *
21195534Sscottl * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22195534Sscottl * or visit www.oracle.com if you need additional information or have any
23195534Sscottl * questions.
24195534Sscottl */
25195534Sscottl
26195534Sscottlpackage com.sun.corba.se.impl.oa.poa ;
27195534Sscottl
28195534Sscottlimport java.util.Set ;
29195534Sscottlimport java.util.HashSet ;
30195534Sscottlimport java.util.Collections ;
31195534Sscottlimport java.util.Iterator ;
32195534Sscottlimport java.util.Map ;
33195534Sscottlimport java.util.WeakHashMap ;
34195534Sscottl
35195534Sscottlimport org.omg.CORBA.OBJECT_NOT_EXIST ;
36195534Sscottlimport org.omg.CORBA.TRANSIENT ;
37195534Sscottl
38195534Sscottlimport org.omg.CORBA.ORBPackage.InvalidName ;
39195534Sscottl
40195534Sscottlimport org.omg.PortableServer.Servant ;
41195534Sscottlimport org.omg.PortableServer.POA ;
42195534Sscottlimport org.omg.PortableServer.POAManager ;
43195534Sscottl
44195534Sscottlimport com.sun.corba.se.spi.oa.ObjectAdapter ;
45195534Sscottlimport com.sun.corba.se.spi.oa.ObjectAdapterFactory ;
46195534Sscottl
47195534Sscottlimport com.sun.corba.se.spi.ior.ObjectAdapterId ;
48195534Sscottl
49195534Sscottlimport com.sun.corba.se.spi.orb.ORB ;
50195534Sscottl
51195534Sscottlimport com.sun.corba.se.spi.orbutil.closure.Closure ;
52195534Sscottlimport com.sun.corba.se.spi.orbutil.closure.ClosureFactory ;
53195534Sscottl
54195534Sscottlimport com.sun.corba.se.spi.protocol.PIHandler ;
55195534Sscottl
56195534Sscottlimport com.sun.corba.se.spi.logging.CORBALogDomains ;
57195534Sscottl
58195534Sscottlimport com.sun.corba.se.impl.logging.POASystemException ;
59195534Sscottlimport com.sun.corba.se.impl.logging.OMGSystemException ;
60195534Sscottl
61195534Sscottlimport com.sun.corba.se.impl.orbutil.ORBConstants ;
62195534Sscottl
63195534Sscottlimport com.sun.corba.se.impl.oa.poa.POAManagerImpl ;
64195534Sscottl
65195534Sscottlpublic class POAFactory implements ObjectAdapterFactory
66195534Sscottl{
67195534Sscottl    // Maps servants to POAs for deactivating servants when unexportObject is called.
68195534Sscottl    // Maintained by POAs activate_object and deactivate_object.
69195534Sscottl    private Map exportedServantsToPOA = new WeakHashMap();
70195534Sscottl
71198849Smav    private Set poaManagers ;
72198849Smav    private int poaManagerId ;
73198849Smav    private int poaId ;
74198849Smav    private POAImpl rootPOA ;
75220616Smav    private DelegateImpl delegateImpl;
76220616Smav    private ORB orb ;
77198849Smav    private POASystemException wrapper ;
78198849Smav    private OMGSystemException omgWrapper ;
79198849Smav    private boolean isShuttingDown = false;
80201139Smav
81201139Smav    public POASystemException getWrapper()
82201139Smav    {
83201139Smav        return wrapper ;
84201139Smav    }
85198849Smav
86198849Smav    /** All object adapter factories must have a no-arg constructor.
87198849Smav    */
88198849Smav    public POAFactory()
89198849Smav    {
90198849Smav        poaManagers = Collections.synchronizedSet(new HashSet(4));
91198849Smav        poaManagerId = 0 ;
92198849Smav        poaId = 0 ;
93198849Smav        rootPOA = null ;
94198849Smav        delegateImpl = null ;
95198849Smav        orb = null ;
96198849Smav    }
97198849Smav
98198849Smav    public synchronized POA lookupPOA (Servant servant)
99198849Smav    {
100198849Smav        return (POA)exportedServantsToPOA.get(servant);
101198849Smav    }
102198849Smav
103200008Smav    public synchronized void registerPOAForServant(POA poa, Servant servant)
104200008Smav    {
105198849Smav        exportedServantsToPOA.put(servant, poa);
106198849Smav    }
107198849Smav
108198849Smav    public synchronized void unregisterPOAForServant(POA poa, Servant servant)
109198849Smav    {
110198849Smav        exportedServantsToPOA.remove(servant);
111235897Smav    }
112235897Smav
113235897Smav// Implementation of ObjectAdapterFactory interface
114235897Smav
115235897Smav    public void init( ORB orb )
116235897Smav    {
117235897Smav        this.orb = orb ;
118235897Smav        wrapper = POASystemException.get( orb,
119235897Smav            CORBALogDomains.OA_LIFECYCLE ) ;
120235897Smav        omgWrapper = OMGSystemException.get( orb,
121198849Smav            CORBALogDomains.OA_LIFECYCLE ) ;
122198849Smav        delegateImpl = new DelegateImpl( orb, this ) ;
123198849Smav        registerRootPOA() ;
124198849Smav
125198849Smav        POACurrent poaCurrent = new POACurrent(orb);
126198849Smav        orb.getLocalResolver().register( ORBConstants.POA_CURRENT_NAME,
127198849Smav            ClosureFactory.makeConstant( poaCurrent ) ) ;
128198849Smav    }
129198849Smav
130198849Smav    public ObjectAdapter find( ObjectAdapterId oaid )
131198849Smav    {
132198849Smav        POA poa=null;
133198849Smav        try {
134198849Smav            boolean first = true ;
135198849Smav            Iterator iter = oaid.iterator() ;
136198849Smav            poa = getRootPOA();
137198849Smav            while (iter.hasNext()) {
138198849Smav                String name = (String)(iter.next()) ;
139200008Smav
140198849Smav                if (first) {
141198849Smav                    if (!name.equals( ORBConstants.ROOT_POA_NAME ))
142198849Smav                        throw wrapper.makeFactoryNotPoa( name ) ;
143198849Smav                    first = false ;
144198849Smav                } else {
145198849Smav                    poa = poa.find_POA( name, true ) ;
146198849Smav                }
147198849Smav            }
148198849Smav        } catch ( org.omg.PortableServer.POAPackage.AdapterNonExistent ex ){
149198849Smav            throw omgWrapper.noObjectAdaptor( ex ) ;
150198849Smav        } catch ( OBJECT_NOT_EXIST ex ) {
151198849Smav            throw ex;
152198849Smav        } catch ( TRANSIENT ex ) {
153198849Smav            throw ex;
154198849Smav        } catch ( Exception ex ) {
155198849Smav            throw wrapper.poaLookupError( ex ) ;
156198849Smav        }
157198849Smav
158198849Smav        if ( poa == null )
159198849Smav            throw wrapper.poaLookupError() ;
160198849Smav
161203421Smav        return (ObjectAdapter)poa;
162203421Smav    }
163203421Smav
164220616Smav    public void shutdown( boolean waitForCompletion )
165220616Smav    {
166198849Smav        // It is important to copy the list of POAManagers first because
167198849Smav        // pm.deactivate removes itself from poaManagers!
168198849Smav        Iterator managers = null ;
169198849Smav        synchronized (this) {
170198849Smav            isShuttingDown = true ;
171198849Smav            managers = (new HashSet(poaManagers)).iterator();
172198849Smav        }
173198849Smav
174238363Sbrueffer        while ( managers.hasNext() ) {
175238363Sbrueffer            try {
176198849Smav                ((POAManager)managers.next()).deactivate(true, waitForCompletion);
177198849Smav            } catch ( org.omg.PortableServer.POAManagerPackage.AdapterInactive e ) {}
178198849Smav        }
179198849Smav    }
180198849Smav
181198849Smav// Special methods used to manipulate global POA related state
182198849Smav
183198849Smav    public synchronized void removePoaManager( POAManager manager )
184198849Smav    {
185198849Smav        poaManagers.remove(manager);
186198849Smav    }
187198849Smav
188198849Smav    public synchronized void addPoaManager( POAManager manager )
189198849Smav    {
190198849Smav        poaManagers.add(manager);
191198849Smav    }
192198849Smav
193198849Smav    synchronized public int newPOAManagerId()
194198849Smav    {
195198849Smav        return poaManagerId++ ;
196198849Smav    }
197198849Smav
198198849Smav    public void registerRootPOA()
199198849Smav    {
200198849Smav        // We delay the evaluation of makeRootPOA until
201198849Smav        // a call to resolve_initial_references( "RootPOA" ).
202198849Smav        // The Future guarantees that makeRootPOA is only called once.
203198849Smav        Closure rpClosure = new Closure() {
204198849Smav            public Object evaluate() {
205198849Smav                return POAImpl.makeRootPOA( orb ) ;
206198849Smav            }
207198849Smav        } ;
208198849Smav
209198849Smav        orb.getLocalResolver().register( ORBConstants.ROOT_POA_NAME,
210198849Smav            ClosureFactory.makeFuture( rpClosure ) ) ;
211198849Smav    }
212198849Smav
213198849Smav
214198849Smav    public synchronized POA getRootPOA()
215198849Smav    {
216198849Smav        if (rootPOA == null) {
217198849Smav            // See if we are trying to getRootPOA while shutting down the ORB.
218203108Smav            if (isShuttingDown) {
219198849Smav                throw omgWrapper.noObjectAdaptor( ) ;
220198849Smav            }
221198849Smav
222198849Smav            try {
223198849Smav                Object obj = orb.resolve_initial_references(
224198849Smav                    ORBConstants.ROOT_POA_NAME ) ;
225198849Smav                rootPOA = (POAImpl)obj ;
226198849Smav            } catch (InvalidName inv) {
227198849Smav                throw wrapper.cantResolveRootPoa( inv ) ;
228198849Smav            }
229198849Smav        }
230198849Smav
231198849Smav        return rootPOA;
232203108Smav    }
233198849Smav
234198849Smav    public org.omg.PortableServer.portable.Delegate getDelegateImpl()
235198849Smav    {
236198849Smav        return delegateImpl ;
237198849Smav    }
238198849Smav
239198849Smav    synchronized public int newPOAId()
240198849Smav    {
241198849Smav        return poaId++ ;
242198849Smav    }
243203108Smav
244198849Smav    public ORB getORB()
245198849Smav    {
246198849Smav        return orb ;
247198849Smav    }
248198849Smav}
249198849Smav