001
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
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 }