1. /*
  2. * @(#)StubDelegateImpl.java 1.17 04/06/26
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * Licensed Materials - Property of IBM
  9. * RMI-IIOP v1.0
  10. * Copyright IBM Corp. 1998 1999 All Rights Reserved
  11. *
  12. * US Government Users Restricted Rights - Use, duplication or
  13. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  14. */
  15. package com.sun.corba.se.impl.javax.rmi.CORBA;
  16. import java.io.IOException;
  17. import java.rmi.RemoteException;
  18. import javax.rmi.CORBA.Tie;
  19. import org.omg.CORBA.ORB;
  20. import org.omg.CORBA.SystemException;
  21. import org.omg.CORBA.BAD_OPERATION;
  22. import org.omg.CORBA.BAD_INV_ORDER;
  23. import org.omg.CORBA.portable.Delegate;
  24. import org.omg.CORBA.portable.OutputStream;
  25. import org.omg.CORBA.portable.InputStream;
  26. import com.sun.corba.se.spi.presentation.rmi.StubAdapter;
  27. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  28. import com.sun.corba.se.impl.util.Utility;
  29. import com.sun.corba.se.impl.ior.StubIORImpl ;
  30. import com.sun.corba.se.impl.presentation.rmi.StubConnectImpl ;
  31. import com.sun.corba.se.impl.logging.UtilSystemException ;
  32. /**
  33. * Base class from which all static RMI-IIOP stubs must inherit.
  34. */
  35. public class StubDelegateImpl implements javax.rmi.CORBA.StubDelegate
  36. {
  37. static UtilSystemException wrapper = UtilSystemException.get(
  38. CORBALogDomains.RMIIIOP ) ;
  39. private StubIORImpl ior ;
  40. public StubIORImpl getIOR()
  41. {
  42. return ior ;
  43. }
  44. public StubDelegateImpl()
  45. {
  46. ior = null ;
  47. }
  48. /**
  49. * Sets the IOR components if not already set.
  50. */
  51. private void init (javax.rmi.CORBA.Stub self)
  52. {
  53. // If the Stub is not connected to an ORB, BAD_OPERATION exception
  54. // will be raised by the code below.
  55. if (ior == null)
  56. ior = new StubIORImpl( self ) ;
  57. }
  58. /**
  59. * Returns a hash code value for the object which is the same for all stubs
  60. * that represent the same remote object.
  61. * @return the hash code value.
  62. */
  63. public int hashCode(javax.rmi.CORBA.Stub self)
  64. {
  65. init(self);
  66. return ior.hashCode() ;
  67. }
  68. /**
  69. * Compares two stubs for equality. Returns <code>true</code> when used to compare stubs
  70. * that represent the same remote object, and <code>false</code> otherwise.
  71. * @param obj the reference object with which to compare.
  72. * @return <code>true</code> if this object is the same as the <code>obj</code>
  73. * argument; <code>false</code> otherwise.
  74. */
  75. public boolean equals(javax.rmi.CORBA.Stub self, java.lang.Object obj)
  76. {
  77. if (self == obj) {
  78. return true;
  79. }
  80. if (!(obj instanceof javax.rmi.CORBA.Stub)) {
  81. return false;
  82. }
  83. // no need to call init() because of calls to hashCode() below
  84. javax.rmi.CORBA.Stub other = (javax.rmi.CORBA.Stub) obj;
  85. if (other.hashCode() != self.hashCode()) {
  86. return false;
  87. }
  88. // hashCodes being the same does not mean equality. The stubs still
  89. // could be pointing to different IORs. So, do a literal comparison.
  90. // Apparently the ONLY way to do this (other than using private
  91. // reflection) toString, because it is not possible to directly
  92. // access the StubDelegateImpl from the Stub.
  93. return self.toString().equals( other.toString() ) ;
  94. }
  95. public boolean equals( Object obj )
  96. {
  97. if (this == obj)
  98. return true ;
  99. if (!(obj instanceof StubDelegateImpl))
  100. return false ;
  101. StubDelegateImpl other = (StubDelegateImpl)obj ;
  102. if (ior == null)
  103. return ior == other.ior ;
  104. else
  105. return ior.equals( other.ior ) ;
  106. }
  107. /**
  108. * Returns a string representation of this stub. Returns the same string
  109. * for all stubs that represent the same remote object.
  110. * @return a string representation of this stub.
  111. */
  112. public String toString(javax.rmi.CORBA.Stub self)
  113. {
  114. if (ior == null)
  115. return null ;
  116. else
  117. return ior.toString() ;
  118. }
  119. /**
  120. * Connects this stub to an ORB. Required after the stub is deserialized
  121. * but not after it is demarshalled by an ORB stream. If an unconnected
  122. * stub is passed to an ORB stream for marshalling, it is implicitly
  123. * connected to that ORB. Application code should not call this method
  124. * directly, but should call the portable wrapper method
  125. * {@link javax.rmi.PortableRemoteObject#connect}.
  126. * @param orb the ORB to connect to.
  127. * @exception RemoteException if the stub is already connected to a different
  128. * ORB, or if the stub does not represent an exported remote or local object.
  129. */
  130. public void connect(javax.rmi.CORBA.Stub self, ORB orb)
  131. throws RemoteException
  132. {
  133. ior = StubConnectImpl.connect( ior, self, self, orb ) ;
  134. }
  135. /**
  136. * Serialization method to restore the IOR state.
  137. */
  138. public void readObject(javax.rmi.CORBA.Stub self,
  139. java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException
  140. {
  141. if (ior == null)
  142. ior = new StubIORImpl() ;
  143. ior.doRead( stream ) ;
  144. }
  145. /**
  146. * Serialization method to save the IOR state.
  147. * @serialData The length of the IOR type ID (int), followed by the IOR type ID
  148. * (byte array encoded using ISO8859-1), followed by the number of IOR profiles
  149. * (int), followed by the IOR profiles. Each IOR profile is written as a
  150. * profile tag (int), followed by the length of the profile data (int), followed
  151. * by the profile data (byte array).
  152. */
  153. public void writeObject(javax.rmi.CORBA.Stub self,
  154. java.io.ObjectOutputStream stream) throws IOException
  155. {
  156. init(self);
  157. ior.doWrite( stream ) ;
  158. }
  159. }