1 7 8 package java.rmi.dgc; 9 10 import java.io.*; 11 import java.net.*; 12 import java.rmi.server.UID ; 13 import java.security.*; 14 15 24 public final class VMID implements java.io.Serializable { 25 26 27 private static byte[] localAddr = computeAddressHash(); 28 29 32 private byte[] addr; 33 34 37 private UID uid; 38 39 40 private static final long serialVersionUID = -538642295484486218L; 41 42 50 public VMID() { 51 addr = localAddr; 52 uid = new UID (); 53 } 54 55 61 @Deprecated 62 public static boolean isUnique() { 63 return true; 64 } 65 66 69 public int hashCode() { 70 return uid.hashCode(); 71 } 72 73 77 public boolean equals(Object obj) { 78 if (obj instanceof VMID ) { 79 VMID vmid = (VMID ) 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 100 public String toString() { 101 StringBuffer result = new StringBuffer (); 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 117 private static byte[] computeAddressHash() { 118 119 122 byte[] addr = (byte[]) java.security.AccessController.doPrivileged( 123 new PrivilegedAction() { 124 public Object run() { 125 try { 126 return InetAddress.getLocalHost().getAddress(); 127 } catch (Exception 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 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 154 addrHash = new byte[0]; 155 } catch (NoSuchAlgorithmException complain) { 156 throw new InternalError (complain.toString()); 157 } 158 return addrHash; 159 } 160 } 161 | Popular Tags |