1 17 18 package org.apache.geronimo.transaction.manager; 19 20 import java.io.Serializable ; 21 import java.util.Arrays ; 22 import javax.transaction.xa.Xid ; 23 24 29 public class XidImpl implements Xid , Serializable { 30 private static int FORMAT_ID = 0x4765526f; private final int formatId; 32 private final byte[] globalId; 33 private final byte[] branchId; 34 private int hash; 36 40 public XidImpl(byte[] globalId) { 41 this.formatId = FORMAT_ID; 42 this.globalId = globalId; 43 branchId = new byte[Xid.MAXBQUALSIZE]; 45 } 46 47 52 public XidImpl(Xid global, byte[] branch) { 53 this.formatId = FORMAT_ID; 54 if (global instanceof XidImpl) { 56 globalId = ((XidImpl) global).globalId; 57 } else { 59 globalId = global.getGlobalTransactionId(); 60 } 62 branchId = branch; 63 } 65 66 public XidImpl(int formatId, byte[] globalId, byte[] branchId) { 67 this.formatId = formatId; 68 this.globalId = globalId; 69 this.branchId = branchId; 70 } 71 72 private int hash(int hash, byte[] id) { 73 for (int i = 0; i < id.length; i++) { 74 hash = (hash * 37) + id[i]; 75 } 76 return hash; 77 } 78 79 public int getFormatId() { 80 return formatId; 81 } 82 83 public byte[] getGlobalTransactionId() { 84 return (byte[]) globalId.clone(); 85 } 86 87 public byte[] getBranchQualifier() { 88 return (byte[]) branchId.clone(); 89 } 90 91 public boolean equals(Object obj) { 92 if (obj instanceof XidImpl == false) { 93 return false; 94 } 95 XidImpl other = (XidImpl) obj; 96 return formatId == other.formatId 97 && Arrays.equals(globalId, other.globalId) 98 && Arrays.equals(branchId, other.branchId); 99 } 100 101 public int hashCode() { 102 if (hash == 0) { 103 hash = hash(hash(0, globalId), branchId); 104 } 105 return hash; 106 } 107 108 public String toString() { 109 StringBuffer s = new StringBuffer (); 110 s.append("[globalId="); 111 for (int i = 0; i < globalId.length; i++) { 112 s.append(Integer.toHexString(globalId[i])); 113 } 114 s.append(",branchId="); 115 for (int i = 0; i < branchId.length; i++) { 116 s.append(Integer.toHexString(branchId[i])); 117 } 118 s.append("]"); 119 return s.toString(); 120 } 121 } 122 | Popular Tags |