From b8065a64d673975ec6f0821ad25140292f217dc2 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Tue, 6 Feb 2024 21:27:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E6=8E=A5=E5=8F=A3=E6=8F=92=E4=BB=B6=E6=97=B6?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E7=BB=84=E4=BB=B6=E8=A7=A3=E6=9E=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dto/request/http/body/BinaryBody.java | 8 +++- .../jmeter/body/MsBinaryBodyConverter.java | 2 +- .../api/service/ApiPluginChangeService.java | 41 +++++++++++++++++++ .../ApiScenarioControllerTests.java | 13 ------ .../api/controller/MsHTTPElementTest.java | 4 +- .../api/controller/PluginSubTypeTests.java | 4 +- .../invoker/PluginChangeServiceInvoker.java | 27 ++++++++++++ .../system/service/PluginChangeService.java | 13 ++++++ .../system/service/PluginLoadService.java | 13 ++++-- .../system/service/PluginService.java | 1 - 10 files changed, 103 insertions(+), 23 deletions(-) create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/service/ApiPluginChangeService.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/invoker/PluginChangeServiceInvoker.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginChangeService.java diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/http/body/BinaryBody.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/http/body/BinaryBody.java index 8487d99d52..50a98fa29c 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/http/body/BinaryBody.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/http/body/BinaryBody.java @@ -1,6 +1,7 @@ package io.metersphere.api.dto.request.http.body; import io.metersphere.api.dto.ApiFile; +import jakarta.validation.Valid; import lombok.Data; /** @@ -9,9 +10,14 @@ import lombok.Data; * @CreateTime: 2023-11-06 18:25 */ @Data -public class BinaryBody extends ApiFile { +public class BinaryBody { /** * 描述 */ private String description; + /** + * 文件对象 + */ + @Valid + private ApiFile file; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/body/MsBinaryBodyConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/body/MsBinaryBodyConverter.java index 83886f49c7..c6e30ef157 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/body/MsBinaryBodyConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/body/MsBinaryBodyConverter.java @@ -14,7 +14,7 @@ import org.apache.jmeter.protocol.http.util.HTTPFileArg; public class MsBinaryBodyConverter extends MsBodyConverter { @Override public void parse(HTTPSamplerProxy sampler, BinaryBody body, ParameterConfig config) { - HTTPFileArg httpFileArg = getHttpFileArg(body); + HTTPFileArg httpFileArg = getHttpFileArg(body.getFile()); sampler.setHTTPFiles(new HTTPFileArg[]{httpFileArg}); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiPluginChangeService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiPluginChangeService.java new file mode 100644 index 0000000000..b9fa79d6bb --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiPluginChangeService.java @@ -0,0 +1,41 @@ +package io.metersphere.api.service; + +import io.metersphere.api.utils.ApiDataUtils; +import io.metersphere.api.utils.JmeterElementConverterRegister; +import io.metersphere.plugin.api.spi.AbstractApiPlugin; +import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; +import io.metersphere.plugin.api.spi.JmeterElementConverter; +import io.metersphere.plugin.api.spi.MsTestElement; +import io.metersphere.sdk.plugin.MsPluginManager; +import io.metersphere.sdk.util.CommonBeanFactory; +import io.metersphere.sdk.util.LogUtils; +import io.metersphere.system.service.PluginChangeService; +import io.metersphere.system.service.PluginLoadService; +import org.pf4j.Plugin; +import org.springframework.stereotype.Service; + +/** + * @Author: jianxing + * @CreateTime: 2024-02-06 20:55 + */ +@Service +public class ApiPluginChangeService implements PluginChangeService { + @Override + public void handlePluginLoad(String pluginId) { + MsPluginManager msPluginManager = CommonBeanFactory.getBean(PluginLoadService.class).getMsPluginManager(); + Plugin plugin = msPluginManager.getPlugin(pluginId).getPlugin(); + try { + if (plugin instanceof AbstractApiPlugin) { + // 注册序列化类 + msPluginManager.getExtensionClasses(MsTestElement.class, pluginId) + .forEach(ApiDataUtils::setResolver); + + // 注册插件元素解析器 + msPluginManager.getExtensionClasses(JmeterElementConverter.class, pluginId) + .forEach(item -> JmeterElementConverterRegister.register((Class>) item)); + } + } catch (Exception e) { + LogUtils.error("注册接口插件实现类失败:", e); + } + } +} diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java index 68b78bd56c..0c14ea332c 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java @@ -17,10 +17,6 @@ import io.metersphere.api.service.definition.ApiDefinitionService; import io.metersphere.api.service.definition.ApiTestCaseService; import io.metersphere.api.service.scenario.ApiScenarioService; import io.metersphere.api.utils.ApiDataUtils; -import io.metersphere.api.utils.JmeterElementConverterRegister; -import io.metersphere.plugin.api.spi.AbstractJmeterElementConverter; -import io.metersphere.plugin.api.spi.JmeterElementConverter; -import io.metersphere.plugin.api.spi.MsTestElement; import io.metersphere.project.api.assertion.MsResponseCodeAssertion; import io.metersphere.project.api.assertion.MsScriptAssertion; import io.metersphere.project.api.processor.MsProcessor; @@ -660,15 +656,6 @@ public class ApiScenarioControllerTests extends BaseTest { request.getStepDetails().put(pluginStep.getId(), pluginStepDetail); Plugin plugin = addEnvTestPlugin(); - List> msTestElementClasses = - pluginLoadService.getMsPluginManager().getExtensionClasses(MsTestElement.class); - // 注册序列化类 - msTestElementClasses.forEach(ApiDataUtils::setResolver); - // 注册转换器 - List> converterClasses = - pluginLoadService.getMsPluginManager().getExtensionClasses(JmeterElementConverter.class); - converterClasses.forEach(item -> JmeterElementConverterRegister.register((Class>) item)); - this.requestPostWithOk(DEBUG, request); pluginService.delete(plugin.getId()); diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/MsHTTPElementTest.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/MsHTTPElementTest.java index 4f41eb05e0..a13a58cf22 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/MsHTTPElementTest.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/MsHTTPElementTest.java @@ -102,7 +102,9 @@ public class MsHTTPElementTest { xmlBody.setValue(""); body.setXmlBody(xmlBody); - body.setBinaryBody(BeanUtils.copyBean(new BinaryBody(), bodyFile)); + BinaryBody binaryBody = new BinaryBody(); + binaryBody.setFile(bodyFile); + body.setBinaryBody(binaryBody); return body; } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/PluginSubTypeTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/PluginSubTypeTests.java index 11a962aadb..d52f4110cb 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/PluginSubTypeTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/PluginSubTypeTests.java @@ -1,12 +1,12 @@ package io.metersphere.api.controller; import com.fasterxml.jackson.databind.jsontype.NamedType; +import io.metersphere.api.dto.request.controller.MsLoopController; +import io.metersphere.api.dto.request.http.MsHTTPElement; import io.metersphere.api.utils.ApiDataUtils; import io.metersphere.plugin.api.dto.TestElementDTO; import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.plugin.api.spi.MsTestElement; -import io.metersphere.api.dto.request.http.MsHTTPElement; -import io.metersphere.api.dto.request.controller.MsLoopController; import io.metersphere.system.base.BaseApiPluginTestService; import io.metersphere.system.service.PluginLoadService; import jakarta.annotation.Resource; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/invoker/PluginChangeServiceInvoker.java b/backend/services/system-setting/src/main/java/io/metersphere/system/invoker/PluginChangeServiceInvoker.java new file mode 100644 index 0000000000..2ab01c7068 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/invoker/PluginChangeServiceInvoker.java @@ -0,0 +1,27 @@ +package io.metersphere.system.invoker; + +import io.metersphere.system.service.PluginChangeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @Author: jianxing + * @CreateTime: 2024-02-06 20:48 + */ +@Component +public class PluginChangeServiceInvoker implements PluginChangeService { + private final List pluginChangeServices; + + + @Autowired + public PluginChangeServiceInvoker(List services) { + this.pluginChangeServices = services; + } + + @Override + public void handlePluginLoad(String pluginId) { + this.pluginChangeServices.forEach(service -> service.handlePluginLoad(pluginId)); + } +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginChangeService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginChangeService.java new file mode 100644 index 0000000000..de5583a8d4 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginChangeService.java @@ -0,0 +1,13 @@ +package io.metersphere.system.service; + +/** + * @Author: jianxing + * @CreateTime: 2024-02-06 20:47 + */ +public interface PluginChangeService { + /** + * 插件时调用 + * @param pluginId 插件ID + */ + void handlePluginLoad(String pluginId); +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginLoadService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginLoadService.java index c7ad405fd1..01a5499212 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginLoadService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginLoadService.java @@ -15,6 +15,7 @@ import io.metersphere.sdk.util.MsFileUtils; import io.metersphere.system.domain.Plugin; import io.metersphere.system.domain.PluginExample; import io.metersphere.system.domain.PluginScript; +import io.metersphere.system.invoker.PluginChangeServiceInvoker; import io.metersphere.system.mapper.PluginMapper; import io.metersphere.system.mapper.PluginScriptMapper; import jakarta.annotation.Resource; @@ -49,6 +50,8 @@ public class PluginLoadService { private PluginMapper pluginMapper; @Resource private PluginScriptMapper pluginScriptMapper; + @Resource + private PluginChangeServiceInvoker pluginChangeServiceInvoker; private MsPluginManager msPluginManager = new MsPluginManager(); /** @@ -65,7 +68,11 @@ public class PluginLoadService { // 文件不存在,则从对象存储重新下载 downloadPluginFromRepository(fileName); } - return msPluginManager.loadPlugin(Paths.get(filePath)); + String pluginId = msPluginManager.loadPlugin(Paths.get(filePath)); + msPluginManager.startPlugin(pluginId); + // 通知其他服务处理插件加载逻辑 + pluginChangeServiceInvoker.handlePluginLoad(pluginId); + return pluginId; } @@ -83,7 +90,7 @@ public class PluginLoadService { if (!file.exists()) { try (InputStream fileAsStream = FileCenter.getDefaultRepository().getFileAsStream(getDefaultRepositoryFileRequest(fileName))) { FileUtils.copyInputStreamToFile(fileAsStream, file); - msPluginManager.loadPlugin(Paths.get(filePath)); + loadPlugin(filePath); } catch (Exception e) { LogUtils.error("从对象存储加载插件异常", e); } @@ -192,7 +199,6 @@ public class PluginLoadService { String fileName = plugin.getFileName(); try { loadPlugin(fileName); - msPluginManager.startPlugin(plugin.getId()); } catch (Throwable e) { LogUtils.error("初始化插件异常" + plugin.getFileName(), e); } @@ -308,7 +314,6 @@ public class PluginLoadService { public void handlePluginAddNotified(String pluginId, String fileName) { if (!hasPlugin(pluginId)) { loadPluginFromRepository(fileName); - msPluginManager.startPlugin(pluginId); } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java index ae19ab3b83..88e72bb796 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java @@ -117,7 +117,6 @@ public class PluginService { // 从文件系统中加载插件 id = pluginLoadService.loadPlugin(file.getOriginalFilename()); - pluginLoadService.getMsPluginManager().startPlugin(id); plugin.setId(id); List extensions = pluginLoadService.getMsPluginManager().getExtensions(Driver.class, id);