001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2008 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.sql.xa;
022:
023: import java.lang.reflect.Method;
024: import java.sql.Connection;
025: import java.sql.SQLException;
026: import java.util.Map;
027:
028: import javax.sql.XAConnection;
029: import javax.sql.XADataSource;
030:
031: import net.sf.hajdbc.Database;
032: import net.sf.hajdbc.sql.InvocationStrategy;
033: import net.sf.hajdbc.sql.Invoker;
034: import net.sf.hajdbc.sql.SQLProxy;
035: import net.sf.hajdbc.sql.TransactionContext;
036: import net.sf.hajdbc.sql.pool.AbstractPooledConnectionInvocationHandler;
037: import net.sf.hajdbc.util.reflect.Methods;
038:
039: /**
040: * @author Paul Ferraro
041: */
042: @SuppressWarnings("nls")
043: public class XAConnectionInvocationHandler
044: extends
045: AbstractPooledConnectionInvocationHandler<XADataSource, XAConnection> {
046: private static final Method getXAResource = Methods.getMethod(
047: XAConnection.class, "getXAResource");
048:
049: /**
050: * @param dataSource
051: * @param proxy
052: * @param invoker
053: * @param objectMap
054: * @throws Exception
055: */
056: public XAConnectionInvocationHandler(XADataSource dataSource,
057: SQLProxy<XADataSource, XADataSource> proxy,
058: Invoker<XADataSource, XADataSource, XAConnection> invoker,
059: Map<Database<XADataSource>, XAConnection> objectMap)
060: throws Exception {
061: super (dataSource, proxy, invoker, XAConnection.class, objectMap);
062: }
063:
064: @Override
065: protected InvocationStrategy<XADataSource, XAConnection, ?> getInvocationStrategy(
066: XAConnection connection, Method method, Object[] parameters)
067: throws Exception {
068: if (method.equals(getXAResource)) {
069: return new XAResourceInvocationStrategy(this .cluster,
070: connection);
071: }
072:
073: return super .getInvocationStrategy(connection, method,
074: parameters);
075: }
076:
077: /**
078: * @see net.sf.hajdbc.sql.pool.AbstractPooledConnectionInvocationHandler#createTransactionContext()
079: */
080: @Override
081: protected TransactionContext<XADataSource> createTransactionContext() {
082: return new TransactionContext<XADataSource>() {
083: @Override
084: public void close() {
085: // Do nothing
086: }
087:
088: @Override
089: public <T, R> InvocationStrategy<XADataSource, T, R> end(
090: InvocationStrategy<XADataSource, T, R> strategy)
091: throws SQLException {
092: return strategy;
093: }
094:
095: @Override
096: public <T, R> InvocationStrategy<XADataSource, T, R> start(
097: InvocationStrategy<XADataSource, T, R> strategy,
098: Connection connection) throws SQLException {
099: return strategy;
100: }
101: };
102: }
103: }
|