KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > log > XidImpl2


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.transaction.log;
19
20 import java.io.Serializable JavaDoc;
21 import java.util.Arrays JavaDoc;
22
23 import javax.transaction.xa.Xid JavaDoc;
24
25 /**
26  * Unique id for a transaction. This implementation is backed by a single byte buffer
27  * so can do less copying than one backed by several byte buffers for the different components.
28  *
29  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
30  */

31 public class XidImpl2 implements Xid JavaDoc, Serializable JavaDoc {
32     private static int HEADER_SIZE = 4;
33     private static int ACTION_POS = 0;
34     private static int GLOBALID_SIZE_POS = 1;
35     private static int BRANCHID_SIZE_POS = 2;
36     //3 unused
37
private static int FORMAT_ID = 0x4765526f; // Gero
38
private static int FORMAT_SIZE = 4;
39
40     private static byte[] FORMAT_ID_BYTES = "Gero".getBytes();
41
42     private final byte[] buffer = new byte[HEADER_SIZE + FORMAT_SIZE + Xid.MAXGTRIDSIZE + Xid.MAXBQUALSIZE];
43     private int hash;
44     private Object JavaDoc key;
45
46     /**
47      * Constructor taking a global id (for the main transaction)
48      * @param globalId the global transaction id
49      */

50     public XidImpl2(byte[] globalId) {
51         System.arraycopy(FORMAT_ID_BYTES, 0, buffer, HEADER_SIZE, FORMAT_SIZE);
52         buffer[GLOBALID_SIZE_POS] = (byte) globalId.length;
53         System.arraycopy(globalId, 0, buffer, HEADER_SIZE + FORMAT_SIZE, Xid.MAXGTRIDSIZE);
54
55         //this.hash = hash(buffer);
56
}
57
58     /**
59      * Constructor for a branch id
60      * @param global the xid of the global transaction this branch belongs to
61      * @param branch the branch id
62      */

63     public XidImpl2(Xid JavaDoc global, byte[] branch) {
64         if (global instanceof XidImpl2) {
65             System.arraycopy(((XidImpl2) global).buffer, 0, buffer, 0, HEADER_SIZE + FORMAT_SIZE + Xid.MAXGTRIDSIZE);
66         } else {
67             System.arraycopy(FORMAT_ID_BYTES, 0, buffer, HEADER_SIZE, FORMAT_SIZE);
68             byte[] globalId = global.getGlobalTransactionId();
69             System.arraycopy(globalId, 0, buffer, HEADER_SIZE + FORMAT_SIZE, globalId.length);
70         }
71         buffer[BRANCHID_SIZE_POS] = (byte) branch.length;
72         System.arraycopy(branch, 0, buffer, HEADER_SIZE + FORMAT_SIZE + Xid.MAXGTRIDSIZE, Xid.MAXBQUALSIZE);
73         //hash = hash(buffer);
74
}
75
76     public XidImpl2(int formatId, byte[] globalId, byte[] branch) {
77         //todo this is wrong, it ignores formatId supplied. Maybe this is ok?
78
System.arraycopy(FORMAT_ID_BYTES, 0, buffer, HEADER_SIZE, FORMAT_SIZE);
79         System.arraycopy(globalId, 0, buffer, HEADER_SIZE + FORMAT_SIZE, globalId.length);
80         buffer[BRANCHID_SIZE_POS] = (byte) branch.length;
81         System.arraycopy(branch, 0, buffer, HEADER_SIZE + FORMAT_SIZE + Xid.MAXGTRIDSIZE, Xid.MAXBQUALSIZE);
82         //hash = hash(buffer);
83
}
84
85     private int hash(byte[] id) {
86         int hash = 0;
87         for (int i = 0; i < id.length; i++) {
88             hash = (hash * 37) + id[i];
89         }
90         return hash;
91     }
92
93     public int getFormatId() {
94         return FORMAT_ID;
95     }
96
97     public byte[] getGlobalTransactionId() {
98         byte[] globalId = new byte[buffer[GLOBALID_SIZE_POS]];
99         System.arraycopy(buffer, HEADER_SIZE + FORMAT_SIZE, globalId, 0, buffer[GLOBALID_SIZE_POS]);
100         return globalId;
101     }
102
103     public byte[] getBranchQualifier() {
104         byte[] branchId = new byte[buffer[BRANCHID_SIZE_POS]];
105         System.arraycopy(buffer, HEADER_SIZE + FORMAT_SIZE + Xid.MAXGTRIDSIZE, branchId, 0, buffer[BRANCHID_SIZE_POS]);
106         return branchId;
107     }
108
109     public boolean equals(Object JavaDoc obj) {
110         if (obj instanceof XidImpl2 == false) {
111             return false;
112         }
113         XidImpl2 other = (XidImpl2) obj;
114         return Arrays.equals(buffer, other.buffer);
115     }
116
117     public int hashCode() {
118         if (hash == 0) {
119             hash = hash(buffer);
120         }
121         return hash;
122     }
123
124     public String JavaDoc toString() {
125         StringBuffer JavaDoc s = new StringBuffer JavaDoc("[formatId=Gero,");
126         s.append("globalId=");
127         for (int i = FORMAT_SIZE; i < FORMAT_SIZE + Xid.MAXGTRIDSIZE; i++) {
128             s.append(Integer.toHexString(buffer[i]));
129         }
130         s.append(",branchId=");
131         for (int i = FORMAT_SIZE + Xid.MAXGTRIDSIZE; i < buffer.length; i++) {
132             s.append(Integer.toHexString(buffer[i]));
133         }
134         s.append("]");
135         return s.toString();
136     }
137
138     byte[] getBuffer(byte action) {
139         buffer[ACTION_POS] = action;
140         return buffer;
141     }
142
143     public void setKey(Object JavaDoc key) {
144         this.key = key;
145     }
146
147     public Object JavaDoc getKey() {
148         return key;
149     }
150 }
151
Popular Tags