01: /*
02: * This program is free software; you can redistribute it and/or modify
03: * it under the terms of the GNU General Public License as published by
04: * the Free Software Foundation; either version 2 of the License, or
05: * (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10: * GNU Library General Public License for more details.
11: *
12: * You should have received a copy of the GNU General Public License
13: * along with this program; if not, write to the Free Software
14: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15: */
16: package web.struts;
17:
18: import java.io.Serializable;
19: import java.lang.reflect.*;
20: import java.sql.Connection;
21: import javax.sql.DataSource;
22:
23: /**
24: * 数据源对象的接管,由于Struts本身使用的是dbcp连接池
25: * 此类就是接管DataSource接口
26: * @author Winter Lau
27: */
28: class _DataSource implements InvocationHandler, Serializable {
29:
30: private final static Class[] infs = new Class[] { DataSource.class };
31:
32: public _DataSource(DataSource ds, boolean encoding) {
33: dataSource = ds;
34: this .encoding = encoding;
35: }
36:
37: /**
38: * 获取DataSource的代理
39: * @return
40: */
41: public DataSource getDataSource() {
42: return (DataSource) Proxy.newProxyInstance(dataSource
43: .getClass().getClassLoader(), infs, this );
44: }
45:
46: public Object invoke(Object proxy, Method m, Object args[])
47: throws Throwable {
48: if (METHOD_NAME.equals(m.getName())) {
49: try {
50: Connection conn = (Connection) m.invoke(dataSource,
51: args);
52: return (conn == null) ? null : (new _Connection(conn,
53: encoding)).getConnection();
54: } catch (InvocationTargetException e) {
55: throw e.getTargetException();
56: }
57: }
58: try {
59: return m.invoke(dataSource, args);
60: } catch (InvocationTargetException e) {
61: throw e.getTargetException();
62: }
63: }
64:
65: private DataSource dataSource;
66:
67: private boolean encoding;
68:
69: private final static String METHOD_NAME = "getConnection";
70: }
|