1 43 package org.objectweb.jotm; 44 45 import java.io.Serializable ; 46 import java.nio.ByteBuffer ; 47 import java.security.SecureRandom ; 48 import javax.transaction.xa.Xid ; 49 50 69 public class JavaXidImpl implements Xid, Serializable { 70 71 public static final int JOTM_FORMAT_ID = 0xBB14; 72 73 private static SecureRandom rand = null; private final byte internalVersId = 1; 76 private static int count = 1; 77 private static long uuid0; 78 private static long uuid1; 79 private static boolean uuidsRecovered = false; 80 private static byte[] gtrid_base = null; private static String host, server; 82 83 private String fullString = ""; 84 private String shortString = ""; 85 86 private boolean hashcodevalid = false; 87 private int myhashcode; 88 89 static String HexDigits[] = { 90 "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", 91 "0a", "0b", "0c", "0d", "0e", "0f", 92 "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 93 "1a", "1b", "1c", "1d", "1e", "1f", 94 "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 95 "2a", "2b", "2c", "2d", "2e", "2f", 96 "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", 97 "3a", "3b", "3c", "3d", "3e", "3f", 98 "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", 99 "4a", "4b", "4c", "4d", "4e", "4f", 100 "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", 101 "5a", "5b", "5c", "5d", "5e", "5f", 102 "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", 103 "6a", "6b", "6c", "6d", "6e", "6f", 104 "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", 105 "7a", "7b", "7c", "7d", "7e", "7f", 106 "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", 107 "8a", "8b", "8c", "8d", "8e", "8f", 108 "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", 109 "9a", "9b", "9c", "9d", "9e", "9f", 110 "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", 111 "aa", "ab", "ac", "ad", "ae", "af", 112 "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", 113 "ba", "bb", "bc", "bd", "be", "bf", 114 "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", 115 "ca", "cb", "cc", "cd", "ce", "cf", 116 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", 117 "da", "db", "dc", "dd", "de", "df", 118 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", 119 "ea", "eb", "ec", "ed", "ee", "ef", 120 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", 121 "fa", "fb", "fc", "fd", "fe", "ff" 122 }; 123 124 128 private int formatId; 129 130 134 private int gtrid_length; 135 136 140 private int bqual_length; 141 142 146 private byte[] gtrid; 147 148 152 private byte[] bqual; 153 154 158 161 public JavaXidImpl(org.objectweb.jotm.Xid jotmXid ) { 162 if (TraceTm.jotm.isDebugEnabled()) { 163 TraceTm.jotm.debug("jotmXID= " + jotmXid); 164 } 165 166 formatId = jotmXid.getFormatId(); 167 gtrid = jotmXid.getGlobalTransactionId(); 168 gtrid_length = gtrid.length; 169 bqual = jotmXid.getBranchQualifier(); 170 bqual_length = bqual.length; 171 } 172 173 177 180 public int getFormatId() { 181 return formatId; 182 } 183 184 187 public byte[] getGlobalTransactionId() { 188 return gtrid; 189 } 190 191 194 public byte[] getBranchQualifier() { 195 return bqual; 196 } 197 198 202 205 static final void byteToHex( byte inbyte, StringBuffer str_buff ) { 206 207 int myByte = 0xFF & inbyte; 208 209 str_buff.append( HexDigits[myByte] ); 210 return; 211 } 212 213 217 public String toString() { 218 return this.toString( false ); 219 } 220 221 public String toString( boolean Full ) { 222 223 byte[] gtrid_local = null; 224 byte[] bqual_local = null; 225 226 if (Full && (fullString.length() != 0) ) { 227 return fullString; 228 } else if (!Full && (shortString.length() != 0) ) { 229 return shortString; 230 } 231 232 StringBuffer str_buff_gtrid = new StringBuffer (MAXGTRIDSIZE * 2); 234 StringBuffer str_buff_bqual = new StringBuffer (MAXBQUALSIZE * 2); 235 236 gtrid_local = new byte[MAXGTRIDSIZE]; 237 ByteBuffer aa = ByteBuffer.wrap(gtrid_local); 238 239 System.arraycopy(gtrid, 0, gtrid_local, 0, gtrid_length); 240 241 for (int i=0; i < gtrid_length; i++) { 242 byteToHex(aa.get(), str_buff_gtrid ); 243 } 244 245 bqual_local = new byte[MAXBQUALSIZE]; 246 ByteBuffer bb = ByteBuffer.wrap(bqual_local); 247 248 if (bqual != null) { 249 System.arraycopy(bqual, 0, bqual_local, 0, bqual_length); 250 251 for (int i=0; i < bqual_length; i++) { 252 byteToHex(bb.get(), str_buff_bqual); 253 } 254 } 255 256 if ((gtrid_length > 30) && !Full ) { int strlen = str_buff_gtrid.length(); 258 str_buff_gtrid.replace( (strlen / 6), (strlen / 6) + 2, "..."); 259 str_buff_gtrid.delete( (strlen / 6) + 3, strlen - 5); 260 } 261 262 if ((bqual_length > 30) && !Full ) { int strlen = str_buff_bqual.length(); 264 str_buff_bqual.replace( (strlen / 6), (strlen / 6) + 2, "..."); 265 str_buff_bqual.delete( (strlen / 6) + 3, strlen - 5); 266 } 267 268 if (Full) { 269 fullString = Long.toHexString(formatId) + ":" + 270 Long.toHexString(gtrid_length) + ":" + 271 Long.toHexString(bqual_length) + ":" + 272 str_buff_gtrid.toString() + ":" + 273 str_buff_bqual.toString(); 274 return fullString; 275 } 276 277 shortString = Long.toHexString(formatId) + ":" + 278 Long.toHexString(gtrid_length) + ":" + 279 Long.toHexString(bqual_length) + ":" + 280 str_buff_gtrid.toString() + ":" + 281 str_buff_bqual.toString(); 282 return shortString; 283 } 284 285 305 306 private byte[] makeGtrid() { 307 makeGtridBase(); 308 long uniqueTimeStamp; 309 310 synchronized( getClass() ) { 311 uniqueTimeStamp = System.currentTimeMillis() * 1024 + count; 312 count++; 313 } 314 315 ByteBuffer bb = ByteBuffer.allocate(gtrid_base.length+8); 316 bb.put(gtrid_base); 317 bb.putLong(uniqueTimeStamp); 318 return bb.array(); 319 } 320 321 private void makeGtridBase() { 322 326 synchronized( getClass() ) { 327 if (rand == null) { 328 rand = new SecureRandom (); 329 330 if (uuidsRecovered == false) { uuid0 = rand.nextLong(); 334 uuid1 = rand.nextLong(); 335 336 350 byte [] UniqueID = new byte[3+8+8]; 351 byte [] [] UniqueIDRecord = new byte [1][3+8+8]; 352 353 String rt1 = "RU1"; 354 355 ByteBuffer rr1 = ByteBuffer.wrap(UniqueID); 356 rr1.put(rt1.getBytes()); 357 rr1.putLong(uuid0); 358 rr1.putLong(uuid1); 359 360 UniqueIDRecord [0] = UniqueID; 361 } 362 363 host = ""; 364 server = ""; 365 host = (host+" ").substring(0,15); 367 server = (server+" ").substring(0,14); 368 gtrid_base = new byte[1+8+8+16+15]; 369 ByteBuffer bb = ByteBuffer.wrap(gtrid_base); 370 bb.put(internalVersId); 371 bb.putLong(uuid0); 372 bb.putLong(uuid1); 373 bb.put(host.getBytes()); 374 bb.put(server.getBytes()); 375 } 376 } 377 } 378 379 383 386 public boolean equals( Object obj2 ) { 387 388 JavaXidImpl xid2 = (JavaXidImpl) obj2; 389 390 if (formatId == xid2.getFormatId() 391 && java.util.Arrays.equals(bqual, xid2.getBranchQualifier()) 392 && java.util.Arrays.equals(gtrid, xid2.getGlobalTransactionId())) { 393 return true; 394 } else { 395 return false; 396 } 397 } 398 399 402 public int hashCode() { 403 404 int hc = 0; 405 406 if (hashcodevalid == false) { 407 408 for (int i = 0; i < gtrid.length; i++) { 409 hc = hc * 37 + gtrid[i]; 410 } 411 412 for (int i = 0; i < bqual.length; i++ ) { 413 hc = hc * 37 + bqual[i]; 414 } 415 416 myhashcode = hc; 417 hashcodevalid = true; 418 } 419 return myhashcode; 420 } 421 } 422 | Popular Tags |