mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
YmlConfigBinder代码整理
This commit is contained in:
@@ -1,156 +1,155 @@
|
|||||||
package com.alibaba.otter.canal.client.adapter.config;
|
package com.alibaba.otter.canal.client.adapter.config;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.springframework.util.PropertyPlaceholderHelper;
|
import org.springframework.util.PropertyPlaceholderHelper;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.alibaba.otter.canal.client.adapter.config.bind.PropertiesConfigurationFactory;
|
import com.alibaba.otter.canal.client.adapter.config.bind.PropertiesConfigurationFactory;
|
||||||
import com.alibaba.otter.canal.client.adapter.config.common.*;
|
import com.alibaba.otter.canal.client.adapter.config.common.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将yaml内容绑定到指定对象, 遵循spring yml的绑定规范
|
* 将yaml内容绑定到指定对象, 遵循spring yml的绑定规范
|
||||||
*
|
*
|
||||||
* @author reweerma 2019-2-1 上午9:14:02
|
* @author reweerma 2019-2-1 上午9:14:02
|
||||||
* @version 1.0.0
|
* @version 1.0.0
|
||||||
*/
|
*/
|
||||||
public class YmlConfigBinder {
|
public class YmlConfigBinder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将当前内容绑定到指定对象
|
* 将当前内容绑定到指定对象
|
||||||
*
|
*
|
||||||
* @param content yml内容
|
* @param content yml内容
|
||||||
* @param clazz 指定对象类型
|
* @param clazz 指定对象类型
|
||||||
* @return 对象
|
* @return 对象
|
||||||
*/
|
*/
|
||||||
public static <T> T bindYmlToObj(String content, Class<T> clazz) {
|
public static <T> T bindYmlToObj(String content, Class<T> clazz) {
|
||||||
return bindYmlToObj(null, content, clazz, null);
|
return bindYmlToObj(null, content, clazz, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将当前内容绑定到指定对象并指定内容编码格式
|
* 将当前内容绑定到指定对象并指定内容编码格式
|
||||||
*
|
*
|
||||||
* @param content yml内容
|
* @param content yml内容
|
||||||
* @param clazz 指定对象类型
|
* @param clazz 指定对象类型
|
||||||
* @param charset yml内容编码格式
|
* @param charset yml内容编码格式
|
||||||
* @return 对象
|
* @return 对象
|
||||||
*/
|
*/
|
||||||
public static <T> T bindYmlToObj(String content, Class<T> clazz, String charset) {
|
public static <T> T bindYmlToObj(String content, Class<T> clazz, String charset) {
|
||||||
return bindYmlToObj(null, content, clazz, charset);
|
return bindYmlToObj(null, content, clazz, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将当前内容指定前缀部分绑定到指定对象
|
* 将当前内容指定前缀部分绑定到指定对象
|
||||||
*
|
*
|
||||||
* @param prefix 指定前缀
|
* @param prefix 指定前缀
|
||||||
* @param content yml内容
|
* @param content yml内容
|
||||||
* @param clazz 指定对象类型
|
* @param clazz 指定对象类型
|
||||||
* @return 对象
|
* @return 对象
|
||||||
*/
|
*/
|
||||||
public static <T> T bindYmlToObj(String prefix, String content, Class<T> clazz) {
|
public static <T> T bindYmlToObj(String prefix, String content, Class<T> clazz) {
|
||||||
return bindYmlToObj(prefix, content, clazz, null);
|
return bindYmlToObj(prefix, content, clazz, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将当前内容指定前缀部分绑定到指定对象并指定内容编码格式
|
* 将当前内容指定前缀部分绑定到指定对象并指定内容编码格式
|
||||||
*
|
*
|
||||||
* @param prefix 指定前缀
|
* @param prefix 指定前缀
|
||||||
* @param content yml内容
|
* @param content yml内容
|
||||||
* @param clazz 指定对象类型
|
* @param clazz 指定对象类型
|
||||||
* @param charset yml内容编码格式
|
* @param charset yml内容编码格式
|
||||||
* @return 对象
|
* @return 对象
|
||||||
*/
|
*/
|
||||||
public static <T> T bindYmlToObj(String prefix, String content, Class<T> clazz, String charset) {
|
public static <T> T bindYmlToObj(String prefix, String content, Class<T> clazz, String charset) {
|
||||||
return bindYmlToObj(prefix, content, clazz, charset, null);
|
return bindYmlToObj(prefix, content, clazz, charset, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将当前内容指定前缀部分绑定到指定对象并用环境变量中的属性替换占位符, 例: 当前内容有属性 zkServers:
|
* 将当前内容指定前缀部分绑定到指定对象并用环境变量中的属性替换占位符, 例: 当前内容有属性 zkServers: ${zookeeper.servers}
|
||||||
* ${zookeeper.servers} 在envProperties中有属性 zookeeper.servers:
|
* 在envProperties中有属性 zookeeper.servers:
|
||||||
* 192.168.0.1:2181,192.168.0.1:2181,192.168.0.1:2181 则当前内容 zkServers 会被替换为
|
* 192.168.0.1:2181,192.168.0.1:2181,192.168.0.1:2181 则当前内容 zkServers 会被替换为
|
||||||
* zkServers: 192.168.0.1:2181,192.168.0.1:2181,192.168.0.1:2181 注: 假设绑定的类中
|
* zkServers: 192.168.0.1:2181,192.168.0.1:2181,192.168.0.1:2181 注: 假设绑定的类中
|
||||||
* zkServers 属性是 List<String> 对象, 则会自动映射成List
|
* zkServers 属性是 List<String> 对象, 则会自动映射成List
|
||||||
*
|
*
|
||||||
* @param prefix 指定前缀
|
* @param prefix 指定前缀
|
||||||
* @param content yml内容
|
* @param content yml内容
|
||||||
* @param clazz 指定对象类型
|
* @param clazz 指定对象类型
|
||||||
* @param charset yml内容编码格式
|
* @param charset yml内容编码格式
|
||||||
* @return 对象
|
* @return 对象
|
||||||
*/
|
*/
|
||||||
public static <T> T bindYmlToObj(String prefix, String content, Class<T> clazz, String charset,
|
public static <T> T bindYmlToObj(String prefix, String content, Class<T> clazz, String charset,
|
||||||
Properties baseProperties) {
|
Properties baseProperties) {
|
||||||
try {
|
try {
|
||||||
byte[] contentBytes;
|
byte[] contentBytes;
|
||||||
if (charset == null) {
|
if (charset == null) {
|
||||||
contentBytes = content.getBytes();
|
contentBytes = content.getBytes();
|
||||||
} else {
|
} else {
|
||||||
contentBytes = content.getBytes(charset);
|
contentBytes = content.getBytes(charset);
|
||||||
}
|
}
|
||||||
YamlPropertySourceLoader propertySourceLoader = new YamlPropertySourceLoader();
|
YamlPropertySourceLoader propertySourceLoader = new YamlPropertySourceLoader();
|
||||||
Resource configResource = new ByteArrayResource(contentBytes);
|
Resource configResource = new ByteArrayResource(contentBytes);
|
||||||
PropertySource<?> propertySource = propertySourceLoader.load("manualBindConfig", configResource, null);
|
PropertySource<?> propertySource = propertySourceLoader.load("manualBindConfig", configResource, null);
|
||||||
|
|
||||||
if (propertySource == null) {
|
if (propertySource == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
Map<String, Object> propertiesRes = new LinkedHashMap<>();
|
Map<String, Object> propertiesRes = new LinkedHashMap<>();
|
||||||
if (!StringUtils.isEmpty(prefix) && !prefix.endsWith(".")) {
|
if (!StringUtils.isEmpty(prefix) && !prefix.endsWith(".")) {
|
||||||
prefix = prefix + ".";
|
prefix = prefix + ".";
|
||||||
}
|
}
|
||||||
|
|
||||||
properties.putAll((Map<?, ?>) propertySource.getSource());
|
properties.putAll((Map<?, ?>) propertySource.getSource());
|
||||||
|
|
||||||
if (baseProperties != null) {
|
if (baseProperties != null) {
|
||||||
baseProperties.putAll(properties);
|
baseProperties.putAll(properties);
|
||||||
properties = baseProperties;
|
properties = baseProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Object o : ((Map<?, ?>) propertySource.getSource()).entrySet()) {
|
for (Map.Entry<?, ?> entry : ((Map<?, ?>) propertySource.getSource()).entrySet()) {
|
||||||
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) o;
|
String key = (String) entry.getKey();
|
||||||
String key = (String) entry.getKey();
|
Object value = entry.getValue();
|
||||||
Object value = entry.getValue();
|
|
||||||
|
if (prefix != null) {
|
||||||
if (prefix != null) {
|
if (key != null && key.startsWith(prefix)) {
|
||||||
if (key != null && key.startsWith(prefix)) {
|
key = key.substring(prefix.length());
|
||||||
key = key.substring(prefix.length());
|
} else {
|
||||||
} else {
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
if (value instanceof String && ((String) value).contains("${") && ((String) value).contains("}")) {
|
||||||
if (value != null && value.toString().contains("${")) {
|
PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${", "}");
|
||||||
PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${", "}");
|
value = propertyPlaceholderHelper.replacePlaceholders((String) value, properties);
|
||||||
value = propertyPlaceholderHelper.replacePlaceholders(value.toString(), properties);
|
}
|
||||||
}
|
|
||||||
|
propertiesRes.put(key, value);
|
||||||
propertiesRes.put(key, value);
|
}
|
||||||
}
|
|
||||||
|
if (propertiesRes.isEmpty()) {
|
||||||
if (propertiesRes.isEmpty()) {
|
return null;
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
propertySource = new MapPropertySource(propertySource.getName(), propertiesRes);
|
||||||
propertySource = new MapPropertySource(propertySource.getName(), propertiesRes);
|
|
||||||
|
T target = clazz.newInstance();
|
||||||
T target = clazz.newInstance();
|
|
||||||
|
MutablePropertySources propertySources = new MutablePropertySources();
|
||||||
MutablePropertySources propertySources = new MutablePropertySources();
|
propertySources.addFirst(propertySource);
|
||||||
propertySources.addFirst(propertySource);
|
|
||||||
|
PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(target);
|
||||||
PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(target);
|
factory.setPropertySources(propertySources);
|
||||||
factory.setPropertySources(propertySources);
|
factory.setIgnoreInvalidFields(true);
|
||||||
factory.setIgnoreInvalidFields(true);
|
factory.setIgnoreUnknownFields(true);
|
||||||
factory.setIgnoreUnknownFields(true);
|
|
||||||
|
factory.bindPropertiesToTarget();
|
||||||
factory.bindPropertiesToTarget();
|
|
||||||
|
return target;
|
||||||
return target;
|
} catch (Exception e) {
|
||||||
} catch (Exception e) {
|
throw new RuntimeException(e);
|
||||||
throw new RuntimeException(e);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user