Feat: add JarLoaderService that loads jar on EventLoaded

- add JarLoaderService registered via JUICEAGENT_REGISTER_SERVICE; it
  listens for EventId::Loaded, reads JuiceAgent.Modules.JarLoader config
  from config.toml, serializes it and calls the Java JarLoader.loadJar
  module to inject the jar into the JVM
- post EventPreLoad during Agent::preload and EventLoaded after services
  initialize so services can react to agent lifecycle
This commit is contained in:
xiaozhou233 committed 2026-08-17 19:59:23 +08:00
1 parent 4833b512f5
commit 20747124d1
2 files changed
+106 -2

No files matched your search

+11 -2
View File
@@ -4,6 +4,7 @@
#include <JuiceAgent/Config.hpp>
#include <jni_common.hpp>
#include <services.hpp>
#include <eventbus.hpp>
namespace JuiceAgent {
// Singleton
@@ -42,7 +43,10 @@ namespace JuiceAgent {
spdlog::warn("[JuiceAgent] No config provided, Modules will not be loaded!");
}
}
// Post preload event
EventPreLoad preloadEvent;
EventBus::getInstance().post(EventId::PreLoad, &preloadEvent);
// Set Environment
setJavaVM(jvm);
@@ -94,12 +98,17 @@ namespace JuiceAgent {
spdlog::error("[JuiceAgent] Could not enable JVMTI event notifications: {}", result);
}
// Initialize services (and their plugins)
// Initialize services
if(!JuiceAgent::Services::initializeAll()) {
spdlog::error("[JuiceAgent] Failed to initialize services");
return false;
}
// Post loaded event
EventLoaded loadedEvent;
EventBus::getInstance().post(EventId::Loaded, &loadedEvent);
return true;
}
}
@@ -0,0 +1,95 @@
#include <services/base.hpp>
#include <services/service_manager.hpp>
#include <event/event_type.hpp>
#include <JuiceAgent/Logger.hpp>
#include <JuiceAgent/Utils.hpp>
namespace JuiceAgent {
namespace Services {
class JarLoaderService : public IService {
public:
const char* name() const override { return "JarLoaderService"; }
bool onInitialize() override {
listen<JarLoaderService, EventLoaded>(
EventId::Loaded,
&JarLoaderService::onLoaded
);
spdlog::info("[JarLoaderService] Initialized");
return true;
}
void onShutdown() override {
spdlog::info("[JarLoaderService] Shutdown");
}
private:
static constexpr const char* MODULE_CLASS =
"cn/xiaozhou233/juiceagent/api/modules/JarLoader";
static constexpr const char* MODULE_METHOD = "loadJar";
void onLoaded(const EventLoaded& /*event*/) {
auto& agent = Agent::getInstance();
auto& cfg = agent.getConfig();
const bool enabled = cfg.get<bool>(
"JuiceAgent.Modules.JarLoader.Enabled", false);
if (!enabled) {
spdlog::info("[JarLoaderService] Disabled by config");
return;
}
const std::string injection_dir = cfg.get<std::string>(
"JuiceAgent.Modules.JarLoader.InjectionDir",
(cfg.runtime_dir() / "injection").string(),
true);
const std::string jar_path = cfg.get<std::string>(
"JuiceAgent.Modules.JarLoader.JarPath",
(cfg.runtime_dir() / "Entry.jar").string(),
true);
const std::string entry_class = cfg.get<std::string>(
"JuiceAgent.Modules.JarLoader.EntryClass",
"Example.Main",
false);
const std::string entry_method = cfg.get<std::string>(
"JuiceAgent.Modules.JarLoader.EntryMethod",
"start",
false);
JuiceAgent::Utils::Serializer ser;
ser.add_kv("InjectionDir", injection_dir);
ser.add_kv("JarPath", jar_path);
ser.add_kv("EntryClass", entry_class);
ser.add_kv("EntryMethod", entry_method);
const std::string data = ser.serialize();
spdlog::debug("[JarLoaderService] Serialized config: {}", data);
JNIEnv* env = agent.getJNIEnv();
if (!env) {
spdlog::error("[JarLoaderService] JNIEnv is null, cannot load jar");
return;
}
bool result = JuiceAgent::Utils::call_java_impl(
env, MODULE_CLASS, MODULE_METHOD, data.c_str());
if (!result) {
spdlog::error("[JarLoaderService] Failed to load jar: {}", jar_path);
return;
}
spdlog::info("[JarLoaderService] Loaded jar: {}", jar_path);
}
};
}
}
JUICEAGENT_REGISTER_SERVICE(::JuiceAgent::Services::JarLoaderService)