diff --git a/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/PropertyPlaceholderHelper.java b/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/PropertyPlaceholderHelper.java index 41957f4de1ec61e6419ce311a62bd6e9a5a276b1..10de12200c8e7afd210051cc37c055708e11c965 100644 --- a/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/PropertyPlaceholderHelper.java +++ b/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/PropertyPlaceholderHelper.java @@ -29,14 +29,11 @@ import java.util.Set; * ${name}}. Using {@code PropertyPlaceholderHelper} these placeholders can be substituted for user-supplied values.

* Values for substitution can be supplied using a {@link Properties} instance or using a {@link PlaceholderResolver}. */ -public class PropertyPlaceholderHelper { - private static final Map WELL_KNOWN_SIMPLE_PREFIXES = new HashMap(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 wellKnownSimplePrefixes = new HashMap(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); } }); diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java index 0c4b8ba725dcf911844e1b44028c9240e7b554ce..f17336b811fec76582015617cff22e51c98bfec0 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java @@ -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. *

* 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` *

- * 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. */ diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializerTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializerTest.java index 305466ef2ce9a71dc00f0a8cd9db278e4a166659..0ed5d4e5279d0643897ae68f1354ab7a1e66a98b 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializerTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializerTest.java @@ -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); diff --git a/oap-server/server-library/library-util/src/test/java/org/apache/skywalking/oap/server/library/util/PropertyPlaceholderHelperTest.java b/oap-server/server-library/library-util/src/test/java/org/apache/skywalking/oap/server/library/util/PropertyPlaceholderHelperTest.java index 6c72092f60404eb50ca3b2ccc24bd64cb68de23f..bd88be2d1af4e7a2f5c4ac4bcd6d8064a72343bd 100644 --- a/oap-server/server-library/library-util/src/test/java/org/apache/skywalking/oap/server/library/util/PropertyPlaceholderHelperTest.java +++ b/oap-server/server-library/library-util/src/test/java/org/apache/skywalking/oap/server/library/util/PropertyPlaceholderHelperTest.java @@ -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 diff --git a/oap-server/server-starter/src/main/java/org/apache/skywalking/oap/server/starter/config/ApplicationConfigLoader.java b/oap-server/server-starter/src/main/java/org/apache/skywalking/oap/server/starter/config/ApplicationConfigLoader.java index 80e965902a1167ed7d097974188a71fdfd598f48..fdc2e7fe616ce1ba46a0652d7f1f1f4e0525de8d 100644 --- a/oap-server/server-starter/src/main/java/org/apache/skywalking/oap/server/starter/config/ApplicationConfigLoader.java +++ b/oap-server/server-starter/src/main/java/org/apache/skywalking/oap/server/starter/config/ApplicationConfigLoader.java @@ -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 { 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); });