1 22 package org.jboss.tm; 23 24 import javax.transaction.xa.Xid ; 25 26 36 public class XidImpl 37 implements Xid , java.io.Serializable 38 { 39 41 static final long serialVersionUID = -4175838107150528488L; 42 43 public static final int JBOSS_FORMAT_ID = 0x0101; 44 45 47 private static boolean trulyGlobalIdsEnabled = false; 48 49 51 55 private final int formatId; 56 57 63 private final byte[] globalId; 64 65 69 private final byte[] branchId; 70 71 74 private final int hash; 75 76 80 private final long localId; 81 82 86 private final GlobalId trulyGlobalId; 87 88 89 91 94 public static void setTrulyGlobalIdsEnabled(boolean newValue) 95 { 96 trulyGlobalIdsEnabled = newValue; 97 } 98 99 102 public static boolean getTrulyGlobalIdsEnabled() 103 { 104 return trulyGlobalIdsEnabled; 105 } 106 107 110 public static String toString(Xid id) 111 { 112 if (id == null) 113 return "[NULL Xid]"; 114 115 String s = id.getClass().getName(); 116 s = s.substring(s.lastIndexOf('.') + 1); 117 s = s + "[FormatId=" + id.getFormatId() 118 + ", GlobalId=" + new String (id.getGlobalTransactionId()).trim() 119 + ", BranchQual=" + new String (id.getBranchQualifier()).trim() 120 + ((id instanceof XidImpl) 121 ? ", localId=" + LocalId.toString(((XidImpl)id).localId ) 122 : "") 123 + "]"; 124 125 return s; 126 } 127 128 130 133 public XidImpl(int formatId, 134 byte[] globalId, byte[] branchId, int hash, long localId) 135 { 136 this.formatId = formatId; 137 this.globalId = globalId; 138 this.branchId = branchId; 139 this.hash = hash; 140 this.localId = localId; 141 this.trulyGlobalId = (trulyGlobalIdsEnabled) 142 ? new GlobalId(formatId, globalId) 143 : null; 144 } 145 146 149 XidImpl(byte[] globalId, byte[] branchId, int hash, long localId) 150 { 151 this.formatId = JBOSS_FORMAT_ID; 152 this.globalId = globalId; 153 this.branchId = branchId; 154 this.hash = hash; 155 this.localId = localId; 156 this.trulyGlobalId = (trulyGlobalIdsEnabled) 157 ? new GlobalId(JBOSS_FORMAT_ID, globalId, hash) 158 : null; 159 } 160 161 168 public XidImpl(final XidImpl xidImpl, final byte[] branchId) 169 { 170 this.formatId = xidImpl.formatId; 171 this.globalId = xidImpl.globalId; this.branchId = branchId; 173 this.hash = xidImpl.hash; 174 this.localId = xidImpl.localId; 175 this.trulyGlobalId = (trulyGlobalIdsEnabled) 176 ? xidImpl.trulyGlobalId 177 : null; 178 } 179 180 189 public XidImpl(final GlobalId globalId, final byte[] branchId, long localId) 190 { 191 this.formatId = globalId.getFormatId(); 192 this.globalId = globalId.getGlobalTransactionId(); 193 this.branchId = branchId; 194 this.hash = globalId.hashCode(); 195 this.localId = localId; 196 this.trulyGlobalId = (trulyGlobalIdsEnabled) 197 ? globalId 198 : null; 199 } 200 201 203 205 208 public byte[] getGlobalTransactionId() 209 { 210 return (byte[])globalId.clone(); 211 } 212 213 216 public byte[] getBranchQualifier() 217 { 218 if (branchId.length == 0) 219 return branchId; else 221 return (byte[])branchId.clone(); 222 } 223 224 230 public int getFormatId() 231 { 232 244 return formatId; 245 } 246 247 254 public boolean equals(Object obj) 255 { 256 if(obj==this) 257 return true; 258 if (obj instanceof XidImpl) { 259 XidImpl other = (XidImpl)obj; 260 261 if (formatId != other.formatId || 262 globalId.length != other.globalId.length || 263 branchId.length != other.branchId.length) 264 return false; 265 266 for (int i = 0; i < globalId.length; ++i) 267 if (globalId[i] != other.globalId[i]) 268 return false; 269 270 for (int i = 0; i < branchId.length; ++i) 271 if (branchId[i] != other.branchId[i]) 272 return false; 273 274 return true; 275 } 276 return false; 277 } 278 279 public int hashCode() 280 { 281 return hash; 282 } 283 284 public String toString() 285 { 286 return toString(this); 287 } 288 289 291 295 public long getLocalIdValue() 296 { 297 return localId; 298 } 299 300 304 public LocalId getLocalId() 305 { 306 return new LocalId(localId); 307 } 308 309 313 public GlobalId getTrulyGlobalId() 314 { 315 return trulyGlobalId; 316 } 317 318 324 public boolean sameTransaction(XidImpl other) 325 { 326 if(other == this) 327 return true; 328 if (formatId != other.formatId || 329 globalId.length != other.globalId.length) 330 return false; 331 332 for (int i = 0; i < globalId.length; ++i) 333 if (globalId[i] != other.globalId[i]) 334 return false; 335 336 return true; 337 } 338 339 345 public byte[] getInternalGlobalTransactionId() 346 { 347 return globalId; 348 } 349 350 } 351 352 | Popular Tags |