1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.store.access.GlobalXact |
4 | |
5 | Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.store.access; |
22 | |
23 | |
24 | /** |
25 | |
26 | This abstract class represents a global transaction id which can be tested |
27 | for equality against other transaction ids, which can be hashed into a |
28 | hash table, and which can be output as a string. |
29 | <P> |
30 | This class has 2 direct subclasses. |
31 | <UL> |
32 | <LI> org.apache.derby.iapi.store.access.xa.XAXactId : |
33 | this class is a specific implementation of the JTA Xid interface |
34 | <LI> org.apache.derby.impl.store.access.GlobalXactId : |
35 | this class represents internal cloudscape transaction ids |
36 | </UL> |
37 | <P> |
38 | The main reason for this class is to ensure that equality etc. works in a |
39 | consistent way across both subclasses. |
40 | **/ |
41 | |
42 | public abstract class GlobalXact { |
43 | |
44 | /************************************************************************** |
45 | * Protected Fields of the class |
46 | ************************************************************************** |
47 | */ |
48 | protected int format_id; |
49 | protected byte[] global_id; |
50 | protected byte[] branch_id; |
51 | |
52 | public boolean equals(Object other) |
53 | { |
54 | if (other == this) |
55 | return true; |
56 | |
57 | if (other instanceof GlobalXact) { |
58 | |
59 | GlobalXact other_xact = (GlobalXact) other; |
60 | |
61 | return( |
62 | java.util.Arrays.equals( |
63 | other_xact.global_id, |
64 | this.global_id) && |
65 | java.util.Arrays.equals( |
66 | other_xact.branch_id, |
67 | this.branch_id) && |
68 | other_xact.format_id == this.format_id); |
69 | |
70 | } |
71 | |
72 | return false; |
73 | } |
74 | |
75 | public String toString() |
76 | { |
77 | String globalhex = ""; |
78 | String branchhex = ""; |
79 | if (global_id != null) |
80 | { |
81 | int mask = 0; |
82 | for (int i = 0; i < global_id.length; i++) |
83 | { |
84 | mask = (global_id[i] & 0xFF); |
85 | globalhex += Integer.toHexString(mask); |
86 | } |
87 | } |
88 | |
89 | if (branch_id != null) |
90 | { |
91 | int mask = 0; |
92 | for (int i = 0; i < branch_id.length; i++) |
93 | { |
94 | mask = (branch_id[i] & 0xFF); |
95 | branchhex += Integer.toHexString(mask); |
96 | } |
97 | } |
98 | |
99 | return("(" + format_id + "," + globalhex + "," + branchhex + ")"); |
100 | |
101 | } |
102 | |
103 | |
104 | /** |
105 | Provide a hashCode which is compatable with the equals() method. |
106 | |
107 | @see java.lang.Object#hashCode |
108 | **/ |
109 | public int hashCode() |
110 | { |
111 | // make sure hash does not overflow int, the only unknown is |
112 | // format_id. Lop off top bits. |
113 | int hash = global_id.length + branch_id.length + (format_id & 0xFFFFFFF); |
114 | |
115 | for (int i = 0; i < global_id.length; i++) |
116 | { |
117 | hash += global_id[i]; |
118 | } |
119 | for (int i = 0; i < branch_id.length; i++) |
120 | { |
121 | hash += branch_id[i]; |
122 | } |
123 | |
124 | return(hash); |
125 | } |
126 | |
127 | } |
128 | |
129 | |
130 | |