From 16038bfa059dfea8b8769c4fc1771ac1fc6dff0b Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Tue, 27 Aug 2024 20:11:23 +0800 Subject: [PATCH 1/3] [task]:fix bug --- .../hertzbeat/manager/service/impl/PluginServiceImpl.java | 1 + .../org/apache/hertzbeat/manager/service/PluginServiceTest.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) 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 5838bb260d9..97b9ccd12ae 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 @@ -110,6 +110,7 @@ public class PluginServiceImpl implements PluginService { private final List pluginClassLoaders = new ArrayList<>(); @Override + @Transactional public void deletePlugins(Set ids) { List plugins = metadataDao.findAllById(ids); // disable the plugins that need to be removed diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java index 172719805cd..59dc49b2014 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java @@ -74,7 +74,7 @@ class PluginServiceTest { @BeforeEach void setUp() { - pluginService = new PluginServiceImpl(metadataDao, itemDao,pluginParamDao); + pluginService = new PluginServiceImpl(metadataDao, itemDao, pluginParamDao); } @Test From 680717b1f2a33446c2d85403ee93fcc4f778d59c Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Wed, 28 Aug 2024 23:37:05 +0800 Subject: [PATCH 2/3] [task]:fix bug --- .../manager/controller/PluginController.java | 4 ++-- .../service/impl/PluginServiceImpl.java | 23 ++++++++++++++----- plugin/src/main/resources/define/define.yml | 0 web-app/src/app/service/plugin.service.ts | 4 ++-- 4 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 plugin/src/main/resources/define/define.yml diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java index 496ebfb5bad..18c24544fac 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java @@ -88,14 +88,14 @@ public ResponseEntity> updatePluginStatus(@RequestBody PluginMetad return ResponseEntity.ok(Message.success("Update success")); } - @GetMapping("/getParamDefine") + @GetMapping("/params/define") @Operation(summary = "get param define", description = "get param define by jar path") public ResponseEntity> getParamDefine(@RequestParam Long pluginMetadataId) { PluginParametersVO plugins = pluginService.getParamDefine(pluginMetadataId); return ResponseEntity.ok(Message.success(plugins)); } - @PostMapping("/saveParams") + @PostMapping("/params") @Operation(summary = "get param define", description = "get param define by jar path") public ResponseEntity> saveParams(@RequestBody List pluginParams) { pluginService.savePluginParam(pluginParams); 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 97b9ccd12ae..f1dd9f4bf4d 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 @@ -17,6 +17,7 @@ package org.apache.hertzbeat.manager.service.impl; +import jakarta.persistence.TableGenerator; import jakarta.persistence.criteria.Predicate; import java.io.File; import java.io.FileOutputStream; @@ -70,6 +71,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.error.YAMLException; /** * plugin service @@ -95,7 +97,7 @@ public class PluginServiceImpl implements PluginService { /** * plugin param define */ - private static final Map> PARAMS_Define_MAP = new ConcurrentHashMap<>(); + private static final Map> PARAMS_DEFINE_MAP = new ConcurrentHashMap<>(); /** * plugin params @@ -172,8 +174,8 @@ public void updateStatus(PluginMetadata plugin) { public PluginParametersVO getParamDefine(Long pluginMetadataId) { PluginParametersVO pluginParametersVO = new PluginParametersVO(); - if (PARAMS_Define_MAP.containsKey(pluginMetadataId)) { - List paramDefines = PARAMS_Define_MAP.get(pluginMetadataId); + if (PARAMS_DEFINE_MAP.containsKey(pluginMetadataId)) { + List paramDefines = PARAMS_DEFINE_MAP.get(pluginMetadataId); List paramsByPluginMetadataId = pluginParamDao.findParamsByPluginMetadataId(pluginMetadataId); pluginParametersVO.setParamDefines(paramDefines); pluginParametersVO.setPluginParams(paramsByPluginMetadataId); @@ -217,8 +219,9 @@ public List validateJarFile(File jarFile) { try { URL jarUrl = new URL("file:" + jarFile.getAbsolutePath()); try (URLClassLoader classLoader = new URLClassLoader(new URL[]{jarUrl}, this.getClass().getClassLoader()); - JarFile jar = new JarFile(jarFile)) { + JarFile jar = new JarFile(jarFile)) { Enumeration entries = jar.entries(); + Yaml yaml = new Yaml(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.getName().endsWith(".class")) { @@ -236,6 +239,11 @@ public List validateJarFile(File jarFile) { System.err.println("Failed to load class: " + className); } } + if ((entry.getName().contains("define")) && (entry.getName().endsWith(".yml") || entry.getName().endsWith(".yaml"))) { + try (InputStream ymlInputStream = jar.getInputStream(entry)) { + yaml.loadAs(ymlInputStream, List.class); + } + } } if (pluginItems.isEmpty()) { throw new CommonException("Illegal plug-ins, please refer to https://hertzbeat.apache.org/docs/help/plugin/"); @@ -247,6 +255,8 @@ public List validateJarFile(File jarFile) { } catch (MalformedURLException e) { log.error("Invalid JAR file URL: {}", jarFile.getAbsoluteFile(), e); throw new CommonException("Invalid JAR file URL: " + jarFile.getAbsolutePath()); + } catch (YAMLException e) { + throw new CommonException("YAML the file format is incorrect"); } return pluginItems; } @@ -259,6 +269,7 @@ private void validateMetadata(PluginMetadata metadata) { @Override @SneakyThrows + @Transactional public void savePlugin(PluginUpload pluginUpload) { String jarPath = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()).getAbsolutePath(); Path extLibPath = Paths.get(new File(jarPath).getParent(), "plugin-lib"); @@ -376,7 +387,7 @@ private void loadJarToClassLoader() { pluginClassLoaders.clear(); System.gc(); } - PARAMS_Define_MAP.clear(); + PARAMS_DEFINE_MAP.clear(); List plugins = metadataDao.findPluginMetadataByEnableStatusTrue(); for (PluginMetadata metadata : plugins) { List urls = loadLibInPlugin(metadata.getJarFilePath(), metadata.getId()); @@ -431,7 +442,7 @@ private List loadLibInPlugin(String pluginJarPath, Long pluginMetadataId) { if ((entry.getName().contains("define")) && (entry.getName().endsWith(".yml") || entry.getName().endsWith(".yaml"))) { try (InputStream ymlInputStream = jarFile.getInputStream(entry)) { List params = yaml.loadAs(ymlInputStream, List.class); - PARAMS_Define_MAP.putIfAbsent(pluginMetadataId, params); + PARAMS_DEFINE_MAP.put(pluginMetadataId, params); } } } diff --git a/plugin/src/main/resources/define/define.yml b/plugin/src/main/resources/define/define.yml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/web-app/src/app/service/plugin.service.ts b/web-app/src/app/service/plugin.service.ts index 1a2bdae5114..1b34ed5cb50 100644 --- a/web-app/src/app/service/plugin.service.ts +++ b/web-app/src/app/service/plugin.service.ts @@ -79,10 +79,10 @@ export class PluginService { pluginMetadataId: pluginId }); const options = { params: httpParams }; - return this.http.get>(`${plugin_uri}/getParamDefine`, options); + return this.http.get>(`${plugin_uri}/params/define`, options); } public savePluginParamDefine(body: any): Observable> { - return this.http.post>(`${plugin_uri}/saveParams`, body); + return this.http.post>(`${plugin_uri}/params`, body); } } From e68db68bbdf654ce38e1ba3d91100196861f48d1 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Thu, 29 Aug 2024 09:12:32 +0800 Subject: [PATCH 3/3] [task]:fix ci --- .../apache/hertzbeat/manager/service/impl/PluginServiceImpl.java | 1 - 1 file changed, 1 deletion(-) 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 f1dd9f4bf4d..4ccae0fd0ed 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 @@ -17,7 +17,6 @@ package org.apache.hertzbeat.manager.service.impl; -import jakarta.persistence.TableGenerator; import jakarta.persistence.criteria.Predicate; import java.io.File; import java.io.FileOutputStream;