001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.test.jdbc;
016    
017    import com.liferay.portal.kernel.util.ProxyUtil;
018    
019    import java.lang.reflect.InvocationHandler;
020    import java.lang.reflect.InvocationTargetException;
021    import java.lang.reflect.Method;
022    
023    import java.sql.CallableStatement;
024    import java.sql.Connection;
025    import java.sql.PreparedStatement;
026    import java.sql.Statement;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class ResetDatabaseConnectionHandler implements InvocationHandler {
032    
033            public ResetDatabaseConnectionHandler(Connection connection) {
034                    _connection = connection;
035            }
036    
037            @Override
038            public Object invoke(Object proxy, Method method, Object[] arguments)
039                    throws Throwable {
040    
041                    try {
042                            String methodName = method.getName();
043    
044                            if (methodName.equals("equals")) {
045                                    if (proxy == arguments[0]) {
046                                            return true;
047                                    }
048    
049                                    return false;
050                            }
051    
052                            if (methodName.equals("hashCode")) {
053                                    return System.identityHashCode(proxy);
054                            }
055    
056                            if (methodName.equals("prepareCall") ||
057                                    methodName.equals("prepareStatement")) {
058    
059                                    ResetDatabaseUtil.processSQL(_connection, (String)arguments[0]);
060                            }
061    
062                            Object returnValue = method.invoke(_connection, arguments);
063    
064                            if (methodName.equals("createStatement") ||
065                                    methodName.equals("prepareCall") ||
066                                    methodName.equals("prepareStatement")) {
067    
068                                    Statement statement = (Statement)returnValue;
069    
070                                    return ProxyUtil.newProxyInstance(
071                                            ResetDatabaseConnectionHandler.class.getClassLoader(),
072                                            getInterfaces(statement),
073                                            new ResetDatabaseStatementHandler(_connection, statement));
074                            }
075    
076                            return returnValue;
077                    }
078                    catch (InvocationTargetException ite) {
079                            throw ite.getTargetException();
080                    }
081            }
082    
083            protected Class<?>[] getInterfaces(Statement statement) {
084                    if (statement instanceof CallableStatement) {
085                            return new Class<?>[] {CallableStatement.class};
086                    }
087    
088                    if (statement instanceof PreparedStatement) {
089                            return new Class<?>[] {PreparedStatement.class};
090                    }
091    
092                    return new Class<?>[] {Statement.class};
093            }
094    
095            private final Connection _connection;
096    
097    }