001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.sql.spy;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.L10N;
033:
034: import javax.transaction.xa.XAException;
035: import javax.transaction.xa.XAResource;
036: import javax.transaction.xa.Xid;
037: import java.util.logging.Level;
038: import java.util.logging.Logger;
039:
040: /**
041: * Spying on a connection.
042: */
043: public class SpyXAResource implements XAResource {
044: protected final static Logger log = Log.open(SpyXAResource.class);
045: protected final static L10N L = new L10N(SpyXAResource.class);
046:
047: // The underlying resource
048: private XAResource _xaResource;
049:
050: private String _id;
051:
052: /**
053: * Creates a new SpyXAResource
054: */
055: public SpyXAResource(String id, XAResource resource) {
056: _xaResource = resource;
057: _id = id;
058: }
059:
060: /**
061: * Returns the underlying resource.
062: */
063: public XAResource getXAResource() {
064: return _xaResource;
065: }
066:
067: /**
068: * Sets the transaction timeout.
069: */
070: public boolean setTransactionTimeout(int seconds)
071: throws XAException {
072: try {
073: boolean ok = _xaResource.setTransactionTimeout(seconds);
074: log.fine(_id + ":set-transaction-timeout(" + seconds
075: + ")->" + ok);
076:
077: return ok;
078: } catch (XAException e) {
079: log.log(Level.FINE, e.toString(), e);
080: throw e;
081: } catch (RuntimeException e) {
082: log.log(Level.FINE, e.toString(), e);
083: throw e;
084: }
085: }
086:
087: /**
088: * Gets the transaction timeout.
089: */
090: public int getTransactionTimeout() throws XAException {
091: try {
092: int seconds = _xaResource.getTransactionTimeout();
093:
094: log.fine(_id + ":transaction-timeout()->" + seconds);
095:
096: return seconds;
097: } catch (XAException e) {
098: log.log(Level.FINE, e.toString(), e);
099: throw e;
100: } catch (RuntimeException e) {
101: log.log(Level.FINE, e.toString(), e);
102: throw e;
103: }
104: }
105:
106: /**
107: * Returns true if the underlying RM is the same.
108: */
109: public boolean isSameRM(XAResource resource) throws XAException {
110: try {
111: if (resource instanceof SpyXAResource)
112: resource = ((SpyXAResource) resource).getXAResource();
113:
114: boolean same = _xaResource.isSameRM(resource);
115:
116: log.fine(_id + ":is-same-rm(resource=" + resource + ")->"
117: + same);
118:
119: return same;
120: } catch (XAException e) {
121: log.log(Level.FINE, e.toString(), e);
122: throw e;
123: } catch (RuntimeException e) {
124: log.log(Level.FINE, e.toString(), e);
125: throw e;
126: }
127: }
128:
129: /**
130: * Starts the resource.
131: */
132: public void start(Xid xid, int flags) throws XAException {
133: try {
134: String flagName = "";
135:
136: if ((flags & TMJOIN) != 0)
137: flagName += ",join";
138: if ((flags & TMRESUME) != 0)
139: flagName += ",resume";
140:
141: log.fine(_id + ":start(xid=" + xid + flagName + ")");
142:
143: _xaResource.start(xid, flags);
144: } catch (XAException e) {
145: log.log(Level.FINE, e.toString(), e);
146: throw e;
147: } catch (RuntimeException e) {
148: log.log(Level.FINE, e.toString(), e);
149: throw e;
150: }
151: }
152:
153: /**
154: * Starts the resource.
155: */
156: public void end(Xid xid, int flags) throws XAException {
157: try {
158: String flagName = "";
159:
160: if ((flags & TMFAIL) != 0)
161: flagName += ",fail";
162: if ((flags & TMSUSPEND) != 0)
163: flagName += ",suspend";
164:
165: log.fine(_id + ":end(xid=" + xid + flagName + ")");
166:
167: _xaResource.end(xid, flags);
168: } catch (XAException e) {
169: log.log(Level.FINE, e.toString(), e);
170: throw e;
171: } catch (RuntimeException e) {
172: log.log(Level.FINE, e.toString(), e);
173: throw e;
174: }
175: }
176:
177: /**
178: * Rolls the resource back
179: */
180: public int prepare(Xid xid) throws XAException {
181: try {
182: int value = _xaResource.prepare(xid);
183: log.fine(_id + ":prepare(xid=" + xid + ")->" + value);
184:
185: return value;
186: } catch (XAException e) {
187: log.log(Level.FINE, e.toString(), e);
188: throw e;
189: } catch (RuntimeException e) {
190: log.log(Level.FINE, e.toString(), e);
191: throw e;
192: }
193: }
194:
195: /**
196: * Commits the resource
197: */
198: public void commit(Xid xid, boolean onePhase) throws XAException {
199: try {
200: log.fine(_id + ":commit(xid=" + xid
201: + (onePhase ? ",1P)" : ",2P)"));
202:
203: _xaResource.commit(xid, onePhase);
204: } catch (XAException e) {
205: log.log(Level.FINE, e.toString(), e);
206: throw e;
207: } catch (RuntimeException e) {
208: log.log(Level.FINE, e.toString(), e);
209: throw e;
210: }
211: }
212:
213: /**
214: * Rolls the resource back
215: */
216: public void rollback(Xid xid) throws XAException {
217: try {
218: log.fine(_id + ":rollback(xid=" + xid + ")");
219:
220: _xaResource.rollback(xid);
221: } catch (XAException e) {
222: log.log(Level.FINE, e.toString(), e);
223: throw e;
224: } catch (RuntimeException e) {
225: log.log(Level.FINE, e.toString(), e);
226: throw e;
227: }
228: }
229:
230: /**
231: * Rolls the resource back
232: */
233: public Xid[] recover(int flags) throws XAException {
234: try {
235: String flagString = "";
236:
237: if ((flags & XAResource.TMSTARTRSCAN) != 0)
238: flagString += "start";
239:
240: if ((flags & XAResource.TMENDRSCAN) != 0) {
241: if (!flagString.equals(""))
242: flagString += ",";
243:
244: flagString += "end";
245: }
246:
247: log.fine(_id + ":recover(flags=" + flagString + ")");
248:
249: return _xaResource.recover(flags);
250: } catch (XAException e) {
251: log.fine(e.toString());
252: throw e;
253: } catch (RuntimeException e) {
254: log.log(Level.FINE, e.toString(), e);
255: throw e;
256: }
257: }
258:
259: /**
260: * Forgets the transaction
261: */
262: public void forget(Xid xid) throws XAException {
263: try {
264: log.fine(_id + ":forget(xid=" + xid + ")");
265:
266: _xaResource.forget(xid);
267: } catch (XAException e) {
268: log.log(Level.FINE, e.toString(), e);
269: throw e;
270: } catch (RuntimeException e) {
271: log.log(Level.FINE, e.toString(), e);
272: throw e;
273: }
274: }
275:
276: public String toString() {
277: return "SpyXAResource[id=" + _id + ",resource=" + _xaResource
278: + "]";
279: }
280: }
|