Search in sources :

Example 1 with PropertiesConfigurationFactory

use of com.alibaba.otter.canal.client.adapter.config.bind.PropertiesConfigurationFactory in project canal by alibaba.

the class YmlConfigBinder method bindYmlToObj.

/**
 * 将当前内容指定前缀部分绑定到指定对象并用环境变量中的属性替换占位符, 例: 当前内容有属性 zkServers: ${zookeeper.servers}
 * 在envProperties中有属性 zookeeper.servers:
 * 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 属性是 List<String> 对象, 则会自动映射成List
 *
 * @param prefix 指定前缀
 * @param content yml内容
 * @param clazz 指定对象类型
 * @param charset yml内容编码格式
 * @return 对象
 */
public static <T> T bindYmlToObj(String prefix, String content, Class<T> clazz, String charset, Properties baseProperties) {
    try {
        byte[] contentBytes;
        if (charset == null) {
            contentBytes = content.getBytes("UTF-8");
        } else {
            contentBytes = content.getBytes(charset);
        }
        YamlPropertySourceLoader propertySourceLoader = new YamlPropertySourceLoader();
        Resource configResource = new ByteArrayResource(contentBytes);
        PropertySource<?> propertySource = propertySourceLoader.load("manualBindConfig", configResource, null);
        if (propertySource == null) {
            return null;
        }
        Properties properties = new Properties();
        Map<String, Object> propertiesRes = new LinkedHashMap<>();
        if (!StringUtils.isEmpty(prefix) && !prefix.endsWith(".")) {
            prefix = prefix + ".";
        }
        properties.putAll((Map<?, ?>) propertySource.getSource());
        if (baseProperties != null) {
            baseProperties.putAll(properties);
            properties = baseProperties;
        }
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) propertySource.getSource()).entrySet()) {
            String key = (String) entry.getKey();
            Object value = entry.getValue();
            if (prefix != null) {
                if (key != null && key.startsWith(prefix)) {
                    key = key.substring(prefix.length());
                } else {
                    continue;
                }
            }
            if (value instanceof String && ((String) value).contains("${") && ((String) value).contains("}")) {
                PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${", "}");
                value = propertyPlaceholderHelper.replacePlaceholders((String) value, properties);
            }
            propertiesRes.put(key, value);
        }
        if (propertiesRes.isEmpty()) {
            return null;
        }
        propertySource = new MapPropertySource(propertySource.getName(), propertiesRes);
        T target = clazz.newInstance();
        MutablePropertySources propertySources = new MutablePropertySources();
        propertySources.addFirst(propertySource);
        PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<>(target);
        factory.setPropertySources(propertySources);
        factory.setIgnoreInvalidFields(true);
        factory.setIgnoreUnknownFields(true);
        factory.bindPropertiesToTarget();
        return target;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Properties(java.util.Properties) LinkedHashMap(java.util.LinkedHashMap) PropertyPlaceholderHelper(org.springframework.util.PropertyPlaceholderHelper) PropertiesConfigurationFactory(com.alibaba.otter.canal.client.adapter.config.bind.PropertiesConfigurationFactory) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

PropertiesConfigurationFactory (com.alibaba.otter.canal.client.adapter.config.bind.PropertiesConfigurationFactory)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 PropertyPlaceholderHelper (org.springframework.util.PropertyPlaceholderHelper)1