KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > CosTransactions > GlobalTID


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: GlobalTID.java
31
//
32
// Description: Transaction global identifier object implementation.
33
//
34
// Product: com.sun.jts.CosTransactions
35
//
36
// Author: Simon Holdsworth
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.CosTransactions;
51
52 // Import required classes.
53

54 import java.io.*;
55
56 import org.omg.CosTransactions.*;
57 import com.sun.jts.trace.*;
58
59
60 import java.util.logging.Logger JavaDoc;
61 import java.util.logging.Level JavaDoc;
62 import com.sun.logging.LogDomains;
63 import com.sun.jts.utils.LogFormatter;
64
65 /**This class provides a wrapper for the otid_t class in the
66  * org.omg.CosTSInteroperation package to allow us to add operations.
67  *
68  * @version 0.01
69  *
70  * @author Simon Holdsworth, IBM Corporation
71  *
72  * @see
73 */

74 //----------------------------------------------------------------------------
75
// CHANGE HISTORY
76
//
77
// Version By Change Description
78
// 0.01 SAJH Initial implementation.
79
//-----------------------------------------------------------------------------
80
public class GlobalTID extends Object JavaDoc {
81     static GlobalTID NullGlobalTID = new GlobalTID(-1,-1,null);
82
83     otid_t realTID = null;
84
85     private String JavaDoc stringForm = null;
86     private int hashCode = 0;
87     private boolean hashed = false;
88     /*
89         Logger to log transaction messages
90     */

91     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER);
92
93     /**Creates a new global identifier which is a copy of the parameter.
94      *
95      * @param otherTID The other global identifier.
96      *
97      * @return
98      *
99      * @see
100      */

101     public GlobalTID( otid_t otherTID ) {
102         realTID = otherTID;
103     }
104
105     /**Creates a new global identifier from the given values.
106      *
107      * @param formatID The format identifier.
108      * @param bqual_length The branch qualifier.
109      * @param data The identifier data.
110      *
111      * @return
112      *
113      * @see
114      */

115     GlobalTID( int formatID,
116                int bqual_length,
117                byte[] tid ) {
118         realTID = new otid_t(formatID, bqual_length, tid);
119     }
120
121     /**
122      * Creates a new global identifier object.
123      *
124      * @param xid The Xid object containing transaction id.
125      */

126     public GlobalTID(javax.transaction.xa.Xid JavaDoc xid) {
127         
128         int glen = xid.getGlobalTransactionId().length;
129         int blen = xid.getBranchQualifier().length;
130         byte[] xidRep = new byte[glen + blen];
131         
132         System.arraycopy(xid.getGlobalTransactionId(), 0, xidRep, 0, glen);
133         System.arraycopy(xid.getBranchQualifier(), 0, xidRep, glen, blen);
134         
135         realTID = new otid_t(xid.getFormatId(), blen, xidRep);
136     }
137
138     /**Creates a GlobalTID from the given stream.
139      *
140      * @param dataIn The DataInputStream for the operation.
141      *
142      * @return
143      *
144      * @see
145      */

146     GlobalTID( DataInputStream dataIn ) {
147         try {
148             int formatID = dataIn.readInt();
149             int bqualLength = dataIn.readInt();
150             int bufferlen = dataIn.readUnsignedShort();
151             byte[] tid = new byte[bufferlen];
152             dataIn.read(tid);
153
154             realTID = new otid_t(formatID,bqualLength,tid);
155         } catch( Throwable JavaDoc exc ) {}
156     }
157
158     /**Creates a global identifier from a byte array.
159      *
160      * @param The array of bytes.
161      *
162      * @return
163      *
164      * @see
165      */

166     GlobalTID( byte[] bytes ) {
167         int formatID = (bytes[0]&255) +
168             ((bytes[1]&255) << 8) +
169             ((bytes[2]&255) << 16) +
170             ((bytes[3]&255) << 24);
171         int bqualLength = (bytes[4]&255) +
172             ((bytes[5]&255) << 8) +
173             ((bytes[6]&255) << 16) +
174             ((bytes[7]&255) << 24);
175         byte[] tid = new byte[bytes.length-8];
176         System.arraycopy(bytes,8,tid,0,tid.length);
177
178         realTID = new otid_t(formatID,bqualLength,tid);
179     }
180
181     /**Creates a new global identifier which is a copy of the target object.
182      *
183      * @param
184      *
185      * @return The copy.
186      *
187      * @see
188      */

189     final GlobalTID copy() {
190         GlobalTID result = new GlobalTID(realTID);
191         result.hashed = hashed;
192         result.hashCode = hashCode;
193         result.stringForm = stringForm;
194
195         return result;
196     }
197
198     /**Determines whether the global identifier represents the null transaction
199      * identifier.
200      *
201      * @param
202      *
203      * @return Indicates whether the global identifier is null.
204      *
205      * @see
206      */

207     final boolean isNull() {
208         return realTID.formatID == -1;
209     }
210
211     /**Compares the two global identifiers.
212      *
213      * @param other The other global identifier to compare.
214      *
215      * @return Indicates the two global identifiers are equal.
216      *
217      * @see
218      */

219     public final boolean equals( Object JavaDoc other ) {
220         otid_t otherTID = null;
221
222         if( other == null )
223             return false;
224         else if( other instanceof otid_t )
225             otherTID = (otid_t)other;
226         else if( other instanceof GlobalTID )
227             otherTID = ((GlobalTID)other).realTID;
228         else
229             return false;
230
231         boolean result = false;
232
233         // If the references are equal, return immediately.
234

235         if( realTID == otherTID ) return true;
236
237         // If the formats are different, then the identifiers cannot be the same.
238

239         if( realTID.formatID != otherTID.formatID ) return false;
240
241         // Determine the GTRID length for each transaction identifier.
242

243         int firstGTRID = realTID.tid.length - realTID.bqual_length;
244         int secondGTRID = otherTID.tid.length - otherTID.bqual_length;
245
246         // If the GTRID lengths are different, the identifiers are different.
247

248         if( firstGTRID != secondGTRID )
249             return false;
250
251         // Compare the global part of the identifier.
252

253         result = true;
254         for( int pos = 0; pos < firstGTRID && result; pos++ )
255             result = (realTID.tid[pos] == otherTID.tid[pos] );
256
257         return result;
258     }
259
260     /**Returns a hash value for the global identifier.
261      *
262      * @param
263      *
264      * @return The hash value.
265      *
266      * @see
267      */

268
269     public final int hashCode() {
270
271         // If the hash code has already been calculated, then return the value.
272

273         if( hashed )
274             return hashCode;
275
276         hashCode = 0;
277
278         // Add up the values in the XID.
279

280         if( realTID.tid != null )
281             for( int pos = 0; pos < realTID.tid.length; pos++ )
282                 hashCode += realTID.tid[pos];
283
284         // Add in the formatId and branch qualifier length.
285

286         hashCode += realTID.formatID + realTID.bqual_length;
287
288         // Multiply the result by the "magic hashing constant".
289

290         hashCode *= 0x71824361;
291
292         hashed = true;
293
294         return hashCode;
295     }
296
297     public GlobalTID(String JavaDoc stid){
298         //invalid data
299
if(stid==null){
300              return ;
301         }
302
303         //there was no proper formatId
304
if(stid.equals("[NULL ID]")){
305             realTID.formatID=-1;
306             return;
307         }
308         if(_logger.isLoggable(Level.FINEST))
309             _logger.logp(Level.FINEST,"GlobalTID","GlobalTID(String)",
310                     "Tid is: "+stid);
311
312         //main part starts here
313
char [] ctid =stid.toCharArray();
314
315         int colon=stid.indexOf(":");
316
317         //bqualLen and globalLen are not real lengths but twice of them
318
int globalLen=0;
319         int bqualLen=0;
320         if(colon==-1){
321             //there was no bqual_length in the tid
322
globalLen=ctid.length-2;
323         }
324         else{
325             globalLen=colon-1;
326             bqualLen=ctid.length -3 - globalLen;
327         }
328
329         if( (globalLen%2!=0) || (bqualLen%2 !=0)){
330             if(_logger.isLoggable(Level.FINEST)){
331                 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)",
332                         "Corrupted gtid string , total length is not integral");
333             }
334             throw new RuntimeException JavaDoc("invalid global tid");
335         }
336
337
338         byte [] b=new byte[(globalLen+bqualLen)/2];
339         int index=1;
340         int bIndex=0;
341
342         //while b gets filled
343
while(bIndex<b.length){
344
345             int t=ctid[index++];
346             int t1=ctid[index++];
347             if(_logger.isLoggable(Level.FINEST))
348                 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)",
349                         "Index is : "+bIndex+" value of t,t1 is : "+t+","+t1);
350             if( t >= 'A'){
351                 t = t - 'A'+10;
352             }
353             else{
354                 t=t-'0';
355             }
356             if( t1 >= 'A'){
357                 t1 = t1 - 'A'+10;
358             }
359             else{
360                 t1=t1-'0';
361             }
362             if(_logger.isLoggable(Level.FINEST))
363                 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)",
364                         " Value of t,t1 is : "+t+","+t1);
365             t=t<<4;
366             if(_logger.isLoggable(Level.FINEST))
367                 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)",
368                         "Value of t is : "+t);
369             t=t|t1;
370             
371             if(_logger.isLoggable(Level.FINEST))
372                 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)",
373                         " Value of t is : "+t);
374             b[bIndex++] = (byte)t;
375             if(_logger.isLoggable(Level.FINEST))
376                 _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)",
377                         "Value of t is : "+(byte)t);
378         }
379
380         realTID = new otid_t(TransactionState.XID_FORMAT_ID,bqualLen/2,b);
381         if(_logger.isLoggable(Level.FINEST))
382             _logger.logp(Level.FINEST,"GlobalTID", "GlobalTID(String)",
383                     "created gtid : "+this);
384     }
385
386
387     /**Converts the global identifier to a string.
388      *
389      * @param
390      *
391      * @return The string representation of the identifier.
392      *
393      * @see
394      */

395
396     public final String JavaDoc toString() {
397
398         // Return a string for the null transaction id.
399

400         if( realTID.formatID == -1 )
401             return "[NULL ID]"/*#Frozen*/;
402
403         // If we have a cached copy of the string form of the global identifier, return
404
// it now.
405

406         if( stringForm != null ) return stringForm;
407
408         // Otherwise format the global identifier.
409

410         //char[] buff = new char[realTID.tid.length*2 + 2 + (realTID.bqual_length>0?1:0)];
411
char[] buff = new char[realTID.tid.length*2 + (realTID.bqual_length>0?1:0)];
412         int pos = 0;
413         //buff[pos++] = '[';
414

415         // Convert the global transaction identifier into a string of hex digits.
416

417         int globalLen = realTID.tid.length - realTID.bqual_length;
418         for( int i = 0; i < globalLen; i++ ) {
419             int currCharHigh = (realTID.tid[i]&0xf0) >> 4;
420             int currCharLow = realTID.tid[i]&0x0f;
421             buff[pos++] = (char)(currCharHigh + (currCharHigh > 9 ? 'A'-10 : '0'));
422             buff[pos++] = (char)(currCharLow + (currCharLow > 9 ? 'A'-10 : '0'));
423         }
424
425         if( realTID.bqual_length > 0 ) {
426             //buff[pos++] = ':';
427
buff[pos++] = '_';
428             for( int i = 0; i < realTID.bqual_length; i++ ) {
429                 int currCharHigh = (realTID.tid[i+globalLen]&0xf0) >> 4;
430                 int currCharLow = realTID.tid[i+globalLen]&0x0f;
431                 buff[pos++] = (char)(currCharHigh + (currCharHigh > 9 ? 'A'-10 : '0'));
432                 buff[pos++] = (char)(currCharLow + (currCharLow > 9 ? 'A'-10 : '0'));
433             }
434         }
435         //buff[pos] = ']';
436

437         // Cache the string form of the global identifier.
438

439         stringForm = new String JavaDoc(buff);
440
441         return stringForm;
442     }
443
444     /**Converts the global identifier to a byte array.
445      *
446      * @param
447      *
448      * @return The byte array representation of the identifier.
449      *
450      * @see
451      */

452     final byte[] toBytes() {
453         if( realTID.formatID == -1 )
454             return null;
455
456         byte[] result = new byte[realTID.tid.length + 8];
457
458         result[0] = (byte) realTID.formatID;
459         result[1] = (byte)(realTID.formatID >> 8);
460         result[2] = (byte)(realTID.formatID >> 16);
461         result[3] = (byte)(realTID.formatID >> 24);
462         result[4] = (byte) realTID.bqual_length;
463         result[5] = (byte)(realTID.bqual_length >> 8);
464         result[6] = (byte)(realTID.bqual_length >> 16);
465         result[7] = (byte)(realTID.bqual_length >> 24);
466
467         System.arraycopy(realTID.tid,0,result,8,realTID.tid.length);
468
469         return result;
470     }
471
472     final byte[] toTidBytes() {
473         return realTID.tid;
474     }
475
476     static GlobalTID fromTIDBytes(byte[] bytes) {
477         return new GlobalTID(TransactionState.XID_FORMAT_ID, 0, bytes);
478     }
479
480
481     /**Writes the contents of the global identifier to the given stream.
482      *
483      * @param dataOut The DataOutputStream for the operation.
484      *
485      * @return
486      *
487      * @see
488      */

489     final void write( DataOutputStream dataOut ) {
490         try {
491             dataOut.writeInt(realTID.formatID);
492             dataOut.writeInt(realTID.bqual_length);
493             dataOut.writeShort(realTID.tid.length);
494             dataOut.write(realTID.tid,0,realTID.tid.length);
495         } catch( Throwable JavaDoc exc ) {}
496     }
497 }
498
Popular Tags