You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/11/24 14:56:39 UTC
[shardingsphere] branch master updated: convert object to string
before set properties to SPI instance (#8317)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new a1e860f convert object to string before set properties to SPI instance (#8317)
a1e860f is described below
commit a1e860f420ff1e42d02af532af518c277a572c0e
Author: kimmking <ki...@163.com>
AuthorDate: Tue Nov 24 22:56:23 2020 +0800
convert object to string before set properties to SPI instance (#8317)
* fix 8204
* add null props check
* extract new method for convert type
* extract new method for convert type
* rename parameter
---
.../shardingsphere/infra/spi/typed/TypedSPIRegistry.java | 10 +++++++++-
.../infra/spi/typed/TypedSPIRegistryTest.java | 14 ++++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/typed/TypedSPIRegistry.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/typed/TypedSPIRegistry.java
index f01782c..af3afa8 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/typed/TypedSPIRegistry.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/typed/TypedSPIRegistry.java
@@ -44,7 +44,7 @@ public final class TypedSPIRegistry {
Optional<T> serviceInstance = ShardingSphereServiceLoader.newServiceInstances(typedSPIClass).stream().filter(each -> each.getType().equalsIgnoreCase(type)).findFirst();
if (serviceInstance.isPresent()) {
T result = serviceInstance.get();
- result.setProps(props);
+ convertPropertiesValueType(props, result);
return result;
}
throw new ServiceProviderNotFoundException(typedSPIClass, type);
@@ -64,4 +64,12 @@ public final class TypedSPIRegistry {
}
throw new ServiceProviderNotFoundException(typedSPIClass);
}
+
+ private static <T extends TypedSPI> void convertPropertiesValueType(final Properties props, final T service) {
+ if (null != props) {
+ Properties newProps = new Properties();
+ props.forEach((key, value) -> newProps.setProperty(key.toString(), null == value ? null : value.toString()));
+ service.setProps(newProps);
+ }
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/typed/TypedSPIRegistryTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/typed/TypedSPIRegistryTest.java
index 5108f67..04e64ca 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/typed/TypedSPIRegistryTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/typed/TypedSPIRegistryTest.java
@@ -25,7 +25,9 @@ import org.apache.shardingsphere.infra.spi.fixture.TypedSPIFixture;
import org.junit.Before;
import org.junit.Test;
+import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
public final class TypedSPIRegistryTest {
@@ -42,6 +44,18 @@ public final class TypedSPIRegistryTest {
}
@Test
+ public void assertPropertiesGetRegisteredService() {
+ Properties properties = new Properties();
+ properties.put("key1", 1);
+ properties.put("key2", 2L);
+ String type = "FIXTURE";
+ TypedSPIFixture actual = TypedSPIRegistry.getRegisteredService(TypedSPIFixture.class, type, properties);
+ assertNotNull(actual);
+ assertThat(actual.getProps().getProperty("key1"), is("1"));
+ assertThat(actual.getProps().getProperty("key2"), is("2"));
+ }
+
+ @Test
public void assertGetRegisteredServiceBySPIClass() {
TypedSPIFixture actual = TypedSPIRegistry.getRegisteredService(TypedSPIFixture.class);
assertNotNull(actual);