KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.transaction.xa.Xid JavaDoc;
25
26 /**
27  * This object encapsulates the ID of a transaction.
28  * This implementation is immutable and always serializable at runtime.
29  *
30  * @see TransactionImpl
31  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard ???berg</a>
32  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
33  * @author <a HREF="reverbel@ime.usp.br">Francisco Reverbel</a>
34  * @version $Revision: 37459 $
35  */

36 public class XidImpl
37    implements Xid JavaDoc, java.io.Serializable JavaDoc
38 {
39    // Constants -----------------------------------------------------
40

41    static final long serialVersionUID = -4175838107150528488L;
42
43    public static final int JBOSS_FORMAT_ID = 0x0101;
44
45    // Static variable -----------------------------------------------
46

47    private static boolean trulyGlobalIdsEnabled = false;
48
49    // Attributes ----------------------------------------------------
50

51    /**
52     * Format id of this instance.
53     * A JBoss-generated Xids has JBOSS_FORMAT_ID in this field.
54     */

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

63    private final byte[] globalId;
64
65    /**
66     * Branch qualifier of this instance.
67     * This identifies the branch of a transaction.
68     */

69    private final byte[] branchId;
70
71    /**
72     * Hash code of this instance. This is really a sequence number.
73     */

74    private final int hash;
75
76    /**
77     * Local id of this instance. This field uniquely identifies a
78     * transaction within a given JBoss server.
79     */

80    private final long localId;
81
82    /**
83     * Global id of this instance. This field uniquely identifies a
84     * transaction in a distributed environment.
85     */

86    private final GlobalId trulyGlobalId;
87
88
89    // Static --------------------------------------------------------
90

91    /**
92     * Setter for class variable trulyGlobalIdsEnabled.
93     */

94    public static void setTrulyGlobalIdsEnabled(boolean newValue)
95    {
96       trulyGlobalIdsEnabled = newValue;
97    }
98
99    /**
100     * Getter for class variable trulyGlobalIdsEnabled.
101     */

102    public static boolean getTrulyGlobalIdsEnabled()
103    {
104       return trulyGlobalIdsEnabled;
105    }
106
107    /**
108     * Return a string that describes any Xid instance.
109     */

110    public static String JavaDoc toString(Xid JavaDoc id)
111    {
112       if (id == null)
113          return "[NULL Xid]";
114
115       String JavaDoc s = id.getClass().getName();
116       s = s.substring(s.lastIndexOf('.') + 1);
117       s = s + "[FormatId=" + id.getFormatId()
118             + ", GlobalId=" + new String JavaDoc(id.getGlobalTransactionId()).trim()
119             + ", BranchQual=" + new String JavaDoc(id.getBranchQualifier()).trim()
120             + ((id instanceof XidImpl)
121                   ? ", localId=" + LocalId.toString(((XidImpl)id).localId )
122                   : "")
123             + "]";
124
125       return s;
126    }
127
128    // Constructors --------------------------------------------------
129

130    /**
131     * Create a new instance.
132     */

133    public XidImpl(int formatId,
134                   byte[] globalId, byte[] branchId, int hash, long localId)
135    {
136       this.formatId = formatId;
137       this.globalId = globalId;
138       this.branchId = branchId;
139       this.hash = hash;
140       this.localId = localId;
141       this.trulyGlobalId = (trulyGlobalIdsEnabled)
142                            ? new GlobalId(formatId, globalId)
143                            : null;
144    }
145
146    /**
147     * Create a new instance with JBOSS_FORMAT_ID.
148     */

149    XidImpl(byte[] globalId, byte[] branchId, int hash, long localId)
150    {
151       this.formatId = JBOSS_FORMAT_ID;
152       this.globalId = globalId;
153       this.branchId = branchId;
154       this.hash = hash;
155       this.localId = localId;
156       this.trulyGlobalId = (trulyGlobalIdsEnabled)
157                            ? new GlobalId(JBOSS_FORMAT_ID, globalId, hash)
158                            : null;
159    }
160
161    /**
162     * Create a new branch of an existing global transaction ID.
163     *
164     * @param xidImpl The transaction ID to create a new branch of.
165     * @param branchId The ID of the new branch.
166     *
167     */

168    public XidImpl(final XidImpl xidImpl, final byte[] branchId)
169    {
170       this.formatId = xidImpl.formatId;
171       this.globalId = xidImpl.globalId; // reuse array, we never modify it
172
this.branchId = branchId;
173       this.hash = xidImpl.hash;
174       this.localId = xidImpl.localId;
175       this.trulyGlobalId = (trulyGlobalIdsEnabled)
176                            ? xidImpl.trulyGlobalId
177                            : null;
178    }
179
180    /**
181     * Create a new branch of an existing global transaction.
182     *
183     * @param globalId identifies the transaction to create a new branch of
184     * @param branchId the id that distiguishes the new branch from other
185     * branches of the same transaction
186     * @param localId the local id of the transaction
187     *
188     */

189    public XidImpl(final GlobalId globalId, final byte[] branchId, long localId)
190    {
191       this.formatId = globalId.getFormatId();
192       this.globalId = globalId.getGlobalTransactionId();
193       this.branchId = branchId;
194       this.hash = globalId.hashCode();
195       this.localId = localId;
196       this.trulyGlobalId = (trulyGlobalIdsEnabled)
197                            ? globalId
198                            : null;
199    }
200
201    // Public --------------------------------------------------------
202

203    // Xid implementation --------------------------------------------
204

205    /**
206     * Return the global transaction id of this transaction.
207     */

208    public byte[] getGlobalTransactionId()
209    {
210       return (byte[])globalId.clone();
211    }
212
213    /**
214     * Return the branch qualifier of this transaction.
215     */

216    public byte[] getBranchQualifier()
217    {
218       if (branchId.length == 0)
219          return branchId; // Zero length arrays are immutable.
220
else
221          return (byte[])branchId.clone();
222    }
223
224    /**
225     * Return the format identifier of this transaction.
226     *
227     * The format identifier augments the global id and specifies
228     * how the global id and branch qualifier should be interpreted.
229     */

230    public int getFormatId()
231    {
232       // The id we return here should be different from all other transaction
233
// implementations.
234
// Known IDs are:
235
// -1: Sometimes used to denote a null transaction id.
236
// 0: OSI TP (javadoc states OSI CCR, but that is a bit misleading
237
// as OSI CCR doesn't even have ACID properties. But OSI CCR and
238
// OSI TP do have the same id format.)
239
// 1: Was used by early betas of jBoss.
240
// 0x0101: The JBOSS_FORMAT_ID we use here.
241
// 0xBB14: Used by JONAS.
242
// 0xBB20: Used by JONAS.
243

244       return formatId;
245    }
246
247    /**
248     * Compare for equality.
249     *
250     * Instances are considered equal if they are both instances of XidImpl,
251     * and if they have the same format id, the same global transaction id
252     * and the same transaction branch qualifier.
253     */

254    public boolean equals(Object JavaDoc obj)
255    {
256       if(obj==this)
257          return true;
258       if (obj instanceof XidImpl) {
259          XidImpl other = (XidImpl)obj;
260
261          if (formatId != other.formatId ||
262              globalId.length != other.globalId.length ||
263              branchId.length != other.branchId.length)
264             return false;
265
266          for (int i = 0; i < globalId.length; ++i)
267             if (globalId[i] != other.globalId[i])
268                return false;
269
270          for (int i = 0; i < branchId.length; ++i)
271             if (branchId[i] != other.branchId[i])
272                return false;
273
274          return true;
275       }
276       return false;
277    }
278
279    public int hashCode()
280    {
281       return hash;
282    }
283
284    public String JavaDoc toString()
285    {
286       return toString(this);
287    }
288
289    // Methods specific to JBoss Xid implementation ------------------
290

291    /**
292     * Return the local id that identifies this transaction
293     * within the JBoss server.
294     */

295    public long getLocalIdValue()
296    {
297       return localId;
298    }
299
300    /**
301     * Return a LocalId instance that identifies this transaction
302     * within the JBoss server.
303     */

304    public LocalId getLocalId()
305    {
306       return new LocalId(localId);
307    }
308
309    /**
310     * Return a GlobalId instance that identifies this transaction
311     * in a distributed environment.
312     */

313    public GlobalId getTrulyGlobalId()
314    {
315       return trulyGlobalId;
316    }
317
318    /**
319     * Compare for same transaction.
320     *
321     * Instances represent the same transaction if they have the same
322     * format id and global transaction id.
323     */

324    public boolean sameTransaction(XidImpl other)
325    {
326       if(other == this)
327          return true;
328       if (formatId != other.formatId ||
329           globalId.length != other.globalId.length)
330          return false;
331
332       for (int i = 0; i < globalId.length; ++i)
333          if (globalId[i] != other.globalId[i])
334             return false;
335
336       return true;
337    }
338
339    /**
340     * Return the global transaction id of this transaction.
341     * Unlike the {@link #getGlobalTransactionId()} method, this one
342     * returns a reference to the global id byte array that may <em>not</em>
343     * be changed.
344     */

345    public byte[] getInternalGlobalTransactionId()
346    {
347       return globalId;
348    }
349
350 }
351
352
Popular Tags