KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > resource > XAResourceWrapper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.resource;
25
26 import javax.transaction.xa.*;
27 import java.util.logging.*;
28 import com.sun.logging.*;
29
30 /**
31  * This is class is used for debugging. It prints out
32  * trace information on TM calls to XAResource before
33  * directing the call to the actual XAResource object
34  *
35  * @author Tony Ng
36  *
37  */

38 public class XAResourceWrapper implements XAResource {
39
40     // the actual XAResource object
41
private XAResource res;
42
43     public XAResourceWrapper(XAResource res) {
44         this.res = res;
45     }
46
47     // Create logger object per Java SDK 1.4 to log messages
48
// introduced Santanu De, Sun Microsystems, March 2002
49

50     static Logger _logger = null;
51     static{
52            _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
53           }
54
55     public void commit(Xid xid, boolean onePhase) throws XAException {
56         print("XAResource.commit: " + xidToString(xid) + "," + onePhase);
57         res.commit(xid, onePhase);
58     }
59
60     public void end(Xid xid, int flags) throws XAException {
61         print("XAResource.end: " + xidToString(xid) + "," +
62               flagToString(flags));
63         res.end(xid, flags);
64     }
65
66     
67     public void forget(Xid xid) throws XAException {
68         print("XAResource.forget: " + xidToString(xid));
69         res.forget(xid);
70     }
71
72     public int getTransactionTimeout() throws XAException {
73         return res.getTransactionTimeout();
74     }
75
76     public boolean isSameRM(XAResource xares) throws XAException {
77         if (xares instanceof XAResourceWrapper) {
78             XAResourceWrapper other = (XAResourceWrapper) xares;
79             boolean result = res.isSameRM(other.res);
80             print("XAResource.isSameRM: " + res + "," + other.res + "," +
81                   result);
82             return result;
83         } else {
84             boolean result = res.isSameRM(xares);
85             print("XAResource.isSameRM: " + res + "," + xares + "," +
86                   result);
87             return result;
88             //throw new IllegalArgumentException("Has to use XAResourceWrapper for all XAResource objects: " + xares);
89
}
90     }
91
92     public int prepare(Xid xid) throws XAException {
93         print("XAResource.prepare: " + xidToString(xid));
94         int result = res.prepare(xid);
95         print("prepare result = " + flagToString(result));
96         return result;
97     }
98     
99     public Xid[] recover(int flag) throws XAException {
100         print("XAResource.recover: " + flagToString(flag));
101         return res.recover(flag);
102     }
103
104     public void rollback(Xid xid) throws XAException {
105         print("XAResource.rollback: " + xidToString(xid));
106         res.rollback(xid);
107     }
108
109     public boolean setTransactionTimeout(int seconds) throws XAException {
110         return res.setTransactionTimeout(seconds);
111     }
112             
113     public void start(Xid xid, int flags) throws XAException {
114         print("XAResource.start: " + xidToString(xid) + "," +
115               flagToString(flags));
116         res.start(xid, flags);
117     }
118
119     private void print(String JavaDoc s) {
120         _logger.log(Level.FINE,s);
121     }
122
123     static public String JavaDoc xidToString(Xid xid) {
124         return String.valueOf((new String JavaDoc(xid.getGlobalTransactionId()) +
125                                new String JavaDoc(xid.getBranchQualifier())).hashCode());
126     }
127
128     static public String JavaDoc flagToString(int flag) {
129         switch (flag) {
130         case TMFAIL:
131             return "TMFAIL";
132         case TMJOIN:
133             return "TMJOIN";
134         case TMNOFLAGS:
135             return "TMNOFLAGS";
136         case TMONEPHASE:
137             return "TMONEPHASE";
138         case TMRESUME:
139             return "TMRESUME";
140         case TMSTARTRSCAN:
141             return "TMSTARTRSCAN";
142         case TMENDRSCAN:
143             return "TMENDRSCAN";
144         case TMSUCCESS:
145             return "TMSUCCESS";
146         case TMSUSPEND:
147             return "TMSUSPEND";
148         case XA_RDONLY:
149             return "XA_RDONLY";
150         default:
151             return "" + Integer.toHexString(flag);
152         }
153     }
154
155     public boolean equals(Object JavaDoc obj) {
156         if (this == obj) return true;
157         if (obj == null) return false;
158         if (obj instanceof XAResourceWrapper) {
159             XAResource other = ((XAResourceWrapper) obj).res;
160             return res.equals(other);
161         }
162         if (obj instanceof XAResource) {
163             XAResource other = (XAResource) obj;
164             return res.equals(other);
165         }
166         return false;
167     }
168
169     public int hashCode() {
170         return res.hashCode();
171     }
172 }
173
Popular Tags