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.lang.reflect.*;
19: import java.sql.PreparedStatement;
20: import java.sql.ResultSet;
21: import java.sql.Statement;
22:
23: /**
24: * 数据库语句对象的代理
25: * @author Winter Lau
26: */
27: class _Statement implements InvocationHandler {
28:
29: private final static Class[] infs = new Class[] { Statement.class };
30: private final static Class[] infs2 = new Class[] { PreparedStatement.class };
31:
32: public _Statement(Statement stmt, boolean decode) {
33: statement = stmt;
34: this .decode = decode;
35: }
36:
37: /**
38: * 获取Statement实例的代理
39: * @return
40: */
41: public Statement getStatement() {
42: if (statement instanceof PreparedStatement)
43: return (Statement) Proxy.newProxyInstance(statement
44: .getClass().getClassLoader(), infs2, this );
45: return (Statement) Proxy.newProxyInstance(statement.getClass()
46: .getClassLoader(), infs, this );
47: }
48:
49: public Object invoke(Object proxy, Method m, Object args[])
50: throws Throwable {
51: String method = m.getName();
52: if (decode && SETSTRING.equals(method))
53: try {
54: String param = (String) args[1];
55: if (param != null)
56: param = new String(param.getBytes(), "8859_1");
57: return m.invoke(statement, new Object[] { args[0],
58: param });
59: } catch (InvocationTargetException e) {
60: throw e.getTargetException();
61: }
62: if (decode && EXECUTEQUERY.equals(method))
63: try {
64: ResultSet rs = (ResultSet) m.invoke(statement, args);
65: return (new _ResultSet(rs, decode)).getResultSet();
66: } catch (InvocationTargetException e) {
67: throw e.getTargetException();
68: }
69: try {
70: return m.invoke(statement, args);
71: } catch (InvocationTargetException e) {
72: if (GETQUERYTIMEOUT.equals(method))
73: return new Integer(0);
74: else
75: throw e.getTargetException();
76: }
77: }
78:
79: private Statement statement;
80:
81: private boolean decode;
82:
83: private static final String GETQUERYTIMEOUT = "getQueryTimeout";
84:
85: private static final int gqt_return = 0;
86:
87: private static final String SETSTRING = "setString";
88:
89: private static final String EXECUTEQUERY = "executeQuery";
90: }
|