KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.tm;
8
9 import java.io.Serializable JavaDoc;
10 import java.net.InetAddress JavaDoc;
11 import java.net.UnknownHostException JavaDoc;
12 import javax.transaction.xa.Xid JavaDoc;
13
14 /**
15  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
16  */

17
18 public class XidImpl implements Xid JavaDoc, Serializable JavaDoc {
19     private static volatile int hash = 1;
20
21     public static final int FORMAT_ID = 0x0808;
22
23     // ???? oracle ????? Global Id ?? Branch Id ??? 64 ??????
24
private byte[] globalId = new byte[Xid.MAXGTRIDSIZE];
25     private byte[] branchId = new byte[Xid.MAXBQUALSIZE];
26
27     private static String JavaDoc host;
28
29     static {
30         try {
31             InetAddress JavaDoc local = InetAddress.getLocalHost();
32             host = local.getHostName() + "-" + local.getHostAddress();
33         }
34         catch(UnknownHostException JavaDoc e) {
35             host = "localhost-0.0.0.0";
36         }
37     }
38
39     public XidImpl() {
40         String JavaDoc xid = new StringBuffer JavaDoc().append(host).append("-").append(hash++).toString();
41         if(xid.length() > Xid.MAXBQUALSIZE) {
42             xid = xid.substring(xid.length() - Xid.MAXBQUALSIZE);
43         }
44
45         byte[] _globalId = xid.getBytes();
46         if(_globalId.length > Xid.MAXGTRIDSIZE) {
47             System.arraycopy(_globalId, 0, globalId, _globalId.length - Xid.MAXGTRIDSIZE, Xid.MAXGTRIDSIZE);
48         }
49         else {
50             System.arraycopy(xid.getBytes(), 0, globalId, 0, xid.getBytes().length);
51         }
52     }
53
54     private XidImpl(byte[] _globalId, byte[] _branchId) {
55         if(_globalId.length > Xid.MAXGTRIDSIZE) {
56             System.arraycopy(_globalId, 0, globalId, _globalId.length - Xid.MAXGTRIDSIZE, Xid.MAXGTRIDSIZE);
57         }
58         else {
59             System.arraycopy(_globalId, 0, globalId, 0, _globalId.length);
60         }
61         if(_branchId.length > Xid.MAXBQUALSIZE) {
62             System.arraycopy(_branchId, 0, branchId, _branchId.length - Xid.MAXBQUALSIZE, Xid.MAXBQUALSIZE);
63         }
64         else {
65             System.arraycopy(_branchId, 0, branchId, 0, _branchId.length);
66         }
67
68     }
69
70     public static Xid JavaDoc createBranchXid(Xid JavaDoc xid, int branchId) {
71         return new XidImpl(xid.getGlobalTransactionId(), Integer.toString(branchId).getBytes());
72     }
73
74     public int getFormatId() {
75         return FORMAT_ID;
76     }
77
78     public byte[] getBranchQualifier() {
79         return branchId;
80     }
81
82     public byte[] getGlobalTransactionId() {
83         return globalId;
84     }
85
86     public int hashCode() {
87         return globalId.hashCode() + branchId.hashCode();
88     }
89
90     public String JavaDoc toString() {
91         return new StringBuffer JavaDoc().append(new String JavaDoc(globalId).trim()).append(":").append(new String JavaDoc(branchId).trim()).toString();
92     }
93
94     public boolean equals(Object JavaDoc obj) {
95         if(!(obj instanceof XidImpl)) return false;
96
97         XidImpl xid2 = (XidImpl) obj;
98         if(FORMAT_ID == xid2.getFormatId()
99                 && java.util.Arrays.equals(branchId, xid2.getBranchQualifier())
100                 && java.util.Arrays.equals(globalId, xid2.getGlobalTransactionId())) {
101             return true;
102         }
103         else {
104             return false;
105         }
106     }
107
108     public static void main(String JavaDoc[] args) {
109         Xid JavaDoc xid = new XidImpl();
110         Xid JavaDoc xid1 = (XidImpl.createBranchXid(xid, 100));
111         Xid JavaDoc xid2 = (XidImpl.createBranchXid(xid, 100));
112         System.out.println(xid);
113         System.out.println(xid1);
114         System.out.println(xid1.equals(xid2));
115     }
116 }
117
118
Popular Tags