1 22 package org.jboss.tm; 23 24 import javax.transaction.xa.Xid ; 25 26 import org.jboss.system.server.ServerConfigUtil; 27 28 37 public class XidFactoryImpl 38 implements XidFactoryBase 39 { 40 41 50 private String baseGlobalId; 51 52 55 private long globalIdNumber = 0; 56 57 62 private boolean pad = false; 63 64 69 private String branchQualifier; 70 71 76 private byte[] baseGlobalIdBytes; 77 78 82 private byte[] branchQualifierBytes; 83 84 88 private byte[] baseBranchQualifierBytes; 89 90 private long uniqueInstanceId = System.currentTimeMillis(); 91 92 public void start() 93 { 94 String hostName = ServerConfigUtil.getSpecificBindAddress(); 95 96 baseGlobalId = hostName + ":" + uniqueInstanceId; 97 if (baseGlobalId.length() > Xid.MAXGTRIDSIZE - 15) 99 baseGlobalId = baseGlobalId.substring(0, Xid.MAXGTRIDSIZE - 15); 100 baseGlobalId = baseGlobalId + "/"; 101 baseGlobalIdBytes = baseGlobalId.getBytes(); 102 103 branchQualifier = hostName + ":" + uniqueInstanceId; 104 int len = pad ? Xid.MAXBQUALSIZE : branchQualifier.length(); 105 branchQualifierBytes = new byte[len]; 106 branchQualifier.getBytes(0, branchQualifier.length(), 109 branchQualifierBytes, 0); 110 111 String baseBranchQualifier = branchQualifier + "/"; 112 baseBranchQualifierBytes = new byte[baseBranchQualifier.length()]; 113 baseBranchQualifier.getBytes(0, baseBranchQualifier.length(), 115 baseBranchQualifierBytes, 0); 116 117 } 118 119 public long getUniqueInstanceId() 120 { 121 return uniqueInstanceId; 122 } 123 124 public void setUniqueInstanceId(long uniqueInstanceId) 125 { 126 this.uniqueInstanceId = uniqueInstanceId; 127 } 128 129 136 public String getBaseGlobalId() 137 { 138 return baseGlobalId; 139 } 140 141 147 public void setBaseGlobalId(final String baseGlobalId) 148 { 149 this.baseGlobalId = baseGlobalId; 150 baseGlobalIdBytes = baseGlobalId.getBytes(); 151 } 152 153 160 public synchronized long getGlobalIdNumber() 161 { 162 return globalIdNumber; 163 } 164 165 171 public synchronized void setGlobalIdNumber(final long globalIdNumber) 172 { 173 this.globalIdNumber = globalIdNumber; 174 } 175 176 183 public String getBranchQualifier() 184 { 185 return branchQualifier; 186 } 187 188 194 public void setBranchQualifier(final String branchQualifier) 195 { 196 this.branchQualifier = branchQualifier; 197 198 int len = pad ? Xid.MAXBQUALSIZE : branchQualifier.length(); 199 branchQualifierBytes = new byte[len]; 200 branchQualifier.getBytes(0, branchQualifier.length(), 204 branchQualifierBytes, 0); 205 206 String baseBranchQualifier = branchQualifier + "/"; 207 baseBranchQualifierBytes = new byte[baseBranchQualifier.length()]; 208 baseBranchQualifier.getBytes(0, baseBranchQualifier.length(), 210 baseBranchQualifierBytes, 0); 211 } 212 213 220 public boolean isPad() 221 { 222 return pad; 223 } 224 225 231 public void setPad(boolean pad) 232 { 233 if (this.pad != pad) 234 { 235 this.pad = pad; 236 if (branchQualifier != null) 237 { 238 int len = pad ? Xid.MAXBQUALSIZE : branchQualifier.length(); 240 branchQualifierBytes = new byte[len]; 241 branchQualifier.getBytes(0, branchQualifier.length(), 245 branchQualifierBytes, 0); 246 } 247 } 248 249 } 250 251 257 public XidImpl newXid() 258 { 259 long localId = getNextId(); 260 return new XidImpl(localIdToGlobalId(localId), 261 branchQualifierBytes, 262 (int) localId, 263 localId); 264 } 265 266 273 public XidImpl newBranch(GlobalId globalId) 274 { 275 long localId = getNextId(); 276 return new XidImpl(globalId, branchQualifierBytes, localId); 277 } 278 279 301 public XidImpl newBranch(XidImpl xid, long branchNum) 302 { 303 String id = Long.toString(branchNum); 304 int len = pad ? Xid.MAXBQUALSIZE 305 : baseBranchQualifierBytes.length + id.length(); 306 byte[] branchQualifier = new byte[len]; 307 System.arraycopy(baseBranchQualifierBytes, 0, 308 branchQualifier, 0, 309 baseBranchQualifierBytes.length); 310 id.getBytes(0, id.length(), branchQualifier, 313 baseBranchQualifierBytes.length); 314 return new XidImpl(xid, branchQualifier); 315 } 316 317 public XidImpl recreateXid(long localId) 318 { 319 return new XidImpl(localIdToGlobalId(localId), 320 branchQualifierBytes, 321 (int) localId, 322 localId); 323 } 324 325 public XidImpl recreateXid(long localId, GlobalId globalId) 326 { 327 return new XidImpl(globalId, branchQualifierBytes, localId); 328 } 329 330 336 public byte[] localIdToGlobalId(long localId) 337 { 338 String id = Long.toString(localId); 339 int len = pad ? Xid.MAXGTRIDSIZE : id.length() + baseGlobalIdBytes.length; 340 byte[] globalId = new byte[len]; 341 System.arraycopy(baseGlobalIdBytes, 0, globalId, 0, baseGlobalIdBytes.length); 342 id.getBytes(0, id.length(), globalId, baseGlobalIdBytes.length); 345 return globalId; 346 } 347 348 355 public long extractLocalIdFrom(byte[] globalId) 356 { 357 int i, start; 358 int len = globalId.length; 359 360 for (i = 0; globalId[i++] != (byte) '/';) 361 ; 362 start = i; 363 while (i < len && globalId[i] != 0) 364 i++; 365 String globalIdNumber = new String (globalId, 0, start, i - start); 366 return Long.parseLong(globalIdNumber); 367 } 368 369 374 public String getBaseBranchQualifier(byte[] branchQualifier) 375 { 376 int len = branchQualifier.length; 377 int i = 0; 378 379 while (branchQualifier[i] != (byte) '/' 380 && branchQualifier[i] != (byte) 0 381 && i < len) 382 i++; 383 384 String base = new String (branchQualifier, 0, 0, i); 385 return base; 386 } 387 388 395 public String toString(Xid xid) 396 { 397 return XidImpl.toString(xid); 398 } 399 400 private synchronized long getNextId() 401 { 402 return ++globalIdNumber; 403 } 404 405 } 406 | Popular Tags |