1 package org
.argeo
.transaction
.simple
;
3 import java
.io
.Serializable
;
4 import java
.nio
.ByteBuffer
;
5 import java
.util
.Arrays
;
8 import javax
.transaction
.xa
.Xid
;
11 * Implementation of {@link Xid} based on {@link UUID}, using max significant
12 * bits as global transaction id, and least significant bits as branch
15 public class UuidXid
implements Xid
, Serializable
{
16 private static final long serialVersionUID
= -5380531989917886819L;
17 public final static int FORMAT
= (int) serialVersionUID
;
19 private final static int BYTES_PER_LONG
= Long
.SIZE
/ Byte
.SIZE
;
21 private final int format
;
22 private final byte[] globalTransactionId
;
23 private final byte[] branchQualifier
;
24 private final String uuid
;
25 private final int hashCode
;
28 this(UUID
.randomUUID());
31 public UuidXid(UUID uuid
) {
33 this.globalTransactionId
= uuidToBytes(uuid
.getMostSignificantBits());
34 this.branchQualifier
= uuidToBytes(uuid
.getLeastSignificantBits());
35 this.uuid
= uuid
.toString();
36 this.hashCode
= uuid
.hashCode();
39 public UuidXid(Xid xid
) {
40 this(xid
.getFormatId(), xid
.getGlobalTransactionId(), xid
41 .getBranchQualifier());
44 private UuidXid(int format
, byte[] globalTransactionId
,
45 byte[] branchQualifier
) {
47 this.globalTransactionId
= globalTransactionId
;
48 this.branchQualifier
= branchQualifier
;
49 this.uuid
= bytesToUUID(globalTransactionId
, branchQualifier
)
51 this.hashCode
= uuid
.hashCode();
55 public int getFormatId() {
60 public byte[] getGlobalTransactionId() {
61 return Arrays
.copyOf(globalTransactionId
, globalTransactionId
.length
);
65 public byte[] getBranchQualifier() {
66 return Arrays
.copyOf(branchQualifier
, branchQualifier
.length
);
70 public int hashCode() {
75 public boolean equals(Object obj
) {
78 if (obj
instanceof UuidXid
) {
79 UuidXid that
= (UuidXid
) obj
;
80 return Arrays
.equals(globalTransactionId
, that
.globalTransactionId
)
81 && Arrays
.equals(branchQualifier
, that
.branchQualifier
);
83 if (obj
instanceof Xid
) {
85 return Arrays
.equals(globalTransactionId
,
86 that
.getGlobalTransactionId())
88 .equals(branchQualifier
, that
.getBranchQualifier());
90 return uuid
.equals(obj
.toString());
94 protected Object
clone() throws CloneNotSupportedException
{
95 return new UuidXid(format
, globalTransactionId
, branchQualifier
);
99 public String
toString() {
103 public UUID
asUuid() {
104 return bytesToUUID(globalTransactionId
, branchQualifier
);
107 public static byte[] uuidToBytes(long bits
) {
108 ByteBuffer buffer
= ByteBuffer
.allocate(BYTES_PER_LONG
);
109 buffer
.putLong(0, bits
);
110 return buffer
.array();
113 public static UUID
bytesToUUID(byte[] most
, byte[] least
) {
114 if (most
.length
< BYTES_PER_LONG
)
115 most
= Arrays
.copyOf(most
, BYTES_PER_LONG
);
116 if (least
.length
< BYTES_PER_LONG
)
117 least
= Arrays
.copyOf(least
, BYTES_PER_LONG
);
118 ByteBuffer buffer
= ByteBuffer
.allocate(2 * BYTES_PER_LONG
);
119 buffer
.put(most
, 0, BYTES_PER_LONG
);
120 buffer
.put(least
, 0, BYTES_PER_LONG
);
122 return new UUID(buffer
.getLong(), buffer
.getLong());
125 // public static void main(String[] args) {
126 // UUID uuid = UUID.randomUUID();
127 // System.out.println(uuid);
128 // uuid = bytesToUUID(uuidToBytes(uuid.getMostSignificantBits()),
129 // uuidToBytes(uuid.getLeastSignificantBits()));
130 // System.out.println(uuid);