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..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 @@ -70,6 +70,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 +96,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 +173,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 +218,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 +238,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 +254,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 +268,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 +386,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 +441,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); } }