mirror of https://github.com/longtai-cn/hippo4j
feat: The registration center in config mode supports JSON format. (#1551)
parent
2c6fbefe13
commit
5ebe215f5f
@ -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<Object, Object> doParse(String content, String prefix) throws IOException {
|
||||
|
||||
Map<String, Object> originMap = MAPPER.readValue(content, LinkedHashMap.class);
|
||||
Map<Object, Object> result = new HashMap<>();
|
||||
|
||||
flatMap(result, originMap, prefix);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void flatMap(Map<Object, Object> result, Map<String, Object> dataMap, String prefix) {
|
||||
|
||||
if (MapUtils.isEmpty(dataMap)) {
|
||||
return;
|
||||
}
|
||||
|
||||
dataMap.forEach((k, v) -> {
|
||||
String fullKey = genFullKey(prefix, k);
|
||||
if (v instanceof Map) {
|
||||
flatMap(result, (Map<String, Object>) v, fullKey);
|
||||
return;
|
||||
} else if (v instanceof Collection) {
|
||||
int count = 0;
|
||||
for (Object obj : (Collection<Object>) 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<Object, Object> doParse(String content) throws IOException {
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
return new HashMap<>(1);
|
||||
}
|
||||
|
||||
return doParse(content,"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ConfigFileTypeEnum> getConfigFileTypes() {
|
||||
return CollectionUtil.newArrayList(ConfigFileTypeEnum.JSON, ConfigFileTypeEnum.JSON);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue