001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mq;
023:
024: import java.io.Serializable;
025: import java.util.Arrays;
026:
027: import javax.transaction.xa.Xid;
028:
029: /**
030: * This class is a wrapper for non-serializable implementations of
031: * java.transaction.xa.Xid.
032: *
033: * @author Daniel Bloomfield Ramagem (daniel.ramagem@gmail.com)
034: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
035: * @version $Revision: 57198 $
036: */
037: public class JBossMQXid implements Serializable, Xid {
038: /** The serialVersionUID */
039: private static final long serialVersionUID = -2227021688745286343L;
040:
041: /** The format id */
042: private int formatId;
043:
044: /** The gid */
045: private byte[] globalTransactionId;
046:
047: /** The branch */
048: private byte[] branchQualifier;
049:
050: /** Cached toString() */
051: private transient String cachedToString;
052:
053: /** Cached hashCode() */
054: private transient int cachedHashCode;
055:
056: /**
057: * Create a new wrapper Xid
058: *
059: * @param xid the wrapped xid
060: */
061: public JBossMQXid(Xid xid) {
062: formatId = xid.getFormatId();
063: globalTransactionId = xid.getGlobalTransactionId();
064: branchQualifier = xid.getBranchQualifier();
065: }
066:
067: public int getFormatId() {
068: return formatId;
069: }
070:
071: public byte[] getGlobalTransactionId() {
072: return globalTransactionId;
073: }
074:
075: public byte[] getBranchQualifier() {
076: return branchQualifier;
077: }
078:
079: public boolean equals(Object object) {
080: if (object == null || (object instanceof Xid) == false)
081: return false;
082:
083: Xid other = (Xid) object;
084: return (formatId == other.getFormatId()
085: && Arrays.equals(globalTransactionId, other
086: .getGlobalTransactionId()) && Arrays.equals(
087: branchQualifier, other.getBranchQualifier()));
088: }
089:
090: public int hashCode() {
091: if (cachedHashCode == 0) {
092: cachedHashCode = formatId;
093: for (int i = 0; i < globalTransactionId.length; ++i)
094: cachedHashCode += globalTransactionId[i];
095: }
096: return cachedHashCode;
097: }
098:
099: public String toString() {
100: if (cachedToString == null) {
101: StringBuffer buffer = new StringBuffer();
102: buffer.append("JBossMQXid[FormatId=").append(getFormatId());
103: buffer.append(" GlobalId=").append(
104: new String(getGlobalTransactionId()).trim());
105: byte[] branchQualifer = getBranchQualifier();
106: buffer.append(" BranchQual=");
107: if (branchQualifer == null)
108: buffer.append("null");
109: else
110: buffer.append(new String(getBranchQualifier()).trim());
111: buffer.append(']');
112: cachedToString = buffer.toString();
113: }
114: return cachedToString;
115: }
116: }
|