1. /*
  2. * @(#)ReflectiveTie.java 1.12 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.presentation.rmi ;
  8. import java.rmi.Remote;
  9. import java.rmi.RemoteException;
  10. import javax.rmi.CORBA.Tie;
  11. import java.lang.reflect.Method ;
  12. import java.lang.reflect.InvocationTargetException ;
  13. import org.omg.CORBA.SystemException;
  14. import org.omg.CORBA_2_3.portable.InputStream;
  15. import org.omg.CORBA_2_3.portable.OutputStream;
  16. import org.omg.CORBA.portable.ResponseHandler;
  17. import org.omg.CORBA.portable.UnknownException;
  18. import org.omg.PortableServer.Servant;
  19. import org.omg.PortableServer.POA;
  20. import org.omg.PortableServer.POAManager;
  21. import com.sun.corba.se.spi.presentation.rmi.PresentationManager ;
  22. import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator ;
  23. import com.sun.corba.se.spi.presentation.rmi.DynamicMethodMarshaller ;
  24. import com.sun.corba.se.spi.orb.ORB ;
  25. import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  26. import com.sun.corba.se.impl.oa.poa.POAManagerImpl ;
  27. public class ReflectiveTie extends Servant implements Tie
  28. {
  29. private Remote target = null ;
  30. private PresentationManager pm ;
  31. private PresentationManager.ClassData classData = null ;
  32. private ORBUtilSystemException wrapper = null ;
  33. public ReflectiveTie( PresentationManager pm, ORBUtilSystemException wrapper )
  34. {
  35. this.pm = pm ;
  36. this.wrapper = wrapper ;
  37. }
  38. public String[] _all_interfaces(org.omg.PortableServer.POA poa,
  39. byte[] objectId)
  40. {
  41. return classData.getTypeIds() ;
  42. }
  43. public void setTarget(Remote target)
  44. {
  45. this.target = target;
  46. if (target == null) {
  47. classData = null ;
  48. } else {
  49. Class targetClass = target.getClass() ;
  50. classData = pm.getClassData( targetClass ) ;
  51. }
  52. }
  53. public Remote getTarget()
  54. {
  55. return target;
  56. }
  57. public org.omg.CORBA.Object thisObject()
  58. {
  59. return _this_object();
  60. }
  61. public void deactivate()
  62. {
  63. try{
  64. _poa().deactivate_object(_poa().servant_to_id(this));
  65. } catch (org.omg.PortableServer.POAPackage.WrongPolicy exception){
  66. // ignore
  67. } catch (org.omg.PortableServer.POAPackage.ObjectNotActive exception){
  68. // ignore
  69. } catch (org.omg.PortableServer.POAPackage.ServantNotActive exception){
  70. // ignore
  71. }
  72. }
  73. public org.omg.CORBA.ORB orb() {
  74. return _orb();
  75. }
  76. public void orb(org.omg.CORBA.ORB orb) {
  77. try {
  78. ORB myORB = (ORB)orb ;
  79. ((org.omg.CORBA_2_3.ORB)orb).set_delegate(this);
  80. } catch (ClassCastException e) {
  81. throw wrapper.badOrbForServant( e ) ;
  82. }
  83. }
  84. public org.omg.CORBA.portable.OutputStream _invoke(String method,
  85. org.omg.CORBA.portable.InputStream _in, ResponseHandler reply)
  86. {
  87. Method javaMethod = null ;
  88. DynamicMethodMarshaller dmm = null;
  89. try {
  90. InputStream in = (InputStream) _in;
  91. javaMethod = classData.getIDLNameTranslator().getMethod( method ) ;
  92. if (javaMethod == null)
  93. throw wrapper.methodNotFoundInTie( method,
  94. target.getClass().getName() ) ;
  95. dmm = pm.getDynamicMethodMarshaller( javaMethod ) ;
  96. Object[] args = dmm.readArguments( in ) ;
  97. Object result = javaMethod.invoke( target, args ) ;
  98. OutputStream os = (OutputStream)reply.createReply() ;
  99. dmm.writeResult( os, result ) ;
  100. return os ;
  101. } catch (IllegalAccessException ex) {
  102. throw wrapper.invocationErrorInReflectiveTie( ex,
  103. javaMethod.getName(),
  104. javaMethod.getDeclaringClass().getName() ) ;
  105. } catch (IllegalArgumentException ex) {
  106. throw wrapper.invocationErrorInReflectiveTie( ex,
  107. javaMethod.getName(),
  108. javaMethod.getDeclaringClass().getName() ) ;
  109. } catch (InvocationTargetException ex) {
  110. // Unwrap the actual exception so that it can be wrapped by an
  111. // UnknownException or thrown if it is a system exception.
  112. // This is expected in the server dispatcher code.
  113. Throwable thr = ex.getCause() ;
  114. if (thr instanceof SystemException)
  115. throw (SystemException)thr ;
  116. else if ((thr instanceof Exception) &&
  117. dmm.isDeclaredException( thr )) {
  118. OutputStream os = (OutputStream)reply.createExceptionReply() ;
  119. dmm.writeException( os, (Exception)thr ) ;
  120. return os ;
  121. } else
  122. throw new UnknownException( thr ) ;
  123. }
  124. }
  125. }