KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > uidgenerator > UIDGenerator


1 package nl.hippo.uidgenerator;
2
3 import java.net.InetAddress JavaDoc;
4 import java.net.UnknownHostException JavaDoc;
5 import java.security.SecureRandom JavaDoc;
6
7 /**
8  * Implementation of a UID generator based on the one in EJB Patterns (freely available at:
9  * http://www.theserverside.com/books/wiley/EJBDesignPatterns/index.tss).
10  *
11  * @author Johan Stuyts
12  */

13 public class UIDGenerator
14 {
15     private SecureRandom JavaDoc m_randomNumberGenerator;
16     
17     private String JavaDoc m_middle16Characters;
18     
19     private static UIDGenerator s_instance;
20     
21     public static String JavaDoc generateUID() throws UIDGeneratorException
22     {
23         return getInstance().generateNext();
24     }
25
26     public UIDGenerator() throws UIDGeneratorException
27     {
28         super();
29         m_randomNumberGenerator = new SecureRandom JavaDoc();
30         StringBuffer JavaDoc middle16Characters = new StringBuffer JavaDoc();
31         middle16Characters.append(toHexString(getLeastSignificantIpAddressData()));
32         middle16Characters.append(toHexString(System.identityHashCode(this)));
33         m_middle16Characters = middle16Characters.toString();
34     }
35         
36      public String JavaDoc generateNext()
37      {
38         int first32Bits = (int) (System.currentTimeMillis() & 0xFFFFFFFFl);
39         int fourth32Bits = getNextRandomNumber();
40         StringBuffer JavaDoc result = new StringBuffer JavaDoc(toHexString(first32Bits));
41         result.append(m_middle16Characters);
42         result.append(toHexString(fourth32Bits));
43         return result.toString();
44     }
45      
46      private static synchronized UIDGenerator getInstance() throws UIDGeneratorException
47      {
48          if (s_instance == null)
49          {
50              s_instance = new UIDGenerator();
51          }
52          return s_instance;
53      }
54     
55     private static int getLeastSignificantIpAddressData() throws UIDGeneratorException
56     {
57         int result = 0;
58         try
59         {
60             byte[] ipAddress = InetAddress.getLocalHost().getAddress();
61             if (ipAddress.length >= 4)
62             {
63                 for (int i = 0; i < 4; i++)
64                 {
65                     result <<= 8;
66                     result += ipAddress[ipAddress.length - 4 + i] & 0xFF;
67                 }
68             }
69             else
70             {
71                 throw new UIDGeneratorException("Not enough information in IP address");
72             }
73         }
74         catch (UnknownHostException JavaDoc e)
75         {
76             throw new UIDGeneratorException(e);
77         }
78         
79         return result;
80     }
81     
82     private static String JavaDoc toHexString(int value)
83     {
84         String JavaDoc hexStringWithLeadingZeros = "0000000" + Integer.toHexString(value);
85         return hexStringWithLeadingZeros.substring(hexStringWithLeadingZeros.length() - 8);
86     }
87     
88     private int getNextRandomNumber()
89     {
90         return m_randomNumberGenerator.nextInt();
91     }
92
93 }
94
Popular Tags