Skip to content

Commit ce9ad7f

Browse files
authored
Merge pull request #34 from reactivegroup/feature/spi_change
feat: diff spi properties
2 parents 3edbe01 + 5df5801 commit ce9ad7f

File tree

18 files changed

+172
-28
lines changed

18 files changed

+172
-28
lines changed

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
@@ -87,7 +87,7 @@ public CapaConfigStore build() {
8787
private CapaConfigStore buildCapaConfigStore() {
8888
// load spi capa config store impl
8989
try {
90-
Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.get();
90+
Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("configuration");
9191
String capaConfigStoreClassPath = properties.getProperty(CapaConfigStore.class.getName());
9292
Class<? extends CapaConfigStore> aClass = (Class<? extends CapaConfigStore>) Class.forName(capaConfigStoreClassPath);
9393
Constructor<? extends CapaConfigStore> constructor = aClass.getConstructor(CapaObjectSerializer.class);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private CapaHttp buildCapaHttp() {
108108

109109
// load spi capa http impl
110110
try {
111-
Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.get();
111+
Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("rpc");
112112
String capaHttpClassPath = properties.getProperty(CapaHttp.class.getName());
113113
Class<? extends CapaHttp> aClass = (Class<? extends CapaHttp>) Class.forName(capaHttpClassPath);
114114
Constructor<? extends CapaHttp> constructor = aClass.getConstructor(OkHttpClient.class, CapaObjectSerializer.class);

sdk-component/src/test/resources/capa-component.properties renamed to sdk-component/src/test/resources/capa-component-configuration.properties

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
group.rxcloud.capa.component.http.CapaHttp=group.rxcloud.capa.component.http.TestCapaHttp
2-
group.rxcloud.capa.component.configstore.CapaConfigStore=group.rxcloud.capa.component.configstore.TestCapaConfigStore
1+
group.rxcloud.capa.component.configstore.CapaConfigStore=group.rxcloud.capa.component.configstore.TestCapaConfigStore
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
group.rxcloud.capa.component.http.CapaHttp=group.rxcloud.capa.component.http.TestCapaHttp

sdk-infrastructure/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@
7777
<artifactId>jackson-databind</artifactId>
7878
<version>${jackson.version}</version>
7979
</dependency>
80+
81+
<!-- unit test -->
82+
<dependency>
83+
<groupId>org.junit.jupiter</groupId>
84+
<artifactId>junit-jupiter-engine</artifactId>
85+
<scope>test</scope>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.mockito</groupId>
89+
<artifactId>mockito-core</artifactId>
90+
<scope>test</scope>
91+
</dependency>
8092
</dependencies>
8193

8294
<build>

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
import java.io.InputStream;
2121
import java.io.InputStreamReader;
2222
import java.nio.charset.StandardCharsets;
23+
import java.util.Map;
24+
import java.util.Objects;
2325
import java.util.Properties;
26+
import java.util.concurrent.ConcurrentHashMap;
27+
import java.util.function.Function;
2428
import java.util.function.Supplier;
2529

26-
import static group.rxcloud.capa.infrastructure.constants.CapaConstants.Properties.CAPA_COMPONENT_PROPERTIES;
30+
import static group.rxcloud.capa.infrastructure.constants.CapaConstants.Properties.CAPA_COMPONENT_PROPERTIES_PREFIX;
31+
import static group.rxcloud.capa.infrastructure.constants.CapaConstants.Properties.CAPA_COMPONENT_PROPERTIES_SUFFIX;
2732

2833
/**
2934
* Global properties for Capa's SDK, using Supplier so they are dynamically resolved.
@@ -50,26 +55,35 @@ public abstract class CapaProperties {
5055
/**
5156
* Capa's timeout in seconds for HTTP client reads.
5257
*/
53-
public static final Supplier<Integer> HTTP_CLIENT_READ_TIMEOUT_SECONDS = () -> DEFAULT_HTTP_CLIENT_READTIMEOUTSECONDS;
58+
public static final Supplier<Integer> HTTP_CLIENT_READ_TIMEOUT_SECONDS
59+
= () -> DEFAULT_HTTP_CLIENT_READTIMEOUTSECONDS;
5460

5561
/**
56-
* Capa's component properties.
62+
* Capa's component properties cache map.
5763
*/
58-
private static final Properties COMPONENT_PROPERTIES = loadCapaComponentProperties();
64+
private static final Map<String, Properties> COMPONENT_PROPERTIES_MAP = new ConcurrentHashMap<>();
5965

6066
/**
6167
* Capa's component properties.
6268
*/
63-
public static final Supplier<Properties> COMPONENT_PROPERTIES_SUPPLIER = () -> COMPONENT_PROPERTIES;
69+
public static final Function<String, Properties> COMPONENT_PROPERTIES_SUPPLIER
70+
= (componentDomain) -> COMPONENT_PROPERTIES_MAP.computeIfAbsent(componentDomain,
71+
s -> loadCapaComponentProperties(componentDomain));
72+
73+
private static Properties loadCapaComponentProperties(final String componentDomain) {
74+
Objects.requireNonNull(componentDomain, "componentDomain not found.");
75+
76+
final String fileName = CAPA_COMPONENT_PROPERTIES_PREFIX
77+
+ componentDomain.toLowerCase()
78+
+ CAPA_COMPONENT_PROPERTIES_SUFFIX;
6479

65-
private static Properties loadCapaComponentProperties() {
66-
try (InputStream in = CapaProperties.class.getResourceAsStream(CAPA_COMPONENT_PROPERTIES)) {
80+
try (InputStream in = CapaProperties.class.getResourceAsStream(fileName)) {
6781
InputStreamReader inputStreamReader = new InputStreamReader(in, StandardCharsets.UTF_8);
6882
Properties properties = new Properties();
6983
properties.load(inputStreamReader);
7084
return properties;
7185
} catch (IOException e) {
72-
throw new IllegalArgumentException(CAPA_COMPONENT_PROPERTIES + " file not found.");
86+
throw new IllegalArgumentException(fileName + " file not found.");
7387
}
7488
}
7589
}

sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/constants/CapaConstants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface Environments {
2727

2828
interface Properties {
2929

30-
String CAPA_COMPONENT_PROPERTIES = "/capa-component.properties";
30+
String CAPA_COMPONENT_PROPERTIES_PREFIX = "/capa-component-";
31+
String CAPA_COMPONENT_PROPERTIES_SUFFIX = ".properties";
3132
}
3233
}

sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/env/CapaEnvironment.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public abstract class CapaEnvironment {
3838

3939
static {
4040
// setup cloud env
41+
initCloudEnv();
42+
43+
// setup vpc env
44+
initVpcEnv();
45+
}
46+
47+
private static void initCloudEnv() {
4148
final String cloudRuntimesEnvDeployCloud = System.getProperty(CLOUD_RUNTIMES_ENV_DEPLOY_CLOUD);
4249
if ("CTRIP".equalsIgnoreCase(cloudRuntimesEnvDeployCloud)) {
4350
deployCloudEnvironment = DeployCloudEnvironment.CTRIP_IDC;
@@ -48,8 +55,9 @@ public abstract class CapaEnvironment {
4855
if (deployCloudEnvironment == null) {
4956
deployCloudEnvironment = DeployCloudEnvironment.CTRIP_IDC;
5057
}
58+
}
5159

52-
// setup vpc env
60+
private static void initVpcEnv() {
5361
final String cloudRuntimesEnvDeployVpc = System.getProperty(CLOUD_RUNTIMES_ENV_DEPLOY_VPC);
5462
if ("FWS".equalsIgnoreCase(cloudRuntimesEnvDeployVpc)
5563
|| "FAT".equalsIgnoreCase(cloudRuntimesEnvDeployVpc)) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.infrastructure.config;
18+
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.Properties;
23+
24+
public class CapaPropertiesTest {
25+
26+
27+
@Test
28+
public void testGetApiProtocol_Success() {
29+
String apiProtocol = CapaProperties.API_PROTOCOL.get();
30+
Assertions.assertEquals("HTTP", apiProtocol);
31+
}
32+
33+
@Test
34+
public void testGetHttpClientReadTimeoutSeconds_Success() {
35+
Integer httpClientReadTimeoutSeconds = CapaProperties.HTTP_CLIENT_READ_TIMEOUT_SECONDS.get();
36+
Assertions.assertEquals(60, httpClientReadTimeoutSeconds.intValue());
37+
}
38+
39+
@Test
40+
public void testGetComponentProperties_Success() {
41+
Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("rpc");
42+
String value = properties.getProperty("key");
43+
Assertions.assertEquals("value", value);
44+
}
45+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.infrastructure.serializer;
18+
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.io.IOException;
24+
25+
public class DefaultObjectSerializerTest {
26+
27+
private DefaultObjectSerializer defaultObjectSerializer;
28+
29+
@BeforeEach
30+
public void setUp() {
31+
defaultObjectSerializer = new DefaultObjectSerializer();
32+
}
33+
34+
@Test
35+
public void testGetContentType_Success() {
36+
String contentType = defaultObjectSerializer.getContentType();
37+
Assertions.assertEquals("application/json", contentType);
38+
}
39+
40+
@Test
41+
public void testSerialize_Success() throws IOException {
42+
byte[] serializeNull = defaultObjectSerializer.serialize(null);
43+
Assertions.assertNull(serializeNull);
44+
45+
byte[] serializeByte = defaultObjectSerializer.serialize(new byte[0]);
46+
Assertions.assertNotNull(serializeByte);
47+
48+
byte[] serializeString = defaultObjectSerializer.serialize("serialize");
49+
Assertions.assertNotNull(serializeString);
50+
}
51+
52+
53+
}

0 commit comments

Comments
 (0)