-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: The registration center in config mode supports JSON format.
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...nfig/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/parser/JsonConfigParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |