mirror of https://github.com/longtai-cn/hippo4j
commit
a4265b5ef9
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package cn.hippo4j.core.starter.refresher.config;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 配置序列化接口
|
||||
*
|
||||
* @author serenity SerenitySir@outlook.com
|
||||
* @since 2022/2/28
|
||||
*/
|
||||
public interface ConfigParser {
|
||||
/**
|
||||
* 对配置进行序列化
|
||||
* @param content 配置的字符串形式
|
||||
* @return 序列化后的k,v
|
||||
*/
|
||||
Map<Object, Object> parseConfig(String content);
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package cn.hippo4j.core.starter.refresher.config.impl;
|
||||
|
||||
import cn.hippo4j.core.starter.refresher.config.ConfigParser;
|
||||
import com.google.common.collect.Maps;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Properties序列化配置
|
||||
*
|
||||
* @author serenity SerenitySir@outlook.com
|
||||
* @since 2022/2/28
|
||||
*/
|
||||
public class PropConfigParser implements ConfigParser {
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public Map<Object, Object> parseConfig(String content) {
|
||||
if (!StringUtils.hasText(content)){
|
||||
return Maps.newHashMap();
|
||||
}
|
||||
|
||||
Properties properties = new Properties();
|
||||
properties.load(new StringReader(content));
|
||||
return properties;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConfigParser propConfigParser = new PropConfigParser();
|
||||
Map<Object, Object> map = propConfigParser.parseConfig("db.aa=11\ndb.bb=22");
|
||||
System.out.println(map.toString());
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package cn.hippo4j.core.starter.refresher.config.impl;
|
||||
|
||||
import cn.hippo4j.common.toolkit.StringUtil;
|
||||
import cn.hippo4j.core.starter.refresher.config.ConfigParser;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Yml序列化配置
|
||||
*
|
||||
* @author serenity SerenitySir@outlook.com
|
||||
* @since 2022/2/28
|
||||
*/
|
||||
public class YmlConfigParser implements ConfigParser {
|
||||
|
||||
@Override
|
||||
public Map<Object, Object> parseConfig(String content) {
|
||||
Map<Object, Object> resultMap = Maps.newHashMap();
|
||||
if (StringUtil.isBlank(content)) {
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
|
||||
yamlPropertiesFactoryBean.setResources(new ByteArrayResource(content.getBytes()));
|
||||
resultMap = yamlPropertiesFactoryBean.getObject();
|
||||
|
||||
return resultMap;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue