Skip to content

Commit

Permalink
Merge pull request #59 from reactivegroup/refactor/optimize
Browse files Browse the repository at this point in the history
refactor: optimize code
  • Loading branch information
kevinten10 authored Dec 1, 2021
2 parents f8c07ec + c95c680 commit 8a25788
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 group.rxcloud.capa.component;

import group.rxcloud.capa.infrastructure.CapaProperties;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
* Capa configuration component common properties.
*/
public interface CapaConfigurationProperties {

abstract class Settings {

private static List<String> storeNames = Collections.singletonList("");

private static final String CONFIGURATION_COMPONENT_STORE_NAMES = "CONFIGURATION_COMPONENT_STORE_NAMES";

static {
Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("configuration-common");

String storeNames = properties.getProperty(CONFIGURATION_COMPONENT_STORE_NAMES, "");
if (storeNames != null) {
String[] split = storeNames.split(",");
Settings.storeNames = Arrays.asList((split));
}
}

public static List<String> getStoreNames() {
return storeNames;
}

private Settings() {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public CapaConfigStore build() {
* @return Instance of {@link CapaConfigStore} implementor
*/
private CapaConfigStore buildCapaConfigStore() {
// TODO: 2021/11/30 build multi component
// TODO: 2021/11/30 build multi component by storeName
// load spi capa config store impl
return CapaClassLoader.loadComponentClassObj(
"configuration",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,13 @@ public CapaHttp(OkHttpClient httpClient, CapaObjectSerializer objectSerializer)
* @param type The Type needed as return for the call.
* @return Asynchronous response
*/
public <T> Mono<HttpResponse<T>> invokeApi(
String httpMethod,
String[] pathSegments,
Map<String, List<String>> urlParameters,
Object requestData,
Map<String, String> headers,
Context context,
TypeRef<T> type) {
public <T> Mono<HttpResponse<T>> invokeApi(String httpMethod,
String[] pathSegments,
Map<String, List<String>> urlParameters,
Object requestData,
Map<String, String> headers,
Context context,
TypeRef<T> type) {
// fromCallable() is needed so the invocation does not happen early, causing a hot mono.
return Mono.fromCallable(() -> doInvokeApi(httpMethod, pathSegments, urlParameters, requestData, headers, context, type))
.flatMap(f -> Mono.fromFuture(f));
Expand All @@ -119,14 +118,13 @@ public <T> Mono<HttpResponse<T>> invokeApi(
* @param type The Type needed as return for the call.
* @return CompletableFuture for Response.
*/
protected abstract <T> CompletableFuture<HttpResponse<T>> doInvokeApi(
String httpMethod,
String[] pathSegments,
Map<String, List<String>> urlParameters,
Object requestData,
Map<String, String> headers,
Context context,
TypeRef<T> type);
protected abstract <T> CompletableFuture<HttpResponse<T>> doInvokeApi(String httpMethod,
String[] pathSegments,
Map<String, List<String>> urlParameters,
Object requestData,
Map<String, String> headers,
Context context,
TypeRef<T> type);

/**
* Shutdown call is not necessary for OkHttpClient.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# configuration component store names
CONFIGURATION_COMPONENT_STORE_NAMES=apollo,aws.appconfig
7 changes: 7 additions & 0 deletions sdk-infrastructure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<description>SDK infrastructure for Capa</description>

<properties>
<vavr.version>0.10.4</vavr.version>
<vrml.version>1.0.1-RELEASE</vrml.version>
<grpc.version>1.39.0</grpc.version>
<jackson.version>2.12.4</jackson.version>
Expand Down Expand Up @@ -71,6 +72,12 @@
<artifactId>vrml-error</artifactId>
<version>${vrml.version}</version>
</dependency>
<!-- vavr -->
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>${vavr.version}</version>
</dependency>

<!-- grpc -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vavr.Function2;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -36,7 +37,9 @@
import static group.rxcloud.capa.infrastructure.CapaConstants.Properties.CAPA_COMPONENT_PROPERTIES_PREFIX;
import static group.rxcloud.capa.infrastructure.CapaConstants.Properties.CAPA_INFRASTRUCTURE_PROPERTIES_PREFIX;
import static group.rxcloud.capa.infrastructure.CapaConstants.Properties.CAPA_PROPERTIES_SUFFIX;
import static group.rxcloud.capa.infrastructure.Module.OBJECT_MAPPER;
import static group.rxcloud.capa.infrastructure.InnerModule.FILE_CACHE_MAP;
import static group.rxcloud.capa.infrastructure.InnerModule.loadCapaConfig;
import static group.rxcloud.capa.infrastructure.InnerModule.loadCapaProperties;

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

/**
* Capa's properties cache map.
*/
private static final Map<String, Object> PROPERTIES_MAP = new ConcurrentHashMap<>();

/**
* Capa's infrastructure properties.
* Capa's infrastructure properties supplier.
*/
public static final Function<String, Properties> INFRASTRUCTURE_PROPERTIES_SUPPLIER
= (infrastructureDomain) -> (Properties) PROPERTIES_MAP.computeIfAbsent(infrastructureDomain,
= (infrastructureDomain) -> (Properties) FILE_CACHE_MAP.computeIfAbsent(infrastructureDomain,
s -> {
final String fileName = CAPA_INFRASTRUCTURE_PROPERTIES_PREFIX
+ infrastructureDomain.toLowerCase()
Expand All @@ -82,18 +80,37 @@ public abstract class CapaProperties {
});

/**
* Capa's component properties.
* Capa's component properties supplier.
*/
public static final Function<String, Properties> COMPONENT_PROPERTIES_SUPPLIER
= (componentDomain) -> (Properties) PROPERTIES_MAP.computeIfAbsent(componentDomain,
= (componentDomain) -> (Properties) FILE_CACHE_MAP.computeIfAbsent(componentDomain,
s -> {
final String fileName = CAPA_COMPONENT_PROPERTIES_PREFIX
+ componentDomain.toLowerCase()
+ CAPA_PROPERTIES_SUFFIX;
return loadCapaProperties(fileName);
});

private static Properties loadCapaProperties(final String fileName) {
/**
* Capa's config file supplier.
*/
public static final Function2<String, Class, Object> CONFIG_FILE_SUPPLIER
= (fileName, clazz) -> FILE_CACHE_MAP.computeIfAbsent(fileName,
s -> loadCapaConfig(s, clazz));
}

interface InnerModule {

/**
* Capa's file cache map.
*/
Map<String, Object> FILE_CACHE_MAP = new ConcurrentHashMap<>();

ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);

static Properties loadCapaProperties(final String fileName) {
Objects.requireNonNull(fileName, "fileName not found.");
try (InputStream in = CapaProperties.class.getResourceAsStream(fileName)) {
InputStreamReader inputStreamReader = new InputStreamReader(in, StandardCharsets.UTF_8);
Expand All @@ -105,7 +122,7 @@ private static Properties loadCapaProperties(final String fileName) {
}
}

public static <T> T loadCapaConfig(final String fileName, Class<T> configClazz) {
static <T> T loadCapaConfig(final String fileName, Class<T> configClazz) {
Objects.requireNonNull(fileName, "fileName not found.");
try (InputStream in = configClazz.getResourceAsStream(fileName)) {
InputStreamReader inputStreamReader = new InputStreamReader(in, StandardCharsets.UTF_8);
Expand All @@ -117,10 +134,3 @@ public static <T> T loadCapaConfig(final String fileName, Class<T> configClazz)
}
}
}

interface Module {

ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public class ObjectSerializer {
protected ObjectSerializer() {
}

/**
* Hook method for custom object mapper.
*/
protected ObjectMapper getObjectMapper() {
return OBJECT_MAPPER;
}

/**
* Serializes a given state object into byte array.
*
Expand Down Expand Up @@ -74,7 +81,7 @@ public byte[] serialize(Object state) throws IOException {
}

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

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

/**
Expand All @@ -100,7 +107,7 @@ public <T> T deserialize(byte[] content, TypeRef<T> type) throws IOException {
* @throws IOException In case content cannot be deserialized.
*/
public <T> T deserialize(byte[] content, Class<T> clazz) throws IOException {
return deserialize(content, OBJECT_MAPPER.constructType(clazz));
return deserialize(content, getObjectMapper().constructType(clazz));
}

private <T> T deserialize(byte[] content, JavaType javaType) throws IOException {
Expand Down Expand Up @@ -138,7 +145,7 @@ private <T> T deserialize(byte[] content, JavaType javaType) throws IOException
}
}

return OBJECT_MAPPER.readValue(content, javaType);
return getObjectMapper().readValue(content, javaType);
}

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

/**
Expand All @@ -161,7 +168,7 @@ public JsonNode parseNode(byte[] content) throws IOException {
* @return Result as corresponding type.
* @throws IOException if cannot deserialize primitive time.
*/
private static <T> T deserializePrimitives(byte[] content, JavaType javaType) throws IOException {
private <T> T deserializePrimitives(byte[] content, JavaType javaType) throws IOException {
if ((content == null) || (content.length == 0)) {
if (javaType.hasRawClass(boolean.class)) {
return (T) Boolean.FALSE;
Expand Down Expand Up @@ -198,6 +205,6 @@ private static <T> T deserializePrimitives(byte[] content, JavaType javaType) th
return null;
}

return OBJECT_MAPPER.readValue(content, javaType);
return getObjectMapper().readValue(content, javaType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

/**
* Load class and create instance from config file.
* TODO move to {@link CapaProperties}
*/
public final class SpiUtils {

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# optional
# optional
group.rxcloud.capa.infrastructure.hook.Mixer$MixerProvider=your-spi-class-path
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ public DemoCapaHttp(OkHttpClient httpClient, CapaObjectSerializer objectSerializ
}

@Override
protected <T> CompletableFuture<HttpResponse<T>> invokeSpiApi(
String appId,
String method,
Object requestData,
String httpMethod,
Map<String, String> headers,
Map<String, List<String>> urlParameters,
TypeRef<T> type,
RpcServiceOptions rpcServiceOptions) {
protected <T> CompletableFuture<HttpResponse<T>> invokeSpiApi(String appId,
String method,
Object requestData,
String httpMethod,
Map<String, String> headers,
Map<String, List<String>> urlParameters,
TypeRef<T> type,
RpcServiceOptions rpcServiceOptions) {
DemoRpcServiceOptions demoRpcServiceOptions = (DemoRpcServiceOptions) rpcServiceOptions;
logger.info("[DemoCapaHttp.invokeSpiApi] rpcServiceOptions[{}]", demoRpcServiceOptions);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# log component
group.rxcloud.capa.component.log.agent.CapaLog4jAppenderAgent$CapaLog4jAppender=group.rxcloud.capa.spi.demo.log.DemoLog4jAppender
group.rxcloud.capa.component.log.agent.CapaLogbackAppenderAgent$CapaLogbackAppender=group.rxcloud.capa.spi.demo.log.DemoLogbackAppender
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,12 @@ protected RpcServiceOptions getRpcServiceOptions(String appId) {
* @param rpcServiceOptions the rpc service options
* @return the async completable future
*/
protected abstract <T> CompletableFuture<HttpResponse<T>> invokeSpiApi(
String appId,
String method,
Object requestData,
String httpMethod,
Map<String, String> headers,
Map<String, List<String>> urlParameters,
TypeRef<T> type,
RpcServiceOptions rpcServiceOptions);
protected abstract <T> CompletableFuture<HttpResponse<T>> invokeSpiApi(String appId,
String method,
Object requestData,
String httpMethod,
Map<String, String> headers,
Map<String, List<String>> urlParameters,
TypeRef<T> type,
RpcServiceOptions rpcServiceOptions);
}
Loading

0 comments on commit 8a25788

Please sign in to comment.