1 9 package org.jboss.portal.common.util; 10 11 import java.net.InetAddress ; 12 import java.security.SecureRandom ; 13 14 24 public class UUIDGenerator 25 { 26 27 29 30 private static final char[] hexDigits = "0123456789ABCDEF".toCharArray(); 31 32 34 35 private SecureRandom seeder; 36 37 38 private String midValue; 39 40 42 public UUIDGenerator() 43 { 44 try 45 { 46 StringBuffer buffer = new StringBuffer ( 16 ); 48 49 byte[] addr = InetAddress.getLocalHost().getAddress(); 51 buffer.append( toHex( toInt(addr), 8 ) ); 52 53 buffer.append( toHex( System.identityHashCode(this), 8 ) ); 55 56 midValue = buffer.toString(); 58 59 seeder = new SecureRandom (); 61 int node = seeder.nextInt(); 62 } 63 catch (Exception e) 64 { 65 throw new Error ("Not possible"); 66 } 67 } 68 69 public String generateKey() 70 { 71 StringBuffer buffer = new StringBuffer (32); 72 73 buffer.append(toHex((int)(System.currentTimeMillis() & 0xFFFFFFFF), 8)); 75 76 buffer.append( midValue ); 78 79 buffer.append( toHex( seeder.nextInt(), 8 ) ); 81 82 return buffer.toString(); 84 } 85 86 88 91 private String toHex(int value, int length) 92 { 93 StringBuffer buffer = new StringBuffer (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 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 |