1 6 7 package org.jfox.ioc.util; 8 9 import java.io.ByteArrayOutputStream ; 10 import java.io.DataOutputStream ; 11 import java.io.IOException ; 12 import java.io.Serializable ; 13 import java.net.InetAddress ; 14 import java.security.DigestOutputStream ; 15 import java.security.MessageDigest ; 16 import java.security.NoSuchAlgorithmException ; 17 import java.util.Arrays ; 18 19 22 23 public class VMID implements Serializable { 24 25 private UUID vmUUID = UUID.randomUUID(); 27 28 private byte[] address = getLocalAddress(); 29 30 private static MessageDigest md; 31 32 static { 33 try { 34 md = MessageDigest.getInstance("SHA"); 35 } 36 catch(NoSuchAlgorithmException e) { 37 try { 38 md = MessageDigest.getInstance("MD5"); 39 } 40 catch(NoSuchAlgorithmException ex) { 41 throw new RuntimeException (ex); 42 } 43 } 44 } 45 46 private static VMID instance = new VMID(); 47 48 private VMID() { 49 } 50 51 public static VMID getInstance() { 52 return instance; 53 } 54 55 public byte[] getAddress() { 56 return address; 57 } 58 59 private static byte[] getLocalAddress() { 60 try { 61 return InetAddress.getLocalHost().getAddress(); 62 } 63 catch(Exception e) { 64 return new byte[]{127, 0, 0, 1}; 65 } 66 } 67 68 public String toString() { 69 StringBuffer buff = new StringBuffer (); 70 byte[] addr = computeAddressHash(address); 71 for(int i = 0; i < addr.length; i++) { 72 int n = (int) (addr[i] & 0xFF); 73 buff.append(Integer.toString(n, Character.MAX_RADIX)); 74 } 75 buff.append("-").append(vmUUID.toString()); 76 77 return buff.toString(); 78 } 79 80 public boolean equals(Object o) { 81 if(this == o) return true; 82 if(!(o instanceof VMID)) return false; 83 84 final VMID vmid = (VMID) o; 85 86 if(!Arrays.equals(address, vmid.address)) return false; 87 if(vmUUID != null ? !vmUUID.equals(vmid.vmUUID) : vmid.vmUUID != null) return false; 88 89 return true; 90 } 91 92 public int hashCode() { 93 return (vmUUID != null ? vmUUID.hashCode() : 0); 94 } 95 96 97 101 private static byte[] computeAddressHash(byte[] addr) { 102 103 106 108 byte[] addrHash; 109 final int ADDR_HASH_LENGTH = 8; 110 111 try { 112 115 116 ByteArrayOutputStream sink = new ByteArrayOutputStream (64); 117 DataOutputStream out = new DataOutputStream (new DigestOutputStream (sink, md)); 118 out.write(addr, 0, addr.length); 119 out.flush(); 120 121 byte digest[] = md.digest(); 122 int hashlength = Math.min(ADDR_HASH_LENGTH, digest.length); 123 addrHash = new byte[hashlength]; 124 System.arraycopy(digest, 0, addrHash, 0, hashlength); 125 126 } 127 catch(IOException ignore) { 128 129 addrHash = new byte[0]; 130 } 131 return addrHash; 132 } 133 134 139 public boolean isCurrentVM() { 140 return this.equals(VMID.getInstance()); 141 } 142 143 public boolean isSameVM(VMID vmid) { 144 return this.equals(vmid); 145 } 146 147 148 public static void main(String [] args) { 149 System.out.println(new VMID().toString()); 150 System.out.println(new VMID().toString()); 151 System.out.println(new VMID().toString()); 152 System.out.println(VMID.getInstance().isCurrentVM()); 153 } 154 } 155 | Popular Tags |