KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > VmId


1 package org.sapia.ubik.rmi.server;
2
3 import java.io.IOException JavaDoc;
4 import java.io.ObjectInput JavaDoc;
5 import java.io.ObjectOutput JavaDoc;
6 import java.net.InetAddress JavaDoc;
7
8
9 /**
10  * A VM unique identifier.
11  *
12  * @author Yanick Duchesne
13  * <dl>
14  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class VmId implements java.io.Externalizable JavaDoc {
20   public static final long serialVersionUID = 1L;
21   private static VmId _instance;
22   private static Object JavaDoc _unique = new Object JavaDoc();
23
24   static {
25     try {
26       _instance = new VmId(InetAddress.getLocalHost().getHostAddress().hashCode() ^
27           VmId.class.hashCode() ^ UIDGenerator.createdUID());
28     } catch (Throwable JavaDoc t) {
29       t.printStackTrace();
30       new IllegalStateException JavaDoc("could not initialize VM identifier");
31     }
32   }
33
34   private long _id;
35   private int _hashCode;
36
37   /** Do not use; meant for externalization only. */
38   public VmId() {
39   }
40
41   VmId(long id) {
42     _id = id;
43     _hashCode = (int) (_id ^ (_id >>> 32)) ^ _unique.hashCode();
44   }
45
46   /**
47    * @return the singleton instance of this VM's VmId.
48    */

49   public static VmId getInstance() {
50     return _instance;
51   }
52
53   public boolean equals(VmId other) {
54     return other._id == _id && _hashCode == other.hashCode();
55   }
56
57   /**
58    * @see java.lang.Object#equals(java.lang.Object)
59    */

60   public boolean equals(Object JavaDoc other) {
61     try {
62       return equals((VmId) other);
63     } catch (ClassCastException JavaDoc e) {
64       return false;
65     }
66   }
67
68   /**
69    * @see java.lang.Object#hashCode()
70    */

71   public int hashCode() {
72     return _hashCode;
73   }
74
75   /**
76    * @see java.io.Externalizable#readExternal(ObjectInput)
77    */

78   public void readExternal(ObjectInput JavaDoc in)
79     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
80     _id = in.readLong();
81     _hashCode = in.readInt();
82   }
83
84   /**
85    * @see java.io.Externalizable#writeExternal(ObjectOutput)
86    */

87   public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
88     out.writeLong(_id);
89     out.writeInt(_hashCode);
90   }
91
92   public String JavaDoc toString() {
93     return "[ id=" + _id + " ]";
94   }
95 }
96
Popular Tags