1 | /* |
2 | |
3 | Derby - Class org.apache.derby.client.ClientXid |
4 | |
5 | Copyright (c) 2003, 2005 The Apache Software Foundation or its licensors, where applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | package org.apache.derby.client; |
21 | |
22 | import javax.transaction.xa.Xid; |
23 | |
24 | public class ClientXid implements Xid { |
25 | // |
26 | // The format identifier for the Xid. A value of -1 indicates |
27 | // that the NULLXid |
28 | // |
29 | private int formatID_; |
30 | |
31 | // |
32 | // The number of bytes in the global transaction identfier |
33 | // |
34 | private int gtrid_length_; |
35 | |
36 | // |
37 | // The number of bytes in the branch qualifier |
38 | // |
39 | private int bqual_length_; |
40 | |
41 | // |
42 | // The data for the Xid. |
43 | // <p> The Xid is made up of two contiguous parts. The first (of size |
44 | // <b>gtrid_length</b>) is the global transaction identfier and the second |
45 | // (of size <b>bqual_length</b>) is the branch qualifier. |
46 | // <p>If the <b>formatID</b> is -1, indicating the NULLXid, the data is |
47 | // ignored. |
48 | // |
49 | private byte data_[]; |
50 | |
51 | // |
52 | // The size of <b>data</b>. |
53 | // |
54 | static private final int XidDATASIZE = 128; |
55 | |
56 | // |
57 | // The maximum size of the global transaction identifier. |
58 | // |
59 | static public final int MAXGTRIDSIZE = 64; |
60 | |
61 | // |
62 | // The maximum size of the branch qualifier. |
63 | // |
64 | static public final int MAXBQUALSIZE = 64; |
65 | |
66 | static private final String hextab_ = "0123456789ABCDEF"; |
67 | |
68 | |
69 | // |
70 | // Constructs a new null Xid. |
71 | // <p>After construction the data within the Xid should be initialized. |
72 | // |
73 | public ClientXid() { |
74 | data_ = new byte[XidDATASIZE]; |
75 | gtrid_length_ = 0; |
76 | bqual_length_ = 0; |
77 | formatID_ = -1; |
78 | } |
79 | |
80 | // |
81 | // another contructor |
82 | // |
83 | public ClientXid(int formatID, byte[] gtrid, byte[] bqual) { |
84 | |
85 | formatID_ = formatID; |
86 | gtrid_length_ = gtrid.length; |
87 | bqual_length_ = bqual.length; |
88 | data_ = new byte[XidDATASIZE]; |
89 | System.arraycopy(gtrid, 0, data_, 0, gtrid_length_); |
90 | System.arraycopy(bqual, 0, data_, gtrid_length_, bqual_length_); |
91 | } |
92 | |
93 | // |
94 | // Return a string representing this Xid for debuging |
95 | // |
96 | // @return the string representation of this Xid |
97 | // |
98 | public String toString() { |
99 | StringBuffer d; // Data String, in HeXidecimal |
100 | String s; // Resultant String |
101 | int i; |
102 | int v; |
103 | int L; |
104 | |
105 | L = gtrid_length_ + bqual_length_; |
106 | d = new StringBuffer(L + L); |
107 | |
108 | for (i = 0; i < L; i++) { |
109 | // Convert data string to hex |
110 | v = data_[i] & 0xff; |
111 | d.append(hextab_.charAt(v / 16)); |
112 | d.append(hextab_.charAt(v & 15)); |
113 | if ((i + 1) % 4 == 0 && (i + 1) < L) { |
114 | d.append(" "); |
115 | } |
116 | } |
117 | |
118 | s = "{ClientXid: " + |
119 | "formatID(" + formatID_ + "), " + |
120 | "gtrid_length(" + gtrid_length_ + "), " + |
121 | "bqual_length(" + bqual_length_ + "), " + |
122 | "data(" + d.toString() + ")" + |
123 | "}"; |
124 | return s; |
125 | } |
126 | |
127 | // |
128 | // Returns the branch qualifier for this Xid. |
129 | // |
130 | // @return the branch qualifier |
131 | // |
132 | public byte[] getBranchQualifier() { |
133 | byte[] bqual = new byte[bqual_length_]; |
134 | System.arraycopy(data_, gtrid_length_, bqual, 0, bqual_length_); |
135 | return bqual; |
136 | } |
137 | |
138 | // |
139 | // Set the branch qualifier for this Xid. |
140 | // |
141 | // @param qual a Byte array containing the branch qualifier to be set. If |
142 | // the size of the array exceeds MAXBQUALSIZE, only the first MAXBQUALSIZE |
143 | // elements of qual will be used. |
144 | // |
145 | public void setBranchQualifier(byte[] qual) { |
146 | bqual_length_ = qual.length > MAXBQUALSIZE ? MAXBQUALSIZE : qual.length; |
147 | System.arraycopy(qual, 0, data_, gtrid_length_, bqual_length_); |
148 | } |
149 | |
150 | // |
151 | // Obtain the format identifier part of the Xid. |
152 | // |
153 | // @return Format identifier. -1 indicates a null Xid |
154 | // |
155 | public int getFormatId() { |
156 | return formatID_; |
157 | } |
158 | |
159 | // |
160 | // Set the format identifier part of the Xid. |
161 | // |
162 | // @param Format identifier. -1 indicates a null Xid. |
163 | // |
164 | public void setFormatID(int formatID) { |
165 | formatID_ = formatID; |
166 | return; |
167 | } |
168 | |
169 | // |
170 | // Returns the global transaction identifier for this Xid. |
171 | // |
172 | // @return the global transaction identifier |
173 | // |
174 | public byte[] getGlobalTransactionId() { |
175 | byte[] gtrid = new byte[gtrid_length_]; |
176 | System.arraycopy(data_, 0, gtrid, 0, gtrid_length_); |
177 | return gtrid; |
178 | } |
179 | |
180 | // |
181 | // return fields of Xid |
182 | // |
183 | public byte[] getData() { |
184 | return data_; |
185 | } |
186 | |
187 | public int getGtridLength() { |
188 | return gtrid_length_; |
189 | } |
190 | |
191 | public int getBqualLength() { |
192 | return bqual_length_; |
193 | } |
194 | |
195 | public int hashCode() { |
196 | if (formatID_ == (-1)) { |
197 | return (-1); |
198 | } |
199 | return formatID_ + gtrid_length_ - bqual_length_; |
200 | } |
201 | |
202 | public boolean equals(Object obj) { |
203 | return org.apache.derby.client.net.NetXAResource.xidsEqual(this, (javax.transaction.xa.Xid) obj); |
204 | } |
205 | } // class Xid |