refactoring PropertyPlaceholderHelper class. (#1989)
This commit is contained in:
parent
93a036499e
commit
dc393b1c12
|
|
@ -29,14 +29,11 @@ import java.util.Set;
|
|||
* ${name}}. Using {@code PropertyPlaceholderHelper} these placeholders can be substituted for user-supplied values. <p>
|
||||
* Values for substitution can be supplied using a {@link Properties} instance or using a {@link PlaceholderResolver}.
|
||||
*/
|
||||
public class PropertyPlaceholderHelper {
|
||||
private static final Map<String, String> WELL_KNOWN_SIMPLE_PREFIXES = new HashMap<String, String>(4);
|
||||
public enum PropertyPlaceholderHelper {
|
||||
|
||||
static {
|
||||
WELL_KNOWN_SIMPLE_PREFIXES.put("}", "{");
|
||||
WELL_KNOWN_SIMPLE_PREFIXES.put("]", "[");
|
||||
WELL_KNOWN_SIMPLE_PREFIXES.put(")", "(");
|
||||
}
|
||||
INSTANCE(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX,
|
||||
PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX,
|
||||
PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true);
|
||||
|
||||
private final String placeholderPrefix;
|
||||
|
||||
|
|
@ -58,14 +55,21 @@ public class PropertyPlaceholderHelper {
|
|||
* @param ignoreUnresolvablePlaceholders indicates whether unresolvable placeholders should be ignored ({@code
|
||||
* true}) or cause an exception ({@code false})
|
||||
*/
|
||||
public PropertyPlaceholderHelper(String placeholderPrefix, String placeholderSuffix,
|
||||
PropertyPlaceholderHelper(String placeholderPrefix, String placeholderSuffix,
|
||||
String valueSeparator, boolean ignoreUnresolvablePlaceholders) {
|
||||
if (StringUtil.isEmpty(placeholderPrefix) || StringUtil.isEmpty(placeholderSuffix)) {
|
||||
throw new UnsupportedOperationException("'placeholderPrefix or placeholderSuffix' must not be null");
|
||||
}
|
||||
|
||||
final Map<String, String> wellKnownSimplePrefixes = new HashMap<String, String>(4);
|
||||
|
||||
wellKnownSimplePrefixes.put("}", "{");
|
||||
wellKnownSimplePrefixes.put("]", "[");
|
||||
wellKnownSimplePrefixes.put(")", "(");
|
||||
|
||||
this.placeholderPrefix = placeholderPrefix;
|
||||
this.placeholderSuffix = placeholderSuffix;
|
||||
String simplePrefixForSuffix = WELL_KNOWN_SIMPLE_PREFIXES.get(this.placeholderSuffix);
|
||||
String simplePrefixForSuffix = wellKnownSimplePrefixes.get(this.placeholderSuffix);
|
||||
if (simplePrefixForSuffix != null && this.placeholderPrefix.endsWith(simplePrefixForSuffix)) {
|
||||
this.simplePrefix = simplePrefixForSuffix;
|
||||
} else {
|
||||
|
|
@ -85,7 +89,8 @@ public class PropertyPlaceholderHelper {
|
|||
*/
|
||||
public String replacePlaceholders(String value, final Properties properties) {
|
||||
return replacePlaceholders(value, new PlaceholderResolver() {
|
||||
@Override public String resolvePlaceholder(String placeholderName) {
|
||||
@Override
|
||||
public String resolvePlaceholder(String placeholderName) {
|
||||
return PropertyPlaceholderHelper.this.getConfigValue(placeholderName, properties);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -34,7 +34,6 @@ import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath;
|
|||
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
|
||||
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
|
||||
import org.apache.skywalking.apm.util.ConfigInitializer;
|
||||
import org.apache.skywalking.apm.util.PlaceholderConfigurerSupport;
|
||||
import org.apache.skywalking.apm.util.PropertyPlaceholderHelper;
|
||||
import org.apache.skywalking.apm.util.StringUtil;
|
||||
|
||||
|
|
@ -51,8 +50,9 @@ public class SnifferConfigInitializer {
|
|||
private static boolean IS_INIT_COMPLETED = false;
|
||||
|
||||
/**
|
||||
* If the specified agent config path is set, the agent will try to locate the specified agent config.
|
||||
* If the specified agent config path is not set , the agent will try to locate `agent.config`, which should be in the /config dictionary of agent package.
|
||||
* If the specified agent config path is set, the agent will try to locate the specified agent config. If the
|
||||
* specified agent config path is not set , the agent will try to locate `agent.config`, which should be in the
|
||||
* /config dictionary of agent package.
|
||||
* <p>
|
||||
* Also try to override the config by system.env and system.properties. All the keys in these two places should
|
||||
* start with {@link #ENV_KEY_PREFIX}. e.g. in env `skywalking.agent.application_code=yourAppName` to override
|
||||
|
|
@ -67,14 +67,10 @@ public class SnifferConfigInitializer {
|
|||
configFileStream = loadConfig();
|
||||
Properties properties = new Properties();
|
||||
properties.load(configFileStream);
|
||||
PropertyPlaceholderHelper helper =
|
||||
new PropertyPlaceholderHelper(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX,
|
||||
PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX,
|
||||
PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true);
|
||||
for (String key : properties.stringPropertyNames()) {
|
||||
String value = (String)properties.get(key);
|
||||
//replace the key's value. properties.replace(key,value) in jdk8+
|
||||
properties.put(key, helper.replacePlaceholders(value, properties));
|
||||
properties.put(key, PropertyPlaceholderHelper.INSTANCE.replacePlaceholders(value, properties));
|
||||
}
|
||||
ConfigInitializer.initialize(properties, Config.class);
|
||||
} catch (Exception e) {
|
||||
|
|
@ -153,11 +149,10 @@ public class SnifferConfigInitializer {
|
|||
}
|
||||
|
||||
/**
|
||||
* Override the config by system properties. The env key must start with `skywalking`, the reuslt should be as same as in
|
||||
* `agent.config`
|
||||
* Override the config by system properties. The env key must start with `skywalking`, the reuslt should be as same
|
||||
* as in `agent.config`
|
||||
* <p>
|
||||
* such as:
|
||||
* Env key of `agent.application_code` shoule be `skywalking.agent.application_code`
|
||||
* such as: Env key of `agent.application_code` shoule be `skywalking.agent.application_code`
|
||||
*
|
||||
* @return the config file {@link InputStream}, or null if not needEnhance.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ import java.util.Properties;
|
|||
import org.apache.skywalking.apm.agent.core.boot.AgentPackageNotFoundException;
|
||||
import org.apache.skywalking.apm.agent.core.logging.core.LogLevel;
|
||||
import org.apache.skywalking.apm.util.ConfigInitializer;
|
||||
import org.apache.skywalking.apm.util.PlaceholderConfigurerSupport;
|
||||
import org.apache.skywalking.apm.util.PropertyPlaceholderHelper;
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
|
|
@ -81,11 +80,7 @@ public class SnifferConfigInitializerTest {
|
|||
properties.put("agent.service_name", "${AGENT_SERVICE_NAME:testAppFromSystem}");
|
||||
properties.put("collector.backend_service", "${AGENT_COLLECTOR_SERVER:127.0.0.1:8090}");
|
||||
properties.put("logging.level", "INFO");
|
||||
|
||||
PropertyPlaceholderHelper placeholderHelper =
|
||||
new PropertyPlaceholderHelper(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX,
|
||||
PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX,
|
||||
PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true);
|
||||
PropertyPlaceholderHelper placeholderHelper = PropertyPlaceholderHelper.INSTANCE;
|
||||
properties.put("agent.service_name", placeholderHelper.replacePlaceholders((String)properties.get("agent.service_name"), properties));
|
||||
properties.put("collector.backend_service", placeholderHelper.replacePlaceholders((String)properties.get("collector.backend_service"), properties));
|
||||
ConfigInitializer.initialize(properties, Config.class);
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ import java.io.FileNotFoundException;
|
|||
import java.io.Reader;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import org.apache.skywalking.apm.util.PlaceholderConfigurerSupport;
|
||||
import org.apache.skywalking.apm.util.PropertyPlaceholderHelper;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
|
|
@ -63,10 +62,7 @@ public class PropertyPlaceholderHelperTest {
|
|||
}
|
||||
});
|
||||
}
|
||||
placeholderHelper =
|
||||
new PropertyPlaceholderHelper(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX,
|
||||
PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX,
|
||||
PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true);
|
||||
placeholderHelper = PropertyPlaceholderHelper.INSTANCE;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -22,10 +22,9 @@ import java.io.FileNotFoundException;
|
|||
import java.io.Reader;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import org.apache.skywalking.apm.util.PropertyPlaceholderHelper;
|
||||
import org.apache.skywalking.oap.server.library.module.ApplicationConfiguration;
|
||||
import org.apache.skywalking.oap.server.library.util.CollectionUtils;
|
||||
import org.apache.skywalking.apm.util.PlaceholderConfigurerSupport;
|
||||
import org.apache.skywalking.apm.util.PropertyPlaceholderHelper;
|
||||
import org.apache.skywalking.oap.server.library.util.ResourceUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
|
@ -68,11 +67,8 @@ public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfigur
|
|||
if (propertiesConfig != null) {
|
||||
propertiesConfig.forEach((key, value) -> {
|
||||
properties.put(key, value);
|
||||
PropertyPlaceholderHelper helper =
|
||||
new PropertyPlaceholderHelper(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX,
|
||||
PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX,
|
||||
PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true);
|
||||
final Object replaceValue = yaml.load(helper.replacePlaceholders(value + "", properties));
|
||||
final Object replaceValue = yaml.load(PropertyPlaceholderHelper.INSTANCE
|
||||
.replacePlaceholders(value + "", properties));
|
||||
properties.replace(key, replaceValue);
|
||||
logger.info("The property with key: {}, value: {}, in {} provider", key, replaceValue.toString(), name);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue