1. /*
  2. * @(#)StubAdapter.java 1.4 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.spi.presentation.rmi ;
  8. import javax.rmi.CORBA.Tie ;
  9. import org.omg.CORBA.portable.Delegate ;
  10. import org.omg.CORBA.portable.ObjectImpl ;
  11. import org.omg.CORBA.portable.OutputStream ;
  12. import org.omg.PortableServer.POA ;
  13. import org.omg.PortableServer.POAManager ;
  14. import org.omg.PortableServer.Servant ;
  15. import org.omg.PortableServer.POAPackage.WrongPolicy ;
  16. import org.omg.PortableServer.POAPackage.ServantNotActive ;
  17. import org.omg.PortableServer.POAManagerPackage.AdapterInactive ;
  18. import org.omg.CORBA.ORB ;
  19. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  20. import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  21. // XXX Getting rid of this requires introducing an ObjectAdapterManager abstraction
  22. // as an interface into the OA framework.
  23. import com.sun.corba.se.impl.oa.poa.POAManagerImpl ;
  24. /** Provide access to stub delegate and type id information
  25. * independent of the stub type. This class exists because
  26. * ObjectImpl does not have an interface for the 3 delegate and
  27. * type id methods, so a DynamicStub has a different type.
  28. * We cannot simply change ObjectImpl as it is a standard API.
  29. * We also cannot change the code generation of Stubs, as that
  30. * is also standard. Hence I am left with this ugly class.
  31. */
  32. public abstract class StubAdapter
  33. {
  34. private StubAdapter() {}
  35. private static ORBUtilSystemException wrapper =
  36. ORBUtilSystemException.get( CORBALogDomains.RPC_PRESENTATION ) ;
  37. public static boolean isStubClass( Class cls )
  38. {
  39. return (ObjectImpl.class.isAssignableFrom( cls )) ||
  40. (DynamicStub.class.isAssignableFrom( cls )) ;
  41. }
  42. public static boolean isStub( Object stub )
  43. {
  44. return (stub instanceof DynamicStub) ||
  45. (stub instanceof ObjectImpl) ;
  46. }
  47. public static void setDelegate( Object stub, Delegate delegate )
  48. {
  49. if (stub instanceof DynamicStub)
  50. ((DynamicStub)stub).setDelegate( delegate ) ;
  51. else if (stub instanceof ObjectImpl)
  52. ((ObjectImpl)stub)._set_delegate( delegate ) ;
  53. else
  54. throw wrapper.setDelegateRequiresStub() ;
  55. }
  56. /** Use implicit activation to get an object reference for the servant.
  57. */
  58. public static org.omg.CORBA.Object activateServant( Servant servant )
  59. {
  60. POA poa = servant._default_POA() ;
  61. org.omg.CORBA.Object ref = null ;
  62. try {
  63. ref = poa.servant_to_reference( servant ) ;
  64. } catch (ServantNotActive sna) {
  65. throw wrapper.getDelegateServantNotActive( sna ) ;
  66. } catch (WrongPolicy wp) {
  67. throw wrapper.getDelegateWrongPolicy( wp ) ;
  68. }
  69. // Make sure that the POAManager is activated if no other
  70. // POAManager state management has taken place.
  71. POAManager mgr = poa.the_POAManager() ;
  72. if (mgr instanceof POAManagerImpl) {
  73. POAManagerImpl mgrImpl = (POAManagerImpl)mgr ;
  74. mgrImpl.implicitActivation() ;
  75. }
  76. return ref ;
  77. }
  78. /** Given any Tie, return the corresponding object refernce, activating
  79. * the Servant if necessary.
  80. */
  81. public static org.omg.CORBA.Object activateTie( Tie tie )
  82. {
  83. /** Any implementation of Tie should be either a Servant or an ObjectImpl,
  84. * depending on which style of code generation is used. rmic -iiop by
  85. * default results in an ObjectImpl-based Tie, while rmic -iiop -poa
  86. * results in a Servant-based Tie. Dynamic RMI-IIOP also uses Servant-based
  87. * Ties (see impl.presentation.rmi.ReflectiveTie).
  88. */
  89. if (tie instanceof ObjectImpl) {
  90. return tie.thisObject() ;
  91. } else if (tie instanceof Servant) {
  92. Servant servant = (Servant)tie ;
  93. return activateServant( servant ) ;
  94. } else {
  95. throw wrapper.badActivateTieCall() ;
  96. }
  97. }
  98. /** This also gets the delegate from a Servant by
  99. * using Servant._this_object()
  100. */
  101. public static Delegate getDelegate( Object stub )
  102. {
  103. if (stub instanceof DynamicStub)
  104. return ((DynamicStub)stub).getDelegate() ;
  105. else if (stub instanceof ObjectImpl)
  106. return ((ObjectImpl)stub)._get_delegate() ;
  107. else if (stub instanceof Tie) {
  108. Tie tie = (Tie)stub ;
  109. org.omg.CORBA.Object ref = activateTie( tie ) ;
  110. return getDelegate( ref ) ;
  111. } else
  112. throw wrapper.getDelegateRequiresStub() ;
  113. }
  114. public static ORB getORB( Object stub )
  115. {
  116. if (stub instanceof DynamicStub)
  117. return ((DynamicStub)stub).getORB() ;
  118. else if (stub instanceof ObjectImpl)
  119. return (ORB)((ObjectImpl)stub)._orb() ;
  120. else
  121. throw wrapper.getOrbRequiresStub() ;
  122. }
  123. public static String[] getTypeIds( Object stub )
  124. {
  125. if (stub instanceof DynamicStub)
  126. return ((DynamicStub)stub).getTypeIds() ;
  127. else if (stub instanceof ObjectImpl)
  128. return ((ObjectImpl)stub)._ids() ;
  129. else
  130. throw wrapper.getTypeIdsRequiresStub() ;
  131. }
  132. public static void connect( Object stub,
  133. ORB orb ) throws java.rmi.RemoteException
  134. {
  135. if (stub instanceof DynamicStub)
  136. ((DynamicStub)stub).connect(
  137. (com.sun.corba.se.spi.orb.ORB)orb ) ;
  138. else if (stub instanceof javax.rmi.CORBA.Stub)
  139. ((javax.rmi.CORBA.Stub)stub).connect( orb ) ;
  140. else if (stub instanceof ObjectImpl)
  141. orb.connect( (org.omg.CORBA.Object)stub ) ;
  142. else
  143. throw wrapper.connectRequiresStub() ;
  144. }
  145. public static boolean isLocal( Object stub )
  146. {
  147. if (stub instanceof DynamicStub)
  148. return ((DynamicStub)stub).isLocal() ;
  149. else if (stub instanceof ObjectImpl)
  150. return ((ObjectImpl)stub)._is_local() ;
  151. else
  152. throw wrapper.isLocalRequiresStub() ;
  153. }
  154. public static OutputStream request( Object stub,
  155. String operation, boolean responseExpected )
  156. {
  157. if (stub instanceof DynamicStub)
  158. return ((DynamicStub)stub).request( operation,
  159. responseExpected ) ;
  160. else if (stub instanceof ObjectImpl)
  161. return ((ObjectImpl)stub)._request( operation,
  162. responseExpected ) ;
  163. else
  164. throw wrapper.requestRequiresStub() ;
  165. }
  166. }