KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > util > UUIDGenerator


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.util;
10
11 import java.net.InetAddress JavaDoc;
12 import java.security.SecureRandom JavaDoc;
13
14 /**
15  * Adapted from ejb plugin key generated.
16  *
17  * The implementation of UUID key generator
18  * based on the algorithm from Floyd Marinescu's EJB Design Patterns.
19  *
20  * @author <a HREF="mailto:alex.loubyansky@jboss.org">Alex Loubyansky</a>
21  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
22  * @version $Revision: 1.1 $
23  */

24 public class UUIDGenerator
25 {
26
27    // Attributes ---------------------------------------------------
28

29    /** Hex digits */
30    private static final char[] hexDigits = "0123456789ABCDEF".toCharArray();
31
32    // Static --------------------------------------------------------
33

34    /** secure random to provide nonrepeating seed */
35    private SecureRandom JavaDoc seeder;
36
37    /** cached middle value */
38    private String JavaDoc midValue;
39
40    // Constructor --------------------------------------------------
41

42    public UUIDGenerator()
43    {
44       try
45       {
46          // Cache the middle part for UUID
47
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc( 16 );
48
49          // Construct host part of the uuid (8 hex digits)
50
byte[] addr = InetAddress.getLocalHost().getAddress();
51          buffer.append( toHex( toInt(addr), 8 ) );
52
53          // Append the hash code for this object (8 hex digits)
54
buffer.append( toHex( System.identityHashCode(this), 8 ) );
55
56          // Set up midValue
57
midValue = buffer.toString();
58
59          // Load up the randomizer
60
seeder = new SecureRandom JavaDoc();
61          int node = seeder.nextInt();
62       }
63       catch (Exception JavaDoc e)
64       {
65          throw new Error JavaDoc("Not possible");
66       }
67    }
68
69    public String JavaDoc generateKey()
70    {
71       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(32);
72
73       // Append current time as unsigned int value
74
buffer.append(toHex((int)(System.currentTimeMillis() & 0xFFFFFFFF), 8));
75
76       // Append cached midValue
77
buffer.append( midValue );
78
79       // Append the next random int
80
buffer.append( toHex( seeder.nextInt(), 8 ) );
81
82       // Return the result
83
return buffer.toString();
84    }
85
86    // Private ------------------------------------------------------
87

88    /**
89     * Converts int value to string hex representation
90     */

91    private String JavaDoc toHex(int value, int length)
92    {
93       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(length);
94       int shift = (length - 1) << 2;
95       int i = -1;
96       while(++i < length)
97       {
98          buffer.append(hexDigits[(value >> shift) & 0x0000000F]);
99          value <<= 4;
100       }
101       return buffer.toString();
102    }
103
104    /**
105     * Constructs int value from byte array
106     */

107    private static int toInt( byte[] bytes )
108    {
109       int value = 0;
110       int i = -1;
111       while (++i < bytes.length)
112       {
113          value <<= 8;
114          int b = bytes[ i ] & 0xff;
115          value |= b;
116       }
117       return value;
118    }
119 }
120
Popular Tags