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.io;
019
020import java.io.IOException;
021import java.sql.SQLException;
022import java.util.LinkedList;
023import java.util.List;
024import java.util.Map;
025
026import net.sf.hajdbc.Database;
027import net.sf.hajdbc.invocation.Invoker;
028import net.sf.hajdbc.sql.AbstractChildProxyFactory;
029import net.sf.hajdbc.sql.ProxyFactory;
030
031/**
032 * Base proxy factory for IO proxies.
033 * @author Paul Ferraro
034 * @param <Z> connection source
035 * @param <D> database
036 * @param <P> parent type
037 * @param <T> IO type
038 */
039public abstract class OutputProxyFactory<Z, D extends Database<Z>, P, T> extends AbstractChildProxyFactory<Z, D, P, SQLException, T, IOException>
040{
041        private List<Invoker<Z, D, T, ?, IOException>> invokers = new LinkedList<Invoker<Z, D, T, ?, IOException>>();
042        
043        protected OutputProxyFactory(P parentProxy, ProxyFactory<Z, D, P, SQLException> parent, Invoker<Z, D, P, T, SQLException> invoker, Map<D, T> map)
044        {
045                super(parentProxy, parent, invoker, map, IOException.class);
046        }
047
048        @Override
049        public void record(Invoker<Z, D, T, ?, IOException> invoker)
050        {
051                this.invokers.add(invoker);
052        }
053
054        @Override
055        public void replay(D database, T output) throws IOException
056        {
057                for (Invoker<Z, D, T, ?, IOException> invoker: this.invokers)
058                {
059                        invoker.invoke(database, output);
060                }
061        }
062}