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 */
021package net.sf.hajdbc.sql.xa;
022
023import java.lang.reflect.Method;
024import java.sql.Connection;
025import java.sql.SQLException;
026import java.util.Map;
027
028import javax.sql.XAConnection;
029import javax.sql.XADataSource;
030
031import net.sf.hajdbc.Database;
032import net.sf.hajdbc.sql.InvocationStrategy;
033import net.sf.hajdbc.sql.Invoker;
034import net.sf.hajdbc.sql.SQLProxy;
035import net.sf.hajdbc.sql.TransactionContext;
036import net.sf.hajdbc.sql.pool.AbstractPooledConnectionInvocationHandler;
037import net.sf.hajdbc.util.reflect.Methods;
038
039/**
040 * @author Paul Ferraro
041 */
042@SuppressWarnings("nls")
043public class XAConnectionInvocationHandler extends AbstractPooledConnectionInvocationHandler<XADataSource, XAConnection>
044{
045        private static final Method getXAResource = Methods.getMethod(XAConnection.class, "getXAResource");
046        
047        /**
048         * @param dataSource
049         * @param proxy
050         * @param invoker
051         * @param objectMap
052         * @throws Exception
053         */
054        public XAConnectionInvocationHandler(XADataSource dataSource, SQLProxy<XADataSource, XADataSource> proxy, Invoker<XADataSource, XADataSource, XAConnection> invoker, Map<Database<XADataSource>, XAConnection> objectMap) throws Exception
055        {
056                super(dataSource, proxy, invoker, XAConnection.class, objectMap);
057        }
058
059        @Override
060        protected InvocationStrategy<XADataSource, XAConnection, ?> getInvocationStrategy(XAConnection connection, Method method, Object[] parameters) throws Exception
061        {
062                if (method.equals(getXAResource))
063                {
064                        return new XAResourceInvocationStrategy(this.cluster, connection);
065                }
066                
067                return super.getInvocationStrategy(connection, method, parameters);
068        }
069
070        /**
071         * @see net.sf.hajdbc.sql.pool.AbstractPooledConnectionInvocationHandler#createTransactionContext()
072         */
073        @Override
074        protected TransactionContext<XADataSource> createTransactionContext()
075        {
076                return new TransactionContext<XADataSource>()
077                {
078                        @Override
079                        public void close()
080                        {
081                                // Do nothing
082                        }
083
084                        @Override
085                        public <T, R> InvocationStrategy<XADataSource, T, R> end(InvocationStrategy<XADataSource, T, R> strategy) throws SQLException
086                        {
087                                return strategy;
088                        }
089
090                        @Override
091                        public <T, R> InvocationStrategy<XADataSource, T, R> start(InvocationStrategy<XADataSource, T, R> strategy, Connection connection) throws SQLException
092                        {
093                                return strategy;
094                        }
095                };
096        }
097}