1 22 package org.jboss.util.id; 23 24 import java.net.InetAddress ; 25 26 import java.security.AccessController ; 27 import java.security.PrivilegedAction ; 28 import java.util.Arrays ; 29 30 import org.jboss.util.HashCode; 31 import org.jboss.util.platform.PID; 32 33 57 public class VMID 58 implements ID 59 { 60 61 protected final byte[] address; 62 63 64 protected final PID pid; 65 66 67 protected final UID uid; 68 69 70 protected final int hashCode; 71 72 81 protected VMID(final byte[] address, final PID pid, final UID uid) { 82 this.address = address; 83 this.pid = pid; 84 this.uid = uid; 85 86 int code = pid.hashCode(); 88 code ^= uid.hashCode(); 89 code ^= HashCode.generate(address); 90 hashCode = code; 91 } 92 93 98 protected VMID(final VMID vmid) { 99 this.address = vmid.address; 100 this.pid = vmid.pid; 101 this.uid = vmid.uid; 102 this.hashCode = vmid.hashCode; 103 } 104 105 110 public final byte[] getAddress() { 111 return address; 112 } 113 114 119 public final PID getProcessID() { 120 return pid; 121 } 122 123 128 public final UID getUID() { 129 return uid; 130 } 131 132 137 public String toString() { 138 StringBuffer buff = new StringBuffer (); 139 140 for (int i=0; i<address.length; i++) { 141 int n = (int) (address[i] & 0xFF); 142 buff.append(Integer.toString(n, Character.MAX_RADIX)); 143 } 144 145 buff.append("-").append(pid.toString(Character.MAX_RADIX)); 146 buff.append("-").append(uid); 147 148 return buff.toString(); 149 } 150 151 156 public final int hashCode() { 157 return hashCode; 158 } 159 160 169 public boolean equals(final Object obj) { 170 if (obj == this) return true; 171 172 if (obj != null && obj.getClass() == getClass()) { 173 VMID vmid = (VMID)obj; 174 return 175 Arrays.equals(vmid.address, address) && 176 vmid.pid.equals(pid) && 177 vmid.uid.equals(uid); 178 } 179 180 return false; 181 } 182 183 188 public Object clone() { 189 try { 190 return super.clone(); 191 } 192 catch (CloneNotSupportedException e) { 193 throw new InternalError (); 194 } 195 } 196 197 202 public static String asString() { 203 return getInstance().toString(); 204 } 205 206 207 211 212 private static VMID instance = null; 213 214 220 public synchronized static VMID getInstance() { 221 if (instance == null) { 222 instance = create(); 223 } 224 return instance; 225 } 226 227 231 public static final byte[] UNKNOWN_HOST = { 0, 0, 0, 0 }; 232 233 236 private static byte[] getHostAddress() { 237 return (byte[]) AccessController.doPrivileged(new PrivilegedAction () { 238 public Object run() { 239 try { 240 return InetAddress.getLocalHost().getAddress(); 241 } 242 catch (Exception e) { 243 return UNKNOWN_HOST; 244 } 245 } 246 }); 247 } 248 249 254 private static VMID create() { 255 byte[] address = getHostAddress(); 257 258 return new VMID(address, PID.getInstance(), new UID()); 259 } 260 } 261 | Popular Tags |