1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.jdbcx; |
7 | |
8 | import java.util.StringTokenizer; |
9 | import javax.transaction.xa.Xid; |
10 | |
11 | import org.h2.api.ErrorCode; |
12 | import org.h2.message.DbException; |
13 | import org.h2.message.TraceObject; |
14 | import org.h2.util.StringUtils; |
15 | |
16 | /** |
17 | * An object of this class represents a transaction id. |
18 | */ |
19 | public class JdbcXid extends TraceObject implements Xid { |
20 | |
21 | private static final String PREFIX = "XID"; |
22 | |
23 | private final int formatId; |
24 | private final byte[] branchQualifier; |
25 | private final byte[] globalTransactionId; |
26 | |
27 | JdbcXid(JdbcDataSourceFactory factory, int id, String tid) { |
28 | setTrace(factory.getTrace(), TraceObject.XID, id); |
29 | try { |
30 | StringTokenizer tokenizer = new StringTokenizer(tid, "_"); |
31 | String prefix = tokenizer.nextToken(); |
32 | if (!PREFIX.equals(prefix)) { |
33 | throw DbException.get(ErrorCode.WRONG_XID_FORMAT_1, tid); |
34 | } |
35 | formatId = Integer.parseInt(tokenizer.nextToken()); |
36 | branchQualifier = StringUtils.convertHexToBytes(tokenizer.nextToken()); |
37 | globalTransactionId = StringUtils.convertHexToBytes(tokenizer.nextToken()); |
38 | } catch (RuntimeException e) { |
39 | throw DbException.get(ErrorCode.WRONG_XID_FORMAT_1, tid); |
40 | } |
41 | } |
42 | |
43 | /** |
44 | * INTERNAL |
45 | */ |
46 | public static String toString(Xid xid) { |
47 | StringBuilder buff = new StringBuilder(PREFIX); |
48 | buff.append('_'). |
49 | append(xid.getFormatId()). |
50 | append('_'). |
51 | append(StringUtils.convertBytesToHex(xid.getBranchQualifier())). |
52 | append('_'). |
53 | append(StringUtils.convertBytesToHex(xid.getGlobalTransactionId())); |
54 | return buff.toString(); |
55 | } |
56 | |
57 | /** |
58 | * Get the format id. |
59 | * |
60 | * @return the format id |
61 | */ |
62 | @Override |
63 | public int getFormatId() { |
64 | debugCodeCall("getFormatId"); |
65 | return formatId; |
66 | } |
67 | |
68 | /** |
69 | * The transaction branch identifier. |
70 | * |
71 | * @return the identifier |
72 | */ |
73 | @Override |
74 | public byte[] getBranchQualifier() { |
75 | debugCodeCall("getBranchQualifier"); |
76 | return branchQualifier; |
77 | } |
78 | |
79 | /** |
80 | * The global transaction identifier. |
81 | * |
82 | * @return the transaction id |
83 | */ |
84 | @Override |
85 | public byte[] getGlobalTransactionId() { |
86 | debugCodeCall("getGlobalTransactionId"); |
87 | return globalTransactionId; |
88 | } |
89 | |
90 | } |