diff --git a/src/libagent/JuiceAgent.cpp b/src/libagent/JuiceAgent.cpp index 7444eb0..760ce49 100644 --- a/src/libagent/JuiceAgent.cpp +++ b/src/libagent/JuiceAgent.cpp @@ -4,6 +4,7 @@ #include #include #include +#include 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; } } \ No newline at end of file diff --git a/src/libagent/services/JarLoaderService.cpp b/src/libagent/services/JarLoaderService.cpp new file mode 100644 index 0000000..f692ff6 --- /dev/null +++ b/src/libagent/services/JarLoaderService.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include + +namespace JuiceAgent { +namespace Services { + +class JarLoaderService : public IService { +public: + const char* name() const override { return "JarLoaderService"; } + + bool onInitialize() override { + listen( + 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( + "JuiceAgent.Modules.JarLoader.Enabled", false); + + if (!enabled) { + spdlog::info("[JarLoaderService] Disabled by config"); + return; + } + + const std::string injection_dir = cfg.get( + "JuiceAgent.Modules.JarLoader.InjectionDir", + (cfg.runtime_dir() / "injection").string(), + true); + + const std::string jar_path = cfg.get( + "JuiceAgent.Modules.JarLoader.JarPath", + (cfg.runtime_dir() / "Entry.jar").string(), + true); + + const std::string entry_class = cfg.get( + "JuiceAgent.Modules.JarLoader.EntryClass", + "Example.Main", + false); + + const std::string entry_method = cfg.get( + "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)