source: Ballon/out/artifacts/geisa_artifact/WEB-INF/lib/mysql-connector-java-5.1.21/src/com/mysql/jdbc/jdbc2/optional/JDBC4StatementWrapper.java @ 848

Last change on this file since 848 was 766, checked in by npipsl, 11 years ago
File size: 7.0 KB
Line 
1/*
2 Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4
5  The MySQL Connector/J is licensed under the terms of the GPLv2
6  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
7  There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
8  this software, see the FLOSS License Exception
9  <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
10
11  This program is free software; you can redistribute it and/or modify it under the terms
12  of the GNU General Public License as published by the Free Software Foundation; version 2
13  of the License.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
16  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  See the GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License along with this
20  program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
21  Floor, Boston, MA 02110-1301  USA
22
23
24
25 */
26package com.mysql.jdbc.jdbc2.optional;
27
28import java.lang.reflect.InvocationHandler;
29import java.lang.reflect.InvocationTargetException;
30import java.lang.reflect.Method;
31import java.lang.reflect.Proxy;
32import java.sql.Array;
33import java.sql.Blob;
34import java.sql.Clob;
35import java.sql.Connection;
36import java.sql.NClob;
37import java.sql.SQLClientInfoException;
38import java.sql.SQLException;
39import java.sql.SQLXML;
40import java.sql.Savepoint;
41import java.sql.Statement;
42import java.sql.Struct;
43import java.util.ArrayList;
44import java.util.HashMap;
45import java.util.List;
46import java.util.Map;
47import java.util.Properties;
48
49import com.mysql.jdbc.ConnectionImpl;
50import com.mysql.jdbc.SQLError;
51import com.mysql.jdbc.jdbc2.optional.ConnectionWrapper;
52import com.mysql.jdbc.jdbc2.optional.MysqlPooledConnection;
53
54/**
55 */
56public class JDBC4StatementWrapper extends StatementWrapper {
57
58        public JDBC4StatementWrapper(ConnectionWrapper c, 
59                        MysqlPooledConnection conn,
60                        Statement toWrap) {
61                super(c, conn, toWrap);
62        }
63       
64        public void close() throws SQLException {
65                try {
66                        super.close();
67                } finally {
68                        this.unwrappedInterfaces = null;
69                }
70        }
71       
72        public boolean isClosed() throws SQLException {
73                try {
74                        if (this.wrappedStmt != null) {
75                                return this.wrappedStmt.isClosed();
76                        } else {
77                                throw SQLError.createSQLException("Statement already closed",
78                                                SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
79                        }
80                } catch (SQLException sqlEx) {
81                        checkAndFireConnectionError(sqlEx);
82                }
83               
84                return false; // We never get here, compiler can't tell
85        }
86       
87        public void setPoolable(boolean poolable) throws SQLException {
88                try {
89                        if (this.wrappedStmt != null) {
90                                this.wrappedStmt.setPoolable(poolable);
91                        } else {
92                                throw SQLError.createSQLException("Statement already closed",
93                                                SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
94                        }
95                } catch (SQLException sqlEx) {
96                        checkAndFireConnectionError(sqlEx);
97                }
98        }
99       
100        public boolean isPoolable() throws SQLException {
101                try {
102                        if (this.wrappedStmt != null) {
103                                return this.wrappedStmt.isPoolable();
104                        } else {
105                                throw SQLError.createSQLException("Statement already closed",
106                                                SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
107                        }
108                } catch (SQLException sqlEx) {
109                        checkAndFireConnectionError(sqlEx);
110                }
111               
112                return false; // We never get here, compiler can't tell
113        }
114   
115        /**
116         * Returns true if this either implements the interface argument or is
117         * directly or indirectly a wrapper for an object that does. Returns false
118         * otherwise. If this implements the interface then return true, else if
119         * this is a wrapper then return the result of recursively calling
120         * <code>isWrapperFor</code> on the wrapped object. If this does not
121         * implement the interface and is not a wrapper, return false. This method
122         * should be implemented as a low-cost operation compared to
123         * <code>unwrap</code> so that callers can use this method to avoid
124         * expensive <code>unwrap</code> calls that may fail. If this method
125         * returns true then calling <code>unwrap</code> with the same argument
126         * should succeed.
127         *
128         * @param interfaces
129         *            a Class defining an interface.
130         * @return true if this implements the interface or directly or indirectly
131         *         wraps an object that does.
132         * @throws java.sql.SQLException
133         *             if an error occurs while determining whether this is a
134         *             wrapper for an object with the given interface.
135         * @since 1.6
136         */
137        public boolean isWrapperFor(Class<?> iface) throws SQLException {
138
139                boolean isInstance = iface.isInstance(this);
140
141                if (isInstance) {
142                        return true;
143                }
144
145                String interfaceClassName = iface.getName();
146               
147                return (interfaceClassName.equals("com.mysql.jdbc.Statement")
148                                || interfaceClassName.equals("java.sql.Statement")
149                                || interfaceClassName.equals("java.sql.Wrapper"));
150        }
151
152        /**
153         * Returns an object that implements the given interface to allow access to
154         * non-standard methods, or standard methods not exposed by the proxy. The
155         * result may be either the object found to implement the interface or a
156         * proxy for that object. If the receiver implements the interface then that
157         * is the object. If the receiver is a wrapper and the wrapped object
158         * implements the interface then that is the object. Otherwise the object is
159         * the result of calling <code>unwrap</code> recursively on the wrapped
160         * object. If the receiver is not a wrapper and does not implement the
161         * interface, then an <code>SQLException</code> is thrown.
162         *
163         * @param iface
164         *            A Class defining an interface that the result must implement.
165         * @return an object that implements the interface. May be a proxy for the
166         *         actual implementing object.
167         * @throws java.sql.SQLException
168         *             If no object found that implements the interface
169         * @since 1.6
170         */
171        public synchronized <T> T unwrap(java.lang.Class<T> iface)
172                        throws java.sql.SQLException {
173                try {
174                        if ("java.sql.Statement".equals(iface.getName())
175                                        || "java.sql.Wrapper.class".equals(iface.getName())) {
176                                return iface.cast(this);
177                        }
178                       
179                        if (unwrappedInterfaces == null) {
180                                unwrappedInterfaces = new HashMap();
181                        }
182                       
183                        Object cachedUnwrapped = unwrappedInterfaces.get(iface);
184                       
185                        if (cachedUnwrapped == null) {
186                                cachedUnwrapped = Proxy.newProxyInstance(
187                                                this.wrappedStmt.getClass().getClassLoader(), 
188                                                new Class[] { iface },
189                                                new ConnectionErrorFiringInvocationHandler(this.wrappedStmt));
190                                unwrappedInterfaces.put(iface, cachedUnwrapped);
191                        }
192                       
193                        return iface.cast(cachedUnwrapped);
194                } catch (ClassCastException cce) {
195                        throw SQLError.createSQLException("Unable to unwrap to "
196                                        + iface.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
197                }
198        }
199}
Note: See TracBrowser for help on using the repository browser.