From 5ebe215f5f4fcfc6fd70a34c6d2d7268a52edf18 Mon Sep 17 00:00:00 2001 From: song <58645531+Song246@users.noreply.github.com> Date: Mon, 17 Jun 2024 16:54:47 +0800 Subject: [PATCH] feat: The registration center in config mode supports JSON format. (#1551) --- .../config/parser/ConfigParserHandler.java | 1 + .../mode/config/parser/JsonConfigParser.java | 96 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/JsonConfigParser.java diff --git a/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/ConfigParserHandler.java b/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/ConfigParserHandler.java index ea61255a..60d1f752 100644 --- a/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/ConfigParserHandler.java +++ b/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/ConfigParserHandler.java @@ -34,6 +34,7 @@ public final class ConfigParserHandler { } PARSERS.add(new PropertiesConfigParser()); PARSERS.add(new YamlConfigParser()); + PARSERS.add(new JsonConfigParser()); } public Map parseConfig(String content, String type) throws IOException { diff --git a/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/JsonConfigParser.java b/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/JsonConfigParser.java new file mode 100644 index 00000000..87fe6ba1 --- /dev/null +++ b/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/JsonConfigParser.java @@ -0,0 +1,96 @@ +/* + * 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 cn.hippo4j.threadpool.dynamic.mode.config.parser; + +import cn.hippo4j.common.toolkit.CollectionUtil; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; + +import java.io.IOException; +import java.util.*; + +/** + * Json config parser. + */ +public class JsonConfigParser extends AbstractConfigParser { + private static final ObjectMapper MAPPER; + private static final String DOT = "."; + private static final String LEFT_BRACE = "{"; + private static final String RIGHT_BRACE = "{"; + private static final String LEFT_BRACKET = "["; + private static final String RIGHT_BRACKET = "]"; + + static { + MAPPER = new ObjectMapper(); + } + + public Map doParse(String content, String prefix) throws IOException { + + Map originMap = MAPPER.readValue(content, LinkedHashMap.class); + Map result = new HashMap<>(); + + flatMap(result, originMap, prefix); + return result; + } + + private void flatMap(Map result, Map dataMap, String prefix) { + + if (MapUtils.isEmpty(dataMap)) { + return; + } + + dataMap.forEach((k, v) -> { + String fullKey = genFullKey(prefix, k); + if (v instanceof Map) { + flatMap(result, (Map) v, fullKey); + return; + } else if (v instanceof Collection) { + int count = 0; + for (Object obj : (Collection) v) { + String kk = LEFT_BRACKET + (count++) + RIGHT_BRACKET; + flatMap(result, Collections.singletonMap(kk, obj), fullKey); + } + return; + } + + result.put(fullKey, v); + }); + } + + private String genFullKey(String prefix, String key) { + if (StringUtils.isEmpty(prefix)) { + return key; + } + return key.startsWith(LEFT_BRACE) ? prefix.concat(key) : prefix.concat(DOT).concat(key); + } + + @Override + public Map doParse(String content) throws IOException { + if (StringUtils.isEmpty(content)) { + return new HashMap<>(1); + } + + return doParse(content,""); + } + + @Override + public List getConfigFileTypes() { + return CollectionUtil.newArrayList(ConfigFileTypeEnum.JSON, ConfigFileTypeEnum.JSON); + } +}