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