001: // UUID.java
002: // $Id: UUID.java,v 1.2 2000/10/18 14:52:49 bmahe Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005: package org.w3c.util;
006:
007: import java.net.InetAddress;
008: import java.net.UnknownHostException;
009:
010: import org.w3c.tools.crypt.Md5;
011:
012: /**
013: * A UUID (from java.rmi.server.UID)
014: * @version $Revision: 1.2 $
015: * @author Benoît Mahé (bmahe@w3.org)
016: */
017: public final class UUID {
018:
019: /**
020: * @serial Integer that helps create a unique UID.
021: */
022: private int unique;
023:
024: /**
025: * @serial Long used to record the time. The <code>time</code>
026: * will be used to create a unique UID.
027: */
028: private long time;
029:
030: /**
031: * InetAddress to make the UID globally unique
032: */
033: private static String address;
034:
035: /**
036: * a random number
037: */
038: private static int hostUnique;
039:
040: /**
041: * Used for synchronization
042: */
043: private static Object mutex;
044:
045: private static long lastTime;
046: private static long DELAY;
047:
048: private static String generateNoNetworkID() {
049: Thread current = Thread.currentThread();
050: String nid = current.activeCount()
051: + System.getProperty("os.version")
052: + System.getProperty("user.name")
053: + System.getProperty("java.version");
054: System.out.println(nid);
055: Md5 md5 = new Md5(nid);
056: md5.processString();
057: return md5.getStringDigest();
058: }
059:
060: static {
061: hostUnique = (new Object()).hashCode();
062: mutex = new Object();
063: lastTime = System.currentTimeMillis();
064: DELAY = 10; // in milliseconds
065: try {
066: String s = InetAddress.getLocalHost().getHostAddress();
067: Md5 md5 = new Md5(s);
068: md5.processString();
069: address = md5.getStringDigest();
070: } catch (UnknownHostException ex) {
071: address = generateNoNetworkID();
072: }
073: }
074:
075: public UUID() {
076: synchronized (mutex) {
077: boolean done = false;
078: while (!done) {
079: time = System.currentTimeMillis();
080: if (time < lastTime + DELAY) {
081: // pause for a second to wait for time to change
082: try {
083: Thread.currentThread().sleep(DELAY);
084: } catch (java.lang.InterruptedException e) {
085: } // ignore exception
086: continue;
087: } else {
088: lastTime = time;
089: done = true;
090: }
091: }
092: unique = hostUnique;
093: }
094: }
095:
096: public String toString() {
097: return Integer.toString(unique, 16) + "-"
098: + Long.toString(time, 16) + "-" + address;
099: }
100:
101: public boolean equals(Object obj) {
102: if ((obj != null) && (obj instanceof UUID)) {
103: UUID uuid = (UUID) obj;
104: return (unique == uuid.unique && time == uuid.time && address
105: .equals(uuid.address));
106: } else {
107: return false;
108: }
109: }
110:
111: public static void main(String args[]) {
112: System.out.println(new UUID());
113: System.out.println(new UUID());
114: System.out.println(new UUID());
115: System.out.println(new UUID());
116: }
117:
118: }
|