001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2008 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.sql.pool;
022:
023: import java.lang.reflect.Method;
024: import java.sql.SQLException;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import javax.sql.CommonDataSource;
029: import javax.sql.PooledConnection;
030:
031: import net.sf.hajdbc.Database;
032: import net.sf.hajdbc.sql.AbstractChildInvocationHandler;
033: import net.sf.hajdbc.sql.ConnectionInvocationStrategy;
034: import net.sf.hajdbc.sql.DriverWriteInvocationStrategy;
035: import net.sf.hajdbc.sql.InvocationStrategy;
036: import net.sf.hajdbc.sql.Invoker;
037: import net.sf.hajdbc.sql.SQLProxy;
038: import net.sf.hajdbc.sql.TransactionContext;
039: import net.sf.hajdbc.util.reflect.Methods;
040:
041: /**
042: * @author Paul Ferraro
043: * @param <D>
044: * @param <C>
045: */
046: @SuppressWarnings("nls")
047: public abstract class AbstractPooledConnectionInvocationHandler<D extends CommonDataSource, C extends PooledConnection>
048: extends AbstractChildInvocationHandler<D, D, C> {
049: private static final Set<Method> eventListenerMethodSet = Methods
050: .findMethods(PooledConnection.class,
051: "(add|remove)(Connection|Statement)EventListener");
052:
053: private static final Method getConnectionMethod = Methods
054: .getMethod(PooledConnection.class, "getConnection");
055: private static final Method closeMethod = Methods.getMethod(
056: PooledConnection.class, "close");
057:
058: /**
059: * @param dataSource
060: * @param proxy
061: * @param invoker
062: * @param proxyClass
063: * @param objectMap
064: * @throws Exception
065: */
066: protected AbstractPooledConnectionInvocationHandler(D dataSource,
067: SQLProxy<D, D> proxy, Invoker<D, D, C> invoker,
068: Class<C> proxyClass, Map<Database<D>, C> objectMap)
069: throws Exception {
070: super (dataSource, proxy, invoker, proxyClass, objectMap);
071: }
072:
073: @Override
074: protected InvocationStrategy<D, C, ?> getInvocationStrategy(
075: C connection, Method method, Object[] parameters)
076: throws Exception {
077: if (eventListenerMethodSet.contains(method)) {
078: return new DriverWriteInvocationStrategy<D, C, Void>();
079: }
080:
081: if (method.equals(getConnectionMethod)) {
082: return new ConnectionInvocationStrategy<D, C>(this .cluster,
083: connection, this .createTransactionContext());
084: }
085:
086: return super .getInvocationStrategy(connection, method,
087: parameters);
088: }
089:
090: @Override
091: protected void postInvoke(C connection, Method method,
092: Object[] parameters) {
093: if (method.equals(closeMethod)) {
094: this .getParentProxy().removeChild(this );
095: }
096: }
097:
098: /**
099: * @see net.sf.hajdbc.sql.AbstractChildInvocationHandler#close(java.lang.Object, java.lang.Object)
100: */
101: @Override
102: protected void close(D dataSource, C connection)
103: throws SQLException {
104: connection.close();
105: }
106:
107: protected abstract TransactionContext<D> createTransactionContext();
108: }
|