未验证 提交 7066affa 编写于 作者: wu-sheng's avatar wu-sheng 提交者: GitHub

Merge branch 'master' into len

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