KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > keygenerator > uuid > UUIDKeyGenerator


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.keygenerator.uuid;
23
24 import org.jboss.ejb.plugins.keygenerator.KeyGenerator;
25
26 import java.net.InetAddress JavaDoc;
27 import java.security.SecureRandom JavaDoc;
28
29
30 /**
31  * The implementation of UUID key generator
32  * based on the algorithm from Floyd Marinescu's EJB Design Patterns.
33  *
34  * @author <a HREF="mailto:loubyansky@ukr.net">Alex Loubyansky</a>
35  *
36  * @version $Revision: 37459 $
37  */

38 public class UUIDKeyGenerator
39    implements KeyGenerator
40 {
41
42    // Attributes ---------------------------------------------------
43

44    /** secure random to provide nonrepeating seed */
45    SecureRandom JavaDoc seeder;
46
47    /** cached middle value */
48    private String JavaDoc midValue;
49
50    // Constructor --------------------------------------------------
51

52    public UUIDKeyGenerator()
53       throws Exception JavaDoc
54    {
55       // cache the middle part for UUID
56

57       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc( 16 );
58
59       // construct host part of the uuid (8 hex digits)
60
byte[] addr = InetAddress.getLocalHost().getAddress();
61       buffer.append( toHex( toInt(addr), 8 ) );
62
63       // append the hash code for this object (8 hex digits)
64
buffer.append( toHex( System.identityHashCode(this), 8 ) );
65
66       // set up midValue
67
midValue = buffer.toString();
68
69       // load up the randomizer
70
seeder = new SecureRandom JavaDoc();
71       int node = seeder.nextInt();
72    }
73
74    // KeyGenerator implementation ----------------------------------
75

76    public Object JavaDoc generateKey()
77    {
78       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc( 32 );
79
80       // append current time as unsigned int value
81
buffer.append(toHex((int)(System.currentTimeMillis() & 0xFFFFFFFF), 8));
82
83       // append cached midValue
84
buffer.append( midValue );
85
86       // append the next random int
87
buffer.append( toHex( seeder.nextInt(), 8 ) );
88
89       // return the result
90
return buffer.toString();
91    }
92
93    // Private ------------------------------------------------------
94

95    /**
96     * Converts int value to string hex representation
97     */

98    private String JavaDoc toHex( int value, int length )
99    {
100       // hex digits
101
char[] hexDigits =
102          { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
103
104       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc( length );
105       int shift = (length - 1) << 2;
106       int i = -1;
107       while( ++i < length )
108       {
109          buffer.append( hexDigits[(value >> shift) & 0x0000000F] );
110          value <<= 4;
111       }
112       return buffer.toString();
113    }
114
115    /**
116     * Constructs int value from byte array
117     */

118    private static int toInt( byte[] bytes )
119    {
120       int value = 0;
121       int i = -1;
122       while( ++i < bytes.length )
123       {
124          value <<= 8;
125          int b = bytes[ i ] & 0xff;
126          value |= b;
127       }
128       return value;
129    }
130 }
131
Popular Tags