KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > jtsxa > XID


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28 //----------------------------------------------------------------------------
29
//
30
// Module: XID.java
31
//
32
// Description: An implementation of the X/Open transaction idenifier (Xid).
33
//
34
// Product: com.sun.jts.jtsxa
35
//
36
// Author: Malcolm Ayres
37
//
38
// Date: March, 1997
39
//
40
// Copyright (c): 1995-1997 IBM Corp.
41
//
42
// The source code for this program is not published or otherwise divested
43
// of its trade secrets, irrespective of what has been deposited with the
44
// U.S. Copyright Office.
45
//
46
// This software contains confidential and proprietary information of
47
// IBM Corp.
48
//----------------------------------------------------------------------------
49

50 package com.sun.jts.jtsxa;
51
52 import org.omg.CosTransactions.*;
53 import javax.transaction.xa.Xid JavaDoc;
54
55 import java.util.logging.Logger JavaDoc;
56 import java.util.logging.Level JavaDoc;
57 import com.sun.logging.LogDomains;
58 import com.sun.jts.utils.LogFormatter;
59
60 /**
61  * The XID class provides an implementation of the X/Open
62  * transaction identifier it implements the javax.transaction.xa.Xid interface.
63  */

64 public class XID implements Xid JavaDoc {
65
66     //-----------------------------------------------------------------------//
67
// Data Area //
68
//-----------------------------------------------------------------------//
69

70     /**
71      * The format identifier for the XID. A value of -1 indicates
72      * that the NULLXID.
73      */

74     private int formatID; // Format identifier
75
// (-1) means that the XID is null
76
/**
77      * The number of bytes in the global transaction identfier
78      */

79     private int gtrid_length; // Value from 1 through 64
80

81     /**
82      * The number of bytes in the branch qualifier
83      */

84     private int bqual_length; // Value from 1 through 64
85

86     /**
87      * The data for the XID.
88      * <p> The XID is made up of two contiguous parts. The first (of size
89      * <b>gtrid_length</b>) is the global transaction identfier and the second
90      * (of size <b>bqual_length</b>) is the branch qualifier.
91      * <p>If the <b>formatID</b> is -1, indicating the NULLXID,
92      * the data is ignored.
93      */

94     private byte data[]; // The XID data (size XIDDATASIZE)
95

96     // ADDITION
97
private byte cachedBqual[] = null;
98     private byte cachedGtrid[] = null;
99
100
101     //-----------------------------------------------------------------------//
102
// Constants //
103
//-----------------------------------------------------------------------//
104

105     /**
106      * The size of <b>data</b>.
107      */

108     static private final int XIDDATASIZE= 128; // Size in bytes
109

110     /**
111      * The maximum size of the global transaction identifier.
112      */

113     static public final int MAXGTRIDSIZE= 64; // Maximum size (in bytes) of gtrid
114

115     /**
116      * The maximum size of the branch qualifier.
117      */

118     static public final int MAXBQUALSIZE= 64; // Maximum size (in bytes) of bqual
119

120     static private final String JavaDoc hextab= "0123456789ABCDEF"/*#Frozen*/;
121
122     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER);
123
124     //-----------------------------------------------------------------------//
125
// XID::Constructor //
126
//-----------------------------------------------------------------------//
127

128     /**
129      * Constructs a new null XID.
130      * <p>After construction the data within the XID should be initialized.
131      */

132     public XID() {
133         data= new byte[XIDDATASIZE];
134         formatID = -1;
135     }
136
137     //-----------------------------------------------------------------------//
138
// XID::Methods //
139
//-----------------------------------------------------------------------//
140

141     /**
142      * Initialize an XID using another XID as the source of data.
143      *
144      * @param from the XID to initialize this XID from
145      *
146      */

147     public void copy(XID from) {
148         int i;
149
150         formatID = -1; // Default, null transaction
151
if (from == null) // If source is physically null
152
{
153             return; // Return the NULL transaction
154
}
155
156         if (from.formatID == (-1)) // If source is a NULL transaction
157
{
158             return; // Return the NULL transaction
159
}
160
161         gtrid_length= from.gtrid_length;
162         bqual_length= from.bqual_length;
163
164         if (data != null && from.data != null) {
165             System.arraycopy(from.data, 0, data, 0, XIDDATASIZE);
166         }
167
168         formatID= from.formatID; // Last, in case of failure
169
}
170
171     /*
172      * Copy the XID from an otid_t format XID.
173      */

174
175     /**
176      * Initialize an XID using an omg otid_t as the source of data.
177      *
178      * @param from the OMG otid_t to initialize this XID from
179      *
180      * @see org.omg.CosTransactions.otid_t
181      */

182     public void copy(otid_t from) {
183         int i;
184         int L;
185
186         formatID= -1; // Default, null transaction
187
if (from == null) // If source is physically null
188
{
189             return; // Return the NULL transaction
190
}
191
192         if (from.formatID == (-1)) // If source is a NULL transaction
193
{
194             return; // Return the NULL transaction
195
}
196
197         L= from.tid.length;
198         gtrid_length= L - from.bqual_length;
199         bqual_length= from.bqual_length;
200
201         if (data != null && from.tid != null) {
202             System.arraycopy(from.tid, 0, data, 0, L);
203         }
204
205         formatID= from.formatID; // Last, in case of failure
206
}
207
208     /*
209      * Are the XIDs equal?
210      */

211
212     /**
213      * Determine whether or not two objects of this type are equal.
214      *
215      * @param o the object to be compared with this XID.
216      *
217      * @return Returns true of the supplied object represents the same
218      * global transaction as this, otherwise returns false.
219      */

220     public boolean equals(Object JavaDoc o) {
221         XID other; // The "other" XID
222
int L; // Combined gtrid_length + bqual_length
223
int i;
224
225         if (!(o instanceof XID)) // If the other XID isn't an XID
226
{
227             return false; // It can't be equal
228
}
229
230         other = (XID)o; // The other XID, now properly cast
231

232         if (formatID == (-1) && other.formatID == (-1))
233         {
234             return true;
235         }
236
237         if (formatID != other.formatID
238                 ||gtrid_length != other.gtrid_length
239                 ||bqual_length != other.bqual_length) {
240             return false;
241         }
242
243         L = gtrid_length + bqual_length;
244
245         for (i = 0; i < L; i++) {
246             if (data[i] != other.data[i]) {
247                 return false;
248             }
249         }
250
251         return true;
252     }
253
254     /*
255      * Compute the hash code.
256      */

257
258     /**
259      * Compute the hash code.
260      *
261      * @return the computed hashcode
262      */

263     public int hashCode() {
264         if (formatID == (-1)) {
265             return (-1);
266         }
267
268         return formatID + gtrid_length - bqual_length;
269
270     }
271
272     /*
273      * Convert to String
274      *
275      * <p> This is normally used to display the XID when debugging.
276      */

277
278     /**
279      * Return a string representing this XID.
280      *
281      * @return the string representation of this XID
282      */

283     public String JavaDoc toString() {
284         /* toString() method is slightly expensive and this needs to be done because
285      * some of the drivers XAResource methods have the "trace("some thing " + xid)"
286          * kind of code which is executing this method resulting in performance degradation.
287          */

288         if (_logger.isLoggable(Level.FINE)) {
289             StringBuffer JavaDoc d; // Data String, in Hexidecimal
290
String JavaDoc s; // Resultant String
291

292             int i;
293             int v;
294             int L;
295
296             L= gtrid_length + bqual_length;
297             d= new StringBuffer JavaDoc(L + L);
298
299             // Convert data string to hex
300
for (i = 0; i < L; i++) {
301                 v = data[i] & 0xff;
302                 d.append(hextab.charAt(v/16));
303                 d.append(hextab.charAt(v&15));
304                 if ((i+1) % 4 == 0 && (i+1) < L) {
305                     d.append(" ");
306                 }
307             }
308
309             s = new String JavaDoc("{XID: " +
310                          "formatID(" + formatID + "), " +
311                          "gtrid_length(" + gtrid_length + "), " +
312                          "bqual_length(" + bqual_length + "), " +
313                          "data(" + d + ")" +
314                          "}"/*#Frozen*/);
315
316             return s;
317         }
318         else
319            return null;
320     }
321
322     /*
323      * Return branch qualifier
324      */

325
326     /**
327      * Returns the branch qualifier for this XID.
328      *
329      * @return the branch qualifier
330      */

331     public byte[] getBranchQualifier() {
332         if (cachedBqual != null) {
333             return cachedBqual;
334         }
335         byte[] bqual = new byte[bqual_length];
336         System.arraycopy(data,gtrid_length,bqual,0,bqual_length);
337         return bqual;
338     }
339
340     /*
341      * Set branch qualifier.
342      *
343      * Note that the branch qualifier has a maximum size.
344      */

345
346     /**
347      * Set the branch qualifier for this XID.
348      *
349      * @param qual a Byte array containing the branch qualifier to be set. If
350      * the size of the array exceeds MAXBQUALSIZE, only the first
351      * MAXBQUALSIZE elements of qual will be used.
352      */

353     public void setBranchQualifier(byte[] qual) {
354         bqual_length = qual.length > MAXBQUALSIZE ? MAXBQUALSIZE : qual.length;
355         System.arraycopy(qual, 0, data, gtrid_length, bqual_length);
356         cachedBqual = qual;
357     }
358
359     /**
360      * Obtain the format identifier part of the XID.
361      *
362      * @return Format identifier. -1 indicates a null XID
363      */

364     public int getFormatID() {
365         return formatID;
366     }
367
368     /**
369      * Set the format identifier part of the XID.
370      *
371      * @param Format identifier. -1 indicates a null Xid.
372      */

373     public void setFormatID(int formatID) {
374         this.formatID = formatID;
375         return;
376     }
377
378     /*
379      * Determine if an array of bytes equals the branch qualifier
380      */

381
382     /**
383      * Compares the input parameter with the branch qualifier for equality.
384      *
385      * @return true if equal
386      */

387     public boolean isEqualBranchQualifier(byte[] data) {
388
389         int L = data.length > MAXBQUALSIZE?MAXBQUALSIZE:data.length;
390         int i;
391
392         if (L != bqual_length) {
393             return false;
394         }
395
396         for (i = 0; i < L; i++) {
397             if (data[i] != this.data[gtrid_length + i]) {
398                 return false;
399             }
400         }
401
402         return true;
403     }
404
405     // added by TN
406

407     /**
408      * Return whether the Gtrid of this is equal to the Gtrid of xid
409      */

410     public boolean isEqualGtrid(XID xid) {
411         if (this.gtrid_length != xid.gtrid_length) {
412             return false;
413         }
414
415         for (int i=0; i<gtrid_length; i++) {
416             if (this.data[i] != xid.data[i]) {
417                 return false;
418             }
419         }
420
421         return true;
422     }
423
424     /*
425      * Return global transaction identifier
426      */

427
428     /**
429      * Returns the global transaction identifier for this XID.
430      *
431      * @return the global transaction identifier
432      */

433     public byte[] getGlobalTransactionIdentifier() {
434        if (cachedGtrid != null) {
435            return cachedGtrid;
436        }
437         byte[] gtrid = new byte[gtrid_length];
438         System.arraycopy(data, 0, gtrid, 0, gtrid_length);
439         cachedGtrid = gtrid;
440         return gtrid;
441     }
442
443     // Addition by Tony Ng to make this class implements
444
// javax.transaction.xa.Xid
445

446     public int getFormatId() {
447         return getFormatID();
448     }
449
450     public byte[] getGlobalTransactionId() {
451         return getGlobalTransactionIdentifier();
452     }
453 }
454
Popular Tags