001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.util;
005:
006: import java.net.InetAddress;
007: import java.security.SecureRandom;
008:
009: /**
010: * Generates a UUID. <p/>A Universally Unique Identifier (UUID) is a 128 bit number generated according to an algorithm
011: * that is garanteed to be unique in time A space from all other UUIDs.
012: *
013: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
014: */
015: public class UuidGenerator {
016: /**
017: * Random seeder.
018: */
019: private static SecureRandom s_seeder = null;
020:
021: /**
022: * Mid value, needed for calculation.
023: */
024: private static String s_midValue = null;
025:
026: /**
027: * Defines if the generator is initialized or not.
028: */
029: private static boolean s_initialized = false;
030:
031: /**
032: * Private constructor to prevent subclassing
033: */
034: private UuidGenerator() {
035: }
036:
037: /**
038: * Returns a unique uuid.
039: *
040: * @param obj the calling object (this)
041: * @return a unique uuid
042: */
043: public static String generate(Object obj) {
044: if (!s_initialized) {
045: initialize(obj);
046: }
047: long timeNow = System.currentTimeMillis();
048:
049: // getDefault int value as unsigned
050: int timeLow = (int) timeNow & 0xFFFFFFFF;
051: int node = s_seeder.nextInt();
052: return (hexFormat(timeLow, 8) + s_midValue + hexFormat(node, 8));
053: }
054:
055: /**
056: * Initializes the factory.
057: *
058: * @param obj
059: */
060: private synchronized static void initialize(final Object obj) {
061: try {
062: InetAddress inet = InetAddress.getLocalHost();
063: byte[] bytes = inet.getAddress();
064: String hexInetAddress = hexFormat(getInt(bytes), 8);
065: String this HashCode = hexFormat(System
066: .identityHashCode(obj), 8);
067: s_midValue = hexInetAddress + this HashCode;
068: s_seeder = new SecureRandom();
069: s_seeder.nextInt();
070: } catch (java.net.UnknownHostException e) {
071: throw new Error(
072: "can not initialize the UuidGenerator generator");
073: }
074: s_initialized = true;
075: }
076:
077: /**
078: * Utility method.
079: *
080: * @param abyte
081: * @return
082: */
083: private static int getInt(final byte[] abyte) {
084: int i = 0;
085: int j = 24;
086: for (int k = 0; j >= 0; k++) {
087: int l = abyte[k] & 0xff;
088: i += (l << j);
089: j -= 8;
090: }
091: return i;
092: }
093:
094: /**
095: * Utility method.
096: *
097: * @param i
098: * @param j
099: * @return
100: */
101: private static String hexFormat(final int i, final int j) {
102: String s = Integer.toHexString(i);
103: return padHex(s, j) + s;
104: }
105:
106: /**
107: * Utility method.
108: *
109: * @param str
110: * @param i
111: * @return
112: */
113: private static String padHex(final String str, final int i) {
114: StringBuffer buf = new StringBuffer();
115: if (str.length() < i) {
116: for (int j = 0; j < (i - str.length()); j++) {
117: buf.append('0');
118: }
119: }
120: return buf.toString();
121: }
122: }
|