KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > transaction > XidImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.transaction;
30
31 import com.caucho.util.Alarm;
32 import com.caucho.util.CharBuffer;
33 import com.caucho.util.L10N;
34
35 import javax.transaction.xa.Xid JavaDoc;
36
37 /**
38  * Transaction identifier implementation.
39  */

40 public class XidImpl implements Xid JavaDoc {
41   private static L10N L = new L10N(XidImpl.class);
42
43   public static final int GLOBAL_LENGTH = 28;
44
45   private byte []_global;
46   private byte []_local;
47
48   /**
49    * Creates a new transaction id.
50    *
51    * @param id the 64 bit number for the id.
52    */

53   public XidImpl(long serverId, long randomId)
54   {
55     _global = new byte[GLOBAL_LENGTH];
56     _local = new byte[4];
57     _local[0] = 1;
58
59     // the global id has the requirement of being globally unique
60

61     // first 4 identify as Resin.
62
_global[0] = 'R';
63     _global[1] = 'e';
64     _global[2] = 's';
65     _global[3] = 'n';
66
67     // next 8 is the crc64 of the caucho.server-id
68
_global[4] = (byte) (serverId >> 56);
69     _global[5] = (byte) (serverId >> 48);
70     _global[6] = (byte) (serverId >> 40);
71     _global[7] = (byte) (serverId >> 32);
72     _global[8] = (byte) (serverId >> 24);
73     _global[9] = (byte) (serverId >> 16);
74     _global[10] = (byte) (serverId >> 8);
75     _global[11] = (byte) (serverId);
76
77     // next 8 is the current timestamp
78
long timestamp = Alarm.getCurrentTime();
79
80     _global[12] = (byte) (timestamp >> 56);
81     _global[13] = (byte) (timestamp >> 48);
82     _global[14] = (byte) (timestamp >> 40);
83     _global[15] = (byte) (timestamp >> 32);
84     _global[16] = (byte) (timestamp >> 24);
85     _global[17] = (byte) (timestamp >> 16);
86     _global[18] = (byte) (timestamp >> 8);
87     _global[19] = (byte) (timestamp);
88     
89     // next 8 is a 64-bit random long
90
_global[20] = (byte) (randomId >> 56);
91     _global[21] = (byte) (randomId >> 48);
92     _global[22] = (byte) (randomId >> 40);
93     _global[23] = (byte) (randomId >> 32);
94     _global[24] = (byte) (randomId >> 24);
95     _global[25] = (byte) (randomId >> 16);
96     _global[26] = (byte) (randomId >> 8);
97     _global[27] = (byte) (randomId);
98   }
99
100   XidImpl(XidImpl base, int branch)
101   {
102     _global = new byte[base._global.length];
103     _local = new byte[4];
104     _local[0] = (byte) (branch);
105
106     System.arraycopy(base._global, 0, _global, 0, _global.length);
107   }
108
109   XidImpl(byte []global, byte []local)
110   {
111     _global = new byte[global.length];
112     _local = new byte[local.length];
113
114     System.arraycopy(global, 0, _global, 0, global.length);
115     System.arraycopy(local, 0, _local, 0, local.length);
116   }
117
118   XidImpl(byte []global)
119   {
120     _global = new byte[global.length];
121     _local = new byte[4];
122
123     System.arraycopy(global, 0, _global, 0, global.length);
124     _local[0] = 1;
125   }
126
127   XidImpl(byte []global, int length)
128   {
129     _global = new byte[length];
130     _local = new byte[4];
131
132     System.arraycopy(global, 0, _global, 0, length);
133     _local[0] = 1;
134   }
135
136   public int getFormatId()
137   {
138     return 1234;
139   }
140
141   public byte []getBranchQualifier()
142   {
143     return _local;
144   }
145
146   public byte []getGlobalTransactionId()
147   {
148     return _global;
149   }
150
151   /**
152    * Clones the xid.
153    */

154   public Object JavaDoc clone()
155   {
156     return new XidImpl(_global, _local);
157   }
158
159   /**
160    * Returns hashCode.
161    */

162   public int hashCode()
163   {
164     byte []global = _global;
165
166     int hash = 37;
167     
168     for (int i = global.length - 1; i >= 0; i--)
169       hash = 65521 * hash + global[i];
170
171     return hash;
172   }
173
174   /**
175    * Returns equality.
176    */

177   public boolean equals(Object JavaDoc o)
178   {
179     if (! (o instanceof Xid JavaDoc))
180       return false;
181
182     Xid JavaDoc xid = (Xid JavaDoc) o;
183
184     byte []global = xid.getGlobalTransactionId();
185     byte []local = xid.getBranchQualifier();
186
187     if (global.length != _global.length)
188       return false;
189
190     byte []selfGlobal = _global;
191     byte []selfLocal = _local;
192
193     for (int i = global.length - 1; i >= 0; i--) {
194       if (global[i] != selfGlobal[i])
195     return false;
196     }
197
198     for (int i = local.length - 1; i >= 0; i--) {
199       if (local[i] != selfLocal[i])
200     return false;
201     }
202
203     return true;
204   }
205
206   /**
207    * Printable version of the transaction id.
208    */

209   public String JavaDoc toString()
210   {
211     CharBuffer cb = CharBuffer.allocate();
212
213     cb.append("Xid[");
214
215     byte []branch = getBranchQualifier();
216
217     addByte(cb, branch[0]);
218
219     cb.append(":");
220     
221     byte []global = getGlobalTransactionId();
222     for (int i = 24; i < 28; i++)
223       addByte(cb, global[i]);
224
225     cb.append("]");
226     
227     return cb.close();
228   }
229
230   /**
231    * Adds hex for debug
232    *
233    * @param cb the character buffer for the new value
234    * @param b the byte value
235    */

236   static private void addByte(CharBuffer cb, int b)
237   {
238     int h = (b / 16) & 0xf;
239     int l = b & 0xf;
240
241     if (h >= 10)
242       cb.append((char) ('a' + h - 10));
243     else
244       cb.append((char) ('0' + h));
245     
246     if (l >= 10)
247       cb.append((char) ('a' + l - 10));
248     else
249       cb.append((char) ('0' + l));
250   }
251 }
252
Popular Tags