Skip to content

Commit

Permalink
Implement DynamicProxy (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
safris authored and malafeev committed Jan 10, 2020
1 parent f6031c2 commit e55efdf
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 33 deletions.
34 changes: 34 additions & 0 deletions src/main/java/io/opentracing/contrib/jdbc/DynamicProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2017-2020 The OpenTracing Authors
*
* 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 io.opentracing.contrib.jdbc;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
* @author Seva Safris
*/
public class DynamicProxy {
@SuppressWarnings("unchecked")
public static <T>T wrap(final T target, final T wrapper) {
final Class<?> cls = target.getClass();
return (T)Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
return method.invoke(wrapper, args);
}
});
}
}
67 changes: 36 additions & 31 deletions src/main/java/io/opentracing/contrib/jdbc/TracingConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.concurrent.Executor;

public class TracingConnection implements Connection {

private final Connection connection;
private final ConnectionInfo connectionInfo;
private final boolean withActiveSpanOnly;
Expand All @@ -54,20 +53,23 @@ public TracingConnection(Connection connection, ConnectionInfo connectionInfo,

@Override
public Statement createStatement() throws SQLException {
return new TracingStatement(connection.createStatement(), connectionInfo, withActiveSpanOnly,
ignoredStatements, tracer);
final Statement statement = connection.createStatement();
return DynamicProxy.wrap(statement, new TracingStatement(statement, connectionInfo, withActiveSpanOnly,
ignoredStatements, tracer));
}

@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
return new TracingPreparedStatement(connection.prepareStatement(sql), sql, connectionInfo,
withActiveSpanOnly, ignoredStatements, tracer);
final PreparedStatement statement = connection.prepareStatement(sql);
return DynamicProxy.wrap(statement, new TracingPreparedStatement(statement, sql, connectionInfo,
withActiveSpanOnly, ignoredStatements, tracer));
}

@Override
public CallableStatement prepareCall(String sql) throws SQLException {
return new TracingCallableStatement(connection.prepareCall(sql), sql, connectionInfo,
withActiveSpanOnly, ignoredStatements, tracer);
final CallableStatement statement = connection.prepareCall(sql);
return DynamicProxy.wrap(statement, new TracingCallableStatement(connection.prepareCall(sql), sql, connectionInfo,
withActiveSpanOnly, ignoredStatements, tracer));
}

@Override
Expand Down Expand Up @@ -153,24 +155,25 @@ public void clearWarnings() throws SQLException {
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
return new TracingStatement(connection.createStatement(resultSetType, resultSetConcurrency),
connectionInfo, withActiveSpanOnly, ignoredStatements, tracer);
final Statement statement = connection.createStatement(resultSetType, resultSetConcurrency);
return DynamicProxy.wrap(statement, new TracingStatement(statement,
connectionInfo, withActiveSpanOnly, ignoredStatements, tracer));
}

@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
return new TracingPreparedStatement(
connection.prepareStatement(sql, resultSetType, resultSetConcurrency), sql, connectionInfo,
withActiveSpanOnly, ignoredStatements, tracer);
final PreparedStatement statement = connection.prepareStatement(sql, resultSetType, resultSetConcurrency);
return DynamicProxy.wrap(statement, new TracingPreparedStatement(statement, sql, connectionInfo,
withActiveSpanOnly, ignoredStatements, tracer));
}

@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
return new TracingCallableStatement(
connection.prepareCall(sql, resultSetType, resultSetConcurrency), sql, connectionInfo,
withActiveSpanOnly, ignoredStatements, tracer);
final CallableStatement statement = connection.prepareCall(sql, resultSetType, resultSetConcurrency);
return DynamicProxy.wrap(statement, new TracingCallableStatement(statement, sql, connectionInfo,
withActiveSpanOnly, ignoredStatements, tracer));
}

@Override
Expand Down Expand Up @@ -216,44 +219,46 @@ public void releaseSavepoint(Savepoint savepoint) throws SQLException {
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
return new TracingStatement(
connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability),
connectionInfo, withActiveSpanOnly, ignoredStatements, tracer);
final Statement statement = connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
return DynamicProxy.wrap(statement, new TracingStatement(statement,
connectionInfo, withActiveSpanOnly, ignoredStatements, tracer));
}

@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
return new TracingPreparedStatement(
connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability),
sql, connectionInfo, withActiveSpanOnly, ignoredStatements, tracer);
final PreparedStatement statement = connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
return DynamicProxy.wrap(statement, new TracingPreparedStatement(statement,
sql, connectionInfo, withActiveSpanOnly, ignoredStatements, tracer));
}

@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
return new TracingCallableStatement(
connection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability), sql,
connectionInfo, withActiveSpanOnly, ignoredStatements, tracer);
final CallableStatement statement = connection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
return DynamicProxy.wrap(statement, new TracingCallableStatement(statement, sql,
connectionInfo, withActiveSpanOnly, ignoredStatements, tracer));
}

@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
return new TracingPreparedStatement(connection.prepareStatement(sql, autoGeneratedKeys), sql,
connectionInfo, withActiveSpanOnly, ignoredStatements, tracer);
final PreparedStatement statement = connection.prepareStatement(sql, autoGeneratedKeys);
return DynamicProxy.wrap(statement, new TracingPreparedStatement(statement, sql,
connectionInfo, withActiveSpanOnly, ignoredStatements, tracer));
}

@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
return new TracingPreparedStatement(connection.prepareStatement(sql, columnIndexes), sql,
connectionInfo, withActiveSpanOnly, ignoredStatements, tracer);
final PreparedStatement statement = connection.prepareStatement(sql, columnIndexes);
return DynamicProxy.wrap(statement, new TracingPreparedStatement(statement, sql,
connectionInfo, withActiveSpanOnly, ignoredStatements, tracer));
}

@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
return new TracingPreparedStatement(connection.prepareStatement(sql, columnNames), sql,
connectionInfo,
withActiveSpanOnly, ignoredStatements, tracer);
final PreparedStatement statement = connection.prepareStatement(sql, columnNames);
return DynamicProxy.wrap(statement, new TracingPreparedStatement(statement, sql, connectionInfo,
withActiveSpanOnly, ignoredStatements, tracer));
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/opentracing/contrib/jdbc/TracingDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ public Connection connect(String url, Properties info) throws SQLException {
span.finish();
}

return new TracingConnection(connection, connectionInfo, withActiveSpanOnly,
ignoreStatements, currentTracer);
return DynamicProxy.wrap(connection, new TracingConnection(connection, connectionInfo, withActiveSpanOnly,
ignoreStatements, currentTracer));
}

@Override
Expand Down

0 comments on commit e55efdf

Please sign in to comment.