KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > util > UuidGenerator


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.util;
5
6 import java.net.InetAddress JavaDoc;
7 import java.security.SecureRandom JavaDoc;
8
9 /**
10  * Generates a UUID. <p/>A Universally Unique Identifier (UUID) is a 128 bit number generated according to an algorithm
11  * that is garanteed to be unique in time A space from all other UUIDs.
12  *
13  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
14  */

15 public class UuidGenerator {
16   /**
17    * Random seeder.
18    */

19   private static SecureRandom JavaDoc s_seeder = null;
20
21   /**
22    * Mid value, needed for calculation.
23    */

24   private static String JavaDoc s_midValue = null;
25
26   /**
27    * Defines if the generator is initialized or not.
28    */

29   private static boolean s_initialized = false;
30
31   /**
32    * Private constructor to prevent subclassing
33    */

34   private UuidGenerator() {
35   }
36
37   /**
38    * Returns a unique uuid.
39    *
40    * @param obj the calling object (this)
41    * @return a unique uuid
42    */

43   public static String JavaDoc generate(Object JavaDoc obj) {
44     if (!s_initialized) {
45       initialize(obj);
46     }
47     long timeNow = System.currentTimeMillis();
48
49     // getDefault int value as unsigned
50
int timeLow = (int) timeNow & 0xFFFFFFFF;
51     int node = s_seeder.nextInt();
52     return (hexFormat(timeLow, 8) + s_midValue + hexFormat(node, 8));
53   }
54
55   /**
56    * Initializes the factory.
57    *
58    * @param obj
59    */

60   private synchronized static void initialize(final Object JavaDoc obj) {
61     try {
62       InetAddress JavaDoc inet = InetAddress.getLocalHost();
63       byte[] bytes = inet.getAddress();
64       String JavaDoc hexInetAddress = hexFormat(getInt(bytes), 8);
65       String JavaDoc thisHashCode = hexFormat(System.identityHashCode(obj), 8);
66       s_midValue = hexInetAddress + thisHashCode;
67       s_seeder = new SecureRandom JavaDoc();
68       s_seeder.nextInt();
69     } catch (java.net.UnknownHostException JavaDoc e) {
70       throw new Error JavaDoc("can not initialize the UuidGenerator generator");
71     }
72     s_initialized = true;
73   }
74
75   /**
76    * Utility method.
77    *
78    * @param abyte
79    * @return
80    */

81   private static int getInt(final byte[] abyte) {
82     int i = 0;
83     int j = 24;
84     for (int k = 0; j >= 0; k++) {
85       int l = abyte[k] & 0xff;
86       i += (l << j);
87       j -= 8;
88     }
89     return i;
90   }
91
92   /**
93    * Utility method.
94    *
95    * @param i
96    * @param j
97    * @return
98    */

99   private static String JavaDoc hexFormat(final int i, final int j) {
100     String JavaDoc s = Integer.toHexString(i);
101     return padHex(s, j) + s;
102   }
103
104   /**
105    * Utility method.
106    *
107    * @param str
108    * @param i
109    * @return
110    */

111   private static String JavaDoc padHex(final String JavaDoc str, final int i) {
112     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
113     if (str.length() < i) {
114       for (int j = 0; j < (i - str.length()); j++) {
115         buf.append('0');
116       }
117     }
118     return buf.toString();
119   }
120 }
Popular Tags