KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > ClientXid


1 /*
2
3    Derby - Class org.apache.derby.client.ClientXid
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

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