KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > sql > spy > SpyXAResource


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.sql.spy;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import javax.transaction.xa.XAException JavaDoc;
35 import javax.transaction.xa.XAResource JavaDoc;
36 import javax.transaction.xa.Xid JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Spying on a connection.
42  */

43 public class SpyXAResource implements XAResource JavaDoc {
44   protected final static Logger JavaDoc log = Log.open(SpyXAResource.class);
45   protected final static L10N L = new L10N(SpyXAResource.class);
46
47   // The underlying resource
48
private XAResource JavaDoc _xaResource;
49
50   private int _id;
51
52   /**
53    * Creates a new SpyXAResource
54    */

55   public SpyXAResource(int id, XAResource JavaDoc resource)
56   {
57     _xaResource = resource;
58     _id = id;
59   }
60
61   /**
62    * Returns the underlying resource.
63    */

64   public XAResource JavaDoc getXAResource()
65   {
66     return _xaResource;
67   }
68
69   /**
70    * Sets the transaction timeout.
71    */

72   public boolean setTransactionTimeout(int seconds)
73     throws XAException JavaDoc
74   {
75     try {
76       boolean ok = _xaResource.setTransactionTimeout(seconds);
77       log.info(_id + ":set-transaction-timeout(" + seconds + ")->" + ok);
78
79       return ok;
80     } catch (XAException JavaDoc e) {
81       log.log(Level.INFO, e.toString(), e);
82       throw e;
83     } catch (RuntimeException JavaDoc e) {
84       log.log(Level.INFO, e.toString(), e);
85       throw e;
86     }
87   }
88
89   /**
90    * Gets the transaction timeout.
91    */

92   public int getTransactionTimeout()
93     throws XAException JavaDoc
94   {
95     try {
96       int seconds = _xaResource.getTransactionTimeout();
97       
98       log.info(_id + ":transaction-timeout()->" + seconds);
99
100       return seconds;
101     } catch (XAException JavaDoc e) {
102       log.log(Level.INFO, e.toString(), e);
103       throw e;
104     } catch (RuntimeException JavaDoc e) {
105       log.log(Level.INFO, e.toString(), e);
106       throw e;
107     }
108   }
109
110   /**
111    * Returns true if the underlying RM is the same.
112    */

113   public boolean isSameRM(XAResource JavaDoc resource)
114     throws XAException JavaDoc
115   {
116     try {
117       if (resource instanceof SpyXAResource)
118         resource = ((SpyXAResource) resource).getXAResource();
119         
120       boolean same = _xaResource.isSameRM(resource);
121       
122       log.info(_id + ":is-same-rm(resource=" + resource + ")->" + same);
123
124       return same;
125     } catch (XAException JavaDoc e) {
126       log.log(Level.INFO, e.toString(), e);
127       throw e;
128     } catch (RuntimeException JavaDoc e) {
129       log.log(Level.INFO, e.toString(), e);
130       throw e;
131     }
132   }
133
134   /**
135    * Starts the resource.
136    */

137   public void start(Xid JavaDoc xid, int flags)
138     throws XAException JavaDoc
139   {
140     try {
141       String JavaDoc flagName = "";
142
143       if ((flags & TMJOIN) != 0)
144         flagName += ",join";
145       if ((flags & TMRESUME) != 0)
146         flagName += ",resume";
147       
148       log.info(_id + ":start(xid=" + xid + flagName + ")");
149
150       _xaResource.start(xid, flags);
151     } catch (XAException JavaDoc e) {
152       log.log(Level.INFO, e.toString(), e);
153       throw e;
154     } catch (RuntimeException JavaDoc e) {
155       log.log(Level.INFO, e.toString(), e);
156       throw e;
157     }
158   }
159
160   /**
161    * Starts the resource.
162    */

163   public void end(Xid JavaDoc xid, int flags)
164     throws XAException JavaDoc
165   {
166     try {
167       String JavaDoc flagName = "";
168
169       if ((flags & TMFAIL) != 0)
170         flagName += ",fail";
171       if ((flags & TMSUSPEND) != 0)
172         flagName += ",suspend";
173       
174       log.info(_id + ":end(xid=" + xid + flagName + ")");
175
176       _xaResource.end(xid, flags);
177     } catch (XAException JavaDoc e) {
178       log.log(Level.INFO, e.toString(), e);
179       throw e;
180     } catch (RuntimeException JavaDoc e) {
181       log.log(Level.INFO, e.toString(), e);
182       throw e;
183     }
184   }
185
186   /**
187    * Rolls the resource back
188    */

189   public int prepare(Xid JavaDoc xid)
190     throws XAException JavaDoc
191   {
192     try {
193       int value = _xaResource.prepare(xid);
194       log.info(_id + ":prepare(xid=" + xid + ")->" + value);
195
196       return value;
197     } catch (XAException JavaDoc e) {
198       log.log(Level.INFO, e.toString(), e);
199       throw e;
200     } catch (RuntimeException JavaDoc e) {
201       log.log(Level.INFO, e.toString(), e);
202       throw e;
203     }
204   }
205
206   /**
207    * Commits the resource
208    */

209   public void commit(Xid JavaDoc xid, boolean onePhase)
210     throws XAException JavaDoc
211   {
212     try {
213       log.info(_id + ":commit(xid=" + xid + (onePhase ? ",1P)" : ",2P)"));
214
215       _xaResource.commit(xid, onePhase);
216     } catch (XAException JavaDoc e) {
217       log.log(Level.INFO, e.toString(), e);
218       throw e;
219     } catch (RuntimeException JavaDoc e) {
220       log.log(Level.INFO, e.toString(), e);
221       throw e;
222     }
223   }
224
225   /**
226    * Rolls the resource back
227    */

228   public void rollback(Xid JavaDoc xid)
229     throws XAException JavaDoc
230   {
231     try {
232       log.info(_id + ":rollback(xid=" + xid + ")");
233
234       _xaResource.rollback(xid);
235     } catch (XAException JavaDoc e) {
236       log.log(Level.INFO, e.toString(), e);
237       throw e;
238     } catch (RuntimeException JavaDoc e) {
239       log.log(Level.INFO, e.toString(), e);
240       throw e;
241     }
242   }
243
244   /**
245    * Rolls the resource back
246    */

247   public Xid JavaDoc []recover(int flags)
248     throws XAException JavaDoc
249   {
250     try {
251       String JavaDoc flagString = "";
252
253       if ((flags & XAResource.TMSTARTRSCAN) != 0)
254     flagString += "start";
255
256       if ((flags & XAResource.TMENDRSCAN) != 0) {
257     if (! flagString.equals(""))
258       flagString += ",";
259         
260     flagString += "end";
261       }
262       
263       log.info(_id + ":recover(flags=" + flagString + ")");
264
265       return _xaResource.recover(flags);
266     } catch (XAException JavaDoc e) {
267       log.info(e.toString());
268       throw e;
269     } catch (RuntimeException JavaDoc e) {
270       log.log(Level.INFO, e.toString(), e);
271       throw e;
272     }
273   }
274
275   /**
276    * Forgets the transaction
277    */

278   public void forget(Xid JavaDoc xid)
279     throws XAException JavaDoc
280   {
281     try {
282       log.info(_id + ":forget(xid=" + xid + ")");
283
284       _xaResource.forget(xid);
285     } catch (XAException JavaDoc e) {
286       log.log(Level.INFO, e.toString(), e);
287       throw e;
288     } catch (RuntimeException JavaDoc e) {
289       log.log(Level.INFO, e.toString(), e);
290       throw e;
291     }
292   }
293
294   public String JavaDoc toString()
295   {
296     return "SpyXAResource[id=" + _id + ",resource=" + _xaResource + "]";
297   }
298 }
299
Popular Tags