1. /*
  2. * @(#)StubFactoryImpl.java 1.9 03/12/03
  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.presentation.rmi ;
  8. import java.security.AccessController;
  9. import java.security.PrivilegedAction;
  10. import java.lang.reflect.Method ;
  11. import java.lang.reflect.InvocationHandler ;
  12. import java.lang.reflect.Proxy ;
  13. import java.lang.reflect.InvocationTargetException ;
  14. import java.io.ObjectInputStream ;
  15. import java.io.ObjectOutputStream ;
  16. import java.io.IOException ;
  17. import java.rmi.Remote ;
  18. import javax.rmi.CORBA.Util ;
  19. import org.omg.CORBA.portable.ObjectImpl ;
  20. import org.omg.CORBA.portable.Delegate ;
  21. import org.omg.CORBA.portable.ServantObject ;
  22. import org.omg.CORBA.portable.ApplicationException ;
  23. import org.omg.CORBA.portable.RemarshalException ;
  24. import org.omg.CORBA.SystemException ;
  25. import com.sun.corba.se.spi.orb.ORB ;
  26. import com.sun.corba.se.pept.transport.ContactInfoList ;
  27. import com.sun.corba.se.spi.transport.CorbaContactInfoList ;
  28. import com.sun.corba.se.spi.protocol.CorbaClientDelegate ;
  29. import com.sun.corba.se.spi.protocol.LocalClientRequestDispatcher ;
  30. import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator ;
  31. import com.sun.corba.se.spi.presentation.rmi.DynamicMethodMarshaller ;
  32. import com.sun.corba.se.spi.presentation.rmi.PresentationManager ;
  33. import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;
  34. import com.sun.corba.se.spi.orbutil.proxy.InvocationHandlerFactory ;
  35. import com.sun.corba.se.spi.orbutil.proxy.LinkedInvocationHandler ;
  36. import com.sun.corba.se.impl.corba.CORBAObjectImpl ;
  37. public class StubInvocationHandlerImpl implements LinkedInvocationHandler
  38. {
  39. private transient PresentationManager.ClassData classData ;
  40. private transient PresentationManager pm ;
  41. private transient org.omg.CORBA.Object stub ;
  42. private transient Proxy self ;
  43. public void setProxy( Proxy self )
  44. {
  45. this.self = self ;
  46. }
  47. public Proxy getProxy()
  48. {
  49. return self ;
  50. }
  51. public StubInvocationHandlerImpl( PresentationManager pm,
  52. PresentationManager.ClassData classData, org.omg.CORBA.Object stub )
  53. {
  54. this.classData = classData ;
  55. this.pm = pm ;
  56. this.stub = stub ;
  57. }
  58. private boolean isLocal()
  59. {
  60. boolean result = false ;
  61. Delegate delegate = StubAdapter.getDelegate( stub ) ;
  62. if (delegate instanceof CorbaClientDelegate) {
  63. CorbaClientDelegate cdel = (CorbaClientDelegate)delegate ;
  64. ContactInfoList cil = cdel.getContactInfoList() ;
  65. if (cil instanceof CorbaContactInfoList) {
  66. CorbaContactInfoList ccil = (CorbaContactInfoList)cil ;
  67. LocalClientRequestDispatcher lcrd =
  68. ccil.getLocalClientRequestDispatcher() ;
  69. result = lcrd.useLocalInvocation( null ) ;
  70. }
  71. }
  72. return result ;
  73. }
  74. /** Invoke the given method with the args and return the result.
  75. * This may result in a remote invocation.
  76. * @param proxy The proxy used for this class (null if not using java.lang.reflect.Proxy)
  77. */
  78. public Object invoke( Object proxy, final Method method,
  79. Object[] args ) throws Throwable
  80. {
  81. String giopMethodName = classData.getIDLNameTranslator().
  82. getIDLName( method ) ;
  83. DynamicMethodMarshaller dmm =
  84. pm.getDynamicMethodMarshaller( method ) ;
  85. Delegate delegate = null ;
  86. try {
  87. delegate = StubAdapter.getDelegate( stub ) ;
  88. } catch (SystemException ex) {
  89. throw Util.mapSystemException(ex) ;
  90. }
  91. if (!isLocal()) {
  92. try {
  93. org.omg.CORBA_2_3.portable.InputStream in = null ;
  94. try {
  95. // create request
  96. org.omg.CORBA_2_3.portable.OutputStream out =
  97. (org.omg.CORBA_2_3.portable.OutputStream)
  98. delegate.request( stub, giopMethodName, true);
  99. // marshal arguments
  100. dmm.writeArguments( out, args ) ;
  101. // finish invocation
  102. in = (org.omg.CORBA_2_3.portable.InputStream)
  103. delegate.invoke( stub, out);
  104. // unmarshal result
  105. return dmm.readResult( in ) ;
  106. } catch (ApplicationException ex) {
  107. throw dmm.readException( ex ) ;
  108. } catch (RemarshalException ex) {
  109. return invoke( proxy, method, args ) ;
  110. } finally {
  111. delegate.releaseReply( stub, in );
  112. }
  113. } catch (SystemException ex) {
  114. throw Util.mapSystemException(ex) ;
  115. }
  116. } else {
  117. // local branch
  118. ORB orb = (ORB)delegate.orb( stub ) ;
  119. ServantObject so = delegate.servant_preinvoke( stub, giopMethodName,
  120. method.getDeclaringClass() );
  121. if (so == null) {
  122. return invoke( stub, method, args ) ;
  123. }
  124. try {
  125. Object[] copies = dmm.copyArguments( args, orb ) ;
  126. if (!method.isAccessible()) {
  127. // Make sure that we can invoke a method from a normally
  128. // inaccessible package, as this reflective class must always
  129. // be able to invoke a non-public method.
  130. AccessController.doPrivileged(new PrivilegedAction() {
  131. public Object run() {
  132. method.setAccessible( true ) ;
  133. return null ;
  134. }
  135. } ) ;
  136. }
  137. Object result = method.invoke( so.servant, copies ) ;
  138. return dmm.copyResult( result, orb ) ;
  139. } catch (InvocationTargetException ex) {
  140. Throwable mex = ex.getCause() ;
  141. // mex should never be null, as null cannot be thrown
  142. Throwable exCopy = (Throwable)Util.copyObject(mex,orb);
  143. if (dmm.isDeclaredException( exCopy ))
  144. throw exCopy ;
  145. else
  146. throw Util.wrapException(exCopy);
  147. } catch (Throwable thr) {
  148. if (thr instanceof ThreadDeath)
  149. throw (ThreadDeath)thr ;
  150. // This is not a user thrown exception from the
  151. // method call, so don't copy it. This is either
  152. // an error or a reflective invoke exception.
  153. throw Util.wrapException( thr ) ;
  154. } finally {
  155. delegate.servant_postinvoke( stub, so);
  156. }
  157. }
  158. }
  159. }