1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
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 /**
26 * Unique id for a transaction. This implementation is backed by a single byte buffer
27 * so can do less copying than one backed by several byte buffers for the different components.
28 *
29 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
30 */
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
37 private static int FORMAT_ID = 0x4765526f;
38 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 /**
47 * Constructor taking a global id (for the main transaction)
48 * @param globalId the global transaction id
49 */
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
56 }
57
58 /**
59 * Constructor for a branch id
60 * @param global the xid of the global transaction this branch belongs to
61 * @param branch the branch id
62 */
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
74 }
75
76 public XidImpl2(int formatId, byte[] globalId, byte[] branch) {
77
78 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
83 }
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 }