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.dao.jdbc.DataSourceFactoryImpl;
018    import com.liferay.portal.dao.jdbc.util.DataSourceWrapper;
019    import com.liferay.portal.kernel.util.ProxyUtil;
020    import com.liferay.portal.kernel.util.ReflectionUtil;
021    
022    import java.lang.reflect.Field;
023    
024    import java.sql.Connection;
025    import java.sql.SQLException;
026    
027    import javax.sql.DataSource;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class ResetDatabaseUtilDataSource extends DataSourceWrapper {
033    
034            public static void initialize() {
035                    try {
036                            if (_paclField.get(null) != _pacl) {
037                                    _paclField.set(null, _pacl);
038                            }
039                    }
040                    catch (Exception e) {
041                            throw new RuntimeException(e);
042                    }
043            }
044    
045            public ResetDatabaseUtilDataSource(DataSource dataSource) {
046                    super(dataSource);
047            }
048    
049            @Override
050            public Connection getConnection() throws SQLException {
051                    return (Connection)ProxyUtil.newProxyInstance(
052                            ResetDatabaseUtilDataSource.class.getClassLoader(),
053                            new Class<?>[] {Connection.class},
054                            new ResetDatabaseConnectionHandler(super.getConnection()));
055            }
056    
057            private static final DataSourceFactoryImpl.PACL _pacl =
058                    new DataSourceFactoryImpl.PACL() {
059    
060                            @Override
061                            public DataSource getDataSource(DataSource dataSource) {
062                                    return new ResetDatabaseUtilDataSource(dataSource);
063                            }
064    
065                    };
066    
067            private static final Field _paclField;
068    
069            static {
070                    try {
071                            _paclField = ReflectionUtil.getDeclaredField(
072                                    DataSourceFactoryImpl.class, "_pacl");
073                    }
074                    catch (Exception e) {
075                            throw new ExceptionInInitializerError(e);
076                    }
077            }
078    
079    }