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);
}
}
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");
}
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);
}
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);
}
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");
}
Aggregations