summaryrefslogtreecommitdiff
path: root/java/common/src/main/java/org/apache/qpid/dtx/XidImpl.java
blob: 69457ca4a9c2dfd1270d62de4fe4c0b54143514f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.qpid.dtx;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.qpid.AMQInvalidArgumentException;

import javax.transaction.xa.Xid;

import java.io.*;
import java.util.Arrays;

/**
 * Implements javax.transaction.dtx.Xid
 */
public class XidImpl implements Xid
{
    /**
     * this session's logger
     */
    private static final Logger _logger = LoggerFactory.getLogger(XidImpl.class);

    /**
     * the transaction branch identifier part of XID as an array of bytes
     */
    private byte[] _branchQualifier;

    /**
     * the format identifier part of the XID.
     */
    private int _formatID;

    /**
     * the global transaction identifier part of XID as an array of bytes.
     */
    private byte[] _globalTransactionID;

    //--- Constructors

    /**
     * Create new Xid.
     * this is an empty constructor.
     */
    public XidImpl()
    {

    }

    /**
     * Create a new XidImpl from an existing Xid.
     * <p> Usefull for casting external Xids
     *
     * @param xid Foreign Xid.
     */
    public XidImpl(Xid xid)
    {
        _branchQualifier = xid.getBranchQualifier();
        _formatID = xid.getFormatId();
        _globalTransactionID = xid.getGlobalTransactionId();
    }

    /**
     * Create a new Xid.
     *
     * @param branchQualifier     The transaction branch identifier part of XID as an array of bytes.
     * @param format              The format identifier part of the XID.
     * @param globalTransactionID The global transaction identifier part of XID as an array of bytes.
     */
    public XidImpl(byte[] branchQualifier, int format, byte[] globalTransactionID)
    {
        _branchQualifier = branchQualifier;
        _formatID = format;
        _globalTransactionID = globalTransactionID;
    }

    /**
     * Create a new Xid form its String form
     * 4         1   1        g            b
     * +---+---+---+---+---+---+---+-  -+---+---+-  -+---+
     * |   format_id   | g | b |   txn-id   |   br-id    |
     * +---+---+---+---+---+---+---+-  -+---+---+-  -+---+
     * 0               4   5   6           6+g         6+g+b
     * format_id: an implementation specific format identifier
     * <p/>
     * gtrid_length: how many bytes of this form the transaction id
     * <p/>
     * bqual_length: how many bytes of this form the branch id
     * <p/>
     * data: a sequence of octets of at most 128 bytes containing the txn id and the
     * branch id
     * <p/>
     * Note - The sum of the two lengths must equal the length of the data field.
     *
     * @param xid an XID STring Form
     * @throws AMQInvalidArgumentException If the string does not represent a valid Xid
     */
    public XidImpl(String xid) throws AMQInvalidArgumentException
    {
        if (_logger.isDebugEnabled())
        {
            _logger.debug("converting string " + xid + " into XidImpl");
        }
        try
        {
            DataInputStream input = new DataInputStream(new ByteArrayInputStream(xid.getBytes()));
            _formatID = (int) input.readLong();
            int g = input.readByte();
            int b = input.readByte();
            _globalTransactionID = new byte[g];
            _branchQualifier = new byte[b];
            if (input.read(_globalTransactionID, 0, g) != g)
            {
                throw new AMQInvalidArgumentException("Cannot convert the string " + xid + " into an Xid", null);
            }
            if (input.read(_branchQualifier, 0, b) != b)
            {
                throw new AMQInvalidArgumentException("Cannot convert the string " + xid + " into an Xid", null);
            }
        }
        catch (IOException e)
        {
            throw new AMQInvalidArgumentException("cannot convert the string " + xid + " into an Xid", e);
        }
    }

    //---  Xid interface implementation

    /**
     * Format identifier. O means the OSI CCR format.
     *
     * @return Global transaction identifier.
     */
    public byte[] getGlobalTransactionId()
    {
        return _globalTransactionID;
    }

    /**
     * Obtain the transaction branch identifier part of XID as an array of bytes.
     *
     * @return Branch identifier part of XID.
     */
    public byte[] getBranchQualifier()
    {
        return _branchQualifier;
    }

    /**
     * Obtain the format identifier part of the XID.
     *
     * @return Format identifier. O means the OSI CCR format.
     */
    public int getFormatId()
    {
        return _formatID;
    }

    //--- Object operations

    /**
     * Indicates whether some other Xid is "equal to" this one.
     * <p> Two Xids are equal if and only if their three elementary parts are equal
     *
     * @param o the object to compare this <code>XidImpl</code> against.
     * @return true if the <code>XidImpl</code> are equal, false otherwise.
     */
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (o instanceof XidImpl)
        {
            XidImpl other = (XidImpl) o;
            if (_formatID == other.getFormatId())
            {
                if (_branchQualifier.length == other.getBranchQualifier().length)
                {
                    for (int i = 0; i < _branchQualifier.length; i++)
                    {
                        if (_branchQualifier[i] != other.getBranchQualifier()[i])
                        {
                            return false;
                        }
                    }
                    if (_globalTransactionID.length == other.getGlobalTransactionId().length)
                    {
                        for (int i = 0; i < _globalTransactionID.length; i++)
                        {
                            if (_globalTransactionID[i] != other.getGlobalTransactionId()[i])
                            {
                                return false;
                            }
                        }
                        // everithing is equal
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public int hashCode()
    {
        int result = _branchQualifier != null ? Arrays.hashCode(_branchQualifier) : 0;
        result = 31 * result + _formatID;
        result = 31 * result + (_globalTransactionID != null ? Arrays.hashCode(_globalTransactionID) : 0);
        return result;
    }

    //-- Static helper method
    /**
     * Convert an Xid into the AMQP String format.
     * 
     * 4         1   1        g            b
     * +---+---+---+---+---+---+---+-  -+---+---+-  -+---+
     * |   format_id   | g | b |   txn-id   |   br-id    |
     * +---+---+---+---+---+---+---+-  -+---+---+-  -+---+
     * 0               4   5   6           6+g         6+g+b
     * format_id: an implementation specific format identifier
     * <p/>
     * gtrid_length: how many bytes of this form the transaction id
     * <p/>
     * bqual_length: how many bytes of this form the branch id
     * <p/>
     * data: a sequence of octets of at most 128 bytes containing the txn id and the
     * branch id
     * <p/>
     * Note - The sum of the two lengths must equal the length of the data field.
     *
     * @param xid an Xid to convert.
     * @return The String representation of this Xid
     */
    public static org.apache.qpid.transport.Xid convert(Xid xid)
    {
        return new org.apache.qpid.transport.Xid(xid.getFormatId(),
                                                    xid.getGlobalTransactionId(),
                                                    xid.getBranchQualifier());
    }
}