1 5 package org.exoplatform.commons.utils; 6 7 import java.net.InetAddress ; 8 import java.security.SecureRandom ; 9 15 public class IdentifierUtil { 16 17 private static String hexServerIP_ = null; 18 private static final SecureRandom seeder_ = new SecureRandom (); 19 20 static public String generateUUID(Object o) { 22 StringBuffer tmpBuffer = new StringBuffer (16); 23 if (hexServerIP_ == null) { 24 InetAddress localInetAddress = null; 25 try { 26 localInetAddress = InetAddress.getLocalHost(); 28 } 29 catch (java.net.UnknownHostException uhe) { 30 System.err.println("ContentSetUtil: Could not get the local IP address using InetAddress.getLocalHost()!"); 31 uhe.printStackTrace(); 33 return null; 34 } 35 byte serverIP[] = localInetAddress.getAddress(); 36 hexServerIP_ = hexFormat(getInt(serverIP), 8); 37 } 38 String 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 guid = new StringBuffer (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 hexFormat(int i, int j) { 65 String s = Integer.toHexString(i); 66 return padHex(s, j) + s; 67 } 68 69 private static String padHex(String s, int i) { 70 StringBuffer tmpBuffer = new StringBuffer (); 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 |