1. /*
  2. * @(#)ObjID.java 1.22 00/04/06
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.rmi.server;
  11. import java.io.ObjectInput;
  12. import java.io.ObjectOutput;
  13. import java.security.SecureRandom;
  14. import java.util.Random;
  15. import sun.security.action.GetBooleanAction;
  16. /**
  17. * An <code>ObjID</code> is used to identify remote objects uniquely
  18. * in a VM over time. Each identifier contains an object number and an
  19. * address space identifier that is unique with respect to a specific host.
  20. * An object identifier is assigned to a remote object when it is exported.
  21. *
  22. * If the property <code>java.rmi.server.randomIDs</code> is true, then the
  23. * object number component (64 bits) of an <code>ObjID</code> created with the
  24. * no argument constructor will contain a cryptographically strong random
  25. * number.
  26. *
  27. * @version 1.22, 04/06/00
  28. * @author Ann Wollrath
  29. * @since JDK1.1
  30. */
  31. public final class ObjID implements java.io.Serializable {
  32. /** well-known id for the registry. */
  33. public static final int REGISTRY_ID = 0;
  34. /** well-known id for the activator. */
  35. public static final int ACTIVATOR_ID = 1;
  36. /** well-known id for the distributed garbage collector. */
  37. public static final int DGC_ID = 2;
  38. /**
  39. * @serial object number
  40. * @see #hashCode
  41. */
  42. private long objNum;
  43. /**
  44. * @serial address space identifier (unique to host)
  45. */
  46. private UID space;
  47. private final static UID mySpace;
  48. private final static Random generator;
  49. /* indicate compatibility with JDK 1.1.x version of class */
  50. private static final long serialVersionUID = -6386392263968365220L;
  51. /**
  52. * Generates a unique object identifier. If the property
  53. * <code>java.rmi.server.randomIDs</code> is true, then the object number
  54. * component (64 bits) of an <code>ObjID</code> created with the no
  55. * argument constructor will contain a cryptographically strong random
  56. * number.
  57. * @since JDK1.1
  58. */
  59. public ObjID () {
  60. /*
  61. * Create a new UID if the SecureRandom generator is used (mySpace
  62. * will be null in this case). Using a different UID for each ObjID
  63. * ensures that ObjIDs will be unique in this VM incarnation when
  64. * paired with the result of the secure random number generator.
  65. */
  66. space = (mySpace != null) ? mySpace : new UID();
  67. objNum = generator.nextLong();
  68. }
  69. /**
  70. * Generates a "well-known" object ID. An object ID generated via
  71. * this constructor will not clash with any object IDs generated
  72. * via the default constructor.
  73. * @param num a unique well-known object number
  74. * @since JDK1.1
  75. */
  76. public ObjID (int num) {
  77. space = new UID((short)0);
  78. objNum = num;
  79. }
  80. /**
  81. * Private constructor for creating an object ID given its contents
  82. * that is read from a stream.
  83. * @since JDK1.1
  84. */
  85. private ObjID(long objNum, UID space) {
  86. this.objNum = objNum;
  87. this.space = space;
  88. }
  89. /**
  90. * Marshals object id to output stream.
  91. * @param out output stream to write object ID to
  92. * @throws IOException if an I/O error occurred
  93. * @since JDK1.1
  94. */
  95. public void write(ObjectOutput out) throws java.io.IOException {
  96. out.writeLong(objNum);
  97. space.write(out);
  98. }
  99. /**
  100. * Constructs an object id whose contents is read from the specified input
  101. * stream.
  102. * @param in input stream to read object ID from
  103. * @return object ID instance read from stream
  104. * @throws IOException if an I/O error occurred
  105. * @since JDK1.1
  106. */
  107. public static ObjID read(ObjectInput in) throws java.io.IOException {
  108. long num = in.readLong();
  109. UID space = UID.read(in);
  110. return new ObjID(num, space);
  111. }
  112. /**
  113. * Returns the hash code for the <code>ObjID</code> (the object number).
  114. * @since JDK1.1
  115. */
  116. public int hashCode() {
  117. return (int) objNum;
  118. }
  119. /**
  120. * Two object identifiers are considered equal if they have the
  121. * same contents.
  122. * @since JDK1.1
  123. */
  124. public boolean equals(Object obj) {
  125. if ((obj != null) && (obj instanceof ObjID)) {
  126. ObjID id = (ObjID)obj;
  127. return (objNum == id.objNum && space.equals(id.space));
  128. } else {
  129. return false;
  130. }
  131. }
  132. /**
  133. * Returns a string containing the object id representation. The
  134. * address space identifier is included in the string
  135. * representation only if the object id is from a non-local
  136. * address space.
  137. * @since JDK1.1
  138. */
  139. public String toString() {
  140. return "[" + (space.equals(mySpace) ? "" : space + ", ") +
  141. objNum + "]";
  142. }
  143. private static class InsecureRandom extends Random {
  144. // use serialVersionUID from JDK 1.2.2 for interoperability
  145. private static final long serialVersionUID = -698228687531590145L;
  146. private long nextNum;
  147. synchronized public long nextLong() {
  148. return nextNum++;
  149. }
  150. }
  151. static {
  152. Boolean tmp = (Boolean) java.security.AccessController.doPrivileged(
  153. new GetBooleanAction("java.rmi.server.randomIDs"));
  154. boolean randomIDs = tmp.booleanValue();
  155. if (randomIDs) {
  156. generator = new SecureRandom();
  157. mySpace = null;
  158. } else {
  159. generator = new InsecureRandom();
  160. /*
  161. * The InsecureRandom implementation guarantees that object
  162. * numbers will not repeat, so the same UID value can be used
  163. * for all instances of ObjID.
  164. */
  165. mySpace = new UID();
  166. }
  167. }
  168. }