KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > util > VMID


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.ioc.util;
8
9 import java.io.ByteArrayOutputStream JavaDoc;
10 import java.io.DataOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.net.InetAddress JavaDoc;
14 import java.security.DigestOutputStream JavaDoc;
15 import java.security.MessageDigest JavaDoc;
16 import java.security.NoSuchAlgorithmException JavaDoc;
17 import java.util.Arrays JavaDoc;
18
19 /**
20  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
21  */

22
23 public class VMID implements Serializable JavaDoc {
24
25     // unique vm uuid
26
private UUID vmUUID = UUID.randomUUID();
27
28     private byte[] address = getLocalAddress();
29
30     private static MessageDigest JavaDoc md;
31
32     static {
33         try {
34             md = MessageDigest.getInstance("SHA");
35         }
36         catch(NoSuchAlgorithmException JavaDoc e) {
37             try {
38                 md = MessageDigest.getInstance("MD5");
39             }
40             catch(NoSuchAlgorithmException JavaDoc ex) {
41                 throw new RuntimeException JavaDoc(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 JavaDoc e) {
64             return new byte[]{127, 0, 0, 1};
65         }
66     }
67
68     public String JavaDoc toString() {
69         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
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 JavaDoc 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     /**
98      * Compute the hash an IP address. The hash is the first 8 bytes
99      * of the SHA digest of the IP address.
100      */

101     private static byte[] computeAddressHash(byte[] addr) {
102
103         /*
104         * Get the local host's IP address.
105         */

106 // byte[] addr = address;
107

108         byte[] addrHash;
109         final int ADDR_HASH_LENGTH = 8;
110
111         try {
112             /*
113             * Calculate message digest of IP address using SHA.
114             */

115
116             ByteArrayOutputStream JavaDoc sink = new ByteArrayOutputStream JavaDoc(64);
117             DataOutputStream JavaDoc out = new DataOutputStream JavaDoc(new DigestOutputStream JavaDoc(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 JavaDoc ignore) {
128             /* can't happen, but be deterministic anyway. */
129             addrHash = new byte[0];
130         }
131         return addrHash;
132     }
133
134     /**
135      * is vmid is stay in current VM
136      *
137      * @return boolean
138      */

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 JavaDoc[] 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