1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.ehcache.transaction.xa;
17
18 import java.io.Serializable;
19 import java.util.Arrays;
20
21 import javax.transaction.xa.Xid;
22
23 /***
24 * A serializable XID
25 *
26 * @author Ludovic Orban
27 */
28 public class SerializableXid implements Xid, Serializable {
29
30 private final int formatId;
31 private final byte[] globalTransactionId;
32 private final byte[] branchQualifier;
33
34 /***
35 * Create a SerializableXid, copying the GTRID and BQUAL of an existing XID
36 *
37 * @param xid a SerializableXid
38 */
39 public SerializableXid(Xid xid) {
40 this.formatId = xid.getFormatId();
41 this.globalTransactionId = xid.getGlobalTransactionId();
42 this.branchQualifier = xid.getBranchQualifier();
43 }
44
45 /***
46 * {@inheritDoc}
47 */
48 public int getFormatId() {
49 return formatId;
50 }
51
52 /***
53 * {@inheritDoc}
54 */
55 public byte[] getBranchQualifier() {
56 return branchQualifier;
57 }
58
59 /***
60 * {@inheritDoc}
61 */
62 public byte[] getGlobalTransactionId() {
63 return globalTransactionId;
64 }
65
66 /***
67 * {@inheritDoc}
68 */
69 public boolean equals(Object obj) {
70 if (!(obj instanceof SerializableXid)) {
71 return false;
72 }
73
74 SerializableXid otherXid = (SerializableXid) obj;
75 return formatId == otherXid.getFormatId() &&
76 Arrays.equals(globalTransactionId, otherXid.getGlobalTransactionId()) &&
77 Arrays.equals(branchQualifier, otherXid.branchQualifier);
78 }
79
80 /***
81 * {@inheritDoc}
82 */
83 public int hashCode() {
84 int hashCode = formatId;
85 if (globalTransactionId != null) {
86 hashCode += Arrays.hashCode(globalTransactionId);
87 }
88 if (branchQualifier != null) {
89 hashCode += Arrays.hashCode(branchQualifier);
90 }
91 return hashCode;
92 }
93
94 /***
95 * {@inheritDoc}
96 */
97 @Override
98 public String toString() {
99 return "SerializableXid{" +
100 "formatId=" + formatId +
101 ", globalTxId=" + Arrays.toString(globalTransactionId) +
102 ", branchQualifier=" + Arrays.toString(branchQualifier) +
103 '}';
104 }
105
106 }