mirror of https://github.com/longtai-cn/hippo4j
parent
cd402653f4
commit
100202fe07
@ -0,0 +1,15 @@
|
||||
package cn.hippo4j.core.starter.parser;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/3/1 07:50
|
||||
* @description:
|
||||
*/
|
||||
public abstract class AbstractConfigParser implements ConfigParser{
|
||||
|
||||
@Override
|
||||
public boolean supports(ConfigFileTypeEnum type) {
|
||||
return getConfigFileTypes().contains(type);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package cn.hippo4j.core.starter.parser;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/3/1 07:47
|
||||
* @description:
|
||||
*/
|
||||
@Getter
|
||||
public enum ConfigFileTypeEnum {
|
||||
|
||||
PROPERTIES("properties"),
|
||||
XML("xml"),
|
||||
JSON("json"),
|
||||
YML("yml"),
|
||||
YAML("yaml"),
|
||||
TXT("txt");
|
||||
|
||||
private final String value;
|
||||
|
||||
ConfigFileTypeEnum(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static ConfigFileTypeEnum of(String value) {
|
||||
for (ConfigFileTypeEnum typeEnum : ConfigFileTypeEnum.values()) {
|
||||
if (typeEnum.value.equals(value)) {
|
||||
return typeEnum;
|
||||
}
|
||||
}
|
||||
return PROPERTIES;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package cn.hippo4j.core.starter.parser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/3/1 07:47
|
||||
* @description:
|
||||
*/
|
||||
public interface ConfigParser {
|
||||
|
||||
boolean supports(ConfigFileTypeEnum type);
|
||||
|
||||
Map<Object, Object> doParse(String content) throws IOException;
|
||||
|
||||
List<ConfigFileTypeEnum> getConfigFileTypes();
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package cn.hippo4j.core.starter.parser;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/3/1 08:02
|
||||
* @description:
|
||||
*/
|
||||
public class ConfigParserHandler {
|
||||
|
||||
private static final List<ConfigParser> PARSERS = Lists.newArrayList();
|
||||
|
||||
private ConfigParserHandler() {
|
||||
ServiceLoader<ConfigParser> loader = ServiceLoader.load(ConfigParser.class);
|
||||
for (ConfigParser configParser : loader) {
|
||||
PARSERS.add(configParser);
|
||||
}
|
||||
|
||||
PARSERS.add(new PropertiesConfigParser());
|
||||
PARSERS.add(new YamlConfigParser());
|
||||
}
|
||||
|
||||
public Map<Object, Object> parseConfig(String content, ConfigFileTypeEnum type) throws IOException {
|
||||
for (ConfigParser parser : PARSERS) {
|
||||
if (parser.supports(type)) {
|
||||
return parser.doParse(content);
|
||||
}
|
||||
}
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
public static ConfigParserHandler getInstance() {
|
||||
return ConfigParserHandlerHolder.INSTANCE;
|
||||
}
|
||||
|
||||
private static class ConfigParserHandlerHolder {
|
||||
private static final ConfigParserHandler INSTANCE = new ConfigParserHandler();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package cn.hippo4j.core.starter.parser;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/3/1 07:49
|
||||
* @description:
|
||||
*/
|
||||
public class PropertiesConfigParser extends AbstractConfigParser{
|
||||
|
||||
|
||||
@Override
|
||||
public Map<Object, Object> doParse(String content) throws IOException {
|
||||
Properties properties = new Properties();
|
||||
properties.load(new StringReader(content));
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ConfigFileTypeEnum> getConfigFileTypes() {
|
||||
return Lists.newArrayList(ConfigFileTypeEnum.PROPERTIES);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package cn.hippo4j.core.starter.parser;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/3/1 07:57
|
||||
* @description:
|
||||
*/
|
||||
public class YamlConfigParser extends AbstractConfigParser{
|
||||
|
||||
@Override
|
||||
public Map<Object, Object> doParse(String content) {
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
return Maps.newHashMapWithExpectedSize(0);
|
||||
}
|
||||
YamlPropertiesFactoryBean bean = new YamlPropertiesFactoryBean();
|
||||
bean.setResources(new ByteArrayResource(content.getBytes()));
|
||||
return bean.getObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ConfigFileTypeEnum> getConfigFileTypes() {
|
||||
return Lists.newArrayList(ConfigFileTypeEnum.YML, ConfigFileTypeEnum.YAML);
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package cn.hippo4j.core.starter.refresher;
|
||||
|
||||
import cn.hippo4j.common.toolkit.StringUtil;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Config parser service.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2022/2/26 17:33
|
||||
*/
|
||||
public class ConfigParserHandler {
|
||||
|
||||
private final List<String> yamlList = Lists.newArrayList("yaml", "yml");
|
||||
|
||||
/**
|
||||
* Parse config.
|
||||
*
|
||||
* @param content
|
||||
* @param configFileType
|
||||
* @return
|
||||
*/
|
||||
public Map<Object, Object> parseConfig(String content, String configFileType) {
|
||||
Map<Object, Object> resultMap = Maps.newHashMap();
|
||||
if (StringUtil.isBlank(content)) {
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
if (yamlList.contains(configFileType)) {
|
||||
YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
|
||||
yamlPropertiesFactoryBean.setResources(new ByteArrayResource(content.getBytes()));
|
||||
resultMap = yamlPropertiesFactoryBean.getObject();
|
||||
}
|
||||
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue