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