KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > command > XATransactionId


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

18 package org.apache.activemq.command;
19
20 import java.util.Arrays JavaDoc;
21
22 import javax.transaction.xa.Xid JavaDoc;
23
24 import org.apache.activemq.util.HexSupport;
25
26
27 /**
28  * @openwire:marshaller code="112"
29  * @version $Revision: 1.6 $
30  */

31 public class XATransactionId extends TransactionId implements Xid JavaDoc {
32     
33     public static final byte DATA_STRUCTURE_TYPE=CommandTypes.ACTIVEMQ_XA_TRANSACTION_ID;
34
35     private int formatId;
36     private byte[] branchQualifier;
37     private byte[] globalTransactionId;
38     
39     private transient int hash;
40     
41     public XATransactionId() {
42     }
43     
44     public XATransactionId(Xid JavaDoc xid) {
45         this.formatId = xid.getFormatId();
46         this.globalTransactionId = xid.getGlobalTransactionId();
47         this.branchQualifier = xid.getBranchQualifier();
48     }
49     
50     public byte getDataStructureType() {
51         return DATA_STRUCTURE_TYPE;
52     }
53         
54     public String JavaDoc getTransactionKey() {
55         return "XID:"+formatId+":"+HexSupport.toHexFromBytes(globalTransactionId)+":"+HexSupport.toHexFromBytes(branchQualifier);
56     }
57
58     public String JavaDoc toString() {
59         return getTransactionKey();
60     }
61     
62     public boolean isXATransaction() {
63         return true;
64     }
65
66     public boolean isLocalTransaction() {
67         return false;
68     }
69
70     /**
71      * @openwire:property version=1
72      */

73     public int getFormatId() {
74         return formatId;
75     }
76
77     /**
78      * @openwire:property version=1
79      */

80     public byte[] getGlobalTransactionId() {
81         return globalTransactionId;
82     }
83
84     /**
85      * @openwire:property version=1
86      */

87     public byte[] getBranchQualifier() {
88         return branchQualifier;
89     }
90
91     public void setBranchQualifier(byte[] branchQualifier) {
92         this.branchQualifier = branchQualifier;
93         this.hash=0;
94     }
95
96     public void setFormatId(int formatId) {
97         this.formatId = formatId;
98         this.hash=0;
99     }
100
101     public void setGlobalTransactionId(byte[] globalTransactionId) {
102         this.globalTransactionId = globalTransactionId;
103         this.hash=0;
104     }
105
106     public int hashCode() {
107         if( hash==0 ) {
108             hash = formatId;
109             hash = hash(globalTransactionId, hash);
110             hash = hash(branchQualifier, hash);
111             if (hash == 0) {
112                 hash = 0xaceace;
113             }
114         }
115         return hash;
116     }
117
118     private static int hash(byte[] bytes, int hash) {
119         for (int i = 0, size = bytes.length; i < size; i++) {
120             hash ^= bytes[i] << ((i % 4) * 8);
121         }
122         return hash;
123     }
124     
125     public boolean equals(Object JavaDoc o) {
126         if( o==null || o.getClass()!=XATransactionId.class )
127             return false;
128         XATransactionId xid = (XATransactionId)o;
129         return xid.formatId==formatId && Arrays.equals(xid.globalTransactionId,globalTransactionId)
130                && Arrays.equals(xid.branchQualifier, branchQualifier);
131     }
132     
133 }
134
Popular Tags