001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2013  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.sql;
019
020import java.sql.Connection;
021import java.sql.ResultSet;
022import java.sql.SQLException;
023import java.sql.Statement;
024import java.util.LinkedList;
025import java.util.List;
026import java.util.Map;
027
028import net.sf.hajdbc.Database;
029import net.sf.hajdbc.invocation.Invoker;
030import net.sf.hajdbc.io.InputSinkRegistry;
031import net.sf.hajdbc.util.Resources;
032import net.sf.hajdbc.util.reflect.Proxies;
033
034/**
035 * 
036 * @author Paul Ferraro
037 */
038public class ResultSetProxyFactory<Z, D extends Database<Z>, S extends Statement> extends AbstractInputSinkRegistryProxyFactory<Z, D, S, ResultSet>
039{
040        private List<Invoker<Z, D, ResultSet, ?, SQLException>> invokers = new LinkedList<Invoker<Z, D, ResultSet, ?, SQLException>>();
041        
042        public ResultSetProxyFactory(S statementProxy, ProxyFactory<Z, D, S, SQLException> statementFactory, Invoker<Z, D, S, ResultSet, SQLException> invoker, Map<D, ResultSet> map, TransactionContext<Z, D> context, InputSinkRegistry<Object> sinkRegistry)
043        {
044                super(statementProxy, statementFactory, invoker, map, context, sinkRegistry);
045        }
046
047        public void addInvoker(Invoker<Z, D, ResultSet, ?, SQLException> invoker)
048        {
049                this.invokers.add(invoker);
050        }
051        
052        public void clearInvokers()
053        {
054                this.invokers.clear();
055        }
056
057        @Override
058        public Connection getConnection(D database) throws SQLException
059        {
060                return this.get(database).getStatement().getConnection();
061        }
062
063        @Override
064        public void replay(D database, ResultSet results) throws SQLException
065        {
066                super.replay(database, results);
067
068                for (Invoker<Z, D, ResultSet, ?, SQLException> invoker: this.invokers)
069                {
070                        invoker.invoke(database, results);
071                }
072        }
073
074        @Override
075        public void close(D database, ResultSet results)
076        {
077                Resources.close(results);
078        }
079
080        @Override
081        public ResultSet createProxy()
082        {
083                return Proxies.createProxy(ResultSet.class, new ResultSetInvocationHandler<Z, D, S>(this));
084        }
085}