001: //jTDS JDBC Driver for Microsoft SQL Server and Sybase
002: //Copyright (C) 2004 The jTDS Project
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: //
018: package net.sourceforge.jtds.jdbcx;
019:
020: import javax.transaction.xa.Xid;
021:
022: import net.sourceforge.jtds.jdbc.Support;
023:
024: /**
025: * jTDS implementation of the <code>Xid</code> interface.
026: *
027: * @version $Id: JtdsXid.java,v 1.3 2005/04/28 14:29:30 alin_sinpalean Exp $
028: */
029: public class JtdsXid implements Xid {
030: /** The size of an XID in bytes. */
031: public static final int XID_SIZE = 140;
032:
033: /** The global transaction ID. */
034: private final byte gtran[];
035: /** The branch qualifier ID. */
036: private final byte bqual[];
037: /** The format ID. */
038: public final int fmtId;
039: /** Precalculated hash value. */
040: public int hash;
041:
042: /**
043: * Construct an XID using an offset into a byte buffer.
044: *
045: * @param buf the byte buffer
046: * @param pos the offset
047: */
048: public JtdsXid(byte[] buf, int pos) {
049: fmtId = (buf[pos] & 0xFF) | ((buf[pos + 1] & 0xFF) << 8)
050: | ((buf[pos + 2] & 0xFF) << 16)
051: | ((buf[pos + 3] & 0xFF) << 24);
052: int t = buf[pos + 4];
053: int b = buf[pos + 8];
054: gtran = new byte[t];
055: bqual = new byte[b];
056: System.arraycopy(buf, 12 + pos, gtran, 0, t);
057: System.arraycopy(buf, 12 + t + pos, bqual, 0, b);
058: calculateHash();
059: }
060:
061: /**
062: * Construct an XID using two byte arrays.
063: *
064: * @param global the global transaction id
065: * @param branch the transaction branch
066: */
067: public JtdsXid(byte[] global, byte[] branch) {
068: fmtId = 0;
069: gtran = global;
070: bqual = branch;
071: calculateHash();
072: }
073:
074: /**
075: * Construct an XID as a clone of another XID.
076: */
077: public JtdsXid(Xid xid) {
078: fmtId = xid.getFormatId();
079: gtran = new byte[xid.getGlobalTransactionId().length];
080: System.arraycopy(xid.getGlobalTransactionId(), 0, gtran, 0,
081: gtran.length);
082: bqual = new byte[xid.getBranchQualifier().length];
083: System.arraycopy(xid.getBranchQualifier(), 0, bqual, 0,
084: bqual.length);
085: calculateHash();
086: }
087:
088: private void calculateHash() {
089: String x = Integer.toString(fmtId) + new String(gtran)
090: + new String(bqual);
091: hash = x.hashCode();
092: }
093:
094: /**
095: * Get the hash code for this object.
096: *
097: * @return the hash value of this object as a <code>int</code>
098: */
099: public int hashCode() {
100: return hash;
101: }
102:
103: /**
104: * Test for equality.
105: *
106: * @param obj the object to test for equality with this
107: * @return <code>boolean</code> true if the parameter equals this
108: */
109: public boolean equals(Object obj) {
110: if (obj == this )
111: return true;
112:
113: if (obj instanceof JtdsXid) {
114: JtdsXid xobj = (JtdsXid) obj;
115:
116: if (gtran.length + bqual.length == xobj.gtran.length
117: + xobj.bqual.length
118: && fmtId == xobj.fmtId) {
119: for (int i = 0; i < gtran.length; ++i) {
120: if (gtran[i] != xobj.gtran[i]) {
121: return false;
122: }
123: }
124:
125: for (int i = 0; i < bqual.length; ++i) {
126: if (bqual[i] != xobj.bqual[i]) {
127: return false;
128: }
129: }
130:
131: return true;
132: }
133: }
134: return false;
135: }
136:
137: //
138: // ------------------- javax.transaction.xa.Xid interface methods -------------------
139: //
140:
141: public int getFormatId() {
142: return fmtId;
143: }
144:
145: public byte[] getBranchQualifier() {
146: return bqual;
147: }
148:
149: public byte[] getGlobalTransactionId() {
150: return gtran;
151: }
152:
153: public String toString() {
154: StringBuffer txt = new StringBuffer(256);
155: txt.append("XID[Format=").append(fmtId).append(", Global=0x");
156: txt.append(Support.toHex(gtran)).append(", Branch=0x");
157: txt.append(Support.toHex(bqual)).append(']');
158: return txt.toString();
159: }
160: }
|