mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
[Refactor] Update configuration handling in JarLoader and enhance Config class
This commit is contained in:
1 parent
3fe9f5a573
commit
9a2dd217f1
5 files changed
+102
-4
No files matched your search
@@ -11,5 +11,9 @@
|
||||
|
||||
## 2026-04-17
|
||||
- [ ] 重构项目为模块化项目
|
||||
- [ ] `libloader` 模块只负责加载`JuiceAgent-API`和`JuiceAgent`, Jar文件由`JuiceAgent`接管
|
||||
- [ ] `libloader` 读取配置文件并传参`JuiceAgentNativePath`和`ConfigDir`到 `JuiceAgent` 中,由`JuiceAgent`读取配置文件继续进行操作
|
||||
- [x] `libloader` 模块只负责加载`JuiceAgent-API`和`JuiceAgent`, Jar文件由`JuiceAgent`接管
|
||||
- [x] `libloader` 读取配置文件并传参`JuiceAgentNativePath`和`ConfigDir`到 `JuiceAgent` 中,由`JuiceAgent`读取配置文件继续进行操作
|
||||
|
||||
## 2026-04-19
|
||||
- [ ] 在 `JuiceAgent/Utils.hpp`做序列化操作
|
||||
- [ ] 统一调用 module的Java实现
|
||||
@@ -64,7 +64,7 @@ public:
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get(const std::string& path, const T& default_value) const {
|
||||
T get(const std::string& path, const T& default_value, bool is_path = false) const {
|
||||
if (!_valid) {
|
||||
return default_value;
|
||||
}
|
||||
@@ -89,7 +89,33 @@ public:
|
||||
start = end + 1;
|
||||
}
|
||||
|
||||
return _read_value<T>(*current);
|
||||
T value = _read_value<T>(*current);
|
||||
|
||||
if constexpr (std::is_same_v<T, std::string>) {
|
||||
if (value.empty()) {
|
||||
return default_value;
|
||||
}
|
||||
|
||||
// Handle relative paths
|
||||
if (is_path) {
|
||||
// Check if starts with "."
|
||||
if (value.starts_with(".")) {
|
||||
// Remove "."
|
||||
value = value.substr(1);
|
||||
// Check if starts with "/"
|
||||
if (value.starts_with("/")) {
|
||||
// Remove "/"
|
||||
value = value.substr(1);
|
||||
}
|
||||
|
||||
// runtime_dir + value
|
||||
value = (_runtime_dir / value).string();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
PLOGW << "Config get failed: " << path << ", using default value";
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace JuiceAgent {
|
||||
private:
|
||||
JavaVM* jvm;
|
||||
jvmtiEnv* jvmti;
|
||||
JNIEnv* env;
|
||||
EventBus eventbus;
|
||||
JuiceAgent::Config::Config config;
|
||||
JuiceAgent::Core::Modules::ModuleManager module_manager;
|
||||
@@ -33,12 +34,14 @@ namespace JuiceAgent {
|
||||
// getters
|
||||
JavaVM* get_jvm() const { return jvm; }
|
||||
jvmtiEnv* get_jvmti() const { return jvmti; }
|
||||
JNIEnv* get_env() const { return env; }
|
||||
EventBus& get_eventbus() { return eventbus; }
|
||||
JuiceAgent::Config::Config& get_config() { return config; }
|
||||
|
||||
// setters
|
||||
void set_jvm(JavaVM* jvm) { this->jvm = jvm; }
|
||||
void set_jvmti(jvmtiEnv* jvmti) { this->jvmti = jvmti; }
|
||||
void set_env(JNIEnv* env) { this->env = env; }
|
||||
void set_config(JuiceAgent::Config::Config& config) { this->config = config; }
|
||||
};
|
||||
}
|
||||
@@ -38,6 +38,7 @@ namespace JuiceAgent {
|
||||
|
||||
set_jvm(jvm);
|
||||
set_jvmti(jvmti);
|
||||
set_env(env);
|
||||
set_config(cfg);
|
||||
PLOGD << "env: " << env << ", jvmti: " << jvmti;
|
||||
|
||||
|
||||
@@ -1,12 +1,44 @@
|
||||
#include <modules/Modules.hpp>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <libagent.hpp>
|
||||
#include <JuiceAgent/Utils.hpp>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
|
||||
struct _Config {
|
||||
bool Enabled = true;
|
||||
std::string InjectionDir;
|
||||
std::string JarPath;
|
||||
std::string EntryClass;
|
||||
std::string EntryMethod;
|
||||
} Config;
|
||||
|
||||
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
|
||||
|
||||
static const char* MODULE_CLASS = "cn/xiaozhou233/juiceagent/api/modules/JarLoader";
|
||||
static const char* MODULE_METHOD = "loadJar";
|
||||
static const char* MODULE_METHOD_DESC = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V";
|
||||
|
||||
bool JarLoaderModule::init() {
|
||||
if (_initialized) return true;
|
||||
|
||||
PLOGI << "JarLoader init";
|
||||
|
||||
JuiceAgent::Config::Config& cfg = agent.get_config();
|
||||
|
||||
Config.Enabled = cfg.get<bool>("JuiceAgent.Modules.JarLoader.Enabled", false);
|
||||
Config.InjectionDir = cfg.get<std::string>("JuiceAgent.Modules.JarLoader.InjectionDir", (agent.get_config().runtime_dir() / "injection").string());
|
||||
Config.JarPath = cfg.get<std::string>("JuiceAgent.Modules.JarLoader.JarPath", "./Entry.jar", true);
|
||||
Config.EntryClass = cfg.get<std::string>("JuiceAgent.Modules.JarLoader.EntryClass", "Example.Main", false);
|
||||
Config.EntryMethod = cfg.get<std::string>("JuiceAgent.Modules.JarLoader.EntryMethod", "start", false);
|
||||
|
||||
PLOGD << ">>> JarLoader Config <<<";
|
||||
PLOGD << "Enabled: " << Config.Enabled;
|
||||
PLOGD << "InjectionDir: " << Config.InjectionDir;
|
||||
PLOGD << "JarPath: " << Config.JarPath;
|
||||
PLOGD << "EntryClass: " << Config.EntryClass;
|
||||
PLOGD << "EntryMethod: " << Config.EntryMethod;
|
||||
PLOGD << ">>> JarLoader Config <<<";
|
||||
_initialized = true;
|
||||
return true;
|
||||
}
|
||||
@@ -16,6 +48,38 @@ bool JarLoaderModule::start() {
|
||||
if (_running) return true;
|
||||
|
||||
PLOGI << "JarLoader start";
|
||||
|
||||
if (!Config.Enabled) {
|
||||
PLOGI << "JarLoader is disabled";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Config.InjectionDir.empty() || Config.JarPath.empty() || Config.EntryClass.empty() || Config.EntryMethod.empty()) {
|
||||
PLOGE << "JarLoader config is invalid";
|
||||
return false;
|
||||
}
|
||||
|
||||
JNIEnv* env = agent.get_env();
|
||||
jclass cls = env->FindClass(MODULE_CLASS);
|
||||
if (!cls) {
|
||||
PLOGE << "JarLoader class not found";
|
||||
return false;
|
||||
}
|
||||
|
||||
jmethodID mid = env->GetStaticMethodID(cls, MODULE_METHOD, MODULE_METHOD_DESC);
|
||||
if (!mid) {
|
||||
PLOGE << "JarLoader method not found";
|
||||
return false;
|
||||
}
|
||||
|
||||
env->CallStaticVoidMethod(cls, mid,
|
||||
env->NewStringUTF(Config.InjectionDir.c_str()),
|
||||
env->NewStringUTF(Config.JarPath.c_str()),
|
||||
env->NewStringUTF(Config.EntryClass.c_str()),
|
||||
env->NewStringUTF(Config.EntryMethod.c_str()));
|
||||
check_and_clear_exception(env, "Invoke JarLoader");
|
||||
|
||||
PLOGI << "JarLoader is running";
|
||||
_running = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user