Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2023 OceanBase.
*
* 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 com.oceanbase.odc.common.util;

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

/**
* @author liuyizhuo.lyz
* @date 2024/1/30
*/
public class ReflectionUtils {

public static <T> T getProxiedFieldValue(Object any, Class<?> type, String fieldName) {
if (!Proxy.isProxyClass(any.getClass()) || Proxy.getInvocationHandler(any).getClass() != type) {
return null;
}
InvocationHandler handler = Proxy.getInvocationHandler(any);
return getFieldValue(handler, fieldName);
}

public static <T> T getFieldValue(Object any, String fieldName) {
try {
Field field = any.getClass().getDeclaredField(fieldName);
if (!field.isAccessible()) {
field.setAccessible(true);
}
return (T) field.get(any);
} catch (NoSuchFieldException | IllegalAccessException | ClassCastException e) {
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2023 OceanBase.
*
* 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 com.oceanbase.odc.common.util;

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

import org.junit.Assert;
import org.junit.Test;

/**
* @author liuyizhuo.lyz
* @date 2024/1/30
*/
public class ReflectionUtilsTest {

@Test
public void test_getFieldValue() throws NoSuchFieldException, IllegalAccessException {
Pojo pojo = new Pojo("fuzzyname");
Object name = ReflectionUtils.getFieldValue(pojo, "name");
Assert.assertEquals("fuzzyname", name);
}

@Test
public void test_getProxiedFieldValue() throws NoSuchFieldException, IllegalAccessException {
Object instance = Proxy.newProxyInstance(Pojo.class.getClassLoader(), new Class[] {FooInterface.class},
new PojoInvocationHandler(new FooInterface() {}));
Object target = ReflectionUtils.getProxiedFieldValue(instance, PojoInvocationHandler.class, "target");
Assert.assertTrue(target instanceof FooInterface);
}

private static class Pojo {
private final String name;

public Pojo(String name) {
this.name = name;
}
}

private interface FooInterface {
}

private static class PojoInvocationHandler implements InvocationHandler {

private FooInterface target;

public PojoInvocationHandler(FooInterface target) {
this.target = target;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private Connection innerCreateConnection() throws SQLException {
* @since ODC_release_3.2.2
* @see java.lang.reflect.InvocationHandler
*/
private static class CloseIgnoreInvocationHandler implements InvocationHandler {
public static class CloseIgnoreInvocationHandler implements InvocationHandler {
private final Connection target;
private final Lock lock;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import com.oceanbase.jdbc.OceanBaseConnection;
import com.oceanbase.odc.common.util.JdbcOperationsUtil;
import com.oceanbase.odc.common.util.ReflectionUtils;
import com.oceanbase.odc.core.datasource.SingleConnectionDataSource.CloseIgnoreInvocationHandler;
import com.oceanbase.odc.plugin.connect.api.SessionExtensionPoint;

import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -65,6 +67,10 @@ public String getConnectionId(Connection connection) {
} catch (Exception e) {
if (connection instanceof OceanBaseConnection) {
connectionId = ((OceanBaseConnection) connection).getServerThreadId() + "";
} else {
OceanBaseConnection actual =
ReflectionUtils.getProxiedFieldValue(connection, CloseIgnoreInvocationHandler.class, "target");
connectionId = actual == null ? "" : actual.getServerThreadId() + "";
}
}
return connectionId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

import com.oceanbase.jdbc.OceanBaseConnection;
import com.oceanbase.odc.common.util.JdbcOperationsUtil;
import com.oceanbase.odc.common.util.ReflectionUtils;
import com.oceanbase.odc.common.util.StringUtils;
import com.oceanbase.odc.core.datasource.SingleConnectionDataSource.CloseIgnoreInvocationHandler;
import com.oceanbase.odc.plugin.connect.api.SessionExtensionPoint;

import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -67,6 +69,10 @@ public String getConnectionId(Connection connection) {
} catch (Exception e) {
if (connection instanceof OceanBaseConnection) {
connectionId = ((OceanBaseConnection) connection).getServerThreadId() + "";
} else {
OceanBaseConnection actual =
ReflectionUtils.getProxiedFieldValue(connection, CloseIgnoreInvocationHandler.class, "target");
connectionId = actual == null ? "" : actual.getServerThreadId() + "";
}
}
return connectionId;
Expand Down