001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  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.lang.reflect.Method;
021import java.sql.Blob;
022import java.sql.Clob;
023import java.sql.SQLException;
024import java.util.Set;
025
026import net.sf.hajdbc.Database;
027import net.sf.hajdbc.sql.io.OutputStreamProxyFactoryFactory;
028import net.sf.hajdbc.sql.io.WriterProxyFactoryFactory;
029import net.sf.hajdbc.util.reflect.Methods;
030
031/**
032 * @author paul
033 *
034 */
035public class ClobInvocationHandler<Z, D extends Database<Z>, P, C extends Clob> extends LocatorInvocationHandler<Z, D, P, C, ClobProxyFactory<Z, D, P, C>>
036{
037        private static final Method SET_ASCII_STREAM_METHOD = Methods.getMethod(Blob.class, "setAsciiStream", Long.TYPE);
038        private static final Method SET_CHARACTER_STREAM_METHOD = Methods.getMethod(Blob.class, "setCharacterStream", Long.TYPE);
039        private static final Set<Method> READ_METHODS = Methods.findMethods(Clob.class, "getAsciiStream", "getCharacterStream", "getSubString", "length", "position");
040        private static final Set<Method> WRITE_METHODS = Methods.findMethods(Clob.class, "setAsciiStream", "setCharacterStream", "setString", "truncate");
041        
042        public ClobInvocationHandler(Class<C> proxyClass, ClobProxyFactory<Z, D, P, C> proxyFactory)
043        {
044                super(proxyClass, proxyFactory, READ_METHODS, WRITE_METHODS);
045        }
046
047        @Override
048        protected ProxyFactoryFactory<Z, D, C, SQLException, ?, ? extends Exception> getProxyFactoryFactory(C object, Method method, Object... parameters) throws SQLException
049        {
050                if (method.equals(SET_ASCII_STREAM_METHOD))
051                {
052                        return new OutputStreamProxyFactoryFactory<Z, D, C>();
053                }
054                if (method.equals(SET_CHARACTER_STREAM_METHOD))
055                {
056                        return new WriterProxyFactoryFactory<Z, D, C>();
057                }
058                
059                return super.getProxyFactoryFactory(object, method, parameters);
060        }
061}