KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > uuid > UuidUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * UuidUtil.java
26  *
27  * Created on October 15, 2002, 9:39 AM
28  */

29
30 package com.sun.enterprise.util.uuid;
31
32 import java.net.InetAddress JavaDoc;
33
34 import java.rmi.server.UID JavaDoc;
35
36 import java.security.SecureRandom JavaDoc;
37
38 /**
39  * Class UuidUtil
40  *
41  *
42  */

43 public class UuidUtil
44 {
45
46     static final String JavaDoc _inetAddr = initInetAddr();
47
48     //first method (from MarketMaker Guid)
49
public static String JavaDoc generateUuidMM() {
50         return new StringBuffer JavaDoc(new UID JavaDoc().toString()).reverse().append(':').append(_inetAddr).toString();
51     }
52
53     //second method
54
public static String JavaDoc generateUuid() {
55         return generateUuid(new Object JavaDoc());
56     }
57     
58     //this method can take in the session object
59
//and insure better uniqueness guarantees
60
public static String JavaDoc generateUuid(Object JavaDoc obj) {
61         
62         //low order time bits
63
long presentTime = System.currentTimeMillis();
64         int presentTimeLow = (int) presentTime & 0xFFFFFFFF;
65         String JavaDoc presentTimeStringLow = formatHexString(presentTimeLow);
66         
67         //random number
68
String JavaDoc nextRandom = getNextRandomString();
69         
70         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(50);
71         sb.append(presentTimeStringLow);
72         //sb.append(":");
73
sb.append(getIdentityHashCode(obj));
74         //sb.append(":");
75
//sb.append(_inetAddr);
76
sb.append(addRandomTo(_inetAddr));
77         //sb.append(":");
78
sb.append(getNextRandomString());
79         return sb.toString();
80     }
81     
82     /**
83      * Method initInetAddr
84      *
85      *
86      * @return
87      *
88      * @audience
89      */

90     private static String JavaDoc initInetAddr() {
91
92         try {
93             byte[] bytes = InetAddress.getLocalHost().getAddress();
94             StringBuffer JavaDoc b = new StringBuffer JavaDoc();
95             String JavaDoc s = null;
96
97             for (int i = 0; i < bytes.length; i++) {
98                 s = Integer.toHexString(bytes[i]);
99
100                 if (bytes[i] < 0) {
101                     b.append(s.substring(s.length() - 2));
102                 } else {
103                     b.append(s);
104                 }
105             }
106
107             return b.toString();
108         } catch (Exception JavaDoc ex) {
109             //must return a value
110
return "a48eb993";
111             //return null;
112
}
113     }
114         
115     private static String JavaDoc addRandomTo(String JavaDoc hexString)
116     {
117         long hexAsLong = convertToLong(hexString);
118         int nextRandom = getNextInt();
119         long resultInt = hexAsLong + nextRandom;
120         String JavaDoc result = Long.toHexString(resultInt);
121         return result;
122     }
123     
124     /**
125      * Method getIdentityHashCode
126      *
127      *
128      * @return
129      *
130      * @audience
131      */

132     private static String JavaDoc getIdentityHashCode(Object JavaDoc obj) {
133         
134         String JavaDoc result = null;
135         try {
136             int hc = System.identityHashCode(obj);
137             return formatHexString(hc);
138                 
139         } catch (Exception JavaDoc ex) {
140             //must return a value
141
//return null;
142
return "8AF5182";
143         }
144     }
145     
146     private static String JavaDoc formatHexString(int inputInt)
147     {
148         String JavaDoc result = null;
149         String JavaDoc s = Integer.toHexString(inputInt);
150         if(s.length() < 8)
151         {
152             result = s;
153         } else {
154             result = s.substring(0, 7);
155         }
156         return result;
157     }
158     
159     private static synchronized int getNextInt() {
160         return _seeder.nextInt();
161     }
162     
163     private static String JavaDoc getNextRandomString() {
164         int nextInt = getNextInt();
165         return formatHexString(nextInt);
166     }
167         
168     private static long convertToLong(String JavaDoc hexString)
169     {
170         long result = 0;
171         try
172         {
173             result = (Long.valueOf(hexString, 16)).longValue();
174         } catch (NumberFormatException JavaDoc ex) {
175         }
176         return result;
177     }
178
179     private static SecureRandom JavaDoc _seeder = new SecureRandom JavaDoc();
180
181     /**
182      * Method main
183      *
184      *
185      * @param args
186      *
187      * @audience
188      */

189     public static void main(String JavaDoc[] args) {
190         System.out.println(UuidUtil.generateUuidMM());
191         System.out.println(UuidUtil.generateUuid());
192         System.out.println(UuidUtil.generateUuid(new Object JavaDoc()));
193     }
194 }
195
196
197
198
Popular Tags