1. /*
  2. * @(#)POAPolicyMediatorBase_R.java 1.24 04/03/01
  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 org.omg.PortableServer.Servant ;
  9. import org.omg.PortableServer.POAPackage.WrongPolicy ;
  10. import org.omg.PortableServer.POAPackage.ServantNotActive ;
  11. import org.omg.PortableServer.POAPackage.ServantAlreadyActive ;
  12. import org.omg.PortableServer.POAPackage.ObjectNotActive ;
  13. import org.omg.PortableServer.POAPackage.ObjectAlreadyActive ;
  14. import com.sun.corba.se.impl.orbutil.concurrent.SyncUtil ;
  15. import com.sun.corba.se.impl.orbutil.ORBUtility ;
  16. import com.sun.corba.se.impl.orbutil.ORBConstants ;
  17. import com.sun.corba.se.impl.javax.rmi.CORBA.Util ;
  18. import com.sun.corba.se.impl.oa.NullServantImpl ;
  19. public abstract class POAPolicyMediatorBase_R extends POAPolicyMediatorBase {
  20. protected ActiveObjectMap activeObjectMap ;
  21. POAPolicyMediatorBase_R( Policies policies, POAImpl poa )
  22. {
  23. super( policies, poa ) ;
  24. // assert policies.retainServants() && policies.useActiveObjectMapOnly()
  25. if (!policies.retainServants())
  26. throw poa.invocationWrapper().policyMediatorBadPolicyInFactory() ;
  27. activeObjectMap = ActiveObjectMap.create(poa, !isUnique);
  28. }
  29. public void returnServant()
  30. {
  31. // NO-OP
  32. }
  33. public void clearAOM()
  34. {
  35. activeObjectMap.clear() ;
  36. activeObjectMap = null ;
  37. }
  38. protected Servant internalKeyToServant( ActiveObjectMap.Key key )
  39. {
  40. AOMEntry entry = activeObjectMap.get(key);
  41. if (entry == null)
  42. return null ;
  43. return activeObjectMap.getServant( entry ) ;
  44. }
  45. protected Servant internalIdToServant( byte[] id )
  46. {
  47. ActiveObjectMap.Key key = new ActiveObjectMap.Key( id ) ;
  48. return internalKeyToServant( key ) ;
  49. }
  50. protected void activateServant( ActiveObjectMap.Key key, AOMEntry entry, Servant servant )
  51. {
  52. setDelegate(servant, key.id );
  53. if (orb.shutdownDebugFlag) {
  54. System.out.println("Activating object " + servant +
  55. " with POA " + poa);
  56. }
  57. activeObjectMap.putServant( servant, entry ) ;
  58. if (Util.instance != null) {
  59. POAManagerImpl pm = (POAManagerImpl)poa.the_POAManager() ;
  60. POAFactory factory = pm.getFactory() ;
  61. factory.registerPOAForServant(poa, servant);
  62. }
  63. }
  64. public final void activateObject(byte[] id, Servant servant)
  65. throws WrongPolicy, ServantAlreadyActive, ObjectAlreadyActive
  66. {
  67. if (isUnique && activeObjectMap.contains(servant))
  68. throw new ServantAlreadyActive();
  69. ActiveObjectMap.Key key = new ActiveObjectMap.Key( id ) ;
  70. // Note that this can't happen for system assigned IDs since the
  71. // POA never hands out the same ID. However, we make this redundant
  72. // check here to share the code.
  73. if (activeObjectMap.containsKey(key))
  74. throw new ObjectAlreadyActive() ;
  75. AOMEntry entry = activeObjectMap.get( key ) ;
  76. entry.activateObject() ;
  77. activateServant( key, entry, servant ) ;
  78. }
  79. public Servant deactivateObject( byte[] id )
  80. throws ObjectNotActive, WrongPolicy
  81. {
  82. ActiveObjectMap.Key key = new ActiveObjectMap.Key( id ) ;
  83. return deactivateObject( key ) ;
  84. }
  85. protected void deactivateHelper( ActiveObjectMap.Key key, AOMEntry entry,
  86. Servant s ) throws ObjectNotActive, WrongPolicy
  87. {
  88. // Default does nothing, but the USE_SERVANT_MANAGER case
  89. // must handle etherealization
  90. activeObjectMap.remove(key);
  91. if (Util.instance != null) {
  92. POAManagerImpl pm = (POAManagerImpl)poa.the_POAManager() ;
  93. POAFactory factory = pm.getFactory() ;
  94. factory.unregisterPOAForServant(poa, s);
  95. }
  96. }
  97. public Servant deactivateObject( ActiveObjectMap.Key key )
  98. throws ObjectNotActive, WrongPolicy
  99. {
  100. if (orb.poaDebugFlag) {
  101. ORBUtility.dprint( this,
  102. "Calling deactivateObject for key " + key ) ;
  103. }
  104. try {
  105. AOMEntry entry = activeObjectMap.get(key);
  106. if (entry == null)
  107. throw new ObjectNotActive();
  108. Servant s = activeObjectMap.getServant( entry ) ;
  109. if (s == null)
  110. throw new ObjectNotActive();
  111. if (orb.poaDebugFlag) {
  112. System.out.println("Deactivating object " + s + " with POA " + poa);
  113. }
  114. deactivateHelper( key, entry, s ) ;
  115. return s ;
  116. } finally {
  117. if (orb.poaDebugFlag) {
  118. ORBUtility.dprint( this,
  119. "Exiting deactivateObject" ) ;
  120. }
  121. }
  122. }
  123. public byte[] servantToId( Servant servant ) throws ServantNotActive, WrongPolicy
  124. {
  125. // XXX needs to handle call from an invocation on this POA
  126. if (!isUnique && !isImplicit)
  127. throw new WrongPolicy();
  128. if (isUnique) {
  129. ActiveObjectMap.Key key = activeObjectMap.getKey(servant);
  130. if (key != null)
  131. return key.id ;
  132. }
  133. // assert !isUnique || (servant not in activateObjectMap)
  134. if (isImplicit)
  135. try {
  136. byte[] id = newSystemId() ;
  137. activateObject( id, servant ) ;
  138. return id ;
  139. } catch (ObjectAlreadyActive oaa) {
  140. // This can't occur here, since id is always brand new.
  141. throw poa.invocationWrapper().servantToIdOaa( oaa ) ;
  142. } catch (ServantAlreadyActive s) {
  143. throw poa.invocationWrapper().servantToIdSaa( s ) ;
  144. } catch (WrongPolicy w) {
  145. throw poa.invocationWrapper().servantToIdWp( w ) ;
  146. }
  147. throw new ServantNotActive();
  148. }
  149. }