1 23 24 28 50 package com.sun.jts.CosTransactions; 51 52 54 import java.io.*; 55 56 import org.omg.CosTransactions.*; 57 import com.sun.jts.trace.*; 58 59 60 import java.util.logging.Logger ; 61 import java.util.logging.Level ; 62 import com.sun.logging.LogDomains; 63 import com.sun.jts.utils.LogFormatter; 64 65 74 public class GlobalTID extends Object { 81 static GlobalTID NullGlobalTID = new GlobalTID(-1,-1,null); 82 83 otid_t realTID = null; 84 85 private String stringForm = null; 86 private int hashCode = 0; 87 private boolean hashed = false; 88 91 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 92 93 101 public GlobalTID( otid_t otherTID ) { 102 realTID = otherTID; 103 } 104 105 115 GlobalTID( int formatID, 116 int bqual_length, 117 byte[] tid ) { 118 realTID = new otid_t(formatID, bqual_length, tid); 119 } 120 121 126 public GlobalTID(javax.transaction.xa.Xid xid) { 127 128 int glen = xid.getGlobalTransactionId().length; 129 int blen = xid.getBranchQualifier().length; 130 byte[] xidRep = new byte[glen + blen]; 131 132 System.arraycopy(xid.getGlobalTransactionId(), 0, xidRep, 0, glen); 133 System.arraycopy(xid.getBranchQualifier(), 0, xidRep, glen, blen); 134 135 realTID = new otid_t(xid.getFormatId(), blen, xidRep); 136 } 137 138 146 GlobalTID( DataInputStream dataIn ) { 147 try { 148 int formatID = dataIn.readInt(); 149 int bqualLength = dataIn.readInt(); 150 int bufferlen = dataIn.readUnsignedShort(); 151 byte[] tid = new byte[bufferlen]; 152 dataIn.read(tid); 153 154 realTID = new otid_t(formatID,bqualLength,tid); 155 } catch( Throwable exc ) {} 156 } 157 158 166 GlobalTID( byte[] bytes ) { 167 int formatID = (bytes[0]&255) + 168 ((bytes[1]&255) << 8) + 169 ((bytes[2]&255) << 16) + 170 ((bytes[3]&255) << 24); 171 int bqualLength = (bytes[4]&255) + 172 ((bytes[5]&255) << 8) + 173 ((bytes[6]&255) << 16) + 174 ((bytes[7]&255) << 24); 175 byte[] tid = new byte[bytes.length-8]; 176 System.arraycopy(bytes,8,tid,0,tid.length); 177 178 realTID = new otid_t(formatID,bqualLength,tid); 179 } 180 181 189 final GlobalTID copy() { 190 GlobalTID result = new GlobalTID(realTID); 191 result.hashed = hashed; 192 result.hashCode = hashCode; 193 result.stringForm = stringForm; 194 195 return result; 196 } 197 198 207 final boolean isNull() { 208 return realTID.formatID == -1; 209 } 210 211 219 public final boolean equals( Object other ) { 220 otid_t otherTID = null; 221 222 if( other == null ) 223 return false; 224 else if( other instanceof otid_t ) 225 otherTID = (otid_t)other; 226 else if( other instanceof GlobalTID ) 227 otherTID = ((GlobalTID)other).realTID; 228 else 229 return false; 230 231 boolean result = false; 232 233 235 if( realTID == otherTID ) return true; 236 237 239 if( realTID.formatID != otherTID.formatID ) return false; 240 241 243 int firstGTRID = realTID.tid.length - realTID.bqual_length; 244 int secondGTRID = otherTID.tid.length - otherTID.bqual_length; 245 246 248 if( firstGTRID != secondGTRID ) 249 return false; 250 251 253 result = true; 254 for( int pos = 0; pos < firstGTRID && result; pos++ ) 255 result = (realTID.tid[pos] == otherTID.tid[pos] ); 256 257 return result; 258 } 259 260 268 269 public final int hashCode() { 270 271 273 if( hashed ) 274 return hashCode; 275 276 hashCode = 0; 277 278 280 if( realTID.tid != null ) 281 for( int pos = 0; pos < realTID.tid.length; pos++ ) 282 hashCode += realTID.tid[pos]; 283 284 286 hashCode += realTID.formatID + realTID.bqual_length; 287 288 290 hashCode *= 0x71824361; 291 292 hashed = true; 293 294 return hashCode; 295 } 296 297 public GlobalTID(String stid){ 298 if(stid==null){ 300 return ; 301 } 302 303 if(stid.equals("[NULL ID]")){ 305 realTID.formatID=-1; 306 return; 307 } 308 if(_logger.isLoggable(Level.FINEST)) 309 _logger.logp(Level.FINEST,"GlobalTID","GlobalTID(String)", 310 "Tid is: "+stid); 311 312 char [] ctid =stid.toCharArray(); 314 315 int colon=stid.indexOf(":"); 316 317 int globalLen=0; 319 int bqualLen=0; 320 if(colon==-1){ 321 globalLen=ctid.length-2; 323 } 324 else{ 325 globalLen=colon-1; 326 bqualLen=ctid.length -3 - globalLen; 327 } 328 329 if( (globalLen%2!=0) || (bqualLen%2 !=0)){ 330 if(_logger.isLoggable(Level.FINEST)){ 331 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)", 332 "Corrupted gtid string , total length is not integral"); 333 } 334 throw new RuntimeException ("invalid global tid"); 335 } 336 337 338 byte [] b=new byte[(globalLen+bqualLen)/2]; 339 int index=1; 340 int bIndex=0; 341 342 while(bIndex<b.length){ 344 345 int t=ctid[index++]; 346 int t1=ctid[index++]; 347 if(_logger.isLoggable(Level.FINEST)) 348 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)", 349 "Index is : "+bIndex+" value of t,t1 is : "+t+","+t1); 350 if( t >= 'A'){ 351 t = t - 'A'+10; 352 } 353 else{ 354 t=t-'0'; 355 } 356 if( t1 >= 'A'){ 357 t1 = t1 - 'A'+10; 358 } 359 else{ 360 t1=t1-'0'; 361 } 362 if(_logger.isLoggable(Level.FINEST)) 363 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)", 364 " Value of t,t1 is : "+t+","+t1); 365 t=t<<4; 366 if(_logger.isLoggable(Level.FINEST)) 367 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)", 368 "Value of t is : "+t); 369 t=t|t1; 370 371 if(_logger.isLoggable(Level.FINEST)) 372 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)", 373 " Value of t is : "+t); 374 b[bIndex++] = (byte)t; 375 if(_logger.isLoggable(Level.FINEST)) 376 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)", 377 "Value of t is : "+(byte)t); 378 } 379 380 realTID = new otid_t(TransactionState.XID_FORMAT_ID,bqualLen/2,b); 381 if(_logger.isLoggable(Level.FINEST)) 382 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)", 383 "created gtid : "+this); 384 } 385 386 387 395 396 public final String toString() { 397 398 400 if( realTID.formatID == -1 ) 401 return "[NULL ID]"; 402 403 406 if( stringForm != null ) return stringForm; 407 408 410 char[] buff = new char[realTID.tid.length*2 + (realTID.bqual_length>0?1:0)]; 412 int pos = 0; 413 415 417 int globalLen = realTID.tid.length - realTID.bqual_length; 418 for( int i = 0; i < globalLen; i++ ) { 419 int currCharHigh = (realTID.tid[i]&0xf0) >> 4; 420 int currCharLow = realTID.tid[i]&0x0f; 421 buff[pos++] = (char)(currCharHigh + (currCharHigh > 9 ? 'A'-10 : '0')); 422 buff[pos++] = (char)(currCharLow + (currCharLow > 9 ? 'A'-10 : '0')); 423 } 424 425 if( realTID.bqual_length > 0 ) { 426 buff[pos++] = '_'; 428 for( int i = 0; i < realTID.bqual_length; i++ ) { 429 int currCharHigh = (realTID.tid[i+globalLen]&0xf0) >> 4; 430 int currCharLow = realTID.tid[i+globalLen]&0x0f; 431 buff[pos++] = (char)(currCharHigh + (currCharHigh > 9 ? 'A'-10 : '0')); 432 buff[pos++] = (char)(currCharLow + (currCharLow > 9 ? 'A'-10 : '0')); 433 } 434 } 435 437 439 stringForm = new String (buff); 440 441 return stringForm; 442 } 443 444 452 final byte[] toBytes() { 453 if( realTID.formatID == -1 ) 454 return null; 455 456 byte[] result = new byte[realTID.tid.length + 8]; 457 458 result[0] = (byte) realTID.formatID; 459 result[1] = (byte)(realTID.formatID >> 8); 460 result[2] = (byte)(realTID.formatID >> 16); 461 result[3] = (byte)(realTID.formatID >> 24); 462 result[4] = (byte) realTID.bqual_length; 463 result[5] = (byte)(realTID.bqual_length >> 8); 464 result[6] = (byte)(realTID.bqual_length >> 16); 465 result[7] = (byte)(realTID.bqual_length >> 24); 466 467 System.arraycopy(realTID.tid,0,result,8,realTID.tid.length); 468 469 return result; 470 } 471 472 final byte[] toTidBytes() { 473 return realTID.tid; 474 } 475 476 static GlobalTID fromTIDBytes(byte[] bytes) { 477 return new GlobalTID(TransactionState.XID_FORMAT_ID, 0, bytes); 478 } 479 480 481 489 final void write( DataOutputStream dataOut ) { 490 try { 491 dataOut.writeInt(realTID.formatID); 492 dataOut.writeInt(realTID.bqual_length); 493 dataOut.writeShort(realTID.tid.length); 494 dataOut.write(realTID.tid,0,realTID.tid.length); 495 } catch( Throwable exc ) {} 496 } 497 } 498 | Popular Tags |