From 9a6ec0ca3d993c65ee64a945d34a92b784a7120b Mon Sep 17 00:00:00 2001 From: mikolls <41845795+mikolls@users.noreply.github.com> Date: Wed, 15 May 2024 10:56:54 +0800 Subject: [PATCH] =?UTF-8?q?[ISSUE#12022]=20Fix=20nacos=20datasource=20plug?= =?UTF-8?q?in=20ClassCastException=20bug=20(#12=E2=80=A6=20(#12031)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [ISSUE#12022] Fix nacos datasource plugin ClassCastException bug (#12022) * [ISSUE#12022] Fix nacos datasource plugin ClassCastException bug (#12022) * [ISSUE#12022] Fix nacos datasource plugin ClassCastException bug (#12022) * [ISSUE#12022] Fix nacos datasource plugin ClassCastException bug (#12022) * [ISSUE#12022] Fix MapperManagerTest test (#12022) * [ISSUE#12022] Fix MapperManagerTest test (#12022) --- .../plugin/datasource/proxy/MapperProxy.java | 19 ++++++++- .../plugin/datasource/MapperManagerTest.java | 11 ++++++ .../plugin/datasource/impl/TestInterface.java | 24 ++++++++++++ .../plugin/datasource/mapper/TestMapper.java | 39 +++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/TestInterface.java create mode 100644 plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/TestMapper.java diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/proxy/MapperProxy.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/proxy/MapperProxy.java index 2a5a4fbede2..b8d2a77adaf 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/proxy/MapperProxy.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/proxy/MapperProxy.java @@ -25,8 +25,12 @@ import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; +import java.util.Arrays; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; /** * DataSource plugin Mapper sql proxy. @@ -40,10 +44,21 @@ public class MapperProxy implements InvocationHandler { private Mapper mapper; private static final Map SINGLE_MAPPER_PROXY_MAP = new ConcurrentHashMap<>(16); - + + /** + * Creates a proxy instance for the sub-interfaces of Mapper.class implemented by the given object. + */ public R createProxy(Mapper mapper) { this.mapper = mapper; - return (R) Proxy.newProxyInstance(MapperProxy.class.getClassLoader(), mapper.getClass().getInterfaces(), this); + Class clazz = mapper.getClass(); + Set> interfacesSet = new HashSet<>(); + while (!clazz.equals(Object.class)) { + interfacesSet.addAll(Arrays.stream(clazz.getInterfaces()) + .filter(Mapper.class::isAssignableFrom) + .collect(Collectors.toSet())); + clazz = clazz.getSuperclass(); + } + return (R) Proxy.newProxyInstance(MapperProxy.class.getClassLoader(), interfacesSet.toArray(new Class[interfacesSet.size()]), this); } /** diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/MapperManagerTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/MapperManagerTest.java index 8bca4765686..7130947cb64 100644 --- a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/MapperManagerTest.java +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/MapperManagerTest.java @@ -18,7 +18,9 @@ import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; +import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoAggrMapper; import com.alibaba.nacos.plugin.datasource.mapper.Mapper; +import com.alibaba.nacos.plugin.datasource.mapper.TestMapper; import org.junit.Assert; import org.junit.Test; @@ -69,4 +71,13 @@ public void testFindMapper() { Mapper mapper = instance.findMapper(DataSourceConstant.MYSQL, "test"); Assert.assertNotNull(mapper); } + + @Test + public void testEnableDataSourceLogJoin() { + MapperManager.join(new TestMapper()); + MapperManager instance = MapperManager.instance(true); + ConfigInfoAggrMapper mapper = instance.findMapper(DataSourceConstant.MYSQL, "enable_data_source_log_test"); + Assert.assertNotNull(mapper); + } + } \ No newline at end of file diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/TestInterface.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/TestInterface.java new file mode 100644 index 00000000000..91587723aee --- /dev/null +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/TestInterface.java @@ -0,0 +1,24 @@ +/* + * Copyright 1999-2022 Alibaba Group Holding Ltd. + * + * 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.alibaba.nacos.plugin.datasource.impl; + +/** + * A custom interface. just for test + * @author mikolls + **/ +public interface TestInterface { +} \ No newline at end of file diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/TestMapper.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/TestMapper.java new file mode 100644 index 00000000000..d8f7a4c902e --- /dev/null +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/TestMapper.java @@ -0,0 +1,39 @@ +/* + * Copyright 1999-2022 Alibaba Group Holding Ltd. + * + * 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.alibaba.nacos.plugin.datasource.mapper; + +import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; +import com.alibaba.nacos.plugin.datasource.impl.TestInterface; +import com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigInfoAggrMapperByMySql; + +/** + * Implement interfaces other than Mapper. just for test + * @author mikolls + **/ +public class TestMapper extends ConfigInfoAggrMapperByMySql implements TestInterface { + + @Override + public String getTableName() { + return "enable_data_source_log_test"; + } + + @Override + public String getDataSource() { + return DataSourceConstant.MYSQL; + } + +}