Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE#12022] Fix nacos datasource plugin ClassCastException bug (#12… #12031

Merged
merged 6 commits into from
May 15, 2024
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;
}

}