Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates for placeholder #301

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
* @version 2014-6-6
*/
public final class DisconfAutowareConfig {

private DisconfAutowareConfig() {

}

protected static final Logger LOGGER = LoggerFactory.getLogger(DisconfAutowareConfig.class);

/**

private static PlaceholderResolver resolver = new PropertyPlaceholderConfigurerResolver();

private static PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper();

/**
* 先用TOMCAT模式进行导入配置文件,若找不到,则用项目目录模式进行导入
*/
private static Properties getProperties(final String propertyFilePath) throws Exception {
Expand Down Expand Up @@ -127,7 +131,7 @@ private static void autowareConfig(final Object obj, Properties prop) throws Exc

String defaultValue = config.defaultValue();
value = prop.getProperty(name, defaultValue);

value = propertyPlaceholderHelper.parseStringValue(value, resolver);
// using disconf as prefix to avoid env confusion
if (value.equals(defaultValue) && name != null) {
if (name.contains("disconf.")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baidu.disconf.client.support;

public interface PlaceholderResolver {

String placeholderPrefix = "${";

String placeholderSuffix = "}";

String resolvePlaceholder(String placeholderName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.baidu.disconf.client.support;

import java.util.Properties;

public class PropertyPlaceholderConfigurerResolver implements PlaceholderResolver{

private Properties props;

public PropertyPlaceholderConfigurerResolver(){
//
}

public PropertyPlaceholderConfigurerResolver(Properties props) {
this.props = props;
}

public String resolvePlaceholder(String placeholderName) {
String propVal = null;
if(this.props != null){
propVal = this.props.getProperty(placeholderName);
}
if(propVal == null){
propVal = System.getProperty(placeholderName);
}
if (propVal == null) {
propVal = System.getenv(placeholderName);
}
return propVal;
}

public Properties getProps() {
return props;
}

public void setProps(Properties props) {
this.props = props;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baidu.disconf.client.support;

public class PropertyPlaceholderHelper {

public String replacePlaceholders(String value, PlaceholderResolver placeholderResolver) {
return parseStringValue(value, placeholderResolver);
}

protected String parseStringValue(String strVal, PlaceholderResolver placeholderResolver) {

StringBuilder result = new StringBuilder(strVal);

int startIndex = strVal.indexOf(PlaceholderResolver.placeholderPrefix);
if (startIndex != -1) {
int endIndex = result.indexOf(PlaceholderResolver.placeholderSuffix);
if (endIndex != -1) {
String placeholder = result.substring(startIndex + PlaceholderResolver.placeholderPrefix.length(), endIndex);
String propVal = placeholderResolver.resolvePlaceholder(placeholder);
if (propVal != null) {
result.replace(startIndex, endIndex + PlaceholderResolver.placeholderSuffix.length(), propVal);
}
}
}
return result.toString();
}
}