This is an automated email from the ASF dual-hosted git repository.

zhaoqingran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new ef377912e [improve] improve plugin upload check (#2666)
ef377912e is described below

commit ef377912ef9fdd8bd0840399933df16e536bd6e4
Author: liutianyou <[email protected]>
AuthorDate: Tue Sep 3 23:44:11 2024 +0800

    [improve] improve plugin upload check (#2666)
    
    Co-authored-by: shown <[email protected]>
    Co-authored-by: Logic <[email protected]>
---
 home/docs/help/plugin.md                              |  4 ++++
 .../current/help/plugin.md                            |  4 ++++
 .../manager/service/impl/PluginServiceImpl.java       | 19 +++++++++++++------
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/home/docs/help/plugin.md b/home/docs/help/plugin.md
index c6aa1417b..7b397367e 100644
--- a/home/docs/help/plugin.md
+++ b/home/docs/help/plugin.md
@@ -17,6 +17,10 @@ In the current usage of `HertzBeat`, interaction with 
external systems only occu
     - Purpose: Execute custom operations after data collection
     - Implementing Interface: `org.apache.hertzbeat.plugin.PostCollectPlugin`
 
+:::tip
+To ensure that plugin functionality is clear and easy to manage, we recommend 
and only support one implementation of one plugin type interface in a plugin.
+:::
+
 If you want to set trigger methods during collection, program startup, etc., 
please submit a `Task` at 
`https://github.com/apache/hertzbeat/issues/new/choose`.
 
 ### Development Steps (Example: Implementing a Post-Alert Plugin)
diff --git 
a/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/plugin.md 
b/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/plugin.md
index b886e35aa..eda393213 100644
--- a/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/plugin.md
+++ b/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/plugin.md
@@ -18,6 +18,10 @@ sidebar_label: 自定义插件
     - 作用:在采集后执行自定义操作
     - 实现接口:`org.apache.hertzbeat.plugin.PostCollectPlugin`
 
+:::tip
+为了保证插件功能清晰,易于管理,我们建议且只支持一个插件中只包含一种插件类型接口的一个实现。
+:::
+
 如需在采集、启动程序等时机设置触发方法,请在`https://github.com/apache/hertzbeat/issues/new/choose` 
提`Task`
 
 ### 开发步骤 (以实现一个告警后插件为例)
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java
 
b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java
index 0399eaed5..28da149cf 100644
--- 
a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java
@@ -39,6 +39,7 @@ import java.util.ServiceLoader;
 import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.BiConsumer;
 import java.util.function.Consumer;
 import java.util.jar.JarEntry;
@@ -221,6 +222,7 @@ public class PluginServiceImpl implements PluginService {
     public PluginMetadata validateJarFile(File jarFile) {
         PluginMetadata metadata = new PluginMetadata();
         List<PluginItem> pluginItems = new ArrayList<>();
+        AtomicInteger pluginImplementationCount = new AtomicInteger(0);
         try {
             URL jarUrl = new URL("file:" + jarFile.getAbsolutePath());
             try (URLClassLoader classLoader = new URLClassLoader(new 
URL[]{jarUrl}, this.getClass().getClassLoader());
@@ -232,13 +234,18 @@ public class PluginServiceImpl implements PluginService {
                         String className = entry.getName().replace("/", 
".").replace(".class", "");
                         try {
                             Class<?> cls = classLoader.loadClass(className);
-                            if (!cls.isInterface()) {
-                                PLUGIN_TYPE_MAPPING.forEach((clazz, type) -> {
-                                    if (clazz.isAssignableFrom(cls)) {
-                                        pluginItems.add(new 
PluginItem(className, type));
-                                    }
-                                });
+                            if (cls.isInterface()) {
+                                continue;
                             }
+                            if (pluginImplementationCount.get() >= 1) {
+                                throw new CommonException("A plugin package 
can only contain one plugin implementation class");
+                            }
+                            PLUGIN_TYPE_MAPPING.forEach((clazz, type) -> {
+                                if (clazz.isAssignableFrom(cls)) {
+                                    pluginItems.add(new PluginItem(className, 
type));
+                                    
pluginImplementationCount.incrementAndGet();
+                                }
+                            });
                         } catch (ClassNotFoundException e) {
                             System.err.println("Failed to load class: " + 
className);
                         }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to