Skip to content

Commit 7f8801a

Browse files
authored
Merge pull request #49 from reactivegroup/refactor/classLoader
refactor: refactor spi class loader
2 parents 8f2f232 + 8526ac0 commit 7f8801a

File tree

49 files changed

+388
-365
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+388
-365
lines changed

sdk-component/src/main/java/group/rxcloud/capa/component/configstore/CapaConfigStoreBuilder.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@
1717
package group.rxcloud.capa.component.configstore;
1818

1919

20-
import group.rxcloud.capa.infrastructure.config.CapaProperties;
20+
import group.rxcloud.capa.infrastructure.CapaClassLoader;
2121
import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
2222
import group.rxcloud.capa.infrastructure.serializer.DefaultObjectSerializer;
2323

24-
import java.lang.reflect.Constructor;
25-
import java.lang.reflect.InvocationTargetException;
26-
import java.util.Properties;
27-
2824
/**
2925
* A builder for the {@link CapaConfigStore} implementor.
3026
*/
@@ -86,15 +82,10 @@ public CapaConfigStore build() {
8682
*/
8783
private CapaConfigStore buildCapaConfigStore() {
8884
// load spi capa config store impl
89-
try {
90-
Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("configuration");
91-
String capaConfigStoreClassPath = properties.getProperty(CapaConfigStore.class.getName());
92-
Class<? extends CapaConfigStore> aClass = (Class<? extends CapaConfigStore>) Class.forName(capaConfigStoreClassPath);
93-
Constructor<? extends CapaConfigStore> constructor = aClass.getConstructor(CapaObjectSerializer.class);
94-
Object newInstance = constructor.newInstance(this.objectSerializer);
95-
return (CapaConfigStore) newInstance;
96-
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
97-
throw new IllegalArgumentException("No CapaConfigStore Client supported.");
98-
}
85+
return CapaClassLoader.loadComponentClassObj(
86+
"configuration",
87+
CapaConfigStore.class,
88+
new Class[]{CapaObjectSerializer.class},
89+
new Object[]{this.objectSerializer});
9990
}
10091
}

sdk-component/src/main/java/group/rxcloud/capa/component/http/CapaHttpBuilder.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
package group.rxcloud.capa.component.http;
1818

1919

20-
import group.rxcloud.capa.infrastructure.config.CapaProperties;
20+
import group.rxcloud.capa.infrastructure.CapaClassLoader;
21+
import group.rxcloud.capa.infrastructure.CapaProperties;
2122
import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
2223
import group.rxcloud.capa.infrastructure.serializer.DefaultObjectSerializer;
2324
import okhttp3.OkHttpClient;
2425

25-
import java.lang.reflect.Constructor;
26-
import java.lang.reflect.InvocationTargetException;
2726
import java.time.Duration;
28-
import java.util.Properties;
2927
import java.util.concurrent.atomic.AtomicReference;
3028

3129
/**
@@ -107,15 +105,10 @@ private CapaHttp buildCapaHttp() {
107105
}
108106

109107
// load spi capa http impl
110-
try {
111-
Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("rpc");
112-
String capaHttpClassPath = properties.getProperty(CapaHttp.class.getName());
113-
Class<? extends CapaHttp> aClass = (Class<? extends CapaHttp>) Class.forName(capaHttpClassPath);
114-
Constructor<? extends CapaHttp> constructor = aClass.getConstructor(OkHttpClient.class, CapaObjectSerializer.class);
115-
Object newInstance = constructor.newInstance(OK_HTTP_CLIENT.get(), this.objectSerializer);
116-
return (CapaHttp) newInstance;
117-
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
118-
throw new IllegalArgumentException("No CapaHttp Client supported.");
119-
}
108+
return CapaClassLoader.loadComponentClassObj(
109+
"rpc",
110+
CapaHttp.class,
111+
new Class[]{OkHttpClient.class, CapaObjectSerializer.class},
112+
new Object[]{OK_HTTP_CLIENT.get(), this.objectSerializer});
120113
}
121114
}

sdk-component/src/main/java/group/rxcloud/capa/component/pubsub/CapaPubSubBuilder.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616
*/
1717
package group.rxcloud.capa.component.pubsub;
1818

19-
import group.rxcloud.capa.infrastructure.config.CapaProperties;
20-
21-
import java.lang.reflect.Constructor;
22-
import java.lang.reflect.InvocationTargetException;
23-
import java.util.Properties;
19+
import group.rxcloud.capa.infrastructure.CapaClassLoader;
2420

2521
/**
2622
* A builder for the {@link CapaPubSub} implementor.
@@ -56,15 +52,6 @@ public CapaPubSub build() {
5652
* @return Instance of {@link CapaPubSub} implementor
5753
*/
5854
private CapaPubSub buildCapaPubSub() {
59-
try {
60-
Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("pubsub");
61-
String capaPubSubClassPath = properties.getProperty(CapaPubSub.class.getName());
62-
Class<? extends CapaPubSub> aClass = (Class<? extends CapaPubSub>) Class.forName(capaPubSubClassPath);
63-
Constructor<? extends CapaPubSub> constructor = aClass.getConstructor();
64-
Object newInstance = constructor.newInstance();
65-
return (CapaPubSub) newInstance;
66-
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
67-
throw new IllegalArgumentException("No CapaPubSub Client supported.");
68-
}
55+
return CapaClassLoader.loadComponentClassObj("pubsub", CapaPubSub.class);
6956
}
7057
}

sdk-component/src/main/java/group/rxcloud/capa/component/state/Store.java

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# configuration component
2+
group.rxcloud.capa.component.configstore.CapaConfigStore=your-spi-class-path
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# pubsub component
2+
group.rxcloud.capa.component.pubsub.CapaPubSub=your-spi-class-path
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# rpc component
2+
group.rxcloud.capa.component.http.CapaHttp=your-spi-class-path

sdk-component/src/test/java/group/rxcloud/capa/component/configstore/StoreConfigTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,4 @@ public void testStoreConfigSetterGetter_Success() {
4646

4747
Assertions.assertEquals("30000", storeConfig.getTimeOut());
4848
}
49-
5049
}

sdk-component/src/test/java/group/rxcloud/capa/component/configstore/SubscribeReqTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @date 2021/10/19
2828
*/
2929
public class SubscribeReqTest {
30+
3031
@Test
3132
public void testSubscribeReqSetterGetter_Success() {
3233
SubscribeReq req = new SubscribeReq();

sdk-component/src/test/java/group/rxcloud/capa/component/configstore/SubscribeRespTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* @date 2021/10/19
2626
*/
2727
public class SubscribeRespTest {
28+
2829
@Test
2930
public void testSubscribeRespSetterGetter_Success() {
3031
SubscribeResp<String> resp = new SubscribeResp<>();

0 commit comments

Comments
 (0)