File: RecoveredXid.java

package info (click to toggle)
libpgjava 8.4-701-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,532 kB
  • ctags: 4,162
  • sloc: java: 33,948; xml: 3,158; makefile: 14; sh: 10
file content (89 lines) | stat: -rw-r--r-- 2,347 bytes parent folder | download | duplicates (2)
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
package org.postgresql.xa;

import java.util.Arrays;
import javax.transaction.xa.Xid;

import org.postgresql.util.Base64;

class RecoveredXid implements Xid {
    int formatId;
    byte[] globalTransactionId;
    byte[] branchQualifier;

    public int getFormatId() {
        return formatId;
    }

    public byte[] getGlobalTransactionId() {
        return globalTransactionId;
    }

    public byte[] getBranchQualifier() {
        return branchQualifier;
    }

    public boolean equals(Object o) {
        if (o == this) // optimization for the common case.
            return true;

        if (!(o instanceof Xid))
            return false;

        Xid other = (Xid) o;
        if (other.getFormatId() != formatId)
            return false;
        if (!Arrays.equals(globalTransactionId, other.getGlobalTransactionId()))
            return false;
        if (!Arrays.equals(branchQualifier, other.getBranchQualifier()))
            return false;

        return true;
    }

    /**
     * This is for debugging purposes only
     */
    public String toString()
    {
        return xidToString(this);
    }

    //--- Routines for converting xid to string and back.

    static String xidToString(Xid xid) {
        return xid.getFormatId() + "_"
               + Base64.encodeBytes(xid.getGlobalTransactionId(), Base64.DONT_BREAK_LINES) + "_"
               + Base64.encodeBytes(xid.getBranchQualifier(), Base64.DONT_BREAK_LINES);
    }

    /**
     * @param s
     * @return recovered xid, or null if s does not represent a
     *   valid xid encoded by the driver.
     */
    static Xid stringToXid(String s) {
        RecoveredXid xid = new RecoveredXid();

        int a = s.indexOf("_");
        int b = s.lastIndexOf("_");

        if (a == b) // this also catches the case a == b == -1.
            return null;

        try
        {
            xid.formatId = Integer.parseInt(s.substring(0, a));
            xid.globalTransactionId = Base64.decode(s.substring(a + 1, b));
            xid.branchQualifier = Base64.decode(s.substring(b + 1));

            if (xid.globalTransactionId == null || xid.branchQualifier == null)
                return null;
        }
        catch (Exception ex)
        {
            return null; // Doesn't seem to be an xid generated by this driver.
        }

        return xid;
    }
}