[766] | 1 | /* |
---|
| 2 | Copyright (c) 2002, 2012, 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 | */ |
---|
| 26 | package com.mysql.jdbc.jdbc2.optional; |
---|
| 27 | |
---|
| 28 | import java.lang.reflect.InvocationHandler; |
---|
| 29 | import java.lang.reflect.InvocationTargetException; |
---|
| 30 | import java.lang.reflect.Method; |
---|
| 31 | import java.lang.reflect.Proxy; |
---|
| 32 | import java.sql.SQLException; |
---|
| 33 | import java.util.Map; |
---|
| 34 | |
---|
| 35 | import com.mysql.jdbc.ExceptionInterceptor; |
---|
| 36 | import com.mysql.jdbc.SQLError; |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * Base class for all wrapped instances created by LogicalHandle |
---|
| 40 | * |
---|
| 41 | * @author Mark matthews |
---|
| 42 | * |
---|
| 43 | * @version $Id$ |
---|
| 44 | */ |
---|
| 45 | abstract class WrapperBase { |
---|
| 46 | protected MysqlPooledConnection pooledConnection; |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Fires connection error event if required, before re-throwing exception |
---|
| 50 | * |
---|
| 51 | * @param sqlEx |
---|
| 52 | * the SQLException that has ocurred |
---|
| 53 | * @throws SQLException |
---|
| 54 | * (rethrown) |
---|
| 55 | */ |
---|
| 56 | protected void checkAndFireConnectionError(SQLException sqlEx) |
---|
| 57 | throws SQLException { |
---|
| 58 | if (this.pooledConnection != null) { |
---|
| 59 | if (SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE.equals(sqlEx |
---|
| 60 | .getSQLState())) { |
---|
| 61 | this.pooledConnection.callConnectionEventListeners( |
---|
| 62 | MysqlPooledConnection.CONNECTION_ERROR_EVENT, sqlEx); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | throw sqlEx; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | protected Map unwrappedInterfaces = null; |
---|
| 70 | protected ExceptionInterceptor exceptionInterceptor; |
---|
| 71 | |
---|
| 72 | protected WrapperBase(MysqlPooledConnection pooledConnection) { |
---|
| 73 | this.pooledConnection = pooledConnection; |
---|
| 74 | this.exceptionInterceptor = this.pooledConnection.getExceptionInterceptor(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | protected class ConnectionErrorFiringInvocationHandler implements InvocationHandler { |
---|
| 78 | Object invokeOn = null; |
---|
| 79 | |
---|
| 80 | public ConnectionErrorFiringInvocationHandler(Object toInvokeOn) { |
---|
| 81 | invokeOn = toInvokeOn; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | public Object invoke(Object proxy, Method method, |
---|
| 85 | Object[] args) throws Throwable { |
---|
| 86 | Object result = null; |
---|
| 87 | |
---|
| 88 | try { |
---|
| 89 | result = method.invoke(invokeOn, args); |
---|
| 90 | |
---|
| 91 | if (result != null) { |
---|
| 92 | result = proxyIfInterfaceIsJdbc(result, |
---|
| 93 | result.getClass()); |
---|
| 94 | } |
---|
| 95 | } catch (InvocationTargetException e) { |
---|
| 96 | if (e.getTargetException() instanceof SQLException) { |
---|
| 97 | checkAndFireConnectionError((SQLException) e |
---|
| 98 | .getTargetException()); |
---|
| 99 | } else { |
---|
| 100 | throw e; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | return result; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | * Recursively checks for interfaces on the given object to determine |
---|
| 109 | * if it implements a java.sql interface, and if so, proxies the |
---|
| 110 | * instance so that we can catch and fire SQL errors. |
---|
| 111 | * @param toProxy |
---|
| 112 | * @param clazz |
---|
| 113 | * @return |
---|
| 114 | */ |
---|
| 115 | private Object proxyIfInterfaceIsJdbc(Object toProxy, Class<?> clazz) { |
---|
| 116 | Class<?>[] interfaces = clazz.getInterfaces(); |
---|
| 117 | |
---|
| 118 | for (Class<?> iclass : interfaces) { |
---|
| 119 | String packageName = iclass.getPackage().getName(); |
---|
| 120 | |
---|
| 121 | if ("java.sql".equals(packageName) || |
---|
| 122 | "javax.sql".equals(packageName)) { |
---|
| 123 | return Proxy.newProxyInstance(toProxy.getClass() |
---|
| 124 | .getClassLoader(), interfaces, |
---|
| 125 | new ConnectionErrorFiringInvocationHandler(toProxy)); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | return proxyIfInterfaceIsJdbc(toProxy, iclass); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | return toProxy; |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | } |
---|