Search in sources :

Example 1 with PropertyPlaceholderHelper

use of org.springframework.util.PropertyPlaceholderHelper 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)

Example 2 with PropertyPlaceholderHelper

use of org.springframework.util.PropertyPlaceholderHelper in project openolat by klemens.

the class ArquillianDeployments method addOlatLocalProperties.

public static WebArchive addOlatLocalProperties(WebArchive archive, Map<String, String> overrideProperties) {
    String profile = System.getProperty("profile");
    if (profile == null || profile.isEmpty()) {
        profile = "mysql";
    }
    File barebonePropertiesFile = new File("src/test/profile/" + profile, "olat.local.properties");
    PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", ":", true);
    Asset propertiesAsset = null;
    try (InputStream inStream = new FileInputStream(barebonePropertiesFile)) {
        Properties properties = new Properties();
        properties.load(inStream);
        List<String> propNames = new ArrayList<>(properties.stringPropertyNames());
        Properties updatedProperties = new Properties();
        for (String name : propNames) {
            String value = properties.getProperty(name);
            String replacedValue = helper.replacePlaceholders(value, new PropertyPlaceholderResolver(properties));
            updatedProperties.setProperty(name, replacedValue);
        }
        for (Map.Entry<String, String> entryToOverride : overrideProperties.entrySet()) {
            updatedProperties.setProperty(entryToOverride.getKey(), entryToOverride.getValue());
        }
        StringWriter writer = new StringWriter();
        updatedProperties.store(writer, "Replaced for Arquillian deployements");
        propertiesAsset = new StringAsset(writer.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return archive.addAsResource(propertiesAsset, "olat.local.properties");
}
Also used : StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) PropertyPlaceholderHelper(org.springframework.util.PropertyPlaceholderHelper) StringWriter(java.io.StringWriter) StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) Asset(org.jboss.shrinkwrap.api.asset.Asset) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with PropertyPlaceholderHelper

use of org.springframework.util.PropertyPlaceholderHelper in project camel by apache.

the class BridgePropertyPlaceholderConfigurer method processProperties.

@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
    super.processProperties(beanFactoryToProcess, props);
    // store all the spring properties so we can refer to them later
    properties.putAll(props);
    // create helper
    helper = new PropertyPlaceholderHelper(configuredPlaceholderPrefix != null ? configuredPlaceholderPrefix : DEFAULT_PLACEHOLDER_PREFIX, configuredPlaceholderSuffix != null ? configuredPlaceholderSuffix : DEFAULT_PLACEHOLDER_SUFFIX, configuredValueSeparator != null ? configuredValueSeparator : DEFAULT_VALUE_SEPARATOR, configuredIgnoreUnresolvablePlaceholders != null ? configuredIgnoreUnresolvablePlaceholders : false);
}
Also used : PropertyPlaceholderHelper(org.springframework.util.PropertyPlaceholderHelper)

Example 4 with PropertyPlaceholderHelper

use of org.springframework.util.PropertyPlaceholderHelper in project spring-security-oauth by spring-projects.

the class SpelView method render.

public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Map<String, Object> map = new HashMap<String, Object>(model);
    String path = ServletUriComponentsBuilder.fromContextPath(request).build().getPath();
    map.put("path", (Object) path == null ? "" : path);
    context.setRootObject(map);
    String maskedTemplate = template.replace("${", prefix);
    PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(prefix, "}");
    String result = helper.replacePlaceholders(maskedTemplate, resolver);
    result = result.replace(prefix, "${");
    response.setContentType(getContentType());
    response.getWriter().append(result);
}
Also used : PropertyPlaceholderHelper(org.springframework.util.PropertyPlaceholderHelper) HashMap(java.util.HashMap)

Example 5 with PropertyPlaceholderHelper

use of org.springframework.util.PropertyPlaceholderHelper in project OpenOLAT by OpenOLAT.

the class ArquillianDeployments method addOlatLocalProperties.

public static WebArchive addOlatLocalProperties(WebArchive archive, Map<String, String> overrideProperties) {
    String profile = System.getProperty("profile");
    if (profile == null || profile.isEmpty()) {
        profile = "mysql";
    }
    File barebonePropertiesFile = new File("src/test/profile/" + profile, "olat.local.properties");
    PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", ":", true);
    Asset propertiesAsset = null;
    try (InputStream inStream = new FileInputStream(barebonePropertiesFile)) {
        Properties properties = new Properties();
        properties.load(inStream);
        List<String> propNames = new ArrayList<>(properties.stringPropertyNames());
        Properties updatedProperties = new Properties();
        for (String name : propNames) {
            String value = properties.getProperty(name);
            String replacedValue = helper.replacePlaceholders(value, new PropertyPlaceholderResolver(properties));
            updatedProperties.setProperty(name, replacedValue);
        }
        for (Map.Entry<String, String> entryToOverride : overrideProperties.entrySet()) {
            updatedProperties.setProperty(entryToOverride.getKey(), entryToOverride.getValue());
        }
        StringWriter writer = new StringWriter();
        updatedProperties.store(writer, "Replaced for Arquillian deployements");
        propertiesAsset = new StringAsset(writer.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return archive.addAsResource(propertiesAsset, "olat.local.properties");
}
Also used : StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) PropertyPlaceholderHelper(org.springframework.util.PropertyPlaceholderHelper) StringWriter(java.io.StringWriter) StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) Asset(org.jboss.shrinkwrap.api.asset.Asset) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PropertyPlaceholderHelper (org.springframework.util.PropertyPlaceholderHelper)5 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Properties (java.util.Properties)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 Asset (org.jboss.shrinkwrap.api.asset.Asset)2 StringAsset (org.jboss.shrinkwrap.api.asset.StringAsset)2 PropertiesConfigurationFactory (com.alibaba.otter.canal.client.adapter.config.bind.PropertiesConfigurationFactory)1 LinkedHashMap (java.util.LinkedHashMap)1