org.springframework.util.PropertyPlaceholderHelper Java Examples

The following examples show how to use org.springframework.util.PropertyPlaceholderHelper. 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: MessageTemplateServiceImpl.java    From JuniperBot with GNU General Public License v3.0 6 votes vote down vote up
private MessageTemplatePlaceholderResolver getPropertyResolver(@NonNull MessageTemplateCompiler compiler) {
    Set<PropertyPlaceholderHelper.PlaceholderResolver> resolvers = new HashSet<>();
    resolvers.add(compiler.getVariables());
    resolvers.addAll(compiler.getResolverSet());
    Locale locale = null;
    if (compiler.getGuild() != null) {
        locale = contextService.getLocale(compiler.getGuild());
        resolvers.add(GuildPlaceholderResolver.of(compiler.getGuild(), locale, applicationContext, "server"));
    }
    if (compiler.getMember() != null) {
        if (locale == null) {
            locale = contextService.getLocale(compiler.getMember().getGuild());
        }
        resolvers.add(MemberPlaceholderResolver.of(compiler.getMember(), locale, applicationContext, "member"));
    }
    TextChannel channel = getTargetChannel(compiler);
    if (channel != null) {
        if (locale == null) {
            locale = contextService.getLocale(channel.getGuild());
        }
        resolvers.add(ChannelPlaceholderResolver.of(channel, locale, applicationContext, "channel"));
    }
    return new MessageTemplatePlaceholderResolver(resolvers);
}
 
Example #3
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 #4
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 #5
Source File: PropertySourcesPlaceholdersResolver.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
public PropertySourcesPlaceholdersResolver(Iterable<PropertySource<?>> sources,
                                           PropertyPlaceholderHelper helper) {
    this.sources = sources;
    this.helper = (helper != null) ? helper
            : new PropertyPlaceholderHelper(SystemPropertyUtils.PLACEHOLDER_PREFIX,
            SystemPropertyUtils.PLACEHOLDER_SUFFIX,
            SystemPropertyUtils.VALUE_SEPARATOR, true);
}
 
Example #6
Source File: MessageTemplatePlaceholderResolver.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String resolvePlaceholder(String placeholderName) {
    return values.computeIfAbsent(placeholderName, p -> {
        for (PropertyPlaceholderHelper.PlaceholderResolver resolver : resolvers) {
            String value = resolver.resolvePlaceholder(p);
            if (value != null) {
                return value;
            }
        }
        return null;
    });
}
 
Example #7
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 #8
Source File: StandaloneMockMvcBuilder.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public StaticStringValueResolver(final Map<String, String> values) {
	this.helper = new PropertyPlaceholderHelper("${", "}", ":", false);
	this.resolver = new PlaceholderResolver() {
		@Override
		public String resolvePlaceholder(String placeholderName) {
			return values.get(placeholderName);
		}
	};
}
 
Example #9
Source File: SimpleContentStoresBeanDefinitionEmitter.java    From alfresco-simple-content-stores with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void afterPropertiesSet()
{
    PropertyCheck.mandatory(this, "propertiesSource", this.propertiesSource);
    PropertyCheck.mandatory(this, "rootStoreProxyName", this.rootStoreProxyName);

    this.placeholderHelper = new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix, this.valueSeparator, true);
}
 
Example #10
Source File: PropertyPlaceholderConfigurer.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public PlaceholderResolvingStringValueResolver(Properties props) {
	this.helper = new PropertyPlaceholderHelper(
			placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
	this.resolver = new PropertyPlaceholderConfigurerResolver(props);
}
 
Example #11
Source File: AbstractPropertyResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {
	return new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix,
			this.valueSeparator, ignoreUnresolvablePlaceholders);
}
 
Example #12
Source File: PropertyPlaceholderConfigurer.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
public PlaceholderResolvingStringValueResolver(Properties props) {
	this.helper = new PropertyPlaceholderHelper(
			placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
	this.resolver = new PropertyPlaceholderConfigurerResolver(props);
}
 
Example #13
Source File: ContractDslSnippet.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
private String replacePlaceholders(
		PropertyPlaceholderHelper.PlaceholderResolver resolver, String input) {
	return this.propertyPlaceholderHelper.replacePlaceholders(input, resolver);
}
 
Example #14
Source File: MessageTemplatePlaceholderResolver.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
public MessageTemplatePlaceholderResolver(Collection<? extends PropertyPlaceholderHelper.PlaceholderResolver> resolvers) {
    this.resolvers = Set.copyOf(resolvers);
}
 
Example #15
Source File: MessageTemplatePlaceholderResolver.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
public MessageTemplatePlaceholderResolver(PropertyPlaceholderHelper.PlaceholderResolver... resolvers) {
    this(resolvers != null ? Arrays.asList(resolvers) : Collections.emptySet());
}
 
Example #16
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 #17
Source File: MessageTemplateCompiler.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
public MessageTemplateCompiler withResolver(PropertyPlaceholderHelper.PlaceholderResolver resolver) {
    this.resolverSet.add(resolver);
    return this;
}
 
Example #18
Source File: AbstractPropertyResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {
	return new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix,
			this.valueSeparator, ignoreUnresolvablePlaceholders);
}
 
Example #19
Source File: AbstractPropertyResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {
	return new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix,
			this.valueSeparator, ignoreUnresolvablePlaceholders);
}
 
Example #20
Source File: ConfigurationResolver.java    From vividus with Apache License 2.0 4 votes vote down vote up
private static PropertyPlaceholderHelper createPropertyPlaceholderHelper(boolean ignoreUnresolvablePlaceholders)
{
    return new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, PLACEHOLDER_VALUE_SEPARATOR,
            ignoreUnresolvablePlaceholders);
}
 
Example #21
Source File: AbstractPropertyResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {
	return new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix,
			this.valueSeparator, ignoreUnresolvablePlaceholders);
}
 
Example #22
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 #23
Source File: StandaloneMockMvcBuilder.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public StaticStringValueResolver(final Map<String, String> values) {
	this.helper = new PropertyPlaceholderHelper("${", "}", ":", false);
	this.resolver = values::get;
}
 
Example #24
Source File: PropertyPlaceholderConfigurer.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public PlaceholderResolvingStringValueResolver(Properties props) {
	this.helper = new PropertyPlaceholderHelper(
			placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
	this.resolver = new PropertyPlaceholderConfigurerResolver(props);
}
 
Example #25
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 #26
Source File: ConfigurationResolver.java    From vividus with Apache License 2.0 4 votes vote down vote up
public static ConfigurationResolver getInstance() throws IOException
{
    if (instance != null)
    {
        return instance;
    }

    PropertiesLoader propertiesLoader = new PropertiesLoader(BeanFactory.getResourcePatternResolver());

    Properties configurationProperties = propertiesLoader.loadFromSingleResource("configuration.properties");
    Properties overridingProperties = propertiesLoader.loadFromOptionalResource("overriding.properties");

    Properties properties = new Properties();
    properties.putAll(configurationProperties);
    properties.putAll(propertiesLoader.loadFromResourceTreeRecursively("defaults"));

    Multimap<String, String> configuration = assembleConfiguration(configurationProperties, overridingProperties);
    for (Entry<String, String> configurationEntry : configuration.entries())
    {
        properties.putAll(propertiesLoader.loadFromResourceTreeRecursively(configurationEntry.getKey(),
                configurationEntry.getValue()));
    }

    properties.putAll(propertiesLoader.loadFromResourceTreeRecursively(ROOT));

    Properties deprecatedProperties = propertiesLoader.loadFromResourceTreeRecursively("deprecated");
    DeprecatedPropertiesHandler deprecatedPropertiesHandler = new DeprecatedPropertiesHandler(
            deprecatedProperties, PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX);
    deprecatedPropertiesHandler.replaceDeprecated(properties);

    Properties overridingAndSystemProperties = new Properties();
    overridingAndSystemProperties.putAll(overridingProperties);
    overridingAndSystemProperties.putAll(System.getenv());
    overridingAndSystemProperties.putAll(loadFilteredSystemProperties());

    deprecatedPropertiesHandler.replaceDeprecated(overridingAndSystemProperties, properties);

    properties.putAll(overridingAndSystemProperties);

    resolveSpelExpressions(properties, true);

    PropertyPlaceholderHelper propertyPlaceholderHelper = createPropertyPlaceholderHelper(false);

    for (Entry<Object, Object> entry : properties.entrySet())
    {
        String key = (String) entry.getKey();
        String value = (String) entry.getValue();
        deprecatedPropertiesHandler.warnIfDeprecated(key, value);
        entry.setValue(propertyPlaceholderHelper.replacePlaceholders(value, properties::getProperty));
    }
    deprecatedPropertiesHandler.removeDeprecated(properties);
    resolveSpelExpressions(properties, false);
    processSystemProperties(properties);

    instance = new ConfigurationResolver(properties);
    return instance;
}
 
Example #27
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 #28
Source File: StandaloneMockMvcBuilder.java    From java-technology-stack with MIT License 4 votes vote down vote up
public StaticStringValueResolver(final Map<String, String> values) {
	this.helper = new PropertyPlaceholderHelper("${", "}", ":", false);
	this.resolver = values::get;
}
 
Example #29
Source File: PropertyPlaceholderConfigurer.java    From java-technology-stack with MIT License 4 votes vote down vote up
public PlaceholderResolvingStringValueResolver(Properties props) {
	this.helper = new PropertyPlaceholderHelper(
			placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
	this.resolver = new PropertyPlaceholderConfigurerResolver(props);
}
 
Example #30
Source File: PropertyPlaceholderConfigurer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public PlaceholderResolvingStringValueResolver(Properties props) {
	this.helper = new PropertyPlaceholderHelper(
			placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
	this.resolver = new PropertyPlaceholderConfigurerResolver(props);
}