KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > jca > JalistoXAResourceWrapper


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.jca;
25
26 import org.objectweb.jalisto.se.impl.trace.Trace;
27
28 import javax.transaction.xa.XAException JavaDoc;
29 import javax.transaction.xa.XAResource JavaDoc;
30 import javax.transaction.xa.Xid JavaDoc;
31 import java.io.PrintStream JavaDoc;
32 import java.io.PrintWriter JavaDoc;
33
34 public class JalistoXAResourceWrapper implements XAResource JavaDoc {
35
36     public JalistoXAResourceWrapper(JalistoManagedConnection mc, Trace trace) {
37         this.trace = trace;
38         this.mc = mc;
39     }
40
41     public boolean isAvailable() {
42         return (currentJdoxar == null);
43     }
44
45     public boolean isSameRM(XAResource JavaDoc resource) throws XAException JavaDoc {
46         try {
47             JalistoXAResourceWrapper candidate = (JalistoXAResourceWrapper) resource;
48             if (currentJdoxar == null) {
49                 return (candidate.currentJdoxar == null);
50             }
51             return currentJdoxar.isSameJDOXAR(candidate.currentJdoxar);
52         } catch (ClassCastException JavaDoc cce) {
53             return false;
54         }
55     }
56
57     public boolean setTransactionTimeout(int i) throws XAException JavaDoc {
58         this.timeout = i;
59         return true;
60     }
61
62     public int getTransactionTimeout() throws XAException JavaDoc {
63         return timeout;
64     }
65
66     public void cleanUp() {
67         out("cleanUpJalisto()");
68         currentJdoxar = null;
69     }
70
71     public void commit(Xid JavaDoc xid, boolean b) throws XAException JavaDoc {
72         JalistoXAResource res = JalistoXARPool.getBoundXAResource(xid, mc);
73         if (res != null) {
74             res.commit(xid, b);
75         } else {
76             // throw new XAException("No resource binded with xid "+String.valueOf(xid));
77
}
78     }
79
80     public void end(Xid JavaDoc xid, int i) throws XAException JavaDoc {
81         out("end");
82     }
83
84     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
85         out("forget");
86     }
87
88     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
89         out("prepare");
90         // No 2PC
91
return XA_OK;
92     }
93
94     public Xid JavaDoc[] recover(int i) throws XAException JavaDoc {
95         out("recover");
96         return new Xid JavaDoc[0];
97     }
98
99     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
100         JalistoXAResource res = JalistoXARPool.getBoundXAResource(xid, mc);
101         if (res != null) {
102             res.rollback(xid);
103         } else {
104             // throw new XAException("No resource binded with xid "+String.valueOf(xid));
105
}
106     }
107
108     public void start(Xid JavaDoc xid, int i) throws XAException JavaDoc {
109         try {
110             currentJdoxar = JalistoXARPool.getOrBindJalistoXAResource(xid, mc);
111             currentJdoxar.start(xid, mc);
112         } catch (Throwable JavaDoc t) {
113             t.printStackTrace();
114             throw new JalistoXAException("ERROR during IES association : ", t);
115         }
116         out("start");
117     }
118
119     public void boundUnderlyingXaResource(Xid JavaDoc xid) throws XAException JavaDoc {
120         try {
121             currentJdoxar = JalistoXARPool.getOrBindJalistoXAResource(xid, mc);
122             mc.setManagedJalistoSession(currentJdoxar.getManagedJalistoSession());
123         } catch (Throwable JavaDoc t) {
124             t.printStackTrace();
125             throw new JalistoXAException("ERROR during IES association : ", t);
126         }
127         out("boundUnderlyingXaResource");
128     }
129
130     private void out(Object JavaDoc o) {
131         if (trace != null) {
132             trace.println(Trace.JCA, TRACE_ID + " {0}", o);
133         }
134     }
135
136     public class JalistoXAException extends XAException JavaDoc {
137         private Throwable JavaDoc cause;
138
139         public JalistoXAException(String JavaDoc message, Throwable JavaDoc throwable) {
140             super(message);
141             this.cause = throwable;
142         }
143
144         public JalistoXAException(Throwable JavaDoc throwable) {
145             super(throwable.getClass().getName() + ": " + throwable.getMessage());
146             this.cause = throwable;
147         }
148
149         public void printStackTrace() {
150             this.printStackTrace(System.err);
151         }
152
153         public void printStackTrace(PrintStream JavaDoc s) {
154             super.printStackTrace(s);
155             if (cause != null) {
156                 s.println("CAUSED BY:");
157                 cause.printStackTrace(s);
158             }
159         }
160
161         public void printStackTrace(PrintWriter JavaDoc s) {
162             super.printStackTrace(s);
163             if (cause != null) {
164                 s.println("CAUSED BY:");
165                 cause.printStackTrace(s);
166             }
167         }
168     }
169
170
171     private JalistoManagedConnection mc;
172     private JalistoXAResource currentJdoxar = null;
173     private int timeout;
174
175     private Trace trace;
176
177
178     private static final String JavaDoc TRACE_ID = "[JalistoXAResourceWrapper]";
179
180 }
181
Popular Tags