From 42ca1570005d04d9c62d0b9e9cf2eb017309e989 Mon Sep 17 00:00:00 2001 From: caoguo Date: Fri, 28 Apr 2017 17:15:15 +0800 Subject: [PATCH] updates for placeholder --- .../client/support/DisconfAutowareConfig.java | 14 ++++--- .../client/support/PlaceholderResolver.java | 10 +++++ ...PropertyPlaceholderConfigurerResolver.java | 39 +++++++++++++++++++ .../support/PropertyPlaceholderHelper.java | 26 +++++++++++++ 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 disconf-client/src/main/java/com/baidu/disconf/client/support/PlaceholderResolver.java create mode 100644 disconf-client/src/main/java/com/baidu/disconf/client/support/PropertyPlaceholderConfigurerResolver.java create mode 100644 disconf-client/src/main/java/com/baidu/disconf/client/support/PropertyPlaceholderHelper.java diff --git a/disconf-client/src/main/java/com/baidu/disconf/client/support/DisconfAutowareConfig.java b/disconf-client/src/main/java/com/baidu/disconf/client/support/DisconfAutowareConfig.java index 8a92b75d7..34bd56c58 100644 --- a/disconf-client/src/main/java/com/baidu/disconf/client/support/DisconfAutowareConfig.java +++ b/disconf-client/src/main/java/com/baidu/disconf/client/support/DisconfAutowareConfig.java @@ -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 { @@ -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.")) { diff --git a/disconf-client/src/main/java/com/baidu/disconf/client/support/PlaceholderResolver.java b/disconf-client/src/main/java/com/baidu/disconf/client/support/PlaceholderResolver.java new file mode 100644 index 000000000..dd5ffc98e --- /dev/null +++ b/disconf-client/src/main/java/com/baidu/disconf/client/support/PlaceholderResolver.java @@ -0,0 +1,10 @@ +package com.baidu.disconf.client.support; + +public interface PlaceholderResolver { + + String placeholderPrefix = "${"; + + String placeholderSuffix = "}"; + + String resolvePlaceholder(String placeholderName); +} diff --git a/disconf-client/src/main/java/com/baidu/disconf/client/support/PropertyPlaceholderConfigurerResolver.java b/disconf-client/src/main/java/com/baidu/disconf/client/support/PropertyPlaceholderConfigurerResolver.java new file mode 100644 index 000000000..b7261f5c4 --- /dev/null +++ b/disconf-client/src/main/java/com/baidu/disconf/client/support/PropertyPlaceholderConfigurerResolver.java @@ -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; + } + +} diff --git a/disconf-client/src/main/java/com/baidu/disconf/client/support/PropertyPlaceholderHelper.java b/disconf-client/src/main/java/com/baidu/disconf/client/support/PropertyPlaceholderHelper.java new file mode 100644 index 000000000..ea8e8d4c6 --- /dev/null +++ b/disconf-client/src/main/java/com/baidu/disconf/client/support/PropertyPlaceholderHelper.java @@ -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(); + } +}