Skip to content

Commit

Permalink
feat: root config path by default close #289 (#290)
Browse files Browse the repository at this point in the history
* feat: root config path by default resolved #289

* test: add null test
  • Loading branch information
observeralone authored Oct 27, 2022
1 parent e502bc6 commit d0a8dfd
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class StartBootstrap {
private StartBootstrap() {}

public static void premain(String args, Instrumentation inst) {
Bootstrap.start(args, inst);
public static void premain(String args, Instrumentation inst, String javaAgentJarPath) {
Bootstrap.start(args, inst, javaAgentJarPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@

package com.megaease.easeagent.config;

import com.megaease.easeagent.config.yaml.YamlReader;
import com.megaease.easeagent.log4j2.Logger;
import com.megaease.easeagent.log4j2.LoggerFactory;
import com.megaease.easeagent.plugin.utils.SystemEnv;
import com.megaease.easeagent.plugin.utils.common.JsonUtil;
import com.megaease.easeagent.plugin.utils.common.StringUtils;
import org.yaml.snakeyaml.parser.ParserException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class ConfigFactory {
Expand Down Expand Up @@ -88,59 +83,18 @@ static Map<String, String> updateEnvCfg() {
private ConfigFactory() {
}

private static boolean checkYaml(String filename) {
return filename.endsWith(".yaml") || filename.endsWith(".yml");
}

private static GlobalConfigs loadFromStream(InputStream in, String filename) throws IOException {
if (in != null) {
Map<String, String> map;
if (checkYaml(filename)) {
try {
map = new YamlReader().load(in).compress();
} catch (ParserException e) {
LOGGER.warn("Wrong Yaml format, load config file failure: {}", filename);
map = Collections.emptyMap();
}
} else {
map = extractPropsMap(in);
}
return new GlobalConfigs(map);
} else {
return new GlobalConfigs(Collections.emptyMap());
}
}

private static GlobalConfigs loadFromClasspath(ClassLoader classLoader, String file) {
try (InputStream in = classLoader.getResourceAsStream(file)) {
return loadFromStream(in, file);
} catch (IOException e) {
LOGGER.warn("Load config file:{} by classloader:{} failure: {}", file, classLoader.toString(), e);
}

return new GlobalConfigs(Collections.emptyMap());
}

public static GlobalConfigs loadFromFile(File file) {
try (FileInputStream in = new FileInputStream(file)) {
return loadFromStream(in, file.getAbsolutePath());
} catch (IOException e) {
LOGGER.warn("Load config file failure: {}", file.getAbsolutePath());
}
return new GlobalConfigs(Collections.emptyMap());
}

public static GlobalConfigs loadConfigs(String pathname, ClassLoader loader) {
// load property configuration file if exist
GlobalConfigs configs = ConfigFactory.loadFromClasspath(loader, CONFIG_PROP_FILE);
GlobalConfigs configs = loadDefaultConfigs(loader, CONFIG_PROP_FILE);

// load yaml configuration file if exist
GlobalConfigs yConfigs = ConfigFactory.loadFromClasspath(loader, CONFIG_YAML_FILE);
GlobalConfigs yConfigs = loadDefaultConfigs(loader, CONFIG_YAML_FILE);
configs.mergeConfigs(yConfigs);

// override by user special config file
if (StringUtils.isNotEmpty(pathname)) {
GlobalConfigs configsFromOuterFile = ConfigFactory.loadFromFile(new File(pathname));
GlobalConfigs configsFromOuterFile = ConfigLoader.loadFromFile(new File(pathname));
configs.mergeConfigs(configsFromOuterFile);
}

Expand All @@ -154,13 +108,12 @@ public static GlobalConfigs loadConfigs(String pathname, ClassLoader loader) {
return configs;
}

private static HashMap<String, String> extractPropsMap(InputStream in) throws IOException {
Properties properties = new Properties();
properties.load(in);
HashMap<String, String> map = new HashMap<>();
for (String one : properties.stringPropertyNames()) {
map.put(one, properties.getProperty(one));
private static GlobalConfigs loadDefaultConfigs(ClassLoader loader, String file) {
GlobalConfigs globalConfigs = JarFileConfigLoader.load(file);
if (globalConfigs != null) {
return globalConfigs;
}
return map;
return ConfigLoader.loadFromClasspath(loader, file);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.megaease.easeagent.config;

import com.megaease.easeagent.config.yaml.YamlReader;
import com.megaease.easeagent.log4j2.Logger;
import com.megaease.easeagent.log4j2.LoggerFactory;
import org.yaml.snakeyaml.parser.ParserException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ConfigLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigLoader.class);

private static boolean checkYaml(String filename) {
return filename.endsWith(".yaml") || filename.endsWith(".yml");
}

static GlobalConfigs loadFromFile(File file) {
try (FileInputStream in = new FileInputStream(file)) {
return ConfigLoader.loadFromStream(in, file.getAbsolutePath());
} catch (IOException e) {
LOGGER.warn("Load config file failure: {}", file.getAbsolutePath());
}
return new GlobalConfigs(Collections.emptyMap());
}

static GlobalConfigs loadFromStream(InputStream in, String filename) throws IOException {
if (in != null) {
Map<String, String> map;
if (checkYaml(filename)) {
try {
map = new YamlReader().load(in).compress();
} catch (ParserException e) {
LOGGER.warn("Wrong Yaml format, load config file failure: {}", filename);
map = Collections.emptyMap();
}
} else {
map = extractPropsMap(in);
}
return new GlobalConfigs(map);
} else {
return new GlobalConfigs(Collections.emptyMap());
}
}

private static HashMap<String, String> extractPropsMap(InputStream in) throws IOException {
Properties properties = new Properties();
properties.load(in);
HashMap<String, String> map = new HashMap<>();
for (String one : properties.stringPropertyNames()) {
map.put(one, properties.getProperty(one));
}
return map;
}

static GlobalConfigs loadFromClasspath(ClassLoader classLoader, String file) {
try (InputStream in = classLoader.getResourceAsStream(file)) {
return ConfigLoader.loadFromStream(in, file);
} catch (IOException e) {
LOGGER.warn("Load config file:{} by classloader:{} failure: {}", file, classLoader.toString(), e);
}

return new GlobalConfigs(Collections.emptyMap());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.megaease.easeagent.config;

import com.megaease.easeagent.log4j2.Logger;
import com.megaease.easeagent.log4j2.LoggerFactory;
import com.megaease.easeagent.plugin.api.config.ConfigConst;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

public class JarFileConfigLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(JarFileConfigLoader.class);

static GlobalConfigs load(String file) {
String agentJarPath = System.getProperty(ConfigConst.AGENT_JAR_PATH);
if (agentJarPath == null) {
return null;
}
try {
JarFile jarFile = new JarFile(new File(agentJarPath));
ZipEntry zipEntry = jarFile.getEntry(file);
if (zipEntry == null) {
return null;
}
try (InputStream in = jarFile.getInputStream(zipEntry)) {
return ConfigLoader.loadFromStream(in, file);
} catch (IOException e) {
LOGGER.debug("Load config file:{} failure: {}", file, e);
}
} catch (IOException e) {
LOGGER.debug("create JarFile:{} failure: {}", agentJarPath, e);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.megaease.easeagent.config;

import com.megaease.easeagent.plugin.api.config.ConfigConst;
import org.junit.After;
import org.junit.Test;

import java.io.File;
import java.net.URISyntaxException;

import static org.junit.Assert.*;

public class JarFileConfigLoaderTest {

@After
public void after() {
System.clearProperty(ConfigConst.AGENT_JAR_PATH);
}


@Test
public void load() throws URISyntaxException {
String fileName = "agent.properties";
assertNull(JarFileConfigLoader.load(null));
assertNull(JarFileConfigLoader.load(fileName));
String jarPath = new File(this.getClass().getClassLoader().getResource("easeagent_config.jar").toURI()).getPath();
System.setProperty(ConfigConst.AGENT_JAR_PATH, jarPath);

GlobalConfigs config = JarFileConfigLoader.load(fileName);
assertNotNull(config);
assertEquals("demo-jar-service", config.getString("name"));
assertEquals("demo-system", config.getString("system"));

config = JarFileConfigLoader.load("agent.yaml");
assertNotNull(config);
assertEquals("test-jar-service", config.getString("name"));
assertEquals("demo-system", config.getString("system"));

config = JarFileConfigLoader.load("agentaaaa.yaml");
assertNull(config);


String nullJarPath = new File("easeagent_config_null.jar").getPath();
System.setProperty(ConfigConst.AGENT_JAR_PATH, nullJarPath);

config = JarFileConfigLoader.load("agent.yaml");
assertNull(config);

}
}
Binary file added config/src/test/resources/easeagent_config.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.megaease.easeagent.httpserver.nano.AgentHttpServer;
import com.megaease.easeagent.log4j2.Logger;
import com.megaease.easeagent.log4j2.LoggerFactory;
import com.megaease.easeagent.plugin.api.config.ConfigConst;
import com.megaease.easeagent.plugin.api.metric.MetricProvider;
import com.megaease.easeagent.plugin.api.middleware.RedirectProcessor;
import com.megaease.easeagent.plugin.api.trace.TracingProvider;
Expand Down Expand Up @@ -79,8 +80,9 @@ private Bootstrap() {
}

@SneakyThrows
public static void start(String args, Instrumentation inst) {
public static void start(String args, Instrumentation inst, String javaAgentJarPath) {
long begin = System.nanoTime();
System.setProperty(ConfigConst.AGENT_JAR_PATH, javaAgentJarPath);

// add bootstrap classes
Set<String> bootstrapClassSet = AppendBootstrapClassLoaderSearch.by(inst, ClassInjector.UsingInstrumentation.Target.BOOTSTRAP);
Expand Down
4 changes: 2 additions & 2 deletions loader/src/main/java/com/megaease/easeagent/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public static void premain(final String args, final Instrumentation inst) throws
switchLoggingProperty(loader, loggingProperty, () -> {
initAgentSlf4jMDC(loader);
loader.loadClass(bootstrap)
.getMethod("premain", String.class, Instrumentation.class)
.invoke(null, args, inst);
.getMethod("premain", String.class, Instrumentation.class, String.class)
.invoke(null, args, inst, jar.getPath());
return null;
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.megaease.easeagent.config;

import java.io.File;

public class MockConfigLoader {
public static GlobalConfigs loadFromFile(File file) {
return ConfigLoader.loadFromFile(file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package com.megaease.easeagent.mock.config;

import com.megaease.easeagent.config.ConfigFactory;
import com.megaease.easeagent.config.Configs;
import com.megaease.easeagent.config.GlobalConfigs;
import com.megaease.easeagent.config.MockConfigLoader;
import com.megaease.easeagent.config.PluginConfigManager;

import java.io.File;
Expand Down Expand Up @@ -50,7 +50,7 @@ public class MockConfig {
url = classLoader.getResource(MOCK_CONFIG_PROP_FILE);
}
if (url != null) {
GlobalConfigs configsFromOuterFile = ConfigFactory.loadFromFile(new File(url.getFile()));
GlobalConfigs configsFromOuterFile = MockConfigLoader.loadFromFile(new File(url.getFile()));
CONFIGS.mergeConfigs(configsFromOuterFile);
}
PLUGIN_CONFIG_MANAGER = PluginConfigManager.builder(CONFIGS).build();
Expand Down
Loading

0 comments on commit d0a8dfd

Please sign in to comment.