KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > manager > XidImpl


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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.manager;
19
20 import java.io.Serializable JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import javax.transaction.xa.Xid JavaDoc;
23
24 /**
25  * Unique id for a transaction.
26  *
27  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
28  */

29 public class XidImpl implements Xid JavaDoc, Serializable JavaDoc {
30     private static int FORMAT_ID = 0x4765526f; // Gero
31
private final int formatId;
32     private final byte[] globalId;
33     private final byte[] branchId;
34     private int hash; //apparently never used by our code, so don't compute it.
35

36     /**
37      * Constructor taking a global id (for the main transaction)
38      * @param globalId the global transaction id
39      */

40     public XidImpl(byte[] globalId) {
41         this.formatId = FORMAT_ID;
42         this.globalId = globalId;
43         //this.hash = hash(0, globalId);
44
branchId = new byte[Xid.MAXBQUALSIZE];
45     }
46
47     /**
48      * Constructor for a branch id
49      * @param global the xid of the global transaction this branch belongs to
50      * @param branch the branch id
51      */

52     public XidImpl(Xid JavaDoc global, byte[] branch) {
53         this.formatId = FORMAT_ID;
54         //int hash;
55
if (global instanceof XidImpl) {
56             globalId = ((XidImpl) global).globalId;
57             //hash = ((XidImpl) global).hash;
58
} else {
59             globalId = global.getGlobalTransactionId();
60             //hash = hash(0, globalId);
61
}
62         branchId = branch;
63         //this.hash = hash(hash, branchId);
64
}
65
66     public XidImpl(int formatId, byte[] globalId, byte[] branchId) {
67         this.formatId = formatId;
68         this.globalId = globalId;
69         this.branchId = branchId;
70     }
71
72     private int hash(int hash, byte[] id) {
73         for (int i = 0; i < id.length; i++) {
74             hash = (hash * 37) + id[i];
75         }
76         return hash;
77     }
78
79     public int getFormatId() {
80         return formatId;
81     }
82
83     public byte[] getGlobalTransactionId() {
84         return (byte[]) globalId.clone();
85     }
86
87     public byte[] getBranchQualifier() {
88         return (byte[]) branchId.clone();
89     }
90
91     public boolean equals(Object JavaDoc obj) {
92         if (obj instanceof XidImpl == false) {
93             return false;
94         }
95         XidImpl other = (XidImpl) obj;
96         return formatId == other.formatId
97                 && Arrays.equals(globalId, other.globalId)
98                 && Arrays.equals(branchId, other.branchId);
99     }
100
101     public int hashCode() {
102         if (hash == 0) {
103             hash = hash(hash(0, globalId), branchId);
104         }
105         return hash;
106     }
107
108     public String JavaDoc toString() {
109         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
110         s.append("[globalId=");
111         for (int i = 0; i < globalId.length; i++) {
112             s.append(Integer.toHexString(globalId[i]));
113         }
114         s.append(",branchId=");
115         for (int i = 0; i < branchId.length; i++) {
116             s.append(Integer.toHexString(branchId[i]));
117         }
118         s.append("]");
119         return s.toString();
120     }
121 }
122
Popular Tags