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.SQLException;
022import java.util.Set;
023
024import net.sf.hajdbc.Database;
025import net.sf.hajdbc.invocation.InvocationStrategies;
026import net.sf.hajdbc.invocation.InvocationStrategy;
027import net.sf.hajdbc.invocation.Invoker;
028import net.sf.hajdbc.util.reflect.Methods;
029
030/**
031 * @author Paul Ferraro
032 * @param <D> 
033 */
034@SuppressWarnings("nls")
035public class CommonDataSourceInvocationHandler<Z extends javax.sql.CommonDataSource, D extends Database<Z>, F extends RootProxyFactory<Z, D>> extends ConnectionSourceInvocationHandler<Z, D, F>
036{
037        private static final Set<Method> getMethodSet = Methods.findMethods(CommonDataSource.class, "get\\w+");
038        private static final Set<Method> setMethodSet = Methods.findMethods(CommonDataSource.class, "set\\w+");
039        
040        protected CommonDataSourceInvocationHandler(Class<Z> targetClass, F factory)
041        {
042                super(targetClass, factory);
043        }
044
045        @Override
046        protected InvocationStrategy getInvocationStrategy(Z dataSource, Method method, Object... parameters) throws SQLException
047        {
048                if (getMethodSet.contains(method))
049                {
050                        return InvocationStrategies.INVOKE_ON_ANY;
051                }
052
053                if (setMethodSet.contains(method))
054                {
055                        return InvocationStrategies.INVOKE_ON_EXISTING;
056                }
057                
058                return super.getInvocationStrategy(dataSource, method, parameters);
059        }
060
061        @Override
062        protected <R> void postInvoke(Invoker<Z, D, Z, R, SQLException> invoker, Z proxy, Method method, Object... parameters)
063        {
064                if (setMethodSet.contains(method))
065                {
066                        this.getProxyFactory().record(invoker);
067                }
068        }
069}