This commit is contained in:
hengyunabc 2021-07-08 16:35:29 +08:00
parent 2ab1bca5f0
commit 43b5b05669
2 changed files with 50 additions and 9 deletions

View File

@ -292,24 +292,25 @@ public class ArthasBootstrap {
return ARTHAS_HOME;
}
static String reslove(ArthasEnvironment arthasEnvironment, String key, String defaultValue) {
String value = arthasEnvironment.getProperty(key);
if (value == null) {
return defaultValue;
}
return arthasEnvironment.resolvePlaceholders(value);
}
// try to load arthas.properties
private void tryToLoadArthasProperties() throws IOException {
this.arthasEnvironment.resolvePlaceholders(CONFIG_LOCATION_PROPERTY);
String location = null;
if (arthasEnvironment.containsProperty(CONFIG_LOCATION_PROPERTY)) {
location = arthasEnvironment.resolvePlaceholders(arthasEnvironment.getProperty(CONFIG_LOCATION_PROPERTY));
}
String location = reslove(arthasEnvironment, CONFIG_LOCATION_PROPERTY, null);
if (location == null) {
location = arthasHome();
}
String configName = "arthas";
if (arthasEnvironment.containsProperty(CONFIG_NAME_PROPERTY)) {
configName = arthasEnvironment.resolvePlaceholders(arthasEnvironment.getProperty(CONFIG_NAME_PROPERTY));
}
String configName = reslove(arthasEnvironment, CONFIG_NAME_PROPERTY, "arthas");
if (location != null) {
if (!location.endsWith(".properties")) {

View File

@ -13,6 +13,7 @@ import org.mockito.internal.util.reflection.FieldSetter;
import com.taobao.arthas.common.JavaVersionUtils;
import com.taobao.arthas.core.bytecode.TestHelper;
import com.taobao.arthas.core.config.Configure;
import com.taobao.arthas.core.env.ArthasEnvironment;
import net.bytebuddy.agent.ByteBuddyAgent;
@ -60,4 +61,43 @@ public class ArthasBootstrapTest {
System.err.println(loadClass);
}
@Test
public void testConfigLocationNull() throws Exception {
ArthasEnvironment arthasEnvironment = new ArthasEnvironment();
String location = ArthasBootstrap.reslove(arthasEnvironment, ArthasBootstrap.CONFIG_LOCATION_PROPERTY, null);
assertThat(location).isEqualTo(null);
}
@Test
public void testConfigLocation() throws Exception {
ArthasEnvironment arthasEnvironment = new ArthasEnvironment();
System.setProperty("hhhh", "fff");
System.setProperty(ArthasBootstrap.CONFIG_LOCATION_PROPERTY, "test${hhhh}");
String location = ArthasBootstrap.reslove(arthasEnvironment, ArthasBootstrap.CONFIG_LOCATION_PROPERTY, null);
System.clearProperty("hhhh");
System.clearProperty(ArthasBootstrap.CONFIG_LOCATION_PROPERTY);
assertThat(location).isEqualTo("test" + "fff");
}
@Test
public void testConfigNameDefault() throws Exception {
ArthasEnvironment arthasEnvironment = new ArthasEnvironment();
String configName = ArthasBootstrap.reslove(arthasEnvironment, ArthasBootstrap.CONFIG_NAME_PROPERTY, "arthas");
assertThat(configName).isEqualTo("arthas");
}
@Test
public void testConfigName() throws Exception {
ArthasEnvironment arthasEnvironment = new ArthasEnvironment();
System.setProperty(ArthasBootstrap.CONFIG_NAME_PROPERTY, "testName");
String configName = ArthasBootstrap.reslove(arthasEnvironment, ArthasBootstrap.CONFIG_NAME_PROPERTY, "arthas");
System.clearProperty(ArthasBootstrap.CONFIG_NAME_PROPERTY);
assertThat(configName).isEqualTo("testName");
}
}