Tie

Java Method Interception

Quick Start Guide

Intercepting a method

Using the injector your created in the previous step, you can now create intercepted proxies to your objects. Below is the code to provide logging around a JDBC statement.

public Statement getStatement(Connection connection) throws SQLException
{
    InterceptionInjector injector = createInjector();
    Statement statement = connection.createStatement();
    return injector.wrapObject(Statement.class, statement);
}

That's all these it to it. Now, if we use get getStatement method to create our statements, then each method call will be logged.

public int getNumberOfOrders() throws SQLException
{
    Connection connection = ConnectionManager.getConnection();
    Statement statement = getStatement(connection);
    try
    {
        ResultSet results = statement.executeQuery("SELECT COUNT(*) FROM orders");
        return DatabaseManager.getIntegerResult(results);
    }
    finally
    {
        statement.close();
    }
}

Now, you could acheive the same result by creating your own class that implements Statement, and adding logging in each method. However, that would

  1. be more work
  2. need to be repeated for PreparedStatement and CallableStatement.
Using the ProxyInterceptor is simple, and can easily be extended to create intercepted versions of PreparedStatement and CallableStatement. The jdbc_wrapper example in the sample code shows a more complete (yet still very simple) implementation of this scenario.

The instructions listed above do not require the "ext" jar to be on the classpath.