1 7 8 package com.sun.corba.se.impl.oa.poa ; 9 10 import java.util.Set ; 11 import java.util.HashSet ; 12 import java.util.Collections ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.WeakHashMap ; 16 17 import org.omg.CORBA.OBJECT_NOT_EXIST ; 18 import org.omg.CORBA.TRANSIENT ; 19 20 import org.omg.CORBA.ORBPackage.InvalidName ; 21 22 import org.omg.PortableServer.Servant ; 23 import org.omg.PortableServer.POA ; 24 import org.omg.PortableServer.POAManager ; 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 private Map exportedServantsToPOA = new WeakHashMap (); 52 53 private Set 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 69 public POAFactory() 70 { 71 poaManagers = Collections.synchronizedSet(new HashSet (4)); 72 poaManagerId = 0 ; 73 poaId = 0 ; 74 rootPOA = null ; 75 delegateImpl = null ; 76 orb = null ; 77 } 78 79 public synchronized POA lookupPOA (Servant servant) 80 { 81 return (POA )exportedServantsToPOA.get(servant); 82 } 83 84 public synchronized void registerPOAForServant(POA poa, Servant servant) 85 { 86 exportedServantsToPOA.put(servant, poa); 87 } 88 89 public synchronized void unregisterPOAForServant(POA poa, Servant servant) 90 { 91 exportedServantsToPOA.remove(servant); 92 } 93 94 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 poa=null; 114 try { 115 boolean first = true ; 116 Iterator iter = oaid.iterator() ; 117 poa = getRootPOA(); 118 while (iter.hasNext()) { 119 String name = (String )(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 ex ){ 130 throw omgWrapper.noObjectAdaptor( ex ) ; 131 } catch ( OBJECT_NOT_EXIST ex ) { 132 throw ex; 133 } catch ( TRANSIENT ex ) { 134 throw ex; 135 } catch ( Exception 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 Iterator managers = null ; 150 synchronized (this) { 151 managers = (new HashSet (poaManagers)).iterator(); 152 } 153 154 while ( managers.hasNext() ) { 155 try { 156 ((POAManager )managers.next()).deactivate(true, waitForCompletion); 157 } catch ( org.omg.PortableServer.POAManagerPackage.AdapterInactive e ) {} 158 } 159 } 160 161 163 public synchronized void removePoaManager( POAManager manager ) 164 { 165 poaManagers.remove(manager); 166 } 167 168 public synchronized void addPoaManager( POAManager 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 Closure rpClosure = new Closure() { 184 public Object 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 getRootPOA() 194 { 195 if (rootPOA == null) { 196 try { 197 Object obj = orb.resolve_initial_references( 198 ORBConstants.ROOT_POA_NAME ) ; 199 rootPOA = (POAImpl)obj ; 200 } catch (InvalidName inv) { 201 throw wrapper.cantResolveRootPoa( inv ) ; 202 } 203 } 204 205 return rootPOA; 206 } 207 208 public org.omg.PortableServer.portable.Delegate 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 |