Skip to content

Commit

Permalink
[improve] improve plugin params (apache#2655)
Browse files Browse the repository at this point in the history
Co-authored-by: shown <[email protected]>
Co-authored-by: Calvin <[email protected]>
  • Loading branch information
3 people authored Sep 2, 2024
1 parent ec92f4e commit 4a69177
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,8 @@ public int hashCode() {
@JoinColumn(name = "metadata_id", referencedColumnName = "id")
private List<PluginItem> items;

@Schema(title = "Param count", example = "1", accessMode = READ_WRITE)
private Integer paramCount;


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package org.apache.hertzbeat.common.entity.plugin;

import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import org.apache.hertzbeat.common.entity.manager.ParamDefine;
Expand All @@ -33,4 +34,8 @@
public class PluginConfig {

private List<ParamDefine> params;

public PluginConfig() {
this.params = new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.hertzbeat.common.constants.PluginType;
import org.apache.hertzbeat.common.entity.dto.PluginUpload;
Expand All @@ -71,7 +72,6 @@
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;

Expand Down Expand Up @@ -216,16 +216,16 @@ private void syncPluginParamMap(Long pluginMetadataId, List<PluginParam> params,
* verify the type of the jar package
*
* @param jarFile jar file
* @return return the full path of the Plugin interface implementation class
* @return return the result of jar package parsed
*/
public List<PluginItem> validateJarFile(File jarFile) {
public PluginMetadata validateJarFile(File jarFile) {
PluginMetadata metadata = new PluginMetadata();
List<PluginItem> pluginItems = new ArrayList<>();
try {
URL jarUrl = new URL("file:" + jarFile.getAbsolutePath());
try (URLClassLoader classLoader = new URLClassLoader(new URL[]{jarUrl}, this.getClass().getClassLoader());
JarFile jar = new JarFile(jarFile)) {
Enumeration<JarEntry> entries = jar.entries();
Yaml yaml = new Yaml();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.getName().endsWith(".class")) {
Expand All @@ -244,9 +244,8 @@ public List<PluginItem> validateJarFile(File jarFile) {
}
}
if ((entry.getName().contains("define")) && (entry.getName().endsWith(".yml") || entry.getName().endsWith(".yaml"))) {
try (InputStream ymlInputStream = jar.getInputStream(entry)) {
yaml.loadAs(ymlInputStream, List.class);
}
PluginConfig config = readPluginConfig(jar, entry);
metadata.setParamCount(CollectionUtils.size(config.getParams()));
}
}
if (pluginItems.isEmpty()) {
Expand All @@ -262,7 +261,8 @@ public List<PluginItem> validateJarFile(File jarFile) {
} catch (YAMLException e) {
throw new CommonException("YAML the file format is incorrect");
}
return pluginItems;
metadata.setItems(pluginItems);
return metadata;
}

private void validateMetadata(PluginMetadata metadata) {
Expand Down Expand Up @@ -290,10 +290,12 @@ public void savePlugin(PluginUpload pluginUpload) {
List<PluginItem> pluginItems;
PluginMetadata pluginMetadata;
try {
pluginItems = validateJarFile(destFile);
PluginMetadata parsed = validateJarFile(destFile);
pluginItems = parsed.getItems();
pluginMetadata = PluginMetadata.builder()
.name(pluginUpload.getName())
.enableStatus(true)
.paramCount(parsed.getParamCount())
.items(pluginItems).jarFilePath(destFile.getAbsolutePath())
.gmtCreate(LocalDateTime.now())
.build();
Expand Down Expand Up @@ -424,7 +426,6 @@ private List<URL> loadLibInPlugin(String pluginJarPath, Long pluginMetadataId) t
List<URL> libUrls = new ArrayList<>();
try (JarFile jarFile = new JarFile(pluginJarPath)) {
Enumeration<JarEntry> entries = jarFile.entries();
Yaml yaml = new Yaml();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
File file = new File(libDir, entry.getName());
Expand All @@ -447,16 +448,30 @@ private List<URL> loadLibInPlugin(String pluginJarPath, Long pluginMetadataId) t
}
}
if ((entry.getName().contains("define")) && (entry.getName().endsWith(".yml") || entry.getName().endsWith(".yaml"))) {
try (InputStream ymlInputStream = jarFile.getInputStream(entry)) {
PluginConfig config = yaml.loadAs(ymlInputStream, PluginConfig.class);
PARAMS_CONFIG_MAP.put(pluginMetadataId, config);
}
PluginConfig config = readPluginConfig(jarFile, entry);
PARAMS_CONFIG_MAP.put(pluginMetadataId, config);
}
}
}
return libUrls;
}

/**
* Read the plugin configuration file from the jar package
*
* @return plugin config
*/
private PluginConfig readPluginConfig(JarFile jarFile, JarEntry entry) throws IOException {
Yaml yaml = new Yaml();
try (InputStream ymlInputStream = jarFile.getInputStream(entry)) {
PluginConfig config = yaml.loadAs(ymlInputStream, PluginConfig.class);
if (config == null) {
return new PluginConfig();
}
return config;
}
}

@Override
public <T> void pluginExecute(Class<T> clazz, Consumer<T> execute) {
for (URLClassLoader pluginClassLoader : pluginClassLoaders) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ void setUp() {
void testSavePlugin() {

List<PluginItem> pluginItems = Collections.singletonList(new PluginItem("org.apache.hertzbeat.PluginTest", PluginType.POST_ALERT));
PluginMetadata metadata = new PluginMetadata();
metadata.setItems(pluginItems);
metadata.setParamCount(0);
PluginServiceImpl service = spy(pluginService);
doReturn(pluginItems).when(service).validateJarFile(any());
doReturn(metadata).when(service).validateJarFile(any());

MockMultipartFile mockFile = new MockMultipartFile("file", "test-plugin.jar", "application/java-archive", "plugin-content".getBytes());
PluginUpload pluginUpload = new PluginUpload(mockFile, "Test Plugin", true);
Expand Down
1 change: 1 addition & 0 deletions web-app/src/app/pojo/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export class Plugin {
name!: string;
enableStatus!: boolean;
items!: PluginItem[];
paramCount!: number;
}
13 changes: 10 additions & 3 deletions web-app/src/app/routes/setting/plugins/plugin.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@
<!-- <td nzAlign="center">{{ data.enableStatus }}</td>-->
<td nzAlign="center">
<div class="actions">
<button nz-button nzType="primary" (click)="onEditPluginParamDefine(data.id)" nz-tooltip [nzTooltipTitle]="'plugin.edit' | i18n">
<button
*ngIf="data.paramCount !== 0"
nz-button
nzType="primary"
(click)="onEditPluginParamDefine(data.id)"
nz-tooltip
[nzTooltipTitle]="'plugin.param.edit' | i18n"
>
<i nz-icon nzType="edit" nzTheme="outline"></i>
</button>
<button
Expand All @@ -111,7 +118,7 @@
</tbody>
</nz-table>

<ng-template #rangeTemplate> {{ 'common.total' | i18n }} {{ total }} </ng-template>
<ng-template #rangeTemplate> {{ 'common.total' | i18n }} {{ total }}</ng-template>

<!-- upload plugin pop-up window -->
<nz-modal
Expand Down Expand Up @@ -174,7 +181,7 @@
<form nz-form #form="ngForm">
<ng-container *ngFor="let paramDefine of paramDefines; let i = index">
<nz-form-item>
<nz-form-label nzSpan="7" [nzRequired]="paramDefine.required" [nzFor]="paramDefine.field">{{ paramDefine.name }} </nz-form-label>
<nz-form-label nzSpan="7" [nzRequired]="paramDefine.required" [nzFor]="paramDefine.field">{{ paramDefine.name }}</nz-form-label>
<nz-form-control nzSpan="8" [nzErrorTip]="'validation.required' | i18n">
<app-form-field [item]="paramDefine" [name]="paramDefine.field" [(ngModel)]="params[paramDefine.field].paramValue" />
</nz-form-control>
Expand Down
1 change: 1 addition & 0 deletions web-app/src/assets/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@
"plugin.type.POST_COLLECT": "POST COLLECT",
"plugin.search": "Search plugins",
"plugin.edit": "Edit plugin",
"plugin.param.edit": "Edit Params",
"define.help": "The monitor templates define each monitoring type, parameter variable, metrics info, collection protocol, etc. You can select an existing monitoring template from the drop-down menu then make modifications according to your own needs. The bottom-left area is the compare area and the bottom-right area is the editing place. <br> You can also click \"New Monitor Type\" to custom define an new type. Currently supported protocols include<a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-http'> HTTP</a>, <a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-jdbc'>JDBC</a>, <a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-ssh'>SSH</a>, <a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-jmx'>JMX</a>, <a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-snmp'> SNMP</a>. <a class='help_module_content' href='https://hertzbeat.apache.org/zh-cn/docs/template'>Monitor Templates</a>.",
"define.help.link": "https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-point/",
"define.save-apply": "Save And Apply",
Expand Down
1 change: 1 addition & 0 deletions web-app/src/assets/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@
"plugin.type.POST_COLLECT": "采集后",
"plugin.search": "搜索插件",
"plugin.edit": "编辑插件",
"plugin.param.edit": "编辑参数",
"define.help": "监控模版定义每一个监控类型,类型的参数变量,指标信息,采集协议等。您可根据需求在下拉菜单中选择已有监控模板修改。左下区域为对照区,右下区域为编辑区。<br>您也可以点击“<i>新增监控类型</i>”来自定义新的的监控类型,目前支持 <a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-http'> HTTP 协议</a>,<a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-jdbc'>JDBC协议</a>,<a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-ssh'>SSH协议</a>,<a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-jmx'> JMX 协议</a>,<a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-snmp'> SNMP 协议</a>,<a class='help_module_content' href='https://hertzbeat.apache.org/zh-cn/docs/template'>点击查看监控模板</a>。\n",
"define.help.link": "https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-point/",
"define.save-apply": "保存并应用",
Expand Down
1 change: 1 addition & 0 deletions web-app/src/assets/i18n/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@
"plugin.type.POST_COLLECT": "採集後",
"plugin.search": "搜尋插件",
"plugin.edit": "編輯插件",
"plugin.param.edit": "編輯參數",
"define.help": "監控模版定義每一個監控類型,類型的參數變量,指標信息,採集協議等。您可根據需求在下拉功能表中選擇已有監控模版進行修改。右下區域為編輯區,左下區域為對照區。<br>您也可以點擊“<i>新增監控類型</i>”來自定義新的的監控類型,現支持<a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-http'> HTTP協議</a>,<a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-jdbc'>JDBC協定</a>,<a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-ssh'>SSH協定</a>,<a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-jmx'> JMX協定</a>,<a href='https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-snmp'> SNMP協定</a>,<a href='https://hertzbeat.apache.org/zh-cn/docs/template'>點擊查看監控範本</a>。",
"define.help.link": "https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-point/",
"define.save-apply": "保存並應用",
Expand Down

0 comments on commit 4a69177

Please sign in to comment.