KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > commons > utils > IdentifierUtil


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.commons.utils;
6
7 import java.net.InetAddress JavaDoc;
8 import java.security.SecureRandom JavaDoc;
9 /**
10  * Jul 19, 2004
11  * @author: Tuan Nguyen
12  * @email: tuan08@users.sourceforge.net
13  * @version: $Id: IdentifierUtil.java,v 1.1 2004/07/20 12:22:42 tuan08 Exp $
14  */

15 public class IdentifierUtil {
16
17     private static String JavaDoc hexServerIP_ = null;
18     private static final SecureRandom JavaDoc seeder_ = new SecureRandom JavaDoc();
19
20     //==============================================================================
21
static public String JavaDoc generateUUID(Object JavaDoc o) {
22       StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc(16);
23       if (hexServerIP_ == null) {
24         InetAddress JavaDoc localInetAddress = null;
25         try {
26           // get the inet address
27
localInetAddress = InetAddress.getLocalHost();
28         }
29         catch (java.net.UnknownHostException JavaDoc uhe) {
30           System.err.println("ContentSetUtil: Could not get the local IP address using InetAddress.getLocalHost()!");
31           // todo: find better way to get around this...
32
uhe.printStackTrace();
33           return null;
34         }
35         byte serverIP[] = localInetAddress.getAddress();
36         hexServerIP_ = hexFormat(getInt(serverIP), 8);
37       }
38       String JavaDoc hashcode = hexFormat(System.identityHashCode(o), 8);
39       tmpBuffer.append(hexServerIP_);
40       tmpBuffer.append(hashcode);
41     
42       long timeNow = System.currentTimeMillis();
43       int timeLow = (int)timeNow & 0xFFFFFFFF;
44       int node = seeder_.nextInt();
45     
46       StringBuffer JavaDoc guid = new StringBuffer JavaDoc(32);
47       guid.append(hexFormat(timeLow, 8));
48       guid.append(tmpBuffer.toString());
49       guid.append(hexFormat(node, 8));
50       return guid.toString();
51     }
52
53     private static int getInt(byte bytes[]) {
54       int i = 0;
55       int j = 24;
56       for (int k = 0; j >= 0; k++) {
57         int l = bytes[k] & 0xff;
58         i += l << j;
59         j -= 8;
60       }
61       return i;
62     }
63
64     private static String JavaDoc hexFormat(int i, int j) {
65       String JavaDoc s = Integer.toHexString(i);
66       return padHex(s, j) + s;
67     }
68
69     private static String JavaDoc padHex(String JavaDoc s, int i) {
70       StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc();
71       if (s.length() < i) {
72         for (int j = 0; j < i - s.length(); j++) {
73           tmpBuffer.append('0');
74         }
75       }
76       return tmpBuffer.toString();
77     }
78 }
Popular Tags