1 55 56 62 63 package org.jboss.axis.components.uuid; 64 65 import java.math.BigInteger ; 66 import java.security.SecureRandom ; 67 import java.util.Random ; 68 69 78 public class SimpleUUIDGen implements UUIDGen 79 { 80 private static final BigInteger countStart = new BigInteger ("-12219292800000"); private static final int clock_sequence = (new Random ()).nextInt(16384); 82 83 92 public String nextUUID() 93 { 94 97 BigInteger count; 99 100 BigInteger current = BigInteger.valueOf(System.currentTimeMillis()); 102 103 BigInteger countMillis = current.subtract(countStart); 105 106 count = countMillis.multiply(BigInteger.valueOf(10000)); 108 109 String bitString = count.toString(2); 110 if (bitString.length() < 60) 111 { 112 int nbExtraZeros = 60 - bitString.length(); 113 String extraZeros = new String (); 114 for (int i = 0; i < nbExtraZeros; i++) 115 extraZeros = extraZeros.concat("0"); 116 117 bitString = extraZeros.concat(bitString); 118 } 119 120 byte[] bits = bitString.getBytes(); 121 122 byte[] time_low = new byte[32]; 124 for (int i = 0; i < 32; i++) 125 time_low[i] = bits[bits.length - i - 1]; 126 127 byte[] time_mid = new byte[16]; 129 for (int i = 0; i < 16; i++) 130 time_mid[i] = bits[bits.length - 32 - i - 1]; 131 132 byte[] time_hi_and_version = new byte[16]; 134 for (int i = 0; i < 12; i++) 135 time_hi_and_version[i] = bits[bits.length - 48 - i - 1]; 136 137 time_hi_and_version[12] = ((new String ("1")).getBytes())[0]; 138 time_hi_and_version[13] = ((new String ("0")).getBytes())[0]; 139 time_hi_and_version[14] = ((new String ("0")).getBytes())[0]; 140 time_hi_and_version[15] = ((new String ("0")).getBytes())[0]; 141 142 BigInteger clockSequence = BigInteger.valueOf(clock_sequence); 144 String clockString = clockSequence.toString(2); 145 if (clockString.length() < 14) 146 { 147 int nbExtraZeros = 14 - bitString.length(); 148 String extraZeros = new String (); 149 for (int i = 0; i < nbExtraZeros; i++) 150 extraZeros = extraZeros.concat("0"); 151 152 clockString = extraZeros.concat(bitString); 153 } 154 155 byte[] clock_bits = clockString.getBytes(); 156 byte[] clock_seq_low = new byte[8]; 157 for (int i = 0; i < 8; i++) 158 clock_seq_low[i] = clock_bits[clock_bits.length - i - 1]; 159 160 byte[] clock_seq_hi_and_reserved = new byte[8]; 162 for (int i = 0; i < 6; i++) 163 clock_seq_hi_and_reserved[i] = clock_bits[clock_bits.length - 8 - i - 1]; 164 165 clock_seq_hi_and_reserved[6] = ((new String ("0")).getBytes())[0]; 166 clock_seq_hi_and_reserved[7] = ((new String ("1")).getBytes())[0]; 167 168 String timeLow = Long.toHexString((new BigInteger (new String (reverseArray(time_low)), 2)).longValue()); 169 if (timeLow.length() < 8) 170 { 171 int nbExtraZeros = 8 - timeLow.length(); 172 String extraZeros = new String (); 173 for (int i = 0; i < nbExtraZeros; i++) 174 extraZeros = extraZeros.concat("0"); 175 176 timeLow = extraZeros.concat(timeLow); 177 } 178 179 String timeMid = Long.toHexString((new BigInteger (new String (reverseArray(time_mid)), 2)).longValue()); 180 if (timeMid.length() < 4) 181 { 182 int nbExtraZeros = 4 - timeMid.length(); 183 String extraZeros = new String (); 184 for (int i = 0; i < nbExtraZeros; i++) 185 extraZeros = extraZeros.concat("0"); 186 timeMid = extraZeros.concat(timeMid); 187 } 188 189 String timeHiAndVersion = Long.toHexString((new BigInteger (new String (reverseArray(time_hi_and_version)), 2)).longValue()); 190 if (timeHiAndVersion.length() < 4) 191 { 192 int nbExtraZeros = 4 - timeHiAndVersion.length(); 193 String extraZeros = new String (); 194 for (int i = 0; i < nbExtraZeros; i++) 195 extraZeros = extraZeros.concat("0"); 196 197 timeHiAndVersion = extraZeros.concat(timeHiAndVersion); 198 } 199 200 String clockSeqHiAndReserved = Long.toHexString((new BigInteger (new String (reverseArray(clock_seq_hi_and_reserved)), 2)).longValue()); 201 if (clockSeqHiAndReserved.length() < 2) 202 { 203 int nbExtraZeros = 2 - clockSeqHiAndReserved.length(); 204 String extraZeros = new String (); 205 for (int i = 0; i < nbExtraZeros; i++) 206 extraZeros = extraZeros.concat("0"); 207 208 clockSeqHiAndReserved = extraZeros.concat(clockSeqHiAndReserved); 209 } 210 211 String clockSeqLow = Long.toHexString((new BigInteger (new String (reverseArray(clock_seq_low)), 2)).longValue()); 212 if (clockSeqLow.length() < 2) 213 { 214 int nbExtraZeros = 2 - clockSeqLow.length(); 215 String extraZeros = new String (); 216 for (int i = 0; i < nbExtraZeros; i++) 217 extraZeros = extraZeros.concat("0"); 218 219 clockSeqLow = extraZeros.concat(clockSeqLow); 220 } 221 222 Random secureRandom = null; 237 try 238 { 239 secureRandom = SecureRandom.getInstance("SHA1PRNG", "SUN"); 240 } 241 catch (Exception e) 242 { 243 secureRandom = new Random (); 244 } 245 246 long nodeValue = secureRandom.nextLong(); 247 nodeValue = Math.abs(nodeValue); 248 while (nodeValue > 140737488355328L) 249 { 250 nodeValue = secureRandom.nextLong(); 251 nodeValue = Math.abs(nodeValue); 252 } 253 254 BigInteger nodeInt = BigInteger.valueOf(nodeValue); 255 String nodeString = nodeInt.toString(2); 256 if (nodeString.length() < 47) 257 { 258 int nbExtraZeros = 47 - nodeString.length(); 259 String extraZeros = new String (); 260 for (int i = 0; i < nbExtraZeros; i++) 261 extraZeros = extraZeros.concat("0"); 262 263 nodeString = extraZeros.concat(nodeString); 264 } 265 266 byte[] node_bits = nodeString.getBytes(); 267 byte[] node = new byte[48]; 268 for (int i = 0; i < 47; i++) 269 node[i] = node_bits[node_bits.length - i - 1]; 270 271 node[47] = ((new String ("1")).getBytes())[0]; 272 String theNode = Long.toHexString((new BigInteger (new String (reverseArray(node)), 2)).longValue()); 273 if (theNode.length() < 12) 274 { 275 int nbExtraZeros = 12 - theNode.length(); 276 String extraZeros = new String (); 277 for (int i = 0; i < nbExtraZeros; i++) 278 extraZeros = extraZeros.concat("0"); 279 theNode = extraZeros.concat(theNode); 280 } 281 282 String result = timeLow + "-" + timeMid + "-" + timeHiAndVersion + "-" + clockSeqHiAndReserved + clockSeqLow + "-" + theNode; 283 284 return result.toUpperCase(); 285 } 286 287 private static byte[] reverseArray(byte[] bits) 288 { 289 byte[] result = new byte[bits.length]; 290 for (int i = 0; i < result.length; i++) 291 result[i] = bits[result.length - 1 - i]; 292 293 return result; 294 } 295 296 public void destroy() 297 { 298 } 299 300 public void init() 301 { 302 } 303 } 304 | Popular Tags |