KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > connectionmanager > xa > JcaXid


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.connectionmanager.xa;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 import javax.transaction.xa.Xid JavaDoc;
28
29 /**
30  * A JcaXid.
31  *
32  * @author <a HREF="weston.price@jboss.com">Weston Price</a>
33  * @version $Revision: 1.1 $
34  */

35 public class JcaXid implements Serializable JavaDoc, Xid JavaDoc
36 {
37
38    /** The serialVersionUID */
39    private static final long serialVersionUID = 8226195409384804425L;
40
41    /** The formatId */
42    private int formatId;
43    
44    /** The globalTransactionId */
45    private byte[] globalTransactionId;
46    
47    /** The branchQualifier */
48    private byte[] branchQualifier;
49    
50    /** Cached toString() */
51    private transient String JavaDoc cachedToString;
52
53    /** Cached hashCode() */
54    private transient int cachedHashCode;
55
56    /** Whether or not to pad the id */
57    private boolean pad;
58    
59    public JcaXid(Xid JavaDoc xid)
60    {
61       this(false, xid);
62       
63    }
64    
65    public JcaXid(boolean pad, Xid JavaDoc xid)
66    {
67       this.pad = pad;
68       
69       branchQualifier = (pad) ? new byte[Xid.MAXBQUALSIZE] : new byte[xid.getBranchQualifier().length];
70       System.arraycopy(xid.getBranchQualifier(), 0, branchQualifier, 0, xid.getBranchQualifier().length);
71       this.globalTransactionId = xid.getGlobalTransactionId();
72       this.formatId = xid.getFormatId();
73       
74    }
75
76    public byte[] getBranchQualifier()
77    {
78       return this.branchQualifier;
79    }
80
81    public int getFormatId()
82    {
83       return this.formatId;
84    }
85
86    public byte[] getGlobalTransactionId()
87    {
88       return this.globalTransactionId;
89    }
90    
91    public boolean equals(Object JavaDoc object)
92    {
93       if (object == null || (object instanceof Xid JavaDoc) == false)
94          return false;
95
96       Xid JavaDoc other = (Xid JavaDoc) object;
97       return
98       (
99          formatId == other.getFormatId() &&
100          Arrays.equals(globalTransactionId, other.getGlobalTransactionId()) &&
101          Arrays.equals(branchQualifier, other.getBranchQualifier())
102       );
103    }
104
105    public int hashCode()
106    {
107       if (cachedHashCode == 0)
108       {
109          cachedHashCode = formatId;
110          for (int i = 0; i < globalTransactionId.length; ++i)
111             cachedHashCode += globalTransactionId[i];
112       }
113       return cachedHashCode;
114    }
115
116    public String JavaDoc toString()
117    {
118       if (cachedToString == null)
119       {
120          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
121          buffer.append("JcaXid[FormatId=").append(getFormatId());
122          buffer.append(" GlobalId=").append(new String JavaDoc(getGlobalTransactionId()).trim());
123          byte[] branchQualifer = getBranchQualifier();
124          buffer.append(" BranchQual=");
125          if (branchQualifer == null)
126             buffer.append("null");
127          else
128             buffer.append(new String JavaDoc(getBranchQualifier()).trim());
129          buffer.append(']');
130          cachedToString = buffer.toString();
131       }
132       return cachedToString;
133    }
134 }
135
Popular Tags