KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > GlobalId


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.tm;
23
24 import java.io.IOException JavaDoc;
25 import javax.transaction.xa.Xid JavaDoc;
26
27 /**
28  * This object encapsulates the global transaction ID of a transaction.
29  * It is similar to an Xid, but holds only the GlobalId part.
30  * This implementation is immutable and always serializable at runtime.
31  *
32  * @see XidImpl
33  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
34  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
35  * @version $Revision: 37459 $
36  */

37 public class GlobalId
38    implements java.io.Externalizable JavaDoc
39 {
40    static final long serialVersionUID = -230282197045463046L;
41
42    private static String JavaDoc thisClassName;
43    
44    static {
45       thisClassName = GlobalId.class.getName();
46       thisClassName =
47          thisClassName.substring(thisClassName.lastIndexOf('.') + 1);
48    }
49    
50    /**
51     * Format id of this instance.
52     */

53     private int formatId;
54
55    /**
56     * Global transaction id of this instance.
57     * The coding of this class depends on the fact that this variable is
58     * initialized in the constructor and never modified. References to
59     * this array are never given away, instead a clone is delivered.
60     */

61    private byte[] globalId;
62
63    /**
64     * Hash code of this instance. For a native GlobalId (one whose formatId
65     * is XidImpl.JBOSS_FORMAT_ID), this is really a sequence number.
66     */

67    private int hash;
68
69    // Constructors --------------------------------------------------
70

71    public GlobalId()
72    {
73       // Used for Externalizable support
74
}
75    
76    /**
77     * Create a new instance. This constructor is package-private, as it
78     * trusts the hash parameter to be good.
79     */

80    GlobalId(int formatId, byte[] globalId, int hash)
81    {
82       this.formatId = formatId;
83       this.globalId = globalId;
84       this.hash = hash;
85    }
86
87    /**
88     * Create a new instance. This constructor is public <em>only</em>
89     * to get around a class loader problem; it should be package-private.
90     */

91    public GlobalId(int formatId, byte[] globalId)
92    {
93       this.formatId = formatId;
94       this.globalId = globalId;
95       hash = computeHash();
96    }
97
98    public GlobalId(Xid JavaDoc xid)
99    {
100       formatId = xid.getFormatId();
101       globalId = xid.getGlobalTransactionId();
102       if (xid instanceof XidImpl && formatId == XidImpl.JBOSS_FORMAT_ID)
103       {
104          // native GlobalId: use its hash code (a sequence number)
105
hash = xid.hashCode();
106       }
107       else
108       {
109          // foreign GlobalId: do the hash computation
110
hash = computeHash();
111       }
112    }
113
114    public GlobalId(int formatId, int bqual_length, byte[] tid)
115    {
116       this.formatId = formatId;
117       if (bqual_length == 0)
118          globalId = tid;
119       else
120       {
121          int len = tid.length - bqual_length;
122          globalId = new byte[len];
123          System.arraycopy(tid, 0, globalId, 0, len);
124       }
125       hash = computeHash();
126    }
127
128    // Public --------------------------------------------------------
129

130    /**
131     * Returns the global transaction id of this transaction.
132     */

133    public byte[] getGlobalTransactionId()
134    {
135       return (byte[])globalId.clone();
136    }
137
138    /**
139     * Returns the format identifier of this transaction.
140     */

141    public int getFormatId()
142    {
143       return formatId;
144    }
145
146    /**
147     * Compare for equality.
148     *
149     * Instances are considered equal if they both refer to the same
150     * global transaction id.
151     */

152    public boolean equals(Object JavaDoc obj)
153    {
154       if (obj instanceof GlobalId) {
155          GlobalId other = (GlobalId)obj;
156
157          if (formatId != other.formatId)
158             return false;
159
160          if (globalId == other.globalId)
161             return true;
162
163          if (globalId.length != other.globalId.length)
164             return false;
165
166          int len = globalId.length;
167          for (int i = 0; i < len; ++i)
168             if (globalId[i] != other.globalId[i])
169                return false;
170
171          return true;
172       }
173       return false;
174    }
175
176    public int hashCode()
177    {
178       return hash;
179    }
180    
181    public String JavaDoc toString()
182    {
183       return thisClassName + "[formatId=" + formatId
184             + ", globalId=" + new String JavaDoc(globalId).trim()
185             + ", hash=" + hash + "]";
186    }
187    
188    // Externalizable implementation ---------------------------------
189

190    public void writeExternal(java.io.ObjectOutput JavaDoc out)
191       throws IOException JavaDoc
192    {
193       out.writeInt(formatId);
194       out.writeObject(globalId);
195    }
196    
197    public void readExternal(java.io.ObjectInput JavaDoc in)
198       throws IOException JavaDoc, ClassNotFoundException JavaDoc
199    {
200       formatId = in.readInt();
201       globalId = (byte[])in.readObject();
202       hash = computeHash();
203    }
204
205    // Private -------------------------------------------------------
206

207    private int computeHash()
208    {
209       if (formatId == XidImpl.JBOSS_FORMAT_ID)
210       {
211          return (int)TransactionImpl.xidFactory.extractLocalIdFrom(globalId);
212       }
213       else
214       {
215          int len = globalId.length;
216          int hashval = 0;
217          
218          // TODO: use a better hash function
219
for (int i = 0; i < len; ++i)
220             hashval = 3 * globalId[i] + hashval;
221          hashval += formatId;
222          return hashval;
223       }
224    }
225
226 }
227
Popular Tags