KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > util > GUIDGenerator


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.util;
15
16 import org.apache.log4j.Logger;
17
18 /** Shamelessly ripped from generated XDoclet source, because I don't want to generate util classes.
19  *
20  * @author XDoclet.sf.net
21  * @version $Id: GUIDGenerator.java,v 1.1 2006/08/05 09:59:37 anatom Exp $
22  */

23 public class GUIDGenerator {
24     private static final Logger log = Logger.getLogger(GUIDGenerator.class);
25
26        /** Cached per JVM server IP. */
27        private static String JavaDoc hexServerIP = null;
28
29        // initialise the secure random instance
30
private static final java.security.SecureRandom JavaDoc seeder = new java.security.SecureRandom JavaDoc();
31        
32        /**
33         * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
34         * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
35         *
36         * Usage: Add an id field (type java.lang.String) to your EJB, and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
37         */

38        public static final String JavaDoc generateGUID(Object JavaDoc o) {
39            StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc(16);
40            if (hexServerIP == null) {
41                java.net.InetAddress JavaDoc localInetAddress = null;
42                try {
43                    // get the inet address
44
localInetAddress = java.net.InetAddress.getLocalHost();
45                }
46                catch (java.net.UnknownHostException JavaDoc uhe) {
47                    log.error("Could not get the local IP address using InetAddress.getLocalHost(): ", uhe);
48                    // todo: find better way to get around this...
49
return null;
50                }
51                byte serverIP[] = localInetAddress.getAddress();
52                hexServerIP = hexFormat(getInt(serverIP), 8);
53            }
54
55            String JavaDoc hashcode = hexFormat(System.identityHashCode(o), 8);
56            tmpBuffer.append(hexServerIP);
57            tmpBuffer.append(hashcode);
58
59            long timeNow = System.currentTimeMillis();
60            int timeLow = (int)timeNow & 0xFFFFFFFF;
61            int node = seeder.nextInt();
62
63            StringBuffer JavaDoc guid = new StringBuffer JavaDoc(32);
64            guid.append(hexFormat(timeLow, 8));
65            guid.append(tmpBuffer.toString());
66            guid.append(hexFormat(node, 8));
67            return guid.toString();
68        }
69        
70        private static int getInt(byte bytes[]) {
71            int i = 0;
72            int j = 24;
73            for (int k = 0; j >= 0; k++) {
74                int l = bytes[k] & 0xff;
75                i += l << j;
76                j -= 8;
77            }
78            return i;
79        }
80
81        private static String JavaDoc hexFormat(int i, int j) {
82            String JavaDoc s = Integer.toHexString(i);
83            return padHex(s, j) + s;
84        }
85
86        private static String JavaDoc padHex(String JavaDoc s, int i) {
87            StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc();
88            if (s.length() < i) {
89                for (int j = 0; j < i - s.length(); j++) {
90                    tmpBuffer.append('0');
91                }
92            }
93            return tmpBuffer.toString();
94        }
95 }
96
Popular Tags