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. package com.sun.corba.se.impl.oa.poa ;
  8. import java.util.Set ;
  9. import java.util.HashSet ;
  10. import java.util.Collections ;
  11. import java.util.Iterator ;
  12. import java.util.Map ;
  13. import java.util.WeakHashMap ;
  14. import org.omg.CORBA.OBJECT_NOT_EXIST ;
  15. import org.omg.CORBA.TRANSIENT ;
  16. import org.omg.CORBA.ORBPackage.InvalidName ;
  17. import org.omg.PortableServer.Servant ;
  18. import org.omg.PortableServer.POA ;
  19. import org.omg.PortableServer.POAManager ;
  20. import com.sun.corba.se.spi.oa.ObjectAdapter ;
  21. import com.sun.corba.se.spi.oa.ObjectAdapterFactory ;
  22. import com.sun.corba.se.spi.ior.ObjectAdapterId ;
  23. import com.sun.corba.se.spi.orb.ORB ;
  24. import com.sun.corba.se.spi.orbutil.closure.Closure ;
  25. import com.sun.corba.se.spi.orbutil.closure.ClosureFactory ;
  26. import com.sun.corba.se.spi.protocol.PIHandler ;
  27. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  28. import com.sun.corba.se.impl.logging.POASystemException ;
  29. import com.sun.corba.se.impl.logging.OMGSystemException ;
  30. import com.sun.corba.se.impl.orbutil.ORBConstants ;
  31. import com.sun.corba.se.impl.oa.poa.POAManagerImpl ;
  32. public class POAFactory implements ObjectAdapterFactory
  33. {
  34. // Maps servants to POAs for deactivating servants when unexportObject is called.
  35. // Maintained by POAs activate_object and deactivate_object.
  36. private Map exportedServantsToPOA = new WeakHashMap();
  37. private Set poaManagers ;
  38. private int poaManagerId ;
  39. private int poaId ;
  40. private POAImpl rootPOA ;
  41. private DelegateImpl delegateImpl;
  42. private ORB orb ;
  43. private POASystemException wrapper ;
  44. private OMGSystemException omgWrapper ;
  45. public POASystemException getWrapper()
  46. {
  47. return wrapper ;
  48. }
  49. /** All object adapter factories must have a no-arg constructor.
  50. */
  51. public POAFactory()
  52. {
  53. poaManagers = Collections.synchronizedSet(new HashSet(4));
  54. poaManagerId = 0 ;
  55. poaId = 0 ;
  56. rootPOA = null ;
  57. delegateImpl = null ;
  58. orb = null ;
  59. }
  60. public synchronized POA lookupPOA (Servant servant)
  61. {
  62. return (POA)exportedServantsToPOA.get(servant);
  63. }
  64. public synchronized void registerPOAForServant(POA poa, Servant servant)
  65. {
  66. exportedServantsToPOA.put(servant, poa);
  67. }
  68. public synchronized void unregisterPOAForServant(POA poa, Servant servant)
  69. {
  70. exportedServantsToPOA.remove(servant);
  71. }
  72. // Implementation of ObjectAdapterFactory interface
  73. public void init( ORB orb )
  74. {
  75. this.orb = orb ;
  76. wrapper = POASystemException.get( orb,
  77. CORBALogDomains.OA_LIFECYCLE ) ;
  78. omgWrapper = OMGSystemException.get( orb,
  79. CORBALogDomains.OA_LIFECYCLE ) ;
  80. delegateImpl = new DelegateImpl( orb, this ) ;
  81. registerRootPOA() ;
  82. POACurrent poaCurrent = new POACurrent(orb);
  83. orb.getLocalResolver().register( ORBConstants.POA_CURRENT_NAME,
  84. ClosureFactory.makeConstant( poaCurrent ) ) ;
  85. }
  86. public ObjectAdapter find( ObjectAdapterId oaid )
  87. {
  88. POA poa=null;
  89. try {
  90. boolean first = true ;
  91. Iterator iter = oaid.iterator() ;
  92. poa = getRootPOA();
  93. while (iter.hasNext()) {
  94. String name = (String)(iter.next()) ;
  95. if (first) {
  96. if (!name.equals( ORBConstants.ROOT_POA_NAME ))
  97. throw wrapper.makeFactoryNotPoa( name ) ;
  98. first = false ;
  99. } else {
  100. poa = poa.find_POA( name, true ) ;
  101. }
  102. }
  103. } catch ( org.omg.PortableServer.POAPackage.AdapterNonExistent ex ){
  104. throw omgWrapper.noObjectAdaptor( ex ) ;
  105. } catch ( OBJECT_NOT_EXIST ex ) {
  106. throw ex;
  107. } catch ( TRANSIENT ex ) {
  108. throw ex;
  109. } catch ( Exception ex ) {
  110. throw wrapper.poaLookupError( ex ) ;
  111. }
  112. if ( poa == null )
  113. throw wrapper.poaLookupError() ;
  114. return (ObjectAdapter)poa;
  115. }
  116. public void shutdown( boolean waitForCompletion )
  117. {
  118. // It is important to copy the list of POAManagers first because
  119. // pm.deactivate removes itself from poaManagers!
  120. Iterator managers = null ;
  121. synchronized (this) {
  122. managers = (new HashSet(poaManagers)).iterator();
  123. }
  124. while ( managers.hasNext() ) {
  125. try {
  126. ((POAManager)managers.next()).deactivate(true, waitForCompletion);
  127. } catch ( org.omg.PortableServer.POAManagerPackage.AdapterInactive e ) {}
  128. }
  129. }
  130. // Special methods used to manipulate global POA related state
  131. public synchronized void removePoaManager( POAManager manager )
  132. {
  133. poaManagers.remove(manager);
  134. }
  135. public synchronized void addPoaManager( POAManager manager )
  136. {
  137. poaManagers.add(manager);
  138. }
  139. synchronized public int newPOAManagerId()
  140. {
  141. return poaManagerId++ ;
  142. }
  143. public void registerRootPOA()
  144. {
  145. // We delay the evaluation of makeRootPOA until
  146. // a call to resolve_initial_references( "RootPOA" ).
  147. // The Future guarantees that makeRootPOA is only called once.
  148. Closure rpClosure = new Closure() {
  149. public Object evaluate() {
  150. return POAImpl.makeRootPOA( orb ) ;
  151. }
  152. } ;
  153. orb.getLocalResolver().register( ORBConstants.ROOT_POA_NAME,
  154. ClosureFactory.makeFuture( rpClosure ) ) ;
  155. }
  156. public synchronized POA getRootPOA()
  157. {
  158. if (rootPOA == null) {
  159. try {
  160. Object obj = orb.resolve_initial_references(
  161. ORBConstants.ROOT_POA_NAME ) ;
  162. rootPOA = (POAImpl)obj ;
  163. } catch (InvalidName inv) {
  164. throw wrapper.cantResolveRootPoa( inv ) ;
  165. }
  166. }
  167. return rootPOA;
  168. }
  169. public org.omg.PortableServer.portable.Delegate getDelegateImpl()
  170. {
  171. return delegateImpl ;
  172. }
  173. synchronized public int newPOAId()
  174. {
  175. return poaId++ ;
  176. }
  177. public ORB getORB()
  178. {
  179. return orb ;
  180. }
  181. }