Skip to content

Commit 8a25788

Browse files
authored
Merge pull request #59 from reactivegroup/refactor/optimize
refactor: optimize code
2 parents f8c07ec + c95c680 commit 8a25788

File tree

14 files changed

+178
-79
lines changed

14 files changed

+178
-79
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package group.rxcloud.capa.component;
18+
19+
import group.rxcloud.capa.infrastructure.CapaProperties;
20+
21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
import java.util.List;
24+
import java.util.Properties;
25+
26+
/**
27+
* Capa configuration component common properties.
28+
*/
29+
public interface CapaConfigurationProperties {
30+
31+
abstract class Settings {
32+
33+
private static List<String> storeNames = Collections.singletonList("");
34+
35+
private static final String CONFIGURATION_COMPONENT_STORE_NAMES = "CONFIGURATION_COMPONENT_STORE_NAMES";
36+
37+
static {
38+
Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("configuration-common");
39+
40+
String storeNames = properties.getProperty(CONFIGURATION_COMPONENT_STORE_NAMES, "");
41+
if (storeNames != null) {
42+
String[] split = storeNames.split(",");
43+
Settings.storeNames = Arrays.asList((split));
44+
}
45+
}
46+
47+
public static List<String> getStoreNames() {
48+
return storeNames;
49+
}
50+
51+
private Settings() {
52+
}
53+
}
54+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public CapaConfigStore build() {
8181
* @return Instance of {@link CapaConfigStore} implementor
8282
*/
8383
private CapaConfigStore buildCapaConfigStore() {
84-
// TODO: 2021/11/30 build multi component
84+
// TODO: 2021/11/30 build multi component by storeName
8585
// load spi capa config store impl
8686
return CapaClassLoader.loadComponentClassObj(
8787
"configuration",

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,13 @@ public CapaHttp(OkHttpClient httpClient, CapaObjectSerializer objectSerializer)
9393
* @param type The Type needed as return for the call.
9494
* @return Asynchronous response
9595
*/
96-
public <T> Mono<HttpResponse<T>> invokeApi(
97-
String httpMethod,
98-
String[] pathSegments,
99-
Map<String, List<String>> urlParameters,
100-
Object requestData,
101-
Map<String, String> headers,
102-
Context context,
103-
TypeRef<T> type) {
96+
public <T> Mono<HttpResponse<T>> invokeApi(String httpMethod,
97+
String[] pathSegments,
98+
Map<String, List<String>> urlParameters,
99+
Object requestData,
100+
Map<String, String> headers,
101+
Context context,
102+
TypeRef<T> type) {
104103
// fromCallable() is needed so the invocation does not happen early, causing a hot mono.
105104
return Mono.fromCallable(() -> doInvokeApi(httpMethod, pathSegments, urlParameters, requestData, headers, context, type))
106105
.flatMap(f -> Mono.fromFuture(f));
@@ -119,14 +118,13 @@ public <T> Mono<HttpResponse<T>> invokeApi(
119118
* @param type The Type needed as return for the call.
120119
* @return CompletableFuture for Response.
121120
*/
122-
protected abstract <T> CompletableFuture<HttpResponse<T>> doInvokeApi(
123-
String httpMethod,
124-
String[] pathSegments,
125-
Map<String, List<String>> urlParameters,
126-
Object requestData,
127-
Map<String, String> headers,
128-
Context context,
129-
TypeRef<T> type);
121+
protected abstract <T> CompletableFuture<HttpResponse<T>> doInvokeApi(String httpMethod,
122+
String[] pathSegments,
123+
Map<String, List<String>> urlParameters,
124+
Object requestData,
125+
Map<String, String> headers,
126+
Context context,
127+
TypeRef<T> type);
130128

131129
/**
132130
* Shutdown call is not necessary for OkHttpClient.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# configuration component store names
2+
CONFIGURATION_COMPONENT_STORE_NAMES=apollo,aws.appconfig

sdk-infrastructure/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<description>SDK infrastructure for Capa</description>
3232

3333
<properties>
34+
<vavr.version>0.10.4</vavr.version>
3435
<vrml.version>1.0.1-RELEASE</vrml.version>
3536
<grpc.version>1.39.0</grpc.version>
3637
<jackson.version>2.12.4</jackson.version>
@@ -71,6 +72,12 @@
7172
<artifactId>vrml-error</artifactId>
7273
<version>${vrml.version}</version>
7374
</dependency>
75+
<!-- vavr -->
76+
<dependency>
77+
<groupId>io.vavr</groupId>
78+
<artifactId>vavr</artifactId>
79+
<version>${vavr.version}</version>
80+
</dependency>
7481

7582
<!-- grpc -->
7683
<dependency>

sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/CapaProperties.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.fasterxml.jackson.databind.DeserializationFeature;
2222
import com.fasterxml.jackson.databind.JsonMappingException;
2323
import com.fasterxml.jackson.databind.ObjectMapper;
24+
import io.vavr.Function2;
2425

2526
import java.io.IOException;
2627
import java.io.InputStream;
@@ -36,7 +37,9 @@
3637
import static group.rxcloud.capa.infrastructure.CapaConstants.Properties.CAPA_COMPONENT_PROPERTIES_PREFIX;
3738
import static group.rxcloud.capa.infrastructure.CapaConstants.Properties.CAPA_INFRASTRUCTURE_PROPERTIES_PREFIX;
3839
import static group.rxcloud.capa.infrastructure.CapaConstants.Properties.CAPA_PROPERTIES_SUFFIX;
39-
import static group.rxcloud.capa.infrastructure.Module.OBJECT_MAPPER;
40+
import static group.rxcloud.capa.infrastructure.InnerModule.FILE_CACHE_MAP;
41+
import static group.rxcloud.capa.infrastructure.InnerModule.loadCapaConfig;
42+
import static group.rxcloud.capa.infrastructure.InnerModule.loadCapaProperties;
4043

4144
/**
4245
* Global properties for Capa's SDK, using Supplier so they are dynamically resolved.
@@ -65,15 +68,10 @@ public abstract class CapaProperties {
6568
= () -> DEFAULT_HTTP_CLIENT_READTIMEOUTSECONDS;
6669

6770
/**
68-
* Capa's properties cache map.
69-
*/
70-
private static final Map<String, Object> PROPERTIES_MAP = new ConcurrentHashMap<>();
71-
72-
/**
73-
* Capa's infrastructure properties.
71+
* Capa's infrastructure properties supplier.
7472
*/
7573
public static final Function<String, Properties> INFRASTRUCTURE_PROPERTIES_SUPPLIER
76-
= (infrastructureDomain) -> (Properties) PROPERTIES_MAP.computeIfAbsent(infrastructureDomain,
74+
= (infrastructureDomain) -> (Properties) FILE_CACHE_MAP.computeIfAbsent(infrastructureDomain,
7775
s -> {
7876
final String fileName = CAPA_INFRASTRUCTURE_PROPERTIES_PREFIX
7977
+ infrastructureDomain.toLowerCase()
@@ -82,18 +80,37 @@ public abstract class CapaProperties {
8280
});
8381

8482
/**
85-
* Capa's component properties.
83+
* Capa's component properties supplier.
8684
*/
8785
public static final Function<String, Properties> COMPONENT_PROPERTIES_SUPPLIER
88-
= (componentDomain) -> (Properties) PROPERTIES_MAP.computeIfAbsent(componentDomain,
86+
= (componentDomain) -> (Properties) FILE_CACHE_MAP.computeIfAbsent(componentDomain,
8987
s -> {
9088
final String fileName = CAPA_COMPONENT_PROPERTIES_PREFIX
9189
+ componentDomain.toLowerCase()
9290
+ CAPA_PROPERTIES_SUFFIX;
9391
return loadCapaProperties(fileName);
9492
});
9593

96-
private static Properties loadCapaProperties(final String fileName) {
94+
/**
95+
* Capa's config file supplier.
96+
*/
97+
public static final Function2<String, Class, Object> CONFIG_FILE_SUPPLIER
98+
= (fileName, clazz) -> FILE_CACHE_MAP.computeIfAbsent(fileName,
99+
s -> loadCapaConfig(s, clazz));
100+
}
101+
102+
interface InnerModule {
103+
104+
/**
105+
* Capa's file cache map.
106+
*/
107+
Map<String, Object> FILE_CACHE_MAP = new ConcurrentHashMap<>();
108+
109+
ObjectMapper OBJECT_MAPPER = new ObjectMapper()
110+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
111+
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
112+
113+
static Properties loadCapaProperties(final String fileName) {
97114
Objects.requireNonNull(fileName, "fileName not found.");
98115
try (InputStream in = CapaProperties.class.getResourceAsStream(fileName)) {
99116
InputStreamReader inputStreamReader = new InputStreamReader(in, StandardCharsets.UTF_8);
@@ -105,7 +122,7 @@ private static Properties loadCapaProperties(final String fileName) {
105122
}
106123
}
107124

108-
public static <T> T loadCapaConfig(final String fileName, Class<T> configClazz) {
125+
static <T> T loadCapaConfig(final String fileName, Class<T> configClazz) {
109126
Objects.requireNonNull(fileName, "fileName not found.");
110127
try (InputStream in = configClazz.getResourceAsStream(fileName)) {
111128
InputStreamReader inputStreamReader = new InputStreamReader(in, StandardCharsets.UTF_8);
@@ -117,10 +134,3 @@ public static <T> T loadCapaConfig(final String fileName, Class<T> configClazz)
117134
}
118135
}
119136
}
120-
121-
interface Module {
122-
123-
ObjectMapper OBJECT_MAPPER = new ObjectMapper()
124-
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
125-
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
126-
}

sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/serializer/ObjectSerializer.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public class ObjectSerializer {
4747
protected ObjectSerializer() {
4848
}
4949

50+
/**
51+
* Hook method for custom object mapper.
52+
*/
53+
protected ObjectMapper getObjectMapper() {
54+
return OBJECT_MAPPER;
55+
}
56+
5057
/**
5158
* Serializes a given state object into byte array.
5259
*
@@ -74,7 +81,7 @@ public byte[] serialize(Object state) throws IOException {
7481
}
7582

7683
// Not string, not primitive, so it is a complex type: we use JSON for that.
77-
return OBJECT_MAPPER.writeValueAsBytes(state);
84+
return getObjectMapper().writeValueAsBytes(state);
7885
}
7986

8087
/**
@@ -87,7 +94,7 @@ public byte[] serialize(Object state) throws IOException {
8794
* @throws IOException In case content cannot be deserialized.
8895
*/
8996
public <T> T deserialize(byte[] content, TypeRef<T> type) throws IOException {
90-
return deserialize(content, OBJECT_MAPPER.constructType(type.getType()));
97+
return deserialize(content, getObjectMapper().constructType(type.getType()));
9198
}
9299

93100
/**
@@ -100,7 +107,7 @@ public <T> T deserialize(byte[] content, TypeRef<T> type) throws IOException {
100107
* @throws IOException In case content cannot be deserialized.
101108
*/
102109
public <T> T deserialize(byte[] content, Class<T> clazz) throws IOException {
103-
return deserialize(content, OBJECT_MAPPER.constructType(clazz));
110+
return deserialize(content, getObjectMapper().constructType(clazz));
104111
}
105112

106113
private <T> T deserialize(byte[] content, JavaType javaType) throws IOException {
@@ -138,7 +145,7 @@ private <T> T deserialize(byte[] content, JavaType javaType) throws IOException
138145
}
139146
}
140147

141-
return OBJECT_MAPPER.readValue(content, javaType);
148+
return getObjectMapper().readValue(content, javaType);
142149
}
143150

144151
/**
@@ -149,7 +156,7 @@ private <T> T deserialize(byte[] content, JavaType javaType) throws IOException
149156
* @throws IOException In case content cannot be parsed.
150157
*/
151158
public JsonNode parseNode(byte[] content) throws IOException {
152-
return OBJECT_MAPPER.readTree(content);
159+
return getObjectMapper().readTree(content);
153160
}
154161

155162
/**
@@ -161,7 +168,7 @@ public JsonNode parseNode(byte[] content) throws IOException {
161168
* @return Result as corresponding type.
162169
* @throws IOException if cannot deserialize primitive time.
163170
*/
164-
private static <T> T deserializePrimitives(byte[] content, JavaType javaType) throws IOException {
171+
private <T> T deserializePrimitives(byte[] content, JavaType javaType) throws IOException {
165172
if ((content == null) || (content.length == 0)) {
166173
if (javaType.hasRawClass(boolean.class)) {
167174
return (T) Boolean.FALSE;
@@ -198,6 +205,6 @@ private static <T> T deserializePrimitives(byte[] content, JavaType javaType) th
198205
return null;
199206
}
200207

201-
return OBJECT_MAPPER.readValue(content, javaType);
208+
return getObjectMapper().readValue(content, javaType);
202209
}
203210
}

sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/utils/SpiUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
/**
4040
* Load class and create instance from config file.
41+
* TODO move to {@link CapaProperties}
4142
*/
4243
public final class SpiUtils {
4344

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
# optional
1+
# optional
2+
group.rxcloud.capa.infrastructure.hook.Mixer$MixerProvider=your-spi-class-path

sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/http/DemoCapaHttp.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,14 @@ public DemoCapaHttp(OkHttpClient httpClient, CapaObjectSerializer objectSerializ
4848
}
4949

5050
@Override
51-
protected <T> CompletableFuture<HttpResponse<T>> invokeSpiApi(
52-
String appId,
53-
String method,
54-
Object requestData,
55-
String httpMethod,
56-
Map<String, String> headers,
57-
Map<String, List<String>> urlParameters,
58-
TypeRef<T> type,
59-
RpcServiceOptions rpcServiceOptions) {
51+
protected <T> CompletableFuture<HttpResponse<T>> invokeSpiApi(String appId,
52+
String method,
53+
Object requestData,
54+
String httpMethod,
55+
Map<String, String> headers,
56+
Map<String, List<String>> urlParameters,
57+
TypeRef<T> type,
58+
RpcServiceOptions rpcServiceOptions) {
6059
DemoRpcServiceOptions demoRpcServiceOptions = (DemoRpcServiceOptions) rpcServiceOptions;
6160
logger.info("[DemoCapaHttp.invokeSpiApi] rpcServiceOptions[{}]", demoRpcServiceOptions);
6261

0 commit comments

Comments
 (0)