feat: Refactor module management and introduce ModuleRegistry for dynamic module loading

This commit is contained in:
xiaozhou233 committed 2026-04-22 13:19:39 +08:00
1 parent 08ec414094
commit 50eb73d4e8
6 files changed
+170 -97

No files matched your search

@@ -12,7 +12,7 @@ public:
ModuleManager() = default;
~ModuleManager() = default;
void register_module(std::unique_ptr<IModule> module);
void load_all();
bool init_all();
bool start_all();
@@ -0,0 +1,56 @@
#pragma once
#include <vector>
#include <memory>
#include <functional>
#include <string>
#include <modules/IModule.hpp>
namespace JuiceAgent::Core::Modules {
class ModuleRegistry {
public:
using Factory = std::function<std::unique_ptr<IModule>()>;
struct Entry {
std::string name;
Factory factory;
};
static ModuleRegistry& instance() {
static ModuleRegistry inst;
return inst;
}
bool add(const std::string& name, Factory factory) {
for (const auto& item : _entries) {
if (item.name == name) {
return false;
}
}
_entries.push_back({name, std::move(factory)});
return true;
}
const std::vector<Entry>& entries() const {
return _entries;
}
private:
std::vector<Entry> _entries;
};
}
#define JUICEAGENT_REGISTER_MODULE(TYPE, NAME) \
namespace { \
const bool registered_##NAME = \
JuiceAgent::Core::Modules::ModuleRegistry::instance().add( \
#NAME, \
[]() -> std::unique_ptr<JuiceAgent::Core::Modules::IModule> { \
return std::make_unique<TYPE>(); \
} \
); \
}
-25
View File
@@ -1,25 +0,0 @@
#pragma once
#include <modules/IModule.hpp>
namespace JuiceAgent::Core::Modules {
class JarLoaderModule : public IModule {
public:
JarLoaderModule() = default;
~JarLoaderModule() override = default;
std::string name() const override {
return "JarLoader";
}
bool init() override;
bool start() override;
void stop() override;
private:
bool _initialized = false;
bool _running = false;
};
}
+3 -3
View File
@@ -4,7 +4,6 @@
#include <event/event_type.hpp>
#include <event/eventbus.hpp>
#include <JuiceAgent/Config.hpp>
#include <modules/Modules.hpp>
namespace JuiceAgent {
// ===== Singleton =====
@@ -42,8 +41,9 @@ namespace JuiceAgent {
set_config(cfg);
PLOGD << "env: " << env << ", jvmti: " << jvmti;
// Register Modules
module_manager.register_module(std::make_unique<JuiceAgent::Core::Modules::JarLoaderModule>());
// Load and Initialize Modules
module_manager.load_all();
module_manager.init_all();
// Initialize Modules
module_manager.init_all();
+94 -64
View File
@@ -1,87 +1,117 @@
#include <modules/Modules.hpp>
#include <modules/ModuleRegistry.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;
struct JarLoaderConfig {
bool enabled = true;
std::string injection_dir;
std::string jar_path;
std::string entry_class;
std::string entry_method;
};
static JarLoaderConfig 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";
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(), true);
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;
}
bool JarLoaderModule::start() {
if (!_initialized) return false;
if (_running) return true;
PLOGI << "JarLoader start";
if (!Config.Enabled) {
PLOGI << "JarLoader is disabled";
return false;
class JarLoaderModule : public IModule {
public:
std::string name() const override {
return "JarLoader";
}
if (Config.InjectionDir.empty() || Config.JarPath.empty() || Config.EntryClass.empty() || Config.EntryMethod.empty()) {
PLOGE << "JarLoader config is invalid";
return false;
bool init() override {
if (_initialized) {
return true;
}
auto& cfg = agent.get_config();
config.enabled = cfg.get<bool>(
"JuiceAgent.Modules.JarLoader.Enabled",
false
);
config.injection_dir = cfg.get<std::string>(
"JuiceAgent.Modules.JarLoader.InjectionDir",
(cfg.runtime_dir() / "injection").string(),
true
);
config.jar_path = cfg.get<std::string>(
"JuiceAgent.Modules.JarLoader.JarPath",
"./Entry.jar",
true
);
config.entry_class = cfg.get<std::string>(
"JuiceAgent.Modules.JarLoader.EntryClass",
"Example.Main",
false
);
config.entry_method = cfg.get<std::string>(
"JuiceAgent.Modules.JarLoader.EntryMethod",
"start",
false
);
PLOGI << "JarLoader init";
_initialized = true;
return true;
}
JuiceAgent::Utils::Serializer ser;
bool start() override {
if (!_initialized) {
return false;
}
ser.add_kv("InjectionDir", Config.InjectionDir);
ser.add_kv("JarPath", Config.JarPath);
ser.add_kv("EntryClass", Config.EntryClass);
ser.add_kv("EntryMethod", Config.EntryMethod);
if (_running || !config.enabled) {
return true;
}
std::string serialized = ser.serialize();
JuiceAgent::Utils::Serializer ser;
ser.add_kv("InjectionDir", config.injection_dir);
ser.add_kv("JarPath", config.jar_path);
ser.add_kv("EntryClass", config.entry_class);
ser.add_kv("EntryMethod", config.entry_method);
PLOGD << "Serialized config: " << serialized;
auto data = ser.serialize();
JNIEnv* env = agent.get_env();
JuiceAgent::Utils::call_java_impl(env, MODULE_CLASS, MODULE_METHOD, serialized.c_str());
JuiceAgent::Utils::call_java_impl(
agent.get_env(),
MODULE_CLASS,
MODULE_METHOD,
data.c_str()
);
PLOGI << "JarLoader start";
_running = true;
return true;
}
void stop() override {
if (!_running) {
return;
}
PLOGI << "JarLoader stop";
_running = false;
}
private:
bool _initialized = false;
bool _running = false;
};
PLOGI << "JarLoader is running";
_running = true;
return true;
}
void JarLoaderModule::stop() {
if (!_running) return;
PLOGI << "JarLoader stop";
_running = false;
}
}
JUICEAGENT_REGISTER_MODULE(
JuiceAgent::Core::Modules::JarLoaderModule,
JarLoader
)
+16 -4
View File
@@ -1,13 +1,21 @@
#include <modules/ModuleManager.hpp>
#include <modules/ModuleRegistry.hpp>
#include <JuiceAgent/Logger.hpp>
namespace JuiceAgent::Core::Modules {
void ModuleManager::register_module(std::unique_ptr<IModule> module) {
if (!module) return;
void ModuleManager::load_all() {
for (const auto& entry : ModuleRegistry::instance().entries()) {
auto module = entry.factory();
PLOGI << "Register module: " << module->name();
_modules.push_back(std::move(module));
if (!module) {
PLOGE << "Failed to create module: " << entry.name;
continue;
}
PLOGI << "Register module: " << module->name();
_modules.push_back(std::move(module));
}
}
bool ModuleManager::init_all() {
@@ -19,6 +27,7 @@ bool ModuleManager::init_all() {
return false;
}
}
return true;
}
@@ -31,11 +40,13 @@ bool ModuleManager::start_all() {
return false;
}
}
return true;
}
void ModuleManager::stop_all() {
for (auto it = _modules.rbegin(); it != _modules.rend(); ++it) {
PLOGI << "Stop module: " << (*it)->name();
(*it)->stop();
}
}
@@ -46,6 +57,7 @@ IModule* ModuleManager::find_module(const std::string& name) {
return module.get();
}
}
return nullptr;
}