1 17 18 package org.apache.geronimo.transaction.log; 19 20 import java.io.Serializable ; 21 import java.util.Arrays ; 22 23 import javax.transaction.xa.Xid ; 24 25 31 public class XidImpl2 implements Xid , Serializable { 32 private static int HEADER_SIZE = 4; 33 private static int ACTION_POS = 0; 34 private static int GLOBALID_SIZE_POS = 1; 35 private static int BRANCHID_SIZE_POS = 2; 36 private static int FORMAT_ID = 0x4765526f; private static int FORMAT_SIZE = 4; 39 40 private static byte[] FORMAT_ID_BYTES = "Gero".getBytes(); 41 42 private final byte[] buffer = new byte[HEADER_SIZE + FORMAT_SIZE + Xid.MAXGTRIDSIZE + Xid.MAXBQUALSIZE]; 43 private int hash; 44 private Object key; 45 46 50 public XidImpl2(byte[] globalId) { 51 System.arraycopy(FORMAT_ID_BYTES, 0, buffer, HEADER_SIZE, FORMAT_SIZE); 52 buffer[GLOBALID_SIZE_POS] = (byte) globalId.length; 53 System.arraycopy(globalId, 0, buffer, HEADER_SIZE + FORMAT_SIZE, Xid.MAXGTRIDSIZE); 54 55 } 57 58 63 public XidImpl2(Xid global, byte[] branch) { 64 if (global instanceof XidImpl2) { 65 System.arraycopy(((XidImpl2) global).buffer, 0, buffer, 0, HEADER_SIZE + FORMAT_SIZE + Xid.MAXGTRIDSIZE); 66 } else { 67 System.arraycopy(FORMAT_ID_BYTES, 0, buffer, HEADER_SIZE, FORMAT_SIZE); 68 byte[] globalId = global.getGlobalTransactionId(); 69 System.arraycopy(globalId, 0, buffer, HEADER_SIZE + FORMAT_SIZE, globalId.length); 70 } 71 buffer[BRANCHID_SIZE_POS] = (byte) branch.length; 72 System.arraycopy(branch, 0, buffer, HEADER_SIZE + FORMAT_SIZE + Xid.MAXGTRIDSIZE, Xid.MAXBQUALSIZE); 73 } 75 76 public XidImpl2(int formatId, byte[] globalId, byte[] branch) { 77 System.arraycopy(FORMAT_ID_BYTES, 0, buffer, HEADER_SIZE, FORMAT_SIZE); 79 System.arraycopy(globalId, 0, buffer, HEADER_SIZE + FORMAT_SIZE, globalId.length); 80 buffer[BRANCHID_SIZE_POS] = (byte) branch.length; 81 System.arraycopy(branch, 0, buffer, HEADER_SIZE + FORMAT_SIZE + Xid.MAXGTRIDSIZE, Xid.MAXBQUALSIZE); 82 } 84 85 private int hash(byte[] id) { 86 int hash = 0; 87 for (int i = 0; i < id.length; i++) { 88 hash = (hash * 37) + id[i]; 89 } 90 return hash; 91 } 92 93 public int getFormatId() { 94 return FORMAT_ID; 95 } 96 97 public byte[] getGlobalTransactionId() { 98 byte[] globalId = new byte[buffer[GLOBALID_SIZE_POS]]; 99 System.arraycopy(buffer, HEADER_SIZE + FORMAT_SIZE, globalId, 0, buffer[GLOBALID_SIZE_POS]); 100 return globalId; 101 } 102 103 public byte[] getBranchQualifier() { 104 byte[] branchId = new byte[buffer[BRANCHID_SIZE_POS]]; 105 System.arraycopy(buffer, HEADER_SIZE + FORMAT_SIZE + Xid.MAXGTRIDSIZE, branchId, 0, buffer[BRANCHID_SIZE_POS]); 106 return branchId; 107 } 108 109 public boolean equals(Object obj) { 110 if (obj instanceof XidImpl2 == false) { 111 return false; 112 } 113 XidImpl2 other = (XidImpl2) obj; 114 return Arrays.equals(buffer, other.buffer); 115 } 116 117 public int hashCode() { 118 if (hash == 0) { 119 hash = hash(buffer); 120 } 121 return hash; 122 } 123 124 public String toString() { 125 StringBuffer s = new StringBuffer ("[formatId=Gero,"); 126 s.append("globalId="); 127 for (int i = FORMAT_SIZE; i < FORMAT_SIZE + Xid.MAXGTRIDSIZE; i++) { 128 s.append(Integer.toHexString(buffer[i])); 129 } 130 s.append(",branchId="); 131 for (int i = FORMAT_SIZE + Xid.MAXGTRIDSIZE; i < buffer.length; i++) { 132 s.append(Integer.toHexString(buffer[i])); 133 } 134 s.append("]"); 135 return s.toString(); 136 } 137 138 byte[] getBuffer(byte action) { 139 buffer[ACTION_POS] = action; 140 return buffer; 141 } 142 143 public void setKey(Object key) { 144 this.key = key; 145 } 146 147 public Object getKey() { 148 return key; 149 } 150 } 151 | Popular Tags |