From 81d013cc50313b2fd4137fe1a1a497cc0d1fbb44 Mon Sep 17 00:00:00 2001 From: daizhenyu <1449308021@qq.com> Date: Tue, 17 Dec 2024 16:57:39 +0800 Subject: [PATCH] dynamic plugins support custom paths Signed-off-by: daizhenyu <1449308021@qq.com> --- .../core/command/DynamicAgentArgsManager.java | 26 +++++++++++++++++++ .../io/sermant/core/plugin/PluginManager.java | 24 ++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/command/DynamicAgentArgsManager.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/command/DynamicAgentArgsManager.java index e4bced760c..78327e2fd0 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/command/DynamicAgentArgsManager.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/command/DynamicAgentArgsManager.java @@ -16,6 +16,10 @@ package io.sermant.core.command; +import io.sermant.core.common.CommonConstant; +import io.sermant.core.utils.StringUtils; + +import java.io.File; import java.util.HashMap; import java.util.Map; @@ -61,4 +65,26 @@ public static String getAgentArg(String key) { public static Map getAgentArgsMap() { return AGENT_ARGS; } + + /** + * get dynamic plugin path + * + * @return dynamic plugin path + */ + public static String getDynamicPluginPackagePath() { + String agentFilePath = AGENT_ARGS.get(CommonConstant.AGENT_FILE_KEY); + if (StringUtils.isEmpty(agentFilePath)) { + return StringUtils.EMPTY; + } + return agentFilePath + File.separatorChar + "pluginPackage"; + } + + /** + * get dynamic agent path + * + * @return dynamic agent path + */ + public static String getAgentPath() { + return AGENT_ARGS.get(CommonConstant.AGENT_PATH_KEY); + } } diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/PluginManager.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/PluginManager.java index 0b22096e8b..0c70bd3c44 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/PluginManager.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/PluginManager.java @@ -17,6 +17,7 @@ package io.sermant.core.plugin; import io.sermant.core.classloader.ClassLoaderManager; +import io.sermant.core.command.DynamicAgentArgsManager; import io.sermant.core.common.BootArgsIndexer; import io.sermant.core.common.LoggerFactory; import io.sermant.core.event.collector.FrameworkEventCollector; @@ -33,6 +34,7 @@ import io.sermant.core.plugin.config.PluginConfigManager; import io.sermant.core.plugin.service.PluginServiceManager; import io.sermant.core.utils.CollectionUtils; +import io.sermant.core.utils.StringUtils; import java.io.File; import java.io.FileFilter; @@ -163,7 +165,11 @@ public static void initPlugins(Set pluginNames, boolean isDynamic) { } final String pluginPackage; try { - pluginPackage = BootArgsIndexer.getPluginPackageDir().getCanonicalPath(); + if (isDynamic) { + pluginPackage = getDynamicPluginPackagePath(); + } else { + pluginPackage = getStaticPluginPackagePath(); + } } catch (IOException ioException) { String names = pluginNames.stream().map(String::valueOf).collect(Collectors.joining(", ")); LOGGER.log(Level.SEVERE, "[INSTALL-PLUGINS] [{0}] Resolve plugin package failed.", names); @@ -414,4 +420,20 @@ private interface JarFileConsumer { */ void consume(JarFile jarFile); } + + private static String getStaticPluginPackagePath() throws IOException { + return BootArgsIndexer.getPluginPackageDir().getCanonicalPath(); + } + + private static String getDynamicPluginPackagePath() throws IOException { + String dynamicPluginPath = DynamicAgentArgsManager.getDynamicPluginPackagePath(); + if (!StringUtils.isEmpty(dynamicPluginPath)) { + return dynamicPluginPath; + } + String agentPath = DynamicAgentArgsManager.getAgentPath(); + if (!StringUtils.isEmpty(agentPath)) { + return agentPath + File.separatorChar + "pluginPackage"; + } + return BootArgsIndexer.getPluginPackageDir().getCanonicalPath(); + } }