From e55efdf828f39d807c3e031f9dfae735b979ea9e Mon Sep 17 00:00:00 2001 From: Seva Safris Date: Fri, 10 Jan 2020 19:06:00 +0700 Subject: [PATCH] Implement DynamicProxy (#59) --- .../contrib/jdbc/DynamicProxy.java | 34 ++++++++++ .../contrib/jdbc/TracingConnection.java | 67 ++++++++++--------- .../contrib/jdbc/TracingDriver.java | 4 +- 3 files changed, 72 insertions(+), 33 deletions(-) create mode 100644 src/main/java/io/opentracing/contrib/jdbc/DynamicProxy.java diff --git a/src/main/java/io/opentracing/contrib/jdbc/DynamicProxy.java b/src/main/java/io/opentracing/contrib/jdbc/DynamicProxy.java new file mode 100644 index 0000000..3f6f9fb --- /dev/null +++ b/src/main/java/io/opentracing/contrib/jdbc/DynamicProxy.java @@ -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 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); + } + }); + } +} \ No newline at end of file diff --git a/src/main/java/io/opentracing/contrib/jdbc/TracingConnection.java b/src/main/java/io/opentracing/contrib/jdbc/TracingConnection.java index 0d131c1..9f79eb3 100644 --- a/src/main/java/io/opentracing/contrib/jdbc/TracingConnection.java +++ b/src/main/java/io/opentracing/contrib/jdbc/TracingConnection.java @@ -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; @@ -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 @@ -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 @@ -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 diff --git a/src/main/java/io/opentracing/contrib/jdbc/TracingDriver.java b/src/main/java/io/opentracing/contrib/jdbc/TracingDriver.java index 173042e..15b6215 100644 --- a/src/main/java/io/opentracing/contrib/jdbc/TracingDriver.java +++ b/src/main/java/io/opentracing/contrib/jdbc/TracingDriver.java @@ -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