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.
/**
* Copyright 2010 Tim Azzopardi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sf.log4jdbc.sql.jdbcapi;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import net.sf.log4jdbc.log.SpyLogDelegator;
import net.sf.log4jdbc.log.SpyLogFactory;
import net.sf.log4jdbc.sql.Spy;
/**
* A proxy datasource that can be used to wrap a real data source allowing log4jdbc to do its work on the real
* data source.
* Inspired by http://groups.google.com/group/log4jdbc/browse_thread/thread/0706611d1b85e210
*
* This can be useful in a Spring context. Imagine your spring context includes this datasource definition
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* You can get log4jdbc to work on this using the following config changes
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* @author tim.azzopardi Log4jdbcProxyDataSource.java
* @author Mathieu Seppey renamed and modified
*
*/
public class DataSourceSpy implements DataSource, Spy {
private DataSource realDataSource;
/**
* The SpyLogDelegator used by this DataSource
* and used by all resources obtained starting from this DataSource
* (Connections, ResultSets, ...).
*/
private SpyLogDelegator log;
/**
* Constructor
* @param realDataSource the real DataSource
*/
public DataSourceSpy(DataSource realDataSource)
{
this.realDataSource = realDataSource;
this.log = SpyLogFactory.getSpyLogDelegator();
}
/**
* Get the SpyLogDelegator that are used by all resources
* obtained starting from this DataSource
* (Connections, ResultSets, ...).
* @return The SpyLogDelegator currently used
* by this DataSource.
*/
public SpyLogDelegator getLogDelegator() {
return this.log;
}
/**
* Set a custom SpyLogDelegator to be used by all resources
* provided by this DataSource, rather than the default
* SpyLogDelegator returned by
* {@link net.sf.log4jdbc.log.SpyLogFactory#getSpyLogDelegator()
* SpyLogFactory#getSpyLogDelegator()}.
* @param spyLogDelegator The SpyLogDelegator to be used by all resources
* obtained starting from this DataSource
* (Connections, ResultSets, ...).
*/
public void setLogDelegator(SpyLogDelegator spyLogDelegator) {
this.log = spyLogDelegator;
}
/**
* Report to the logger all exceptions which have to be reported by this class
* @param methodCall the method which threw an exception
* @param exception the thrown exception
*/
protected void reportException(String methodCall, SQLException exception)
{
log.exceptionOccured(this, methodCall, exception, null, -1L);
}
/**
* Report to the logger all returns which have to be reported by this class
* and which are not exceptions
* @param methodCall the method which report the value
* @param value the reported value
* @return Object The object originally returned by the method called
*/
private Object reportReturn(String methodCall, Object value)
{
log.methodReturned(this, methodCall, "");
return value;
}
public Connection getConnection() throws SQLException
{
String methodCall = "getConnection()";
long tstart = System.currentTimeMillis();
try
{
final Connection connection = realDataSource.getConnection();
if (log.isJdbcLoggingEnabled()) {
return (Connection) reportReturn(methodCall,
new ConnectionSpy(connection, DriverSpy.getRdbmsSpecifics(connection),
System.currentTimeMillis() - tstart, this.log));
}
//if logging is not enable, return the real connection,
//so that there is no useless costs
return connection;
}
catch (SQLException s)
{
reportException(methodCall,s);
throw s;
}
}
public Connection getConnection(String username, String password) throws SQLException
{
String methodCall = "getConnection("+ username +", password***)";
long tstart = System.currentTimeMillis();
try
{
final Connection connection = realDataSource.getConnection(username, password);
if (log.isJdbcLoggingEnabled()) {
return (Connection) reportReturn(methodCall,
new ConnectionSpy(connection, DriverSpy.getRdbmsSpecifics(connection),
System.currentTimeMillis() - tstart, this.log));
}
//if logging is not enable, return the real connection,
//so that there is no useless costs
return connection;
}
catch (SQLException s)
{
reportException(methodCall,s);
throw s;
}
}
public int getLoginTimeout() throws SQLException {
String methodCall = "getLoginTimeout()";
try
{
return (Integer) reportReturn(methodCall,realDataSource.getLoginTimeout());
}
catch (SQLException s)
{
reportException(methodCall,s);
throw s;
}
}
public PrintWriter getLogWriter() throws SQLException {
String methodCall = "getLogWriter()";
try
{
return (PrintWriter) reportReturn(methodCall,realDataSource.getLogWriter());
}
catch (SQLException s)
{
reportException(methodCall,s);
throw s;
}
}
public void setLoginTimeout(int seconds) throws SQLException {
String methodCall = "setLoginTimeout("+ seconds +")";
try
{
realDataSource.setLoginTimeout(seconds);
}
catch (SQLException s)
{
reportException(methodCall,s);
throw s;
}
}
public void setLogWriter(PrintWriter out) throws SQLException {
String methodCall = "setLogWriter("+ out +")";
try
{
realDataSource.setLogWriter(out);
}
catch (SQLException s)
{
reportException(methodCall,s);
throw s;
}
}
public String getClassType()
{
return "DataSource";
}
public Integer getConnectionNumber()
{
// No connection number in this case
return null;
}
}