1. /*
  2. * @(#)JNDIStateFactoryImpl.java 1.6 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.Field ;
  9. import java.util.Hashtable;
  10. import javax.naming.*;
  11. import javax.naming.spi.StateFactory;
  12. import java.security.AccessController ;
  13. import java.security.PrivilegedAction ;
  14. import javax.rmi.PortableRemoteObject ;
  15. import com.sun.corba.se.spi.orb.ORB;
  16. import java.rmi.Remote;
  17. import java.rmi.server.ExportException;
  18. // XXX This creates a dependendcy on the implementation
  19. // of the CosNaming service provider.
  20. import com.sun.jndi.cosnaming.CNCtx ;
  21. import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;
  22. /**
  23. * StateFactory that turns java.rmi.Remote objects to org.omg.CORBA.Object.
  24. * This version works either with standard RMI-IIOP or Dynamic RMI-IIOP.
  25. * Based on the original com.sun.jndi.cosnaming.RemoteToCorba and
  26. * com.sun.jndi.toolkit.corba.CorbaUtils.
  27. *
  28. * @author Ken Cavanaugh
  29. */
  30. public class JNDIStateFactoryImpl implements StateFactory
  31. {
  32. private static final Field orbField ;
  33. static {
  34. orbField = (Field) AccessController.doPrivileged(
  35. new PrivilegedAction() {
  36. public Object run() {
  37. Field fld = null ;
  38. try {
  39. Class cls = CNCtx.class ;
  40. fld = cls.getDeclaredField( "_orb" ) ;
  41. fld.setAccessible( true ) ;
  42. } catch (Exception exc) {
  43. // XXX log exception at FINE
  44. }
  45. return fld ;
  46. }
  47. }
  48. ) ;
  49. }
  50. public JNDIStateFactoryImpl()
  51. {
  52. }
  53. /**
  54. * Returns the CORBA object for a Remote object.
  55. * If input is not a Remote object, or if Remote object uses JRMP, return null.
  56. * If the RMI-IIOP library is not available, throw ConfigurationException.
  57. *
  58. * @param orig The object to turn into a CORBA object. If not Remote,
  59. * or if is a JRMP stub or impl, return null.
  60. * @param name Ignored
  61. * @param ctx The non-null CNCtx whose ORB to use.
  62. * @param env Ignored
  63. * @return The CORBA object for <tt>orig</tt> or null.
  64. * @exception ConfigurationException If the CORBA object cannot be obtained
  65. * due to configuration problems
  66. * @exception NamingException If some other problem prevented a CORBA
  67. * object from being obtained from the Remote object.
  68. */
  69. public Object getStateToBind(Object orig, Name name, Context ctx,
  70. Hashtable<?,?> env) throws NamingException
  71. {
  72. if (orig instanceof org.omg.CORBA.Object)
  73. return orig ;
  74. if (!(orig instanceof Remote))
  75. // Not for this StateFactory
  76. return null ;
  77. ORB orb = getORB( ctx ) ;
  78. if (orb == null)
  79. // Wrong kind of context, so just give up and let another StateFactory
  80. // try to satisfy getStateToBind.
  81. return null ;
  82. Remote stub = null;
  83. try {
  84. stub = PortableRemoteObject.toStub( (Remote)orig ) ;
  85. } catch (Exception exc) {
  86. // XXX log at FINE level?
  87. // Wrong sort of object: just return null to allow another StateFactory
  88. // to handle this. This can happen easily because this StateFactory
  89. // is specified for the application, not the service context provider.
  90. return null ;
  91. }
  92. if (StubAdapter.isStub( stub )) {
  93. try {
  94. StubAdapter.connect( stub, orb ) ;
  95. } catch (Exception exc) {
  96. if (!(exc instanceof java.rmi.RemoteException)) {
  97. // XXX log at FINE level?
  98. // Wrong sort of object: just return null to allow another StateFactory
  99. // to handle this call.
  100. return null ;
  101. }
  102. // ignore RemoteException because stub might have already
  103. // been connected
  104. }
  105. }
  106. return stub ;
  107. }
  108. // This is necessary because the _orb field is package private in
  109. // com.sun.jndi.cosnaming.CNCtx. This is not an ideal solution.
  110. // The best solution for our ORB is to change the CosNaming provider
  111. // to use the StubAdapter. But this has problems as well, because
  112. // other vendors may use the CosNaming provider with a different ORB
  113. // entirely.
  114. private ORB getORB( Context ctx )
  115. {
  116. ORB orb = null ;
  117. try {
  118. orb = (ORB)orbField.get( ctx ) ;
  119. } catch (Exception exc) {
  120. // XXX log this exception at FINE level
  121. // ignore the exception and return null.
  122. // Note that the exception may be because ctx
  123. // is not a CosNaming context.
  124. }
  125. return orb ;
  126. }
  127. }