Thu, 31 Aug 2017 18:10:36 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.corba.se.impl.oa.poa ; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.util.Set ; |
aoqi@0 | 29 | import java.util.HashSet ; |
aoqi@0 | 30 | import java.util.Collections ; |
aoqi@0 | 31 | import java.util.Iterator ; |
aoqi@0 | 32 | import java.util.Map ; |
aoqi@0 | 33 | import java.util.WeakHashMap ; |
aoqi@0 | 34 | |
aoqi@0 | 35 | import org.omg.CORBA.OBJECT_NOT_EXIST ; |
aoqi@0 | 36 | import org.omg.CORBA.TRANSIENT ; |
aoqi@0 | 37 | |
aoqi@0 | 38 | import org.omg.CORBA.ORBPackage.InvalidName ; |
aoqi@0 | 39 | |
aoqi@0 | 40 | import org.omg.PortableServer.Servant ; |
aoqi@0 | 41 | import org.omg.PortableServer.POA ; |
aoqi@0 | 42 | import org.omg.PortableServer.POAManager ; |
aoqi@0 | 43 | |
aoqi@0 | 44 | import com.sun.corba.se.spi.oa.ObjectAdapter ; |
aoqi@0 | 45 | import com.sun.corba.se.spi.oa.ObjectAdapterFactory ; |
aoqi@0 | 46 | |
aoqi@0 | 47 | import com.sun.corba.se.spi.ior.ObjectAdapterId ; |
aoqi@0 | 48 | |
aoqi@0 | 49 | import com.sun.corba.se.spi.orb.ORB ; |
aoqi@0 | 50 | |
aoqi@0 | 51 | import com.sun.corba.se.spi.orbutil.closure.Closure ; |
aoqi@0 | 52 | import com.sun.corba.se.spi.orbutil.closure.ClosureFactory ; |
aoqi@0 | 53 | |
aoqi@0 | 54 | import com.sun.corba.se.spi.protocol.PIHandler ; |
aoqi@0 | 55 | |
aoqi@0 | 56 | import com.sun.corba.se.spi.logging.CORBALogDomains ; |
aoqi@0 | 57 | |
aoqi@0 | 58 | import com.sun.corba.se.impl.logging.POASystemException ; |
aoqi@0 | 59 | import com.sun.corba.se.impl.logging.OMGSystemException ; |
aoqi@0 | 60 | |
aoqi@0 | 61 | import com.sun.corba.se.impl.orbutil.ORBConstants ; |
aoqi@0 | 62 | |
aoqi@0 | 63 | import com.sun.corba.se.impl.oa.poa.POAManagerImpl ; |
aoqi@0 | 64 | |
aoqi@0 | 65 | public class POAFactory implements ObjectAdapterFactory |
aoqi@0 | 66 | { |
aoqi@0 | 67 | // Maps servants to POAs for deactivating servants when unexportObject is called. |
aoqi@0 | 68 | // Maintained by POAs activate_object and deactivate_object. |
aoqi@0 | 69 | private Map exportedServantsToPOA = new WeakHashMap(); |
aoqi@0 | 70 | |
aoqi@0 | 71 | private Set poaManagers ; |
aoqi@0 | 72 | private int poaManagerId ; |
aoqi@0 | 73 | private int poaId ; |
aoqi@0 | 74 | private POAImpl rootPOA ; |
aoqi@0 | 75 | private DelegateImpl delegateImpl; |
aoqi@0 | 76 | private ORB orb ; |
aoqi@0 | 77 | private POASystemException wrapper ; |
aoqi@0 | 78 | private OMGSystemException omgWrapper ; |
aoqi@0 | 79 | private boolean isShuttingDown = false; |
aoqi@0 | 80 | |
aoqi@0 | 81 | public POASystemException getWrapper() |
aoqi@0 | 82 | { |
aoqi@0 | 83 | return wrapper ; |
aoqi@0 | 84 | } |
aoqi@0 | 85 | |
aoqi@0 | 86 | /** All object adapter factories must have a no-arg constructor. |
aoqi@0 | 87 | */ |
aoqi@0 | 88 | public POAFactory() |
aoqi@0 | 89 | { |
aoqi@0 | 90 | poaManagers = Collections.synchronizedSet(new HashSet(4)); |
aoqi@0 | 91 | poaManagerId = 0 ; |
aoqi@0 | 92 | poaId = 0 ; |
aoqi@0 | 93 | rootPOA = null ; |
aoqi@0 | 94 | delegateImpl = null ; |
aoqi@0 | 95 | orb = null ; |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | public synchronized POA lookupPOA (Servant servant) |
aoqi@0 | 99 | { |
aoqi@0 | 100 | return (POA)exportedServantsToPOA.get(servant); |
aoqi@0 | 101 | } |
aoqi@0 | 102 | |
aoqi@0 | 103 | public synchronized void registerPOAForServant(POA poa, Servant servant) |
aoqi@0 | 104 | { |
aoqi@0 | 105 | exportedServantsToPOA.put(servant, poa); |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | public synchronized void unregisterPOAForServant(POA poa, Servant servant) |
aoqi@0 | 109 | { |
aoqi@0 | 110 | exportedServantsToPOA.remove(servant); |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | // Implementation of ObjectAdapterFactory interface |
aoqi@0 | 114 | |
aoqi@0 | 115 | public void init( ORB orb ) |
aoqi@0 | 116 | { |
aoqi@0 | 117 | this.orb = orb ; |
aoqi@0 | 118 | wrapper = POASystemException.get( orb, |
aoqi@0 | 119 | CORBALogDomains.OA_LIFECYCLE ) ; |
aoqi@0 | 120 | omgWrapper = OMGSystemException.get( orb, |
aoqi@0 | 121 | CORBALogDomains.OA_LIFECYCLE ) ; |
aoqi@0 | 122 | delegateImpl = new DelegateImpl( orb, this ) ; |
aoqi@0 | 123 | registerRootPOA() ; |
aoqi@0 | 124 | |
aoqi@0 | 125 | POACurrent poaCurrent = new POACurrent(orb); |
aoqi@0 | 126 | orb.getLocalResolver().register( ORBConstants.POA_CURRENT_NAME, |
aoqi@0 | 127 | ClosureFactory.makeConstant( poaCurrent ) ) ; |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | public ObjectAdapter find( ObjectAdapterId oaid ) |
aoqi@0 | 131 | { |
aoqi@0 | 132 | POA poa=null; |
aoqi@0 | 133 | try { |
aoqi@0 | 134 | boolean first = true ; |
aoqi@0 | 135 | Iterator iter = oaid.iterator() ; |
aoqi@0 | 136 | poa = getRootPOA(); |
aoqi@0 | 137 | while (iter.hasNext()) { |
aoqi@0 | 138 | String name = (String)(iter.next()) ; |
aoqi@0 | 139 | |
aoqi@0 | 140 | if (first) { |
aoqi@0 | 141 | if (!name.equals( ORBConstants.ROOT_POA_NAME )) |
aoqi@0 | 142 | throw wrapper.makeFactoryNotPoa( name ) ; |
aoqi@0 | 143 | first = false ; |
aoqi@0 | 144 | } else { |
aoqi@0 | 145 | poa = poa.find_POA( name, true ) ; |
aoqi@0 | 146 | } |
aoqi@0 | 147 | } |
aoqi@0 | 148 | } catch ( org.omg.PortableServer.POAPackage.AdapterNonExistent ex ){ |
aoqi@0 | 149 | throw omgWrapper.noObjectAdaptor( ex ) ; |
aoqi@0 | 150 | } catch ( OBJECT_NOT_EXIST ex ) { |
aoqi@0 | 151 | throw ex; |
aoqi@0 | 152 | } catch ( TRANSIENT ex ) { |
aoqi@0 | 153 | throw ex; |
aoqi@0 | 154 | } catch ( Exception ex ) { |
aoqi@0 | 155 | throw wrapper.poaLookupError( ex ) ; |
aoqi@0 | 156 | } |
aoqi@0 | 157 | |
aoqi@0 | 158 | if ( poa == null ) |
aoqi@0 | 159 | throw wrapper.poaLookupError() ; |
aoqi@0 | 160 | |
aoqi@0 | 161 | return (ObjectAdapter)poa; |
aoqi@0 | 162 | } |
aoqi@0 | 163 | |
aoqi@0 | 164 | public void shutdown( boolean waitForCompletion ) |
aoqi@0 | 165 | { |
aoqi@0 | 166 | // It is important to copy the list of POAManagers first because |
aoqi@0 | 167 | // pm.deactivate removes itself from poaManagers! |
aoqi@0 | 168 | Iterator managers = null ; |
aoqi@0 | 169 | synchronized (this) { |
aoqi@0 | 170 | isShuttingDown = true ; |
aoqi@0 | 171 | managers = (new HashSet(poaManagers)).iterator(); |
aoqi@0 | 172 | } |
aoqi@0 | 173 | |
aoqi@0 | 174 | while ( managers.hasNext() ) { |
aoqi@0 | 175 | try { |
aoqi@0 | 176 | ((POAManager)managers.next()).deactivate(true, waitForCompletion); |
aoqi@0 | 177 | } catch ( org.omg.PortableServer.POAManagerPackage.AdapterInactive e ) {} |
aoqi@0 | 178 | } |
aoqi@0 | 179 | } |
aoqi@0 | 180 | |
aoqi@0 | 181 | // Special methods used to manipulate global POA related state |
aoqi@0 | 182 | |
aoqi@0 | 183 | public synchronized void removePoaManager( POAManager manager ) |
aoqi@0 | 184 | { |
aoqi@0 | 185 | poaManagers.remove(manager); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | |
aoqi@0 | 188 | public synchronized void addPoaManager( POAManager manager ) |
aoqi@0 | 189 | { |
aoqi@0 | 190 | poaManagers.add(manager); |
aoqi@0 | 191 | } |
aoqi@0 | 192 | |
aoqi@0 | 193 | synchronized public int newPOAManagerId() |
aoqi@0 | 194 | { |
aoqi@0 | 195 | return poaManagerId++ ; |
aoqi@0 | 196 | } |
aoqi@0 | 197 | |
aoqi@0 | 198 | public void registerRootPOA() |
aoqi@0 | 199 | { |
aoqi@0 | 200 | // We delay the evaluation of makeRootPOA until |
aoqi@0 | 201 | // a call to resolve_initial_references( "RootPOA" ). |
aoqi@0 | 202 | // The Future guarantees that makeRootPOA is only called once. |
aoqi@0 | 203 | Closure rpClosure = new Closure() { |
aoqi@0 | 204 | public Object evaluate() { |
aoqi@0 | 205 | return POAImpl.makeRootPOA( orb ) ; |
aoqi@0 | 206 | } |
aoqi@0 | 207 | } ; |
aoqi@0 | 208 | |
aoqi@0 | 209 | orb.getLocalResolver().register( ORBConstants.ROOT_POA_NAME, |
aoqi@0 | 210 | ClosureFactory.makeFuture( rpClosure ) ) ; |
aoqi@0 | 211 | } |
aoqi@0 | 212 | |
aoqi@0 | 213 | |
aoqi@0 | 214 | public synchronized POA getRootPOA() |
aoqi@0 | 215 | { |
aoqi@0 | 216 | if (rootPOA == null) { |
aoqi@0 | 217 | // See if we are trying to getRootPOA while shutting down the ORB. |
aoqi@0 | 218 | if (isShuttingDown) { |
aoqi@0 | 219 | throw omgWrapper.noObjectAdaptor( ) ; |
aoqi@0 | 220 | } |
aoqi@0 | 221 | |
aoqi@0 | 222 | try { |
aoqi@0 | 223 | Object obj = orb.resolve_initial_references( |
aoqi@0 | 224 | ORBConstants.ROOT_POA_NAME ) ; |
aoqi@0 | 225 | rootPOA = (POAImpl)obj ; |
aoqi@0 | 226 | } catch (InvalidName inv) { |
aoqi@0 | 227 | throw wrapper.cantResolveRootPoa( inv ) ; |
aoqi@0 | 228 | } |
aoqi@0 | 229 | } |
aoqi@0 | 230 | |
aoqi@0 | 231 | return rootPOA; |
aoqi@0 | 232 | } |
aoqi@0 | 233 | |
aoqi@0 | 234 | public org.omg.PortableServer.portable.Delegate getDelegateImpl() |
aoqi@0 | 235 | { |
aoqi@0 | 236 | return delegateImpl ; |
aoqi@0 | 237 | } |
aoqi@0 | 238 | |
aoqi@0 | 239 | synchronized public int newPOAId() |
aoqi@0 | 240 | { |
aoqi@0 | 241 | return poaId++ ; |
aoqi@0 | 242 | } |
aoqi@0 | 243 | |
aoqi@0 | 244 | public ORB getORB() |
aoqi@0 | 245 | { |
aoqi@0 | 246 | return orb ; |
aoqi@0 | 247 | } |
aoqi@0 | 248 | } |