001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: James Devenish
005: */
006: package org.h2.test.jdbcx;
007:
008: import java.net.InetAddress;
009: import java.net.UnknownHostException;
010: import java.text.NumberFormat;
011: import javax.transaction.xa.Xid;
012:
013: /**
014: * A utility class for the basic XA test.
015: */
016: public class TestXid implements Xid {
017: private static final NumberFormat NF;
018:
019: private static int fXidCounter;
020: private int fFormatId;
021: private byte[] fGlobalTransactionId;
022: private byte[] fBranchQualifier;
023: private int fId;
024: private long fCreationTime;
025:
026: static {
027: NumberFormat nf = NumberFormat.getIntegerInstance();
028: nf.setMaximumIntegerDigits(5);
029: nf.setMinimumIntegerDigits(5);
030: nf.setGroupingUsed(false);
031: NF = nf;
032: }
033:
034: public static void main(String[] args) {
035: new TestXid();
036: }
037:
038: public TestXid() {
039: this (1);
040: }
041:
042: public TestXid(int branch) {
043: synchronized (TestXid.class) {
044: fXidCounter++;
045: fId = fXidCounter;
046: }
047: fCreationTime = System.currentTimeMillis();
048: String host;
049: try {
050: InetAddress ia = InetAddress.getLocalHost();
051: host = ia.getHostName();
052: } catch (UnknownHostException e) {
053: host = "localhost";
054: }
055:
056: fFormatId = 0;
057: fGlobalTransactionId = new byte[MAXGTRIDSIZE];
058: fBranchQualifier = new byte[MAXBQUALSIZE];
059:
060: StringBuffer sb;
061: byte[] ba;
062:
063: sb = new StringBuffer();
064: sb.append(host);
065: sb.append(":");
066: sb.append(fId);
067: sb.append(":");
068: sb.append(fCreationTime);
069: // System.out.println("global transaction id: " + sb.toString());
070: ba = sb.toString().getBytes();
071:
072: for (int i = 0; i < MAXGTRIDSIZE; i++) {
073: fGlobalTransactionId[i] = (byte) ' ';
074: }
075: for (int i = 0; i < ba.length; i++) {
076: fGlobalTransactionId[i] = ba[i];
077: }
078:
079: sb = new StringBuffer(NF.format(branch));
080: // System.out.println("branch qualifier: " + sb.toString());
081: ba = sb.toString().getBytes();
082: for (int i = 0; i < MAXBQUALSIZE; i++) {
083: fBranchQualifier[i] = (byte) ' ';
084: }
085: for (int i = 0; i < ba.length; i++) {
086: fBranchQualifier[i] = ba[i];
087: }
088: }
089:
090: public int getFormatId() {
091: return fFormatId;
092: }
093:
094: public byte[] getGlobalTransactionId() {
095: return fGlobalTransactionId;
096: }
097:
098: public byte[] getBranchQualifier() {
099: return fBranchQualifier;
100: }
101:
102: }
|