1 18 package org.apache.activemq.command; 19 20 import java.util.Arrays ; 21 22 import javax.transaction.xa.Xid ; 23 24 import org.apache.activemq.util.HexSupport; 25 26 27 31 public class XATransactionId extends TransactionId implements Xid { 32 33 public static final byte DATA_STRUCTURE_TYPE=CommandTypes.ACTIVEMQ_XA_TRANSACTION_ID; 34 35 private int formatId; 36 private byte[] branchQualifier; 37 private byte[] globalTransactionId; 38 39 private transient int hash; 40 41 public XATransactionId() { 42 } 43 44 public XATransactionId(Xid xid) { 45 this.formatId = xid.getFormatId(); 46 this.globalTransactionId = xid.getGlobalTransactionId(); 47 this.branchQualifier = xid.getBranchQualifier(); 48 } 49 50 public byte getDataStructureType() { 51 return DATA_STRUCTURE_TYPE; 52 } 53 54 public String getTransactionKey() { 55 return "XID:"+formatId+":"+HexSupport.toHexFromBytes(globalTransactionId)+":"+HexSupport.toHexFromBytes(branchQualifier); 56 } 57 58 public String toString() { 59 return getTransactionKey(); 60 } 61 62 public boolean isXATransaction() { 63 return true; 64 } 65 66 public boolean isLocalTransaction() { 67 return false; 68 } 69 70 73 public int getFormatId() { 74 return formatId; 75 } 76 77 80 public byte[] getGlobalTransactionId() { 81 return globalTransactionId; 82 } 83 84 87 public byte[] getBranchQualifier() { 88 return branchQualifier; 89 } 90 91 public void setBranchQualifier(byte[] branchQualifier) { 92 this.branchQualifier = branchQualifier; 93 this.hash=0; 94 } 95 96 public void setFormatId(int formatId) { 97 this.formatId = formatId; 98 this.hash=0; 99 } 100 101 public void setGlobalTransactionId(byte[] globalTransactionId) { 102 this.globalTransactionId = globalTransactionId; 103 this.hash=0; 104 } 105 106 public int hashCode() { 107 if( hash==0 ) { 108 hash = formatId; 109 hash = hash(globalTransactionId, hash); 110 hash = hash(branchQualifier, hash); 111 if (hash == 0) { 112 hash = 0xaceace; 113 } 114 } 115 return hash; 116 } 117 118 private static int hash(byte[] bytes, int hash) { 119 for (int i = 0, size = bytes.length; i < size; i++) { 120 hash ^= bytes[i] << ((i % 4) * 8); 121 } 122 return hash; 123 } 124 125 public boolean equals(Object o) { 126 if( o==null || o.getClass()!=XATransactionId.class ) 127 return false; 128 XATransactionId xid = (XATransactionId)o; 129 return xid.formatId==formatId && Arrays.equals(xid.globalTransactionId,globalTransactionId) 130 && Arrays.equals(xid.branchQualifier, branchQualifier); 131 } 132 133 } 134 | Popular Tags |