1. /*
  2. * @(#)InvocationHandlerFactoryImpl.java 1.8 04/07/27
  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.lang.reflect.InvocationHandler ;
  9. import java.lang.reflect.Proxy ;
  10. import java.lang.reflect.Method ;
  11. import org.omg.CORBA.portable.ObjectImpl ;
  12. import java.io.ObjectStreamException ;
  13. import java.io.Serializable ;
  14. import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator ;
  15. import com.sun.corba.se.spi.presentation.rmi.PresentationManager ;
  16. import com.sun.corba.se.spi.presentation.rmi.DynamicStub ;
  17. import com.sun.corba.se.spi.orbutil.proxy.LinkedInvocationHandler ;
  18. import com.sun.corba.se.spi.orbutil.proxy.InvocationHandlerFactory ;
  19. import com.sun.corba.se.spi.orbutil.proxy.DelegateInvocationHandlerImpl ;
  20. import com.sun.corba.se.spi.orbutil.proxy.CompositeInvocationHandler ;
  21. import com.sun.corba.se.spi.orbutil.proxy.CompositeInvocationHandlerImpl ;
  22. public class InvocationHandlerFactoryImpl implements InvocationHandlerFactory
  23. {
  24. private final PresentationManager.ClassData classData ;
  25. private final PresentationManager pm ;
  26. private Class[] proxyInterfaces ;
  27. public InvocationHandlerFactoryImpl( PresentationManager pm,
  28. PresentationManager.ClassData classData )
  29. {
  30. this.classData = classData ;
  31. this.pm = pm ;
  32. Class[] remoteInterfaces =
  33. classData.getIDLNameTranslator().getInterfaces() ;
  34. proxyInterfaces = new Class[ remoteInterfaces.length + 1 ] ;
  35. for (int ctr=0; ctr<remoteInterfaces.length; ctr++)
  36. proxyInterfaces[ctr] = remoteInterfaces[ctr] ;
  37. proxyInterfaces[remoteInterfaces.length] = DynamicStub.class ;
  38. }
  39. private class CustomCompositeInvocationHandlerImpl extends
  40. CompositeInvocationHandlerImpl implements LinkedInvocationHandler,
  41. Serializable
  42. {
  43. private transient DynamicStub stub ;
  44. public void setProxy( Proxy proxy )
  45. {
  46. ((DynamicStubImpl)stub).setSelf( (DynamicStub)proxy ) ;
  47. }
  48. public Proxy getProxy()
  49. {
  50. return (Proxy)((DynamicStubImpl)stub).getSelf() ;
  51. }
  52. public CustomCompositeInvocationHandlerImpl( DynamicStub stub )
  53. {
  54. this.stub = stub ;
  55. }
  56. /** Return the stub, which will actually be written to the stream.
  57. * It will be custom marshalled, with the actual writing done in
  58. * StubIORImpl. There is a corresponding readResolve method on
  59. * DynamicStubImpl which will re-create the full invocation
  60. * handler on read, and return the invocation handler on the
  61. * readResolve method.
  62. */
  63. public Object writeReplace() throws ObjectStreamException
  64. {
  65. return stub ;
  66. }
  67. }
  68. public InvocationHandler getInvocationHandler()
  69. {
  70. final DynamicStub stub = new DynamicStubImpl(
  71. classData.getTypeIds() ) ;
  72. return getInvocationHandler( stub ) ;
  73. }
  74. // This is also used in DynamicStubImpl to implement readResolve.
  75. InvocationHandler getInvocationHandler( DynamicStub stub )
  76. {
  77. // Create an invocation handler for the methods defined on DynamicStub,
  78. // which extends org.omg.CORBA.Object. This handler delegates all
  79. // calls directly to a DynamicStubImpl, which extends
  80. // org.omg.CORBA.portable.ObjectImpl.
  81. InvocationHandler dynamicStubHandler =
  82. DelegateInvocationHandlerImpl.create( stub ) ;
  83. // Create an invocation handler that handles any remote interface
  84. // methods.
  85. InvocationHandler stubMethodHandler = new StubInvocationHandlerImpl(
  86. pm, classData, stub ) ;
  87. // Create a composite handler that handles the DynamicStub interface
  88. // as well as the remote interfaces.
  89. final CompositeInvocationHandler handler =
  90. new CustomCompositeInvocationHandlerImpl( stub ) ;
  91. handler.addInvocationHandler( DynamicStub.class,
  92. dynamicStubHandler ) ;
  93. handler.addInvocationHandler( org.omg.CORBA.Object.class,
  94. dynamicStubHandler ) ;
  95. handler.addInvocationHandler( Object.class,
  96. dynamicStubHandler ) ;
  97. // If the method passed to invoke is not from DynamicStub or its superclasses,
  98. // it must be from an implemented interface, so we just handle
  99. // all of these with the stubMethodHandler. This used to be
  100. // done be adding explicit entries for stubMethodHandler for
  101. // each remote interface, but that does not work correctly
  102. // for abstract interfaces, since the graph analysis ignores
  103. // abstract interfaces in order to compute the type ids
  104. // correctly (see PresentationManagerImpl.NodeImpl.getChildren).
  105. // Rather than produce more graph traversal code to handle this
  106. // problem, we simply use a default.
  107. // This also points to a possible optimization: just use explict
  108. // checks for the three special classes, rather than a general
  109. // table lookup that usually fails.
  110. handler.setDefaultHandler( stubMethodHandler ) ;
  111. return handler ;
  112. }
  113. public Class[] getProxyInterfaces()
  114. {
  115. return proxyInterfaces ;
  116. }
  117. }