1 7 package java.rmi.server; 8 9 import java.io.DataInput ; 10 import java.io.DataOutput ; 11 import java.io.EOFException ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.ObjectInput ; 15 import java.io.ObjectOutput ; 16 import java.io.OutputStream ; 17 import java.security.SecureRandom ; 18 import java.util.Random ; 19 20 53 public final class ObjID implements java.io.Serializable { 54 55 56 public static final int REGISTRY_ID = 0; 57 58 59 public static final int ACTIVATOR_ID = 1; 60 61 65 public static final int DGC_ID = 2; 66 67 68 private static final long serialVersionUID = -6386392263968365220L; 69 70 private static final UID mySpace; 71 private static final Random generator; 72 73 77 private final long objNum; 78 79 82 private final UID space; 83 84 93 public ObjID() { 94 100 space = (mySpace != null) ? mySpace : new UID (); 101 objNum = generator.nextLong(); 102 } 103 104 113 public ObjID(int objNum) { 114 space = new UID ((short) 0); 115 this.objNum = objNum; 116 } 117 118 121 private ObjID(long objNum, UID space) { 122 this.objNum = objNum; 123 this.space = space; 124 } 125 126 142 public void write(ObjectOutput out) throws IOException { 143 out.writeLong(objNum); 144 space.write(out); 145 } 146 147 168 public static ObjID read(ObjectInput in) throws IOException { 169 long num = in.readLong(); 170 UID space = UID.read(in); 171 return new ObjID (num, space); 172 } 173 174 180 public int hashCode() { 181 return (int) objNum; 182 } 183 184 197 public boolean equals(Object obj) { 198 if (obj instanceof ObjID ) { 199 ObjID id = (ObjID )obj; 200 return objNum == id.objNum && space.equals(id.space); 201 } else { 202 return false; 203 } 204 } 205 206 211 216 public String toString() { 217 return "[" + (space.equals(mySpace) ? "" : space + ", ") + 218 objNum + "]"; 219 } 220 221 private static final class InsecureRandom extends Random { 222 223 224 private static final long serialVersionUID = -698228687531590145L; 225 226 private long nextNum; 227 228 public synchronized long nextLong() { 229 return nextNum++; 230 } 231 } 232 233 static { 234 boolean randomIDs = 235 ((Boolean ) java.security.AccessController.doPrivileged( 236 new sun.security.action.GetBooleanAction( 237 "java.rmi.server.randomIDs"))).booleanValue(); 238 239 if (randomIDs) { 240 generator = new SecureRandom (); 241 mySpace = null; 242 } else { 243 generator = new InsecureRandom(); 244 249 mySpace = new UID (); 250 } 251 } 252 } 253 | Popular Tags |