File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\jdbcx\JdbcXid.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.jdbcx;
8
9 import java.util.StringTokenizer;
10 import javax.transaction.xa.Xid;
11 import org.h2.constant.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 public int getFormatId() {
63 debugCodeCall("getFormatId");
64 return formatId;
65 }
66
67 /**
68 * The transaction branch identifier.
69 *
70 * @return the identifier
71 */
72 public byte[] getBranchQualifier() {
73 debugCodeCall("getBranchQualifier");
74 return branchQualifier;
75 }
76
77 /**
78 * The global transaction identifier.
79 *
80 * @return the transaction id
81 */
82 public byte[] getGlobalTransactionId() {
83 debugCodeCall("getGlobalTransactionId");
84 return globalTransactionId;
85 }
86
87 }
88