From 0ce134537db5f84da1ef019bd94cee73f72c54c6 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Wed, 18 Sep 2024 15:27:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E6=8F=92=E4=BB=B6SPI=E5=8F=AA=E5=8F=91=E7=8E=B0=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E5=AE=9E=E7=8E=B0=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sdk/plugin/MsPluginManager.java | 8 ++-- ... => MsServiceProviderExtensionFinder.java} | 44 ++++++++++++------- .../controller/PluginControllerTests.java | 2 +- 3 files changed, 31 insertions(+), 23 deletions(-) rename backend/framework/sdk/src/main/java/io/metersphere/sdk/plugin/{JdbcDriverServiceProviderExtensionFinder.java => MsServiceProviderExtensionFinder.java} (73%) diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/plugin/MsPluginManager.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/plugin/MsPluginManager.java index 8e9b9bf3ba..a4cbf37325 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/plugin/MsPluginManager.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/plugin/MsPluginManager.java @@ -5,16 +5,14 @@ import org.pf4j.*; /** * @author jianxing * 为了支持加载使用 SPI 机制加载的 jdbc 驱动 - * 这里加入自定义的 JdbcDriverServiceProviderExtensionFinder 和 JdbcDriverPluginDescriptorFinder + * 这里加入自定义的 MsServiceProviderExtensionFinder 和 JdbcDriverPluginDescriptorFinder */ public class MsPluginManager extends DefaultPluginManager { @Override protected ExtensionFinder createExtensionFinder() { DefaultExtensionFinder extensionFinder = (DefaultExtensionFinder) super.createExtensionFinder(); - // 添加 jdbc 驱动支持 - extensionFinder.add(new JdbcDriverServiceProviderExtensionFinder(this)); - // 添加 SPI 支持 - extensionFinder.addServiceProviderExtensionFinder(); + // 添加 SPI 发现机制,只发现指定实现类 + extensionFinder.add(new MsServiceProviderExtensionFinder(this)); return extensionFinder; } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/plugin/JdbcDriverServiceProviderExtensionFinder.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/plugin/MsServiceProviderExtensionFinder.java similarity index 73% rename from backend/framework/sdk/src/main/java/io/metersphere/sdk/plugin/JdbcDriverServiceProviderExtensionFinder.java rename to backend/framework/sdk/src/main/java/io/metersphere/sdk/plugin/MsServiceProviderExtensionFinder.java index f73433ff08..2aedb8d835 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/plugin/JdbcDriverServiceProviderExtensionFinder.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/plugin/MsServiceProviderExtensionFinder.java @@ -21,18 +21,22 @@ import java.util.*; /** * @author jianxing - * 支持加载 jdbc 驱动 + * 支持加载 jdbc 驱动和 xstream 的 Converter * pf4j 中 ServiceProviderExtensionFinder 本身是支持 SPI * 默认会读取 META-INF/services 下的文件 * 但是遍历 JarEntry 发现 jdbc 资源中没有 META-INF/services 只有 META-INF/services/java.sql.Driver * 所以使用默认的 ServiceProviderExtensionFinder 会无法加载,这里重写后只修改了 EXTENSIONS_RESOURCE */ -public class JdbcDriverServiceProviderExtensionFinder extends ServiceProviderExtensionFinder { +public class MsServiceProviderExtensionFinder extends ServiceProviderExtensionFinder { - // 重写后只修改了这个常量 - public static final String EXTENSIONS_RESOURCE = ServiceProviderExtensionStorage.EXTENSIONS_RESOURCE + "/java.sql.Driver"; + public static final String EXTENSIONS_RESOURCE = ServiceProviderExtensionStorage.EXTENSIONS_RESOURCE; - public JdbcDriverServiceProviderExtensionFinder(PluginManager pluginManager) { + /** + * 需要支持的 SPI 文件 + */ + public static final List SPI_FILES = List.of("java.sql.Driver", "com.thoughtworks.xstream.converters.Converter"); + + public MsServiceProviderExtensionFinder(PluginManager pluginManager) { super(pluginManager); } @@ -43,11 +47,14 @@ public class JdbcDriverServiceProviderExtensionFinder extends ServiceProviderExt final Set bucket = new HashSet<>(); try { - Enumeration urls = getClass().getClassLoader().getResources(EXTENSIONS_RESOURCE); - if (urls.hasMoreElements()) { - jdbcCollectExtensions(urls, bucket); - } else { - LogUtils.debug("Cannot find '{}'", EXTENSIONS_RESOURCE); + for (String spiFile : SPI_FILES) { + String spiResource = EXTENSIONS_RESOURCE + "/" + spiFile; + Enumeration urls = getClass().getClassLoader().getResources(spiResource); + if (urls.hasMoreElements()) { + jdbcCollectExtensions(urls, bucket); + } else { + LogUtils.debug("Cannot find '{}'", spiResource); + } } debugExtensions(bucket); @@ -71,11 +78,14 @@ public class JdbcDriverServiceProviderExtensionFinder extends ServiceProviderExt LogUtils.debug("Reading extensions storages for plugin '{}'", pluginId); final Set bucket = new HashSet<>(); try { - Enumeration urls = ((PluginClassLoader) plugin.getPluginClassLoader()).findResources(EXTENSIONS_RESOURCE); - if (urls.hasMoreElements()) { - jdbcCollectExtensions(urls, bucket); - } else { - LogUtils.debug("Cannot find '{}'", EXTENSIONS_RESOURCE); + for (String spiFile : SPI_FILES) { + String spiResource = EXTENSIONS_RESOURCE + "/" + spiFile; + Enumeration urls = ((PluginClassLoader) plugin.getPluginClassLoader()).findResources(spiResource); + if (urls.hasMoreElements()) { + jdbcCollectExtensions(urls, bucket); + } else { + LogUtils.debug("Cannot find '{}'", spiResource); + } } debugExtensions(bucket); @@ -101,7 +111,7 @@ public class JdbcDriverServiceProviderExtensionFinder extends ServiceProviderExt Path extensionPath; if (url.toURI().getScheme().equals("jar")) { - extensionPath = FileUtils.getPath(url.toURI(), EXTENSIONS_RESOURCE); + extensionPath = FileUtils.getPath(url.toURI(), EXTENSIONS_RESOURCE + "/" + url.getPath().substring(url.getPath().lastIndexOf("/") + 1)); } else { extensionPath = Paths.get(url.toURI()); } @@ -115,7 +125,7 @@ public class JdbcDriverServiceProviderExtensionFinder extends ServiceProviderExt private Set jdbcReadExtensions(Path extensionPath) throws IOException { final Set result = new HashSet<>(); - Files.walkFileTree(extensionPath, Collections.emptySet(), 1, new SimpleFileVisitor() { + Files.walkFileTree(extensionPath, Collections.emptySet(), 1, new SimpleFileVisitor<>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 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 faf6f0ef4c..a46befcc4d 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 @@ -187,7 +187,7 @@ public class PluginControllerTests extends BaseTest { ); this.requestMultipartWithOkAndReturn(DEFAULT_ADD, getDefaultMultiPartParam(request, myDriver)); - Assertions.assertEquals(jdbcDriverPluginService.getJdbcDriverClass(DEFAULT_ORGANIZATION_ID), Arrays.asList("io.jianxing.MyDriver", "io.jianxing.MyDriver", "com.mysql.cj.jdbc.Driver")); + Assertions.assertEquals(jdbcDriverPluginService.getJdbcDriverClass(DEFAULT_ORGANIZATION_ID), Arrays.asList("io.jianxing.MyDriver", "com.mysql.cj.jdbc.Driver")); // 校验QUOTA动上传成功 request.setName("cloud-quota-plugin");