KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > rmi > dgc > VMID


1 /*
2  * @(#)VMID.java 1.20 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.rmi.dgc;
9
10 import java.io.*;
11 import java.net.*;
12 import java.rmi.server.UID JavaDoc;
13 import java.security.*;
14
15 /**
16  * A VMID is a identifier that is unique across all Java virtual
17  * machines. VMIDs are used by the distributed garbage collector
18  * to identify client VMs.
19  *
20  * @version 1.20, 05/18/04
21  * @author Ann Wollrath
22  * @author Peter Jones
23  */

24 public final class VMID implements java.io.Serializable JavaDoc {
25
26     /** array of bytes uniquely identifying this host */
27     private static byte[] localAddr = computeAddressHash();
28     
29     /**
30      * @serial array of bytes uniquely identifying host created on
31      */

32     private byte[] addr;
33
34     /**
35      * @serial unique identifier with respect to host created on
36      */

37     private UID JavaDoc uid;
38
39     /** indicate compatibility with JDK 1.1.x version of class */
40     private static final long serialVersionUID = -538642295484486218L;
41
42     /**
43      * Create a new VMID. Each new VMID returned from this constructor
44      * is unique for all Java virtual machines under the following
45      * conditions: a) the conditions for uniqueness for objects of
46      * the class <code>java.rmi.server.UID</code> are satisfied, and b) an
47      * address can be obtained for this host that is unique and constant
48      * for the lifetime of this object. <p>
49      */

50     public VMID() {
51     addr = localAddr;
52     uid = new UID JavaDoc();
53     }
54
55     /**
56      * Return true if an accurate address can be determined for this
57      * host. If false, reliable VMID cannot be generated from this host
58      * @return true if host address can be determined, false otherwise
59      * @deprecated
60      */

61     @Deprecated JavaDoc
62     public static boolean isUnique() {
63     return true;
64     }
65
66     /**
67      * Compute hash code for this VMID.
68      */

69     public int hashCode() {
70     return uid.hashCode();
71     }
72
73     /**
74      * Compare this VMID to another, and return true if they are the
75      * same identifier.
76      */

77     public boolean equals(Object JavaDoc obj) {
78     if (obj instanceof VMID JavaDoc) {
79         VMID JavaDoc vmid = (VMID JavaDoc) obj;
80         if (!uid.equals(vmid.uid))
81         return false;
82         if ((addr == null) ^ (vmid.addr == null))
83         return false;
84         if (addr != null) {
85         if (addr.length != vmid.addr.length)
86             return false;
87         for (int i = 0; i < addr.length; ++ i)
88             if (addr[i] != vmid.addr[i])
89             return false;
90         }
91         return true;
92     } else {
93         return false;
94     }
95     }
96
97     /**
98      * Return string representation of this VMID.
99      */

100     public String JavaDoc toString() {
101     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
102     if (addr != null)
103         for (int i = 0; i < addr.length; ++ i) {
104         int x = (int) (addr[i] & 0xFF);
105         result.append((x < 0x10 ? "0" : "") +
106                   Integer.toString(x, 16));
107         }
108     result.append(':');
109     result.append(uid.toString());
110     return result.toString();
111     }
112     
113     /**
114      * Compute the hash an IP address. The hash is the first 8 bytes
115      * of the SHA digest of the IP address.
116      */

117     private static byte[] computeAddressHash() {
118
119     /*
120      * Get the local host's IP address.
121      */

122     byte[] addr = (byte[]) java.security.AccessController.doPrivileged(
123         new PrivilegedAction() {
124         public Object JavaDoc run() {
125         try {
126             return InetAddress.getLocalHost().getAddress();
127         } catch (Exception JavaDoc e) {
128         }
129         return new byte[] { 0, 0, 0, 0 };
130         }
131     });
132
133     byte[] addrHash;
134     final int ADDR_HASH_LENGTH = 8;
135     
136     try {
137         /*
138          * Calculate message digest of IP address using SHA.
139          */

140         MessageDigest md = MessageDigest.getInstance("SHA");
141         ByteArrayOutputStream sink = new ByteArrayOutputStream(64);
142         DataOutputStream out = new DataOutputStream(
143         new DigestOutputStream(sink, md));
144         out.write(addr, 0, addr.length);
145         out.flush();
146         
147         byte digest[] = md.digest();
148         int hashlength = Math.min(ADDR_HASH_LENGTH, digest.length);
149         addrHash = new byte[hashlength];
150         System.arraycopy(digest, 0, addrHash, 0, hashlength);
151
152     } catch (IOException ignore) {
153         /* can't happen, but be deterministic anyway. */
154         addrHash = new byte[0];
155     } catch (NoSuchAlgorithmException complain) {
156         throw new InternalError JavaDoc(complain.toString());
157     }
158     return addrHash;
159     }
160 }
161
Popular Tags