1. /*
  2. * @(#)Stub.java 1.29 03/01/23
  3. *
  4. * Copyright 2003 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 javax.rmi.CORBA;
  16. import org.omg.CORBA.ORB;
  17. import org.omg.CORBA_2_3.portable.ObjectImpl;
  18. import java.io.IOException;
  19. import java.rmi.RemoteException;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.security.AccessController;
  23. import java.security.PrivilegedAction;
  24. import sun.security.action.GetPropertyAction;
  25. import java.util.Properties;
  26. /**
  27. * Base class from which all RMI-IIOP stubs must inherit.
  28. */
  29. public abstract class Stub extends ObjectImpl
  30. implements java.io.Serializable {
  31. private static final long serialVersionUID = 1087775603798577179L;
  32. // This can only be set at object construction time (no sync necessary).
  33. private transient StubDelegate stubDelegate = null;
  34. private static Class stubDelegateClass = null;
  35. private static final String StubClassKey = "javax.rmi.CORBA.StubClass";
  36. private static final String defaultStubImplName = "com.sun.corba.se.internal.javax.rmi.CORBA.StubDelegateImpl";
  37. static {
  38. Object stubDelegateInstance = (Object) createDelegateIfSpecified(StubClassKey, defaultStubImplName);
  39. if (stubDelegateInstance != null)
  40. stubDelegateClass = stubDelegateInstance.getClass();
  41. }
  42. /**
  43. * Returns a hash code value for the object which is the same for all stubs
  44. * that represent the same remote object.
  45. * @return the hash code value.
  46. */
  47. public int hashCode() {
  48. if (stubDelegate == null) {
  49. setDefaultDelegate();
  50. }
  51. if (stubDelegate != null) {
  52. return stubDelegate.hashCode(this);
  53. }
  54. return 0;
  55. }
  56. /**
  57. * Compares two stubs for equality. Returns <code>true</code> when used to compare stubs
  58. * that represent the same remote object, and <code>false</code> otherwise.
  59. * @param obj the reference object with which to compare.
  60. * @return <code>true</code> if this object is the same as the <code>obj</code>
  61. * argument; <code>false</code> otherwise.
  62. */
  63. public boolean equals(java.lang.Object obj) {
  64. if (stubDelegate == null) {
  65. setDefaultDelegate();
  66. }
  67. if (stubDelegate != null) {
  68. return stubDelegate.equals(this, obj);
  69. }
  70. return false;
  71. }
  72. /**
  73. * Returns a string representation of this stub. Returns the same string
  74. * for all stubs that represent the same remote object.
  75. * @return a string representation of this stub.
  76. */
  77. public String toString() {
  78. if (stubDelegate == null) {
  79. setDefaultDelegate();
  80. }
  81. String ior;
  82. if (stubDelegate != null) {
  83. ior = stubDelegate.toString(this);
  84. if (ior == null) {
  85. return super.toString();
  86. } else {
  87. return ior;
  88. }
  89. }
  90. return super.toString();
  91. }
  92. /**
  93. * Connects this stub to an ORB. Required after the stub is deserialized
  94. * but not after it is demarshalled by an ORB stream. If an unconnected
  95. * stub is passed to an ORB stream for marshalling, it is implicitly
  96. * connected to that ORB. Application code should not call this method
  97. * directly, but should call the portable wrapper method
  98. * {@link javax.rmi.PortableRemoteObject#connect}.
  99. * @param orb the ORB to connect to.
  100. * @exception RemoteException if the stub is already connected to a different
  101. * ORB, or if the stub does not represent an exported remote or local object.
  102. */
  103. public void connect(ORB orb) throws RemoteException {
  104. if (stubDelegate == null) {
  105. setDefaultDelegate();
  106. }
  107. if (stubDelegate != null) {
  108. stubDelegate.connect(this, orb);
  109. }
  110. }
  111. /**
  112. * Serialization method to restore the IOR state.
  113. */
  114. private void readObject(java.io.ObjectInputStream stream)
  115. throws IOException, ClassNotFoundException {
  116. if (stubDelegate == null) {
  117. setDefaultDelegate();
  118. }
  119. if (stubDelegate != null) {
  120. stubDelegate.readObject(this, stream);
  121. }
  122. }
  123. /**
  124. * Serialization method to save the IOR state.
  125. * @serialData The length of the IOR type ID (int), followed by the IOR type ID
  126. * (byte array encoded using ISO8859-1), followed by the number of IOR profiles
  127. * (int), followed by the IOR profiles. Each IOR profile is written as a
  128. * profile tag (int), followed by the length of the profile data (int), followed
  129. * by the profile data (byte array).
  130. */
  131. private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
  132. if (stubDelegate == null) {
  133. setDefaultDelegate();
  134. }
  135. if (stubDelegate != null) {
  136. stubDelegate.writeObject(this, stream);
  137. }
  138. }
  139. private void setDefaultDelegate() {
  140. if (stubDelegateClass != null) {
  141. try {
  142. stubDelegate = (javax.rmi.CORBA.StubDelegate) stubDelegateClass.newInstance();
  143. } catch (Exception ex) {
  144. // what kind of exception to throw
  145. // delegate not set therefore it is null and will return default
  146. // values
  147. }
  148. }
  149. }
  150. // Same code as in PortableRemoteObject. Can not be shared because they
  151. // are in different packages and the visibility needs to be package for
  152. // security reasons. If you know a better solution how to share this code
  153. // then remove it from PortableRemoteObject. Also in Util.java
  154. private static Object createDelegateIfSpecified(String classKey, String defaultClassName) {
  155. String className = (String)
  156. AccessController.doPrivileged(new GetPropertyAction(classKey));
  157. if (className == null) {
  158. Properties props = getORBPropertiesFile();
  159. if (props != null) {
  160. className = props.getProperty(classKey);
  161. }
  162. }
  163. if (className == null) {
  164. className = defaultClassName;
  165. }
  166. try {
  167. return Util.loadClass(className, null, null).newInstance();
  168. } catch (ClassNotFoundException ex) {
  169. throw new org.omg.CORBA.INITIALIZE(
  170. "cannot instantiate " + className);
  171. } catch (Exception ex) {
  172. throw new org.omg.CORBA.INITIALIZE(
  173. "cannot instantiate " + className);
  174. }
  175. }
  176. /**
  177. * Load the orb.properties file.
  178. */
  179. private static Properties getORBPropertiesFile () {
  180. return (Properties) AccessController.doPrivileged(new GetORBPropertiesFileAction());
  181. }
  182. }