home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / rmi / server / ObjID.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  3.5 KB  |  131 lines

  1. /*
  2.  * @(#)ObjID.java    1.10 98/03/18
  3.  *
  4.  * Copyright 1996, 1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.rmi.server;
  15.  
  16. import java.io.ObjectInput;
  17. import java.io.ObjectOutput;
  18.  
  19. /**
  20.  * The class ObjID is used to identify remote objects uniquely in a VM
  21.  * over time. Each identifier contains an object number and an address
  22.  * space identifier that is unique with respect to a specific host. An
  23.  * object identifier is assigned to a remote object when it is
  24.  * exported.
  25.  */
  26. public final class ObjID implements java.io.Serializable {    
  27.     /** well-known id for the registry */
  28.     public static final int REGISTRY_ID = 0;
  29.     /** well-known id for the activator */
  30.     public static final int ACTIVATOR_ID = 1;
  31.     /** well-known id for the distributed garbage collector */
  32.     public static final int DGC_ID = 2;
  33.  
  34.     /** object number */
  35.     private long objNum;
  36.     /** address space identifier (unique to host) */
  37.     private UID space;
  38.  
  39.     private static long nextNum = 0;
  40.     private static UID mySpace = new UID();
  41.  
  42.     /* indicate compatibility with JDK 1.1.x version of class */
  43.     private static final long serialVersionUID = -6386392263968365220L;
  44.  
  45.     /**
  46.      * Generate unique object identifier.
  47.      */
  48.     public ObjID () {
  49.     synchronized (mySpace) {
  50.         space = mySpace;
  51.         objNum = nextNum++;
  52.     }
  53.     }
  54.  
  55.     /**
  56.      * Generate a "well-known" object ID.  An object ID generated via
  57.      * this constructor will not clash with any object IDs generated
  58.      * via the default constructor.
  59.      * @param num a unique well-known object number
  60.      */
  61.     public ObjID (int num) {
  62.     space = new UID((short)0);
  63.     objNum = num;
  64.     }
  65.  
  66.     /**
  67.      * Private constructor for creating an object ID given its contents
  68.      * that is read from a stream.
  69.      */
  70.     private ObjID(long objNum, UID space) 
  71.     {
  72.     this.objNum = objNum;
  73.     this.space = space;
  74.     }
  75.     
  76.     /**
  77.      * Marshal object id to output stream.
  78.      */
  79.     public void write(ObjectOutput out) throws java.io.IOException
  80.     {
  81.     out.writeLong(objNum);
  82.     space.write(out);
  83.     }
  84.     
  85.     /**
  86.      * The read method constructs an object id whose contents is read
  87.      * from the specified input stream.
  88.      */
  89.     public static ObjID read(ObjectInput in)
  90.     throws java.io.IOException
  91.     {
  92.     long num = in.readLong();
  93.     UID space = UID.read(in);
  94.     return new ObjID(num, space);
  95.     }
  96.  
  97.     /**
  98.      * The hashcode is the object number.
  99.      */
  100.     public int hashCode() 
  101.     {
  102.     return (int) objNum;
  103.     }
  104.  
  105.     /**
  106.      * Two object identifiers are considered equal if they have the
  107.      * same contents.
  108.      */
  109.     public boolean equals(Object obj) 
  110.     {
  111.     if ((obj != null) && (obj instanceof ObjID)) {
  112.         ObjID id = (ObjID)obj;
  113.         return (objNum == id.objNum && space.equals(id.space));
  114.     } else {
  115.         return false;
  116.     }
  117.     }
  118.  
  119.     /**
  120.      * Returns a string containing the object id representation. The
  121.      * address space identifier is included in the string
  122.      * representation only if the object id is from a non-local
  123.      * address space.
  124.      */
  125.     public String toString()
  126.     {
  127.     return "[" + (space.equals(mySpace) ? "" : space + ", ") + 
  128.         objNum + "]";
  129.     }
  130. }
  131.