Skip to content

Commit

Permalink
[ISSUE#12022] Fix nacos datasource plugin ClassCastException bug (#12… (
Browse files Browse the repository at this point in the history
#12031)

* [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)
  • Loading branch information
mikolls authored May 15, 2024
1 parent 15c9663 commit 9a6ec0c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -40,10 +44,21 @@ public class MapperProxy implements InvocationHandler {
private Mapper mapper;

private static final Map<String, Mapper> 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> R createProxy(Mapper mapper) {
this.mapper = mapper;
return (R) Proxy.newProxyInstance(MapperProxy.class.getClassLoader(), mapper.getClass().getInterfaces(), this);
Class<?> clazz = mapper.getClass();
Set<Class<?>> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 9a6ec0c

Please sign in to comment.