You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2020/04/10 06:51:00 UTC
[servicecomb-java-chassis] 01/02: [SCB-1861] fix config kie value
parsing errors
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit 9187d24eb2c5c9a288c6572a6f43e8c936477772
Author: liubao <bi...@qq.com>
AuthorDate: Thu Apr 9 15:29:25 2020 +0800
[SCB-1861] fix config kie value parsing errors
---
.../servicecomb/config/kie/client/KieClient.java | 2 +-
.../servicecomb/config/kie/client/KieUtil.java | 37 +++++++++++------
.../servicecomb/config/kie/model/ValueType.java | 22 +++--------
.../servicecomb/config/kie/client/TestKieUtil.java | 46 ++++++++++++++++++++++
4 files changed, 78 insertions(+), 29 deletions(-)
diff --git a/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/client/KieClient.java b/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/client/KieClient.java
index 7a7c9c7..adcff94 100644
--- a/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/client/KieClient.java
+++ b/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/client/KieClient.java
@@ -90,7 +90,7 @@ public class KieClient {
EXECUTOR.execute(new ConfigRefresh(serviceUri));
} else {
EXECUTOR.scheduleWithFixedDelay(new ConfigRefresh(serviceUri), firstRefreshInterval,
- refreshInterval, TimeUnit.SECONDS);
+ refreshInterval, TimeUnit.MILLISECONDS);
}
}
diff --git a/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/client/KieUtil.java b/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/client/KieUtil.java
index 2e5030d..74b2a4d 100644
--- a/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/client/KieUtil.java
+++ b/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/client/KieUtil.java
@@ -29,6 +29,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
+
+import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.config.kie.model.KVDoc;
import org.apache.servicecomb.config.kie.model.KVResponse;
import org.apache.servicecomb.config.kie.model.ValueType;
@@ -36,7 +38,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ByteArrayResource;
-import org.springframework.util.StringUtils;
public class KieUtil {
@@ -109,36 +110,46 @@ public class KieUtil {
}
public static Map<String, String> processValueType(KVDoc kvDoc) {
- ValueType vtype;
- try {
- vtype = ValueType.valueOf(kvDoc.getValueType());
- } catch (IllegalArgumentException e) {
- throw new RuntimeException("value type not support");
- }
+ ValueType valueType = parseValueType(kvDoc.getValueType());
+
Properties properties = new Properties();
Map<String, String> kvMap = new HashMap<>();
try {
- if (vtype == (ValueType.YMAL) || vtype == (ValueType.YML)) {
+ if (valueType == (ValueType.YAML) || valueType == (ValueType.YML)) {
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setResources(new ByteArrayResource(kvDoc.getValue().getBytes()));
properties = yamlFactory.getObject();
- } else if (vtype == (ValueType.PROPERTIES)) {
+ } else if (valueType == (ValueType.PROPERTIES)) {
properties.load(new StringReader(kvDoc.getValue()));
- } else if (vtype == (ValueType.TEXT) || vtype == (ValueType.STRING)) {
+ } else if (valueType == (ValueType.TEXT) || valueType == (ValueType.STRING)) {
kvMap.put(kvDoc.getKey(), kvDoc.getValue());
return kvMap;
} else {
+ // ValueType.JSON
kvMap.put(kvDoc.getKey(), kvDoc.getValue());
return kvMap;
}
kvMap = toMap(kvDoc.getKey(), properties);
return kvMap;
} catch (Exception e) {
- LOGGER.error("read config failed");
+ LOGGER.error("read config failed", e);
}
return Collections.emptyMap();
}
+ private static ValueType parseValueType(String valueType) {
+ if (StringUtils.isEmpty(valueType)) {
+ return ValueType.STRING;
+ }
+
+ try {
+ return ValueType.valueOf(valueType.toUpperCase());
+ } catch (IllegalArgumentException e) {
+ LOGGER.warn("read unrecognized value type {}", valueType);
+ return ValueType.STRING;
+ }
+ }
+
@SuppressWarnings("unchecked")
private static Map<String, String> toMap(String prefix, Properties properties) {
@@ -146,10 +157,12 @@ public class KieUtil {
Enumeration<String> keys = (Enumeration<String>) properties.propertyNames();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
+ Object value = properties.getProperty(key);
+
if (!StringUtils.isEmpty(prefix)) {
key = prefix + "." + key;
}
- Object value = properties.getProperty(key);
+
if (value != null) {
result.put(key, ((String) value).trim());
} else {
diff --git a/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/model/ValueType.java b/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/model/ValueType.java
index 0658be2..8190878 100644
--- a/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/model/ValueType.java
+++ b/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/model/ValueType.java
@@ -18,20 +18,10 @@
package org.apache.servicecomb.config.kie.model;
public enum ValueType {
- YML("yml"),
- YMAL("yaml"),
- STRING("string"),
- TEXT("text"),
- JSON("json"),
- PROPERTIES("properties");
-
- private String value;
-
- ValueType(String value) {
- this.value = value;
- }
-
- public String getValue() {
- return this.value;
- }
+ YML,
+ YAML,
+ STRING,
+ TEXT,
+ JSON,
+ PROPERTIES
}
diff --git a/dynamic-config/config-kie/src/test/java/org/apache/servicecomb/config/kie/client/TestKieUtil.java b/dynamic-config/config-kie/src/test/java/org/apache/servicecomb/config/kie/client/TestKieUtil.java
new file mode 100644
index 0000000..f5acc1b
--- /dev/null
+++ b/dynamic-config/config-kie/src/test/java/org/apache/servicecomb/config/kie/client/TestKieUtil.java
@@ -0,0 +1,46 @@
+package org.apache.servicecomb.config.kie.client;
+
+import java.util.Map;
+
+import org.apache.servicecomb.config.kie.model.KVDoc;
+import org.apache.servicecomb.config.kie.model.ValueType;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestKieUtil {
+ @Test
+ public void test_processValueType() {
+ KVDoc kvDoc = new KVDoc();
+ kvDoc.setKey("hello");
+ kvDoc.setValue("world");
+ Map<String, String> result = KieUtil.processValueType(kvDoc);
+ Assert.assertEquals("world", result.get("hello"));
+
+ kvDoc.setValueType("text");
+ result = KieUtil.processValueType(kvDoc);
+ Assert.assertEquals("world", result.get("hello"));
+
+ kvDoc.setValueType("string");
+ result = KieUtil.processValueType(kvDoc);
+ Assert.assertEquals("world", result.get("hello"));
+
+ kvDoc.setValueType("json");
+ result = KieUtil.processValueType(kvDoc);
+ Assert.assertEquals("world", result.get("hello"));
+
+ kvDoc.setValueType("yml");
+ kvDoc.setValue("hello: world");
+ result = KieUtil.processValueType(kvDoc);
+ Assert.assertEquals("world", result.get("hello.hello"));
+
+ kvDoc.setValueType("yaml");
+ kvDoc.setValue("hello: world");
+ result = KieUtil.processValueType(kvDoc);
+ Assert.assertEquals("world", result.get("hello.hello"));
+
+ kvDoc.setValueType("properties");
+ kvDoc.setValue("hello=world");
+ result = KieUtil.processValueType(kvDoc);
+ Assert.assertEquals("world", result.get("hello.hello"));
+ }
+}