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 77f7aa953e..c14a1326bd 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 @@ -70,6 +70,7 @@ public class PluginLoadService { /** * 从默认的对象存储下载插件到本地,再加载 + * * @param fileName * @return * @throws Exception @@ -78,15 +79,13 @@ public class PluginLoadService { MsFileUtils.validateFileName(fileName); String filePath = LocalRepositoryDir.getPluginDir() + "/" + fileName; File file = new File(filePath); - try { - if (!file.exists()) { - InputStream fileAsStream = FileCenter.getDefaultRepository() - .getFileAsStream(getDefaultRepositoryFileRequest(fileName)); + if (!file.exists()) { + try (InputStream fileAsStream = FileCenter.getDefaultRepository().getFileAsStream(getDefaultRepositoryFileRequest(fileName))) { FileUtils.copyInputStreamToFile(fileAsStream, file); + msPluginManager.loadPlugin(Paths.get(filePath)); + } catch (Exception e) { + LogUtils.error("从对象存储加载插件异常", e); } - msPluginManager.loadPlugin(Paths.get(filePath)); - } catch (Exception e) { - LogUtils.error("从对象存储加载插件异常", e); } } @@ -227,6 +226,7 @@ public class PluginLoadService { /** * 删除本地插件 + * * @param fileName */ public void deleteLocalPluginFile(String fileName) { @@ -258,9 +258,10 @@ public class PluginLoadService { /** * 获取插件中的是实现类列表 + * * @param clazz - * @return * @param + * @return */ public List getExtensions(Class clazz) { return msPluginManager.getExtensions(clazz); @@ -268,10 +269,11 @@ public class PluginLoadService { /** * 获取插件中的是实现类 + * * @param clazz * @param pluginId - * @return * @param + * @return */ public Class getExtensionsClass(Class clazz, String pluginId) { List> classes = msPluginManager.getExtensionClasses(clazz, pluginId); diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java index 773f9de287..ee87ab5e36 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java @@ -5,6 +5,7 @@ import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PluginScenarioType; import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.util.JSON; +import io.metersphere.sdk.util.LogUtils; import io.metersphere.system.base.BasePluginTestService; import io.metersphere.system.base.BaseTest; import io.metersphere.system.controller.param.PluginUpdateRequestDefinition; @@ -136,14 +137,14 @@ public class PluginControllerTests extends BaseTest { addPlugin = plugin; // 模拟其他节点加载插件 - pluginLoadService.handlePluginDeleteNotified(plugin.getId(), jarFile.getName()); + unloadAndDeletePlugin(jarFile, plugin); pluginLoadService.handlePluginAddNotified(plugin.getId(), jarFile.getName()); // 增加覆盖率 - pluginLoadService.handlePluginDeleteNotified(plugin.getId(), jarFile.getName()); + unloadAndDeletePlugin(jarFile, plugin); pluginLoadService.loadPlugin(jarFile.getName()); - pluginLoadService.handlePluginDeleteNotified(plugin.getId(), jarFile.getName()); + unloadAndDeletePlugin(jarFile, plugin); pluginLoadService.loadPlugins(); pluginLoadService.getExtensions(Platform.class); @@ -235,6 +236,14 @@ public class PluginControllerTests extends BaseTest { requestMultipartPermissionTest(PermissionConstants.SYSTEM_PLUGIN_ADD, DEFAULT_ADD, multiValueMap); } + private void unloadAndDeletePlugin(File jarFile, Plugin plugin) { + try { + pluginLoadService.handlePluginDeleteNotified(plugin.getId(), jarFile.getName()); + } catch (Exception e) { + LogUtils.error(e); + } + } + @Test @Order(2) public void update() throws Exception {