1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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;
21 import java.util.Arrays;
22 import javax.transaction.xa.Xid;
23
24 /**
25 * Unique id for a transaction.
26 *
27 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
28 */
29 public class XidImpl implements Xid, Serializable {
30 private static int FORMAT_ID = 0x4765526f;
31 private final int formatId;
32 private final byte[] globalId;
33 private final byte[] branchId;
34 private int hash;
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
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 global, byte[] branch) {
53 this.formatId = FORMAT_ID;
54
55 if (global instanceof XidImpl) {
56 globalId = ((XidImpl) global).globalId;
57
58 } else {
59 globalId = global.getGlobalTransactionId();
60
61 }
62 branchId = branch;
63
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 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 toString() {
109 StringBuffer s = new StringBuffer();
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 }