001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jca.adapter;
023:
024: import javax.sql.XADataSource;
025: import javax.sql.XAConnection;
026: import javax.sql.ConnectionEventListener;
027: import javax.transaction.xa.XAResource;
028: import javax.transaction.xa.XAException;
029: import javax.transaction.xa.Xid;
030: import java.util.Map;
031: import java.util.HashMap;
032: import java.util.Arrays;
033: import java.io.PrintWriter;
034: import java.sql.SQLException;
035: import java.sql.Connection;
036: import java.sql.DatabaseMetaData;
037: import java.sql.SQLWarning;
038: import java.sql.Savepoint;
039: import java.sql.Statement;
040: import java.sql.CallableStatement;
041: import java.sql.PreparedStatement;
042: import java.lang.reflect.Proxy;
043: import java.lang.reflect.InvocationHandler;
044: import java.lang.reflect.Method;
045:
046: /**
047: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
048: * @version <tt>$Revision: 57211 $</tt>
049: */
050: public class MockedXADataSource implements XADataSource {
051: private static final Map instances = new HashMap();
052:
053: public static MockedXADataSource getInstance(String url) {
054: return (MockedXADataSource) instances.get(url);
055: }
056:
057: public static void stop(String url) {
058: getInstance(url).stopped = true;
059: }
060:
061: public static void start(String url) {
062: getInstance(url).stopped = false;
063: }
064:
065: public static String[] getUrls() {
066: return (String[]) instances.keySet().toArray(
067: new String[instances.size()]);
068: }
069:
070: private String url;
071: private boolean stopped;
072: private int loginTimeout;
073: private PrintWriter logWriter;
074:
075: public String getURL() {
076: return url;
077: }
078:
079: public void setURL(String url) {
080: this .url = url;
081: instances.put(url, this );
082: }
083:
084: public int getLoginTimeout() throws SQLException {
085: return loginTimeout;
086: }
087:
088: public void setLoginTimeout(int seconds) throws SQLException {
089: this .loginTimeout = seconds;
090: }
091:
092: public PrintWriter getLogWriter() throws SQLException {
093: return logWriter;
094: }
095:
096: public void setLogWriter(PrintWriter out) throws SQLException {
097: this .logWriter = out;
098: }
099:
100: public XAConnection getXAConnection() throws SQLException {
101: return new MockedXAConnection();
102: }
103:
104: public XAConnection getXAConnection(String user, String password)
105: throws SQLException {
106: return new MockedXAConnection();
107: }
108:
109: // Inner
110:
111: public class MockedXAConnection implements XAConnection {
112: private boolean closed;
113: private Connection con = new MockedConnection();
114: private XAResource xaResource = new MockedXAResource();
115:
116: public XAResource getXAResource() throws SQLException {
117: return xaResource;
118: }
119:
120: public void close() throws SQLException {
121: closed = true;
122: }
123:
124: public Connection getConnection() throws SQLException {
125: return con;
126: }
127:
128: public void addConnectionEventListener(
129: ConnectionEventListener listener) {
130: }
131:
132: public void removeConnectionEventListener(
133: ConnectionEventListener listener) {
134: }
135:
136: class MockedConnection implements Connection {
137: private int holdability;
138: private int txIsolation;
139: private boolean autoCommit;
140: private boolean readOnly;
141: private String catalog;
142:
143: public String getUrl() {
144: return url;
145: }
146:
147: public int getHoldability() throws SQLException {
148: check();
149: return holdability;
150: }
151:
152: public int getTransactionIsolation() throws SQLException {
153: check();
154: return txIsolation;
155: }
156:
157: public void clearWarnings() throws SQLException {
158: check();
159: }
160:
161: public void close() throws SQLException {
162: check();
163: closed = true;
164: }
165:
166: public void commit() throws SQLException {
167: check();
168: }
169:
170: public void rollback() throws SQLException {
171: check();
172: }
173:
174: public boolean getAutoCommit() throws SQLException {
175: check();
176: return autoCommit;
177: }
178:
179: public boolean isClosed() throws SQLException {
180: check();
181: return closed;
182: }
183:
184: public boolean isReadOnly() throws SQLException {
185: check();
186: return readOnly;
187: }
188:
189: public void setHoldability(int holdability)
190: throws SQLException {
191: check();
192: this .holdability = holdability;
193: }
194:
195: public void setTransactionIsolation(int level)
196: throws SQLException {
197: check();
198: this .txIsolation = level;
199: }
200:
201: public void setAutoCommit(boolean autoCommit)
202: throws SQLException {
203: check();
204: this .autoCommit = autoCommit;
205: }
206:
207: public void setReadOnly(boolean readOnly)
208: throws SQLException {
209: check();
210: this .readOnly = readOnly;
211: }
212:
213: public String getCatalog() throws SQLException {
214: check();
215: return catalog;
216: }
217:
218: public void setCatalog(String catalog) throws SQLException {
219: check();
220: this .catalog = catalog;
221: }
222:
223: public DatabaseMetaData getMetaData() throws SQLException {
224: check();
225: return (DatabaseMetaData) Proxy.newProxyInstance(Thread
226: .currentThread().getContextClassLoader(),
227: new Class[] { DatabaseMetaData.class },
228: new InvocationHandler() {
229: public Object invoke(Object proxy,
230: Method method, Object[] args)
231: throws Throwable {
232: if ("getURL".equals(method.getName())) {
233: return url;
234: }
235:
236: return new UnsupportedOperationException(
237: "Not implemented: method="
238: + method.getName()
239: + ", args="
240: + (args == null ? (Object) "null"
241: : Arrays
242: .asList(args)));
243: }
244: });
245: }
246:
247: public SQLWarning getWarnings() throws SQLException {
248: check();
249: return null;
250: }
251:
252: public Savepoint setSavepoint() throws SQLException {
253: check();
254: throw new UnsupportedOperationException(
255: "setSavepoint() is not implemented.");
256: }
257:
258: public void releaseSavepoint(Savepoint savepoint)
259: throws SQLException {
260: check();
261: throw new UnsupportedOperationException(
262: "releaseSavepoint() is not implemented.");
263: }
264:
265: public void rollback(Savepoint savepoint)
266: throws SQLException {
267: check();
268: }
269:
270: public Statement createStatement() throws SQLException {
271: check();
272: return (Statement) Proxy.newProxyInstance(Thread
273: .currentThread().getContextClassLoader(),
274: new Class[] { Statement.class },
275: new InvocationHandler() {
276: public Object invoke(Object proxy,
277: Method method, Object[] args)
278: throws Throwable {
279: String methodName = method.getName();
280: if ("execute".equals(methodName)) {
281: // let's suppose it went well!
282: return Boolean.FALSE;
283: }
284:
285: return new UnsupportedOperationException(
286: "Not implemented: method="
287: + methodName
288: + ", args="
289: + (args == null ? (Object) "null"
290: : Arrays
291: .asList(args)));
292: }
293: });
294: }
295:
296: public Statement createStatement(int resultSetType,
297: int resultSetConcurrency) throws SQLException {
298: check();
299: throw new UnsupportedOperationException(
300: "Not implemented.");
301: }
302:
303: public Statement createStatement(int resultSetType,
304: int resultSetConcurrency, int resultSetHoldability)
305: throws SQLException {
306: check();
307: throw new UnsupportedOperationException(
308: "Not implemented.");
309: }
310:
311: public Map getTypeMap() throws SQLException {
312: check();
313: throw new UnsupportedOperationException(
314: "Not implemented.");
315: }
316:
317: public void setTypeMap(Map map) throws SQLException {
318: check();
319: throw new UnsupportedOperationException(
320: "Not implemented.");
321: }
322:
323: public String nativeSQL(String sql) throws SQLException {
324: check();
325: throw new UnsupportedOperationException(
326: "Not implemented.");
327: }
328:
329: public CallableStatement prepareCall(String sql)
330: throws SQLException {
331: check();
332: throw new UnsupportedOperationException(
333: "Not implemented.");
334: }
335:
336: public CallableStatement prepareCall(String sql,
337: int resultSetType, int resultSetConcurrency)
338: throws SQLException {
339: check();
340: throw new UnsupportedOperationException(
341: "Not implemented.");
342: }
343:
344: public CallableStatement prepareCall(String sql,
345: int resultSetType, int resultSetConcurrency,
346: int resultSetHoldability) throws SQLException {
347: check();
348: throw new UnsupportedOperationException(
349: "Not implemented.");
350: }
351:
352: public PreparedStatement prepareStatement(String sql)
353: throws SQLException {
354: check();
355: throw new UnsupportedOperationException(
356: "Not implemented.");
357: }
358:
359: public PreparedStatement prepareStatement(String sql,
360: int autoGeneratedKeys) throws SQLException {
361: check();
362: throw new UnsupportedOperationException(
363: "Not implemented.");
364: }
365:
366: public PreparedStatement prepareStatement(String sql,
367: int resultSetType, int resultSetConcurrency)
368: throws SQLException {
369: check();
370: throw new UnsupportedOperationException(
371: "Not implemented.");
372: }
373:
374: public PreparedStatement prepareStatement(String sql,
375: int resultSetType, int resultSetConcurrency,
376: int resultSetHoldability) throws SQLException {
377: check();
378: throw new UnsupportedOperationException(
379: "Not implemented.");
380: }
381:
382: public PreparedStatement prepareStatement(String sql,
383: int columnIndexes[]) throws SQLException {
384: check();
385: throw new UnsupportedOperationException(
386: "Not implemented.");
387: }
388:
389: public Savepoint setSavepoint(String name)
390: throws SQLException {
391: check();
392: throw new UnsupportedOperationException(
393: "Not implemented.");
394: }
395:
396: public PreparedStatement prepareStatement(String sql,
397: String columnNames[]) throws SQLException {
398: check();
399: throw new UnsupportedOperationException(
400: "Not implemented.");
401: }
402:
403: // Private
404:
405: private void check() throws SQLException {
406: if (stopped) {
407: throw new SQLException(
408: "The database is not available: " + url);
409: }
410: }
411: }
412: }
413:
414: private static class MockedXAResource implements XAResource {
415: private int txTimeOut;
416:
417: public int getTransactionTimeout() throws XAException {
418: return txTimeOut;
419: }
420:
421: public boolean setTransactionTimeout(int i) throws XAException {
422: this .txTimeOut = i;
423: return true;
424: }
425:
426: public boolean isSameRM(XAResource xaResource)
427: throws XAException {
428: return xaResource instanceof MockedXAResource;
429: }
430:
431: public Xid[] recover(int i) throws XAException {
432: throw new UnsupportedOperationException(
433: "recover is not implemented.");
434: }
435:
436: public int prepare(Xid xid) throws XAException {
437: return XAResource.XA_OK;
438: }
439:
440: public void forget(Xid xid) throws XAException {
441: }
442:
443: public void rollback(Xid xid) throws XAException {
444: }
445:
446: public void end(Xid xid, int i) throws XAException {
447: }
448:
449: public void start(Xid xid, int i) throws XAException {
450: }
451:
452: public void commit(Xid xid, boolean b) throws XAException {
453: }
454: }
455: }
|