1. /*
  2. * @(#)StubConnectImpl.java 1.3 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.rmi.RemoteException;
  9. import javax.rmi.CORBA.Tie;
  10. import org.omg.CORBA.ORB;
  11. import org.omg.CORBA.SystemException;
  12. import org.omg.CORBA.BAD_OPERATION;
  13. import org.omg.CORBA.BAD_INV_ORDER;
  14. import org.omg.CORBA.portable.ObjectImpl;
  15. import org.omg.CORBA.portable.Delegate;
  16. import com.sun.corba.se.spi.presentation.rmi.StubAdapter;
  17. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  18. import com.sun.corba.se.impl.util.Utility;
  19. import com.sun.corba.se.impl.ior.StubIORImpl ;
  20. import com.sun.corba.se.impl.logging.UtilSystemException ;
  21. import com.sun.corba.se.impl.corba.CORBAObjectImpl ;
  22. public abstract class StubConnectImpl
  23. {
  24. static UtilSystemException wrapper = UtilSystemException.get(
  25. CORBALogDomains.RMIIIOP ) ;
  26. /** Connect the stub to the orb if necessary.
  27. * @param ior The StubIORImpl for this stub (may be null)
  28. * @param proxy The externally visible stub seen by the user (may be the same as stub)
  29. * @param stub The stub implementation that extends ObjectImpl
  30. * @param orb The ORB to which we connect the stub.
  31. */
  32. public static StubIORImpl connect( StubIORImpl ior, org.omg.CORBA.Object proxy,
  33. org.omg.CORBA.portable.ObjectImpl stub, ORB orb ) throws RemoteException
  34. {
  35. Delegate del = null ;
  36. try {
  37. try {
  38. del = StubAdapter.getDelegate( stub );
  39. if (del.orb(stub) != orb)
  40. throw wrapper.connectWrongOrb() ;
  41. } catch (org.omg.CORBA.BAD_OPERATION err) {
  42. if (ior == null) {
  43. // No IOR, can we get a Tie for this stub?
  44. Tie tie = (javax.rmi.CORBA.Tie) Utility.getAndForgetTie(proxy);
  45. if (tie == null)
  46. throw wrapper.connectNoTie() ;
  47. // Is the tie already connected? If it is, check that it's
  48. // connected to the same ORB, otherwise connect it.
  49. ORB existingOrb = orb ;
  50. try {
  51. existingOrb = tie.orb();
  52. } catch (BAD_OPERATION exc) {
  53. // Thrown when tie is an ObjectImpl and its delegate is not set.
  54. tie.orb(orb);
  55. } catch (BAD_INV_ORDER exc) {
  56. // Thrown when tie is a Servant and its delegate is not set.
  57. tie.orb(orb);
  58. }
  59. if (existingOrb != orb)
  60. throw wrapper.connectTieWrongOrb() ;
  61. // Get the delegate for the stub from the tie.
  62. del = StubAdapter.getDelegate( tie ) ;
  63. ObjectImpl objref = new CORBAObjectImpl() ;
  64. objref._set_delegate( del ) ;
  65. ior = new StubIORImpl( objref ) ;
  66. } else {
  67. // ior is initialized, so convert ior to an object, extract
  68. // the delegate, and set it on ourself
  69. del = ior.getDelegate( orb ) ;
  70. }
  71. StubAdapter.setDelegate( stub, del ) ;
  72. }
  73. } catch (SystemException exc) {
  74. throw new RemoteException("CORBA SystemException", exc );
  75. }
  76. return ior ;
  77. }
  78. }