001/* 002 * HA-JDBC: High-Availability JDBC 003 * Copyright (c) 2004-2007 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 */ 021package net.sf.hajdbc.sql; 022 023import java.sql.SQLException; 024import java.util.Map; 025 026import net.sf.hajdbc.Database; 027 028/** 029 * @author Paul Ferraro 030 * @param <D> 031 * @param <P> 032 * @param <T> 033 */ 034public abstract class AbstractChildInvocationHandler<D, P, T> extends AbstractInvocationHandler<D, T> 035{ 036 private P parentObject; 037 private SQLProxy<D, P> parentProxy; 038 private Invoker<D, P, T> parentInvoker; 039 040 protected AbstractChildInvocationHandler(P parent, SQLProxy<D, P> proxy, Invoker<D, P, T> invoker, Class<T> proxyClass, Map<Database<D>, T> objectMap) throws Exception 041 { 042 super(proxy.getDatabaseCluster(), proxyClass, objectMap); 043 044 this.parentObject = parent; 045 this.parentProxy = proxy; 046 this.parentInvoker = invoker; 047 this.parentProxy.addChild(this); 048 } 049 050 @Override 051 protected T createObject(Database<D> database) throws Exception 052 { 053 P object = this.parentProxy.getObject(database); 054 055 if (object == null) 056 { 057 throw new IllegalStateException(); 058 } 059 060 return this.parentInvoker.invoke(database, object); 061 } 062 063 @Override 064 protected void close(Database<D> database, T object) 065 { 066 try 067 { 068 this.close(this.parentProxy.getObject(database), object); 069 } 070 catch (SQLException e) 071 { 072 this.logger.info(e.getMessage(), e); 073 } 074 } 075 076 protected abstract void close(P parent, T object) throws SQLException; 077 078 /** 079 * @see net.sf.hajdbc.sql.SQLProxy#getRoot() 080 */ 081 @Override 082 public final SQLProxy<D, ?> getRoot() 083 { 084 return this.parentProxy.getRoot(); 085 } 086 087 protected P getParent() 088 { 089 return this.parentObject; 090 } 091 092 protected SQLProxy<D, P> getParentProxy() 093 { 094 return this.parentProxy; 095 } 096}