Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* HA-JDBC: High-Availability JDBC
* Copyright (C) 2013 Paul Ferraro
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package net.sf.hajdbc.sql.pool;
import java.lang.reflect.Proxy;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.sql.ConnectionEvent;
import javax.sql.ConnectionEventListener;
import javax.sql.PooledConnection;
import javax.sql.StatementEvent;
import javax.sql.StatementEventListener;
import net.sf.hajdbc.Database;
import net.sf.hajdbc.invocation.Invoker;
import net.sf.hajdbc.logging.Level;
import net.sf.hajdbc.sql.AbstractTransactionalProxyFactory;
import net.sf.hajdbc.sql.LocalTransactionContext;
import net.sf.hajdbc.sql.ProxyFactory;
/**
*
* @author Paul Ferraro
*/
public abstract class AbstractPooledConnectionProxyFactory, C extends PooledConnection> extends AbstractTransactionalProxyFactory
{
private Map> connectionEventListenerInvokers = new HashMap>();
private Map> statementEventListenerInvokers = new HashMap>();
protected AbstractPooledConnectionProxyFactory(Z parentProxy, ProxyFactory parent, Invoker invoker, Map map)
{
super(parentProxy, parent, invoker, map, new LocalTransactionContext(parent.getDatabaseCluster()));
}
public void addConnectionEventListener(ConnectionEventListener listener, Invoker invoker)
{
this.connectionEventListenerInvokers.put(listener, invoker);
}
public void removeConnectionEventListener(ConnectionEventListener listener)
{
this.connectionEventListenerInvokers.remove(listener);
}
public void addStatementEventListener(StatementEventListener listener, Invoker invoker)
{
this.statementEventListenerInvokers.put(listener, invoker);
}
public void removeStatementEventListener(StatementEventListener listener)
{
this.statementEventListenerInvokers.remove(listener);
}
@Override
public void replay(D database, C connection) throws SQLException
{
super.replay(database, connection);
for (Invoker invoker: this.connectionEventListenerInvokers.values())
{
invoker.invoke(database, connection);
}
for (Invoker invoker: this.statementEventListenerInvokers.values())
{
invoker.invoke(database, connection);
}
}
@Override
public void close(D database, C connection)
{
try
{
connection.close();
}
catch (SQLException e)
{
this.logger.log(Level.INFO, e);
}
}
@SuppressWarnings("unused")
private static class ConnectionEventListenerFilter, C extends PooledConnection> implements ConnectionEventListener
{
private AbstractPooledConnectionProxyFactory proxyFactory;
private final D database;
private final ConnectionEventListener listener;
ConnectionEventListenerFilter(AbstractPooledConnectionProxyFactory proxyFactory, D database, ConnectionEventListener listener)
{
this.proxyFactory = proxyFactory;
this.database = database;
this.listener = listener;
}
@Override
public void connectionClosed(ConnectionEvent event)
{
ConnectionEvent e = this.getEvent(event);
if (e != null)
{
this.listener.connectionClosed(e);
}
}
@Override
public void connectionErrorOccurred(ConnectionEvent event)
{
ConnectionEvent e = this.getEvent(event);
if (e != null)
{
this.listener.connectionErrorOccurred(e);
}
}
private ConnectionEvent getEvent(ConnectionEvent event)
{
Object source = event.getSource();
C connection = this.proxyFactory.get(this.database);
if (Proxy.isProxyClass(source.getClass()) && Proxy.getInvocationHandler(source) instanceof AbstractPooledConnectionInvocationHandler)
{
return new ConnectionEvent(connection, event.getSQLException());
}
return event.getSource().equals(connection) ? event : null;
}
}
@SuppressWarnings("unused")
private static class StatementEventListenerFilter, C extends PooledConnection> implements StatementEventListener
{
private AbstractPooledConnectionProxyFactory proxyFactory;
private final D database;
private final StatementEventListener listener;
StatementEventListenerFilter(AbstractPooledConnectionProxyFactory proxyFactory, D database, StatementEventListener listener)
{
this.proxyFactory = proxyFactory;
this.database = database;
this.listener = listener;
}
@Override
public void statementClosed(StatementEvent event)
{
StatementEvent e = this.getEvent(event);
if (e != null)
{
this.listener.statementClosed(e);
}
}
@Override
public void statementErrorOccurred(StatementEvent event)
{
StatementEvent e = this.getEvent(event);
if (e != null)
{
this.listener.statementErrorOccurred(e);
}
}
private StatementEvent getEvent(StatementEvent event)
{
Object source = event.getSource();
C connection = this.proxyFactory.get(this.database);
if (Proxy.isProxyClass(source.getClass()) && Proxy.getInvocationHandler(source) instanceof AbstractPooledConnectionInvocationHandler)
{
return new StatementEvent(connection, event.getStatement(), event.getSQLException());
}
return source.equals(connection) ? event : null;
}
}
}