1 6 7 package org.jfox.tm; 8 9 import java.io.Serializable ; 10 import java.net.InetAddress ; 11 import java.net.UnknownHostException ; 12 import javax.transaction.xa.Xid ; 13 14 17 18 public class XidImpl implements Xid , Serializable { 19 private static volatile int hash = 1; 20 21 public static final int FORMAT_ID = 0x0808; 22 23 private byte[] globalId = new byte[Xid.MAXGTRIDSIZE]; 25 private byte[] branchId = new byte[Xid.MAXBQUALSIZE]; 26 27 private static String host; 28 29 static { 30 try { 31 InetAddress local = InetAddress.getLocalHost(); 32 host = local.getHostName() + "-" + local.getHostAddress(); 33 } 34 catch(UnknownHostException e) { 35 host = "localhost-0.0.0.0"; 36 } 37 } 38 39 public XidImpl() { 40 String xid = new StringBuffer ().append(host).append("-").append(hash++).toString(); 41 if(xid.length() > Xid.MAXBQUALSIZE) { 42 xid = xid.substring(xid.length() - Xid.MAXBQUALSIZE); 43 } 44 45 byte[] _globalId = xid.getBytes(); 46 if(_globalId.length > Xid.MAXGTRIDSIZE) { 47 System.arraycopy(_globalId, 0, globalId, _globalId.length - Xid.MAXGTRIDSIZE, Xid.MAXGTRIDSIZE); 48 } 49 else { 50 System.arraycopy(xid.getBytes(), 0, globalId, 0, xid.getBytes().length); 51 } 52 } 53 54 private XidImpl(byte[] _globalId, byte[] _branchId) { 55 if(_globalId.length > Xid.MAXGTRIDSIZE) { 56 System.arraycopy(_globalId, 0, globalId, _globalId.length - Xid.MAXGTRIDSIZE, Xid.MAXGTRIDSIZE); 57 } 58 else { 59 System.arraycopy(_globalId, 0, globalId, 0, _globalId.length); 60 } 61 if(_branchId.length > Xid.MAXBQUALSIZE) { 62 System.arraycopy(_branchId, 0, branchId, _branchId.length - Xid.MAXBQUALSIZE, Xid.MAXBQUALSIZE); 63 } 64 else { 65 System.arraycopy(_branchId, 0, branchId, 0, _branchId.length); 66 } 67 68 } 69 70 public static Xid createBranchXid(Xid xid, int branchId) { 71 return new XidImpl(xid.getGlobalTransactionId(), Integer.toString(branchId).getBytes()); 72 } 73 74 public int getFormatId() { 75 return FORMAT_ID; 76 } 77 78 public byte[] getBranchQualifier() { 79 return branchId; 80 } 81 82 public byte[] getGlobalTransactionId() { 83 return globalId; 84 } 85 86 public int hashCode() { 87 return globalId.hashCode() + branchId.hashCode(); 88 } 89 90 public String toString() { 91 return new StringBuffer ().append(new String (globalId).trim()).append(":").append(new String (branchId).trim()).toString(); 92 } 93 94 public boolean equals(Object obj) { 95 if(!(obj instanceof XidImpl)) return false; 96 97 XidImpl xid2 = (XidImpl) obj; 98 if(FORMAT_ID == xid2.getFormatId() 99 && java.util.Arrays.equals(branchId, xid2.getBranchQualifier()) 100 && java.util.Arrays.equals(globalId, xid2.getGlobalTransactionId())) { 101 return true; 102 } 103 else { 104 return false; 105 } 106 } 107 108 public static void main(String [] args) { 109 Xid xid = new XidImpl(); 110 Xid xid1 = (XidImpl.createBranchXid(xid, 100)); 111 Xid xid2 = (XidImpl.createBranchXid(xid, 100)); 112 System.out.println(xid); 113 System.out.println(xid1); 114 System.out.println(xid1.equals(xid2)); 115 } 116 } 117 118 | Popular Tags |