1 23 24 29 30 package com.sun.enterprise.util.uuid; 31 32 import java.net.InetAddress ; 33 34 import java.rmi.server.UID ; 35 36 import java.security.SecureRandom ; 37 38 43 public class UuidUtil 44 { 45 46 static final String _inetAddr = initInetAddr(); 47 48 public static String generateUuidMM() { 50 return new StringBuffer (new UID ().toString()).reverse().append(':').append(_inetAddr).toString(); 51 } 52 53 public static String generateUuid() { 55 return generateUuid(new Object ()); 56 } 57 58 public static String generateUuid(Object obj) { 61 62 long presentTime = System.currentTimeMillis(); 64 int presentTimeLow = (int) presentTime & 0xFFFFFFFF; 65 String presentTimeStringLow = formatHexString(presentTimeLow); 66 67 String nextRandom = getNextRandomString(); 69 70 StringBuffer sb = new StringBuffer (50); 71 sb.append(presentTimeStringLow); 72 sb.append(getIdentityHashCode(obj)); 74 sb.append(addRandomTo(_inetAddr)); 77 sb.append(getNextRandomString()); 79 return sb.toString(); 80 } 81 82 90 private static String initInetAddr() { 91 92 try { 93 byte[] bytes = InetAddress.getLocalHost().getAddress(); 94 StringBuffer b = new StringBuffer (); 95 String 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 ex) { 109 return "a48eb993"; 111 } 113 } 114 115 private static String addRandomTo(String hexString) 116 { 117 long hexAsLong = convertToLong(hexString); 118 int nextRandom = getNextInt(); 119 long resultInt = hexAsLong + nextRandom; 120 String result = Long.toHexString(resultInt); 121 return result; 122 } 123 124 132 private static String getIdentityHashCode(Object obj) { 133 134 String result = null; 135 try { 136 int hc = System.identityHashCode(obj); 137 return formatHexString(hc); 138 139 } catch (Exception ex) { 140 return "8AF5182"; 143 } 144 } 145 146 private static String formatHexString(int inputInt) 147 { 148 String result = null; 149 String 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 getNextRandomString() { 164 int nextInt = getNextInt(); 165 return formatHexString(nextInt); 166 } 167 168 private static long convertToLong(String hexString) 169 { 170 long result = 0; 171 try 172 { 173 result = (Long.valueOf(hexString, 16)).longValue(); 174 } catch (NumberFormatException ex) { 175 } 176 return result; 177 } 178 179 private static SecureRandom _seeder = new SecureRandom (); 180 181 189 public static void main(String [] args) { 190 System.out.println(UuidUtil.generateUuidMM()); 191 System.out.println(UuidUtil.generateUuid()); 192 System.out.println(UuidUtil.generateUuid(new Object ())); 193 } 194 } 195 196 197 198 | Popular Tags |