1 22 package org.jboss.ejb.plugins.keygenerator.uuid; 23 24 import org.jboss.ejb.plugins.keygenerator.KeyGenerator; 25 26 import java.net.InetAddress ; 27 import java.security.SecureRandom ; 28 29 30 38 public class UUIDKeyGenerator 39 implements KeyGenerator 40 { 41 42 44 45 SecureRandom seeder; 46 47 48 private String midValue; 49 50 52 public UUIDKeyGenerator() 53 throws Exception 54 { 55 57 StringBuffer buffer = new StringBuffer ( 16 ); 58 59 byte[] addr = InetAddress.getLocalHost().getAddress(); 61 buffer.append( toHex( toInt(addr), 8 ) ); 62 63 buffer.append( toHex( System.identityHashCode(this), 8 ) ); 65 66 midValue = buffer.toString(); 68 69 seeder = new SecureRandom (); 71 int node = seeder.nextInt(); 72 } 73 74 76 public Object generateKey() 77 { 78 StringBuffer buffer = new StringBuffer ( 32 ); 79 80 buffer.append(toHex((int)(System.currentTimeMillis() & 0xFFFFFFFF), 8)); 82 83 buffer.append( midValue ); 85 86 buffer.append( toHex( seeder.nextInt(), 8 ) ); 88 89 return buffer.toString(); 91 } 92 93 95 98 private String toHex( int value, int length ) 99 { 100 char[] hexDigits = 102 { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; 103 104 StringBuffer buffer = new StringBuffer ( 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 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 |