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.io.IOException; 021import java.lang.reflect.Method; 022import java.sql.SQLException; 023import java.util.ArrayList; 024import java.util.Arrays; 025import java.util.List; 026 027import net.sf.hajdbc.Database; 028import net.sf.hajdbc.ExceptionFactory; 029import net.sf.hajdbc.invocation.Invoker; 030import net.sf.hajdbc.io.InputSinkChannel; 031import net.sf.hajdbc.util.reflect.Methods; 032 033/** 034 * 035 * @author Paul Ferraro 036 */ 037public class InputSinkRegistryInvocationHandler<Z, D extends Database<Z>, P, T, F extends InputSinkRegistryProxyFactory<Z, D, P, T>> extends ChildInvocationHandler<Z, D, P, SQLException, T, SQLException, F> 038{ 039 protected InputSinkRegistryInvocationHandler(Class<T> proxyClass, F proxyFactory, Method parentMethod) 040 { 041 super(proxyClass, proxyFactory, parentMethod); 042 } 043 044 @Override 045 protected <R, X> Invoker<Z, D, T, R, SQLException> getInvoker(Class<X> parameterClass, final int parameterIndex, T proxy, final Method method, final Object... parameters) throws SQLException 046 { 047 if (parameterClass.equals(method.getParameterTypes()[parameterIndex]) && !parameterClass.isPrimitive()) 048 { 049 X parameter = parameterClass.cast(parameters[parameterIndex]); 050 051 if (parameter != null) 052 { 053 final InputSinkChannel<X, Object> channel = this.getProxyFactory().getInputSinkRegistry().get(parameterClass); 054 055 if (channel != null) 056 { 057 final ExceptionFactory<SQLException> exceptionFactory = this.getProxyFactory().getExceptionFactory(); 058 try 059 { 060 final Object sink = channel.write(parameter); 061 062 return new Invoker<Z, D, T, R, SQLException>() 063 { 064 @Override 065 public R invoke(D database, T object) throws SQLException 066 { 067 List<Object> parameterList = new ArrayList<Object>(Arrays.asList(parameters)); 068 069 try 070 { 071 parameterList.set(parameterIndex, channel.read(sink)); 072 073 return Methods.<R, SQLException>invoke(method, exceptionFactory, object, parameterList.toArray()); 074 } 075 catch (IOException e) 076 { 077 throw exceptionFactory.createException(e); 078 } 079 } 080 }; 081 } 082 catch (IOException e) 083 { 084 throw exceptionFactory.createException(e); 085 } 086 } 087 } 088 } 089 090 return super.getInvoker(parameterClass, parameterIndex, proxy, method, parameters); 091 } 092}