Java Code Examples for org.springframework.util.PropertyPlaceholderHelper#replacePlaceholders()

The following examples show how to use org.springframework.util.PropertyPlaceholderHelper#replacePlaceholders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigurationResolver.java    From vividus with Apache License 2.0 6 votes vote down vote up
private static Multimap<String, String> assembleConfiguration(Properties configurationProperties,
        Properties overridingProperties)
{
    String profiles = getCompetingConfigurationPropertyValue(configurationProperties, overridingProperties,
            Pair.of(PROFILE, PROFILES));
    String environments = getConfigurationPropertyValue(configurationProperties, overridingProperties,
            ENVIRONMENTS);
    String suite = getConfigurationPropertyValue(configurationProperties, overridingProperties, SUITE);

    Properties mergedProperties = new Properties();
    mergedProperties.putAll(configurationProperties);
    mergedProperties.putAll(overridingProperties);
    PropertyPlaceholderHelper propertyPlaceholderHelper = createPropertyPlaceholderHelper(true);

    profiles = propertyPlaceholderHelper.replacePlaceholders(profiles, mergedProperties::getProperty);
    environments = propertyPlaceholderHelper.replacePlaceholders(environments, mergedProperties::getProperty);
    suite = propertyPlaceholderHelper.replacePlaceholders(suite, mergedProperties::getProperty);

    Multimap<String, String> configuration = LinkedHashMultimap.create();
    configuration.putAll(PROFILE, asPaths(profiles));
    configuration.putAll(ENVIRONMENT, asPaths(environments));
    configuration.put(SUITE, suite);
    return configuration;
}
 
Example 2
Source File: AbstractPropertyResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
	return helper.replacePlaceholders(text, new PropertyPlaceholderHelper.PlaceholderResolver() {
		@Override
		public String resolvePlaceholder(String placeholderName) {
			return getPropertyAsRawString(placeholderName);
		}
	});
}
 
Example 3
Source File: AbstractPropertyResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
	return helper.replacePlaceholders(text, new PropertyPlaceholderHelper.PlaceholderResolver() {
		@Override
		public String resolvePlaceholder(String placeholderName) {
			return getPropertyAsRawString(placeholderName);
		}
	});
}
 
Example 4
Source File: ApplicationInitializer.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
/**
 * Load the JSON template and perform string substitution on the ${...} tokens
 * @param is
 * @return
 * @throws IOException
 */
@SuppressWarnings("unchecked")
Map<String, Object> loadJsonFile(InputStream is, Properties sliProps) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer);
    String template = writer.toString();
    PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}"); 
    template = helper.replacePlaceholders(template, sliProps);
    return (Map<String, Object>) JSON.parse(template);
}
 
Example 5
Source File: AbstractPropertyResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
	return helper.replacePlaceholders(text, this::getPropertyAsRawString);
}
 
Example 6
Source File: YmlConfigBinder.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
/**
 * 将当前内容指定前缀部分绑定到指定对象并用环境变量中的属性替换占位符, 例: 当前内容有属性 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<Object>(target);
        factory.setPropertySources(propertySources);
        factory.setIgnoreInvalidFields(true);
        factory.setIgnoreUnknownFields(true);

        factory.bindPropertiesToTarget();

        return target;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source File: AbstractPropertyResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
	return helper.replacePlaceholders(text, this::getPropertyAsRawString);
}
 
Example 8
Source File: YmlConfigBinder.java    From canal with Apache License 2.0 4 votes vote down vote up
/**
 * 将当前内容指定前缀部分绑定到指定对象并用环境变量中的属性替换占位符, 例: 当前内容有属性 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<Object>(target);
        factory.setPropertySources(propertySources);
        factory.setIgnoreInvalidFields(true);
        factory.setIgnoreUnknownFields(true);

        factory.bindPropertiesToTarget();

        return target;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: ServletContextPropertyUtils.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Resolve ${...} placeholders in the given text, replacing them with corresponding
 * servlet context init parameter or system property values. Unresolvable placeholders
 * with no default value are ignored and passed through unchanged if the flag is set to true.
 * @param text the String to resolve
 * @param servletContext the servletContext to use for lookups.
 * @param ignoreUnresolvablePlaceholders flag to determine is unresolved placeholders are ignored
 * @return the resolved String
 * @throws IllegalArgumentException if there is an unresolvable placeholder and the flag is false
 * @see SystemPropertyUtils#PLACEHOLDER_PREFIX
 * @see SystemPropertyUtils#PLACEHOLDER_SUFFIX
 * @see SystemPropertyUtils#resolvePlaceholders(String, boolean)
 */
public static String resolvePlaceholders(String text, ServletContext servletContext,
		boolean ignoreUnresolvablePlaceholders) {

	PropertyPlaceholderHelper helper = (ignoreUnresolvablePlaceholders ? nonStrictHelper : strictHelper);
	return helper.replacePlaceholders(text, new ServletContextPlaceholderResolver(text, servletContext));
}
 
Example 10
Source File: ServletContextPropertyUtils.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Resolve ${...} placeholders in the given text, replacing them with corresponding
 * servlet context init parameter or system property values. Unresolvable placeholders
 * with no default value are ignored and passed through unchanged if the flag is set to true.
 * @param text the String to resolve
 * @param servletContext the servletContext to use for lookups.
 * @param ignoreUnresolvablePlaceholders flag to determine is unresolved placeholders are ignored
 * @return the resolved String
 * @throws IllegalArgumentException if there is an unresolvable placeholder and the flag is false
 * @see SystemPropertyUtils#PLACEHOLDER_PREFIX
 * @see SystemPropertyUtils#PLACEHOLDER_SUFFIX
 * @see SystemPropertyUtils#resolvePlaceholders(String, boolean)
 */
public static String resolvePlaceholders(String text, ServletContext servletContext,
		boolean ignoreUnresolvablePlaceholders) {

	PropertyPlaceholderHelper helper = (ignoreUnresolvablePlaceholders ? nonStrictHelper : strictHelper);
	return helper.replacePlaceholders(text, new ServletContextPlaceholderResolver(text, servletContext));
}
 
Example 11
Source File: PropertyPlaceholderConfigurer.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Parse the given String value for placeholder resolution.
 * @param strVal the String value to parse
 * @param props the Properties to resolve placeholders against
 * @param visitedPlaceholders the placeholders that have already been visited
 * during the current resolution attempt (ignored in this version of the code)
 * @deprecated as of Spring 3.0, in favor of using {@link #resolvePlaceholder}
 * with {@link org.springframework.util.PropertyPlaceholderHelper}.
 * Only retained for compatibility with Spring 2.5 extensions.
 */
@Deprecated
protected String parseStringValue(String strVal, Properties props, Set<?> visitedPlaceholders) {
	PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(
			placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
	PlaceholderResolver resolver = new PropertyPlaceholderConfigurerResolver(props);
	return helper.replacePlaceholders(strVal, resolver);
}
 
Example 12
Source File: PropertyPlaceholderConfigurer.java    From blog_demos with Apache License 2.0 3 votes vote down vote up
/**
 * Parse the given String value for placeholder resolution.
 * @param strVal the String value to parse
 * @param props the Properties to resolve placeholders against
 * @param visitedPlaceholders the placeholders that have already been visited
 * during the current resolution attempt (ignored in this version of the code)
 * @deprecated as of Spring 3.0, in favor of using {@link #resolvePlaceholder}
 * with {@link PropertyPlaceholderHelper}.
 * Only retained for compatibility with Spring 2.5 extensions.
 */
@Deprecated
protected String parseStringValue(String strVal, Properties props, Set<?> visitedPlaceholders) {
	PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(
			placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
	PlaceholderResolver resolver = new PropertyPlaceholderConfigurerResolver(props);
	return helper.replacePlaceholders(strVal, resolver);
}
 
Example 13
Source File: PropertyPlaceholderConfigurer.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Parse the given String value for placeholder resolution.
 * @param strVal the String value to parse
 * @param props the Properties to resolve placeholders against
 * @param visitedPlaceholders the placeholders that have already been visited
 * during the current resolution attempt (ignored in this version of the code)
 * @deprecated as of Spring 3.0, in favor of using {@link #resolvePlaceholder}
 * with {@link org.springframework.util.PropertyPlaceholderHelper}.
 * Only retained for compatibility with Spring 2.5 extensions.
 */
@Deprecated
protected String parseStringValue(String strVal, Properties props, Set<?> visitedPlaceholders) {
	PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(
			placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
	PlaceholderResolver resolver = new PropertyPlaceholderConfigurerResolver(props);
	return helper.replacePlaceholders(strVal, resolver);
}
 
Example 14
Source File: ServletContextPropertyUtils.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Resolve ${...} placeholders in the given text, replacing them with corresponding
 * servlet context init parameter or system property values. Unresolvable placeholders
 * with no default value are ignored and passed through unchanged if the flag is set to true.
 * @param text the String to resolve
    * @param servletContext the servletContext to use for lookups.
 * @param ignoreUnresolvablePlaceholders flag to determine is unresolved placeholders are ignored
 * @return the resolved String
 * @see SystemPropertyUtils#PLACEHOLDER_PREFIX
 * @see SystemPropertyUtils#PLACEHOLDER_SUFFIX
    * @see SystemPropertyUtils#resolvePlaceholders(String, boolean)
 * @throws IllegalArgumentException if there is an unresolvable placeholder and the flag is false
 */
public static String resolvePlaceholders(String text, ServletContext servletContext, boolean ignoreUnresolvablePlaceholders) {
	PropertyPlaceholderHelper helper = (ignoreUnresolvablePlaceholders ? nonStrictHelper : strictHelper);
	return helper.replacePlaceholders(text, new ServletContextPlaceholderResolver(text, servletContext));
}
 
Example 15
Source File: ServletContextPropertyUtils.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Resolve ${...} placeholders in the given text, replacing them with corresponding
 * servlet context init parameter or system property values. Unresolvable placeholders
 * with no default value are ignored and passed through unchanged if the flag is set to true.
 * @param text the String to resolve
    * @param servletContext the servletContext to use for lookups.
 * @param ignoreUnresolvablePlaceholders flag to determine is unresolved placeholders are ignored
 * @return the resolved String
 * @see SystemPropertyUtils#PLACEHOLDER_PREFIX
 * @see SystemPropertyUtils#PLACEHOLDER_SUFFIX
    * @see SystemPropertyUtils#resolvePlaceholders(String, boolean)
 * @throws IllegalArgumentException if there is an unresolvable placeholder and the flag is false
 */
public static String resolvePlaceholders(String text, ServletContext servletContext, boolean ignoreUnresolvablePlaceholders) {
	PropertyPlaceholderHelper helper = (ignoreUnresolvablePlaceholders ? nonStrictHelper : strictHelper);
	return helper.replacePlaceholders(text, new ServletContextPlaceholderResolver(text, servletContext));
}