01: /*
02: * HA-JDBC: High-Availability JDBC
03: * Copyright (c) 2004-2007 Paul Ferraro
04: *
05: * This library is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU Lesser General Public License as published by the
07: * Free Software Foundation; either version 2.1 of the License, or (at your
08: * option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13: * for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public License
16: * along with this library; if not, write to the Free Software Foundation,
17: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * Contact: ferraro@users.sourceforge.net
20: */
21: package net.sf.hajdbc.sql;
22:
23: import java.sql.SQLException;
24: import java.util.Map;
25:
26: import net.sf.hajdbc.Database;
27:
28: /**
29: * @author Paul Ferraro
30: * @param <D>
31: * @param <P>
32: * @param <T>
33: */
34: public abstract class AbstractChildInvocationHandler<D, P, T> extends
35: AbstractInvocationHandler<D, T> {
36: private P parentObject;
37: private SQLProxy<D, P> parentProxy;
38: private Invoker<D, P, T> parentInvoker;
39:
40: protected AbstractChildInvocationHandler(P parent,
41: SQLProxy<D, P> proxy, Invoker<D, P, T> invoker,
42: Class<T> proxyClass, Map<Database<D>, T> objectMap)
43: throws Exception {
44: super (proxy.getDatabaseCluster(), proxyClass, objectMap);
45:
46: this .parentObject = parent;
47: this .parentProxy = proxy;
48: this .parentInvoker = invoker;
49: this .parentProxy.addChild(this );
50: }
51:
52: @Override
53: protected T createObject(Database<D> database) throws Exception {
54: P object = this .parentProxy.getObject(database);
55:
56: if (object == null) {
57: throw new IllegalStateException();
58: }
59:
60: return this .parentInvoker.invoke(database, object);
61: }
62:
63: @Override
64: protected void close(Database<D> database, T object) {
65: try {
66: this .close(this .parentProxy.getObject(database), object);
67: } catch (SQLException e) {
68: this .logger.info(e.getMessage(), e);
69: }
70: }
71:
72: protected abstract void close(P parent, T object)
73: throws SQLException;
74:
75: /**
76: * @see net.sf.hajdbc.sql.SQLProxy#getRoot()
77: */
78: @Override
79: public final SQLProxy<D, ?> getRoot() {
80: return this .parentProxy.getRoot();
81: }
82:
83: protected P getParent() {
84: return this .parentObject;
85: }
86:
87: protected SQLProxy<D, P> getParentProxy() {
88: return this.parentProxy;
89: }
90: }
|