From 0cefb29cffe08738b08e68b00298d1b731ca379b Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Thu, 13 Jul 2023 11:59:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8F=92=E4=BB=B6=E5=88=A0=E9=99=A4=E5=90=8E?= =?UTF-8?q?=E5=86=8D=E6=AC=A1=E4=B8=8A=E4=BC=A0=E6=8F=92=E4=BB=B6id?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1027677 --user=王孝刚 【接口测试】上传mqtt插件版本显示错误,调试mqtt协议的场景,协议信息显示异常,调试会提示资源池配置异常 https://www.tapd.cn/55049933/s/1392904 --- .../metersphere/service/ApiPluginService.java | 70 ++++++++----------- 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/system-setting/backend/src/main/java/io/metersphere/service/ApiPluginService.java b/system-setting/backend/src/main/java/io/metersphere/service/ApiPluginService.java index e236d4b9a0..b2af6bc49d 100644 --- a/system-setting/backend/src/main/java/io/metersphere/service/ApiPluginService.java +++ b/system-setting/backend/src/main/java/io/metersphere/service/ApiPluginService.java @@ -14,22 +14,18 @@ import io.metersphere.dto.PluginResourceDTO; import io.metersphere.i18n.Translator; import io.metersphere.plugin.core.api.UiScriptApi; import io.metersphere.plugin.core.ui.PluginResource; +import io.metersphere.plugin.loader.PluginClassLoader; +import io.metersphere.plugin.loader.PluginManager; import io.metersphere.utils.CommonUtil; +import io.metersphere.utils.PluginManagerUtil; import jakarta.annotation.Resource; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.jorphan.reflect.ClassFinder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; -import java.io.File; -import java.lang.reflect.Method; -import java.net.URL; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.UUID; +import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -39,13 +35,13 @@ public class ApiPluginService { @Resource private PluginMapper pluginMapper; - + PluginManager pluginManager = new PluginManager(); public List addApiPlugin(MultipartFile file) { String id = UUID.randomUUID().toString(); String path = FileUtils.create(id, file); List addPlugins = new ArrayList<>(); if (StringUtils.isNotEmpty(path)) { - List resources = this.getMethod(path, file.getOriginalFilename()); + List resources = this.getMethod(path, file.getOriginalFilename(), file); if (CollectionUtils.isNotEmpty(resources)) { for (PluginResourceDTO resource : resources) { PluginExample example = new PluginExample(); @@ -63,30 +59,6 @@ public class ApiPluginService { return addPlugins; } - private boolean loadJar(String jarPath) { - try { - ClassLoader classLoader = ClassLoader.getSystemClassLoader(); - try { - File file = new File(jarPath); - if (!file.exists()) { - return false; - } - Method method = classLoader.getClass().getDeclaredMethod("addURL", URL.class); - method.setAccessible(true); - method.invoke(classLoader, file.toURI().toURL()); - } catch (NoSuchMethodException e) { - Method method = classLoader.getClass() - .getDeclaredMethod("appendToClassPathForInstrumentation", String.class); - method.setAccessible(true); - method.invoke(classLoader, jarPath); - } - return true; - } catch (Exception e) { - LogUtil.error(e); - } - return false; - } - /** * 校验是否是平台插件,避免被系统类加载器加载后,无法卸载,影响功能 * @param jarPath @@ -108,31 +80,47 @@ public class ApiPluginService { } } - private List getMethod(String path, String fileName) { + private List getMethod(String path, String fileName, MultipartFile file) { List resources = new LinkedList<>(); validatePluginType(path); try { - this.loadJar(path); - String jarPath[] = new String[]{path}; - List classes = ClassFinder.findClassesThatExtend(jarPath, new Class[]{UiScriptApi.class}, true); - for (String clazzName : classes) { - UiScriptApi uiScriptApi = Class.forName(clazzName) + PluginManagerUtil.loadPlugin(path, pluginManager,file); + PluginClassLoader classLoader = pluginManager.getClassLoader(path); + List> classes = this.findImplementations(UiScriptApi.class, classLoader.getClazzSet()); + for (Class clazzName : classes) { + UiScriptApi uiScriptApi = classLoader.loadClass(clazzName.getName()) .asSubclass(UiScriptApi.class) .getDeclaredConstructor().newInstance(); PluginResource pluginObj = uiScriptApi.init(); if (pluginObj != null) { PluginResourceDTO pluginResourceDTO = new PluginResourceDTO(); BeanUtils.copyBean(pluginResourceDTO, pluginObj); - pluginResourceDTO.setEntry(clazzName); + pluginResourceDTO.setEntry(clazzName.getName()); resources.add(pluginResourceDTO); } } + pluginManager.deletePlugin(path); } catch (Exception e) { this.init(fileName, resources); } return resources; } + public List> findImplementations(Class interfaceClass, Set allClasses) { + List> implementations = new ArrayList<>(); + + try { + for (Class clazz : allClasses) { + if (interfaceClass.isAssignableFrom(clazz) && !clazz.equals(interfaceClass)) { + implementations.add(clazz); + } + } + } catch (Exception e) { + LogUtil.error("获取jar包类异常:" + e); + } + + return implementations; + } private void init(String fileName, List resources) { List> classes = CommonUtil.getSubClass(fileName); try {