KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > util > GUIDGenerator


1 /*
2     Mdarad-Toolobox is a collection of tools for Architected RAD
3     (Rapid Application Development) based on an MDA approach.
4     The toolbox contains frameworks and generators for many environments
5     (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
6     applications from a design Model
7     Copyright (C) 2004-2005 Elapse Technologies Inc.
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     General Public License for more details.
18
19     You should have received a copy of the GNU General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23
24 package org.mdarad.framework.util;
25
26 import org.mdarad.framework.util.GUIDGenerationException;
27
28 import java.net.UnknownHostException JavaDoc;
29 import java.net.InetAddress JavaDoc;
30
31 /**
32  * Created by IntelliJ IDEA.
33  * User: Fran├žois Eric
34  * Date: 2005-02-01
35  * Time: 14:21:46
36  * To change this template use File | Settings | File Templates.
37  */

38 public class GUIDGenerator {
39
40     /**
41      * Cached per JVM server IP.
42      */

43     private static String JavaDoc hexServerIP = null;
44
45     // initialise the secure random instance
46
private static final java.security.SecureRandom JavaDoc seeder = new java.security.SecureRandom JavaDoc();
47
48     /**
49      * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
50      * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
51      * <p/>
52      */

53     public static final String JavaDoc generateGUID(Object JavaDoc o) throws GUIDGenerationException {
54         StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc(16);
55         if (hexServerIP == null) {
56             InetAddress JavaDoc localInetAddress = null;
57
58             try {
59                 // get the inet address
60
localInetAddress = InetAddress.getLocalHost();
61                 byte serverIP[] = localInetAddress.getAddress();
62                 hexServerIP = hexFormat(getInt(serverIP), 8);
63             } catch (UnknownHostException JavaDoc e) {
64                 throw new GUIDGenerationException("Could not generate GUID, for unable to reach localhost", e);
65             }
66         }
67         String JavaDoc hashcode = hexFormat(System.identityHashCode(o), 8);
68         tmpBuffer.append(hexServerIP);
69         tmpBuffer.append(hashcode);
70
71         long timeNow = System.currentTimeMillis();
72         int timeLow = (int) timeNow & 0xFFFFFFFF;
73         int node = seeder.nextInt();
74
75         StringBuffer JavaDoc guid = new StringBuffer JavaDoc(32);
76         guid.append(hexFormat(timeLow, 8));
77         guid.append(tmpBuffer.toString());
78         guid.append(hexFormat(node, 8));
79         return guid.toString();
80     }
81
82
83     private static int getInt(byte bytes[]) {
84         int i = 0;
85         int j = 24;
86         for (int k = 0; j >= 0; k++) {
87             int l = bytes[k] & 0xff;
88             i += l << j;
89             j -= 8;
90         }
91         return i;
92     }
93
94     private static String JavaDoc hexFormat(int i, int j) {
95         String JavaDoc s = Integer.toHexString(i);
96         return padHex(s, j) + s;
97     }
98
99     private static String JavaDoc padHex(String JavaDoc s, int i) {
100         StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc();
101         if (s.length() < i) {
102             for (int j = 0; j < i - s.length(); j++) {
103                 tmpBuffer.append('0');
104             }
105         }
106         return tmpBuffer.toString();
107     }
108
109 }
110
Popular Tags