refactor(接口测试): 插件SPI只发现指定实现类

This commit is contained in:
AgAngle 2024-09-18 15:27:10 +08:00 committed by jianxing
parent bbb11660e9
commit 0ce134537d
3 changed files with 31 additions and 23 deletions

View File

@ -5,16 +5,14 @@ import org.pf4j.*;
/** /**
* @author jianxing * @author jianxing
* 为了支持加载使用 SPI 机制加载的 jdbc 驱动 * 为了支持加载使用 SPI 机制加载的 jdbc 驱动
* 这里加入自定义的 JdbcDriverServiceProviderExtensionFinder JdbcDriverPluginDescriptorFinder * 这里加入自定义的 MsServiceProviderExtensionFinder JdbcDriverPluginDescriptorFinder
*/ */
public class MsPluginManager extends DefaultPluginManager { public class MsPluginManager extends DefaultPluginManager {
@Override @Override
protected ExtensionFinder createExtensionFinder() { protected ExtensionFinder createExtensionFinder() {
DefaultExtensionFinder extensionFinder = (DefaultExtensionFinder) super.createExtensionFinder(); DefaultExtensionFinder extensionFinder = (DefaultExtensionFinder) super.createExtensionFinder();
// 添加 jdbc 驱动支持 // 添加 SPI 发现机制只发现指定实现类
extensionFinder.add(new JdbcDriverServiceProviderExtensionFinder(this)); extensionFinder.add(new MsServiceProviderExtensionFinder(this));
// 添加 SPI 支持
extensionFinder.addServiceProviderExtensionFinder();
return extensionFinder; return extensionFinder;
} }

View File

@ -21,18 +21,22 @@ import java.util.*;
/** /**
* @author jianxing * @author jianxing
* 支持加载 jdbc 驱动 * 支持加载 jdbc 驱动 xstream Converter
* pf4j ServiceProviderExtensionFinder 本身是支持 SPI * pf4j ServiceProviderExtensionFinder 本身是支持 SPI
* 默认会读取 META-INF/services 下的文件 * 默认会读取 META-INF/services 下的文件
* 但是遍历 JarEntry 发现 jdbc 资源中没有 META-INF/services 只有 META-INF/services/java.sql.Driver * 但是遍历 JarEntry 发现 jdbc 资源中没有 META-INF/services 只有 META-INF/services/java.sql.Driver
* 所以使用默认的 ServiceProviderExtensionFinder 会无法加载这里重写后只修改了 EXTENSIONS_RESOURCE * 所以使用默认的 ServiceProviderExtensionFinder 会无法加载这里重写后只修改了 EXTENSIONS_RESOURCE
*/ */
public class JdbcDriverServiceProviderExtensionFinder extends ServiceProviderExtensionFinder { public class MsServiceProviderExtensionFinder extends ServiceProviderExtensionFinder {
// 重写后只修改了这个常量 public static final String EXTENSIONS_RESOURCE = ServiceProviderExtensionStorage.EXTENSIONS_RESOURCE;
public static final String EXTENSIONS_RESOURCE = ServiceProviderExtensionStorage.EXTENSIONS_RESOURCE + "/java.sql.Driver";
public JdbcDriverServiceProviderExtensionFinder(PluginManager pluginManager) { /**
* 需要支持的 SPI 文件
*/
public static final List<String> SPI_FILES = List.of("java.sql.Driver", "com.thoughtworks.xstream.converters.Converter");
public MsServiceProviderExtensionFinder(PluginManager pluginManager) {
super(pluginManager); super(pluginManager);
} }
@ -43,11 +47,14 @@ public class JdbcDriverServiceProviderExtensionFinder extends ServiceProviderExt
final Set<String> bucket = new HashSet<>(); final Set<String> bucket = new HashSet<>();
try { try {
Enumeration<URL> urls = getClass().getClassLoader().getResources(EXTENSIONS_RESOURCE); for (String spiFile : SPI_FILES) {
if (urls.hasMoreElements()) { String spiResource = EXTENSIONS_RESOURCE + "/" + spiFile;
jdbcCollectExtensions(urls, bucket); Enumeration<URL> urls = getClass().getClassLoader().getResources(spiResource);
} else { if (urls.hasMoreElements()) {
LogUtils.debug("Cannot find '{}'", EXTENSIONS_RESOURCE); jdbcCollectExtensions(urls, bucket);
} else {
LogUtils.debug("Cannot find '{}'", spiResource);
}
} }
debugExtensions(bucket); debugExtensions(bucket);
@ -71,11 +78,14 @@ public class JdbcDriverServiceProviderExtensionFinder extends ServiceProviderExt
LogUtils.debug("Reading extensions storages for plugin '{}'", pluginId); LogUtils.debug("Reading extensions storages for plugin '{}'", pluginId);
final Set<String> bucket = new HashSet<>(); final Set<String> bucket = new HashSet<>();
try { try {
Enumeration<URL> urls = ((PluginClassLoader) plugin.getPluginClassLoader()).findResources(EXTENSIONS_RESOURCE); for (String spiFile : SPI_FILES) {
if (urls.hasMoreElements()) { String spiResource = EXTENSIONS_RESOURCE + "/" + spiFile;
jdbcCollectExtensions(urls, bucket); Enumeration<URL> urls = ((PluginClassLoader) plugin.getPluginClassLoader()).findResources(spiResource);
} else { if (urls.hasMoreElements()) {
LogUtils.debug("Cannot find '{}'", EXTENSIONS_RESOURCE); jdbcCollectExtensions(urls, bucket);
} else {
LogUtils.debug("Cannot find '{}'", spiResource);
}
} }
debugExtensions(bucket); debugExtensions(bucket);
@ -101,7 +111,7 @@ public class JdbcDriverServiceProviderExtensionFinder extends ServiceProviderExt
Path extensionPath; Path extensionPath;
if (url.toURI().getScheme().equals("jar")) { 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 { } else {
extensionPath = Paths.get(url.toURI()); extensionPath = Paths.get(url.toURI());
} }
@ -115,7 +125,7 @@ public class JdbcDriverServiceProviderExtensionFinder extends ServiceProviderExt
private Set<String> jdbcReadExtensions(Path extensionPath) throws IOException { private Set<String> jdbcReadExtensions(Path extensionPath) throws IOException {
final Set<String> result = new HashSet<>(); final Set<String> result = new HashSet<>();
Files.walkFileTree(extensionPath, Collections.<FileVisitOption>emptySet(), 1, new SimpleFileVisitor<Path>() { Files.walkFileTree(extensionPath, Collections.emptySet(), 1, new SimpleFileVisitor<>() {
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

View File

@ -187,7 +187,7 @@ public class PluginControllerTests extends BaseTest {
); );
this.requestMultipartWithOkAndReturn(DEFAULT_ADD, this.requestMultipartWithOkAndReturn(DEFAULT_ADD,
getDefaultMultiPartParam(request, myDriver)); 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动上传成功 // 校验QUOTA动上传成功
request.setName("cloud-quota-plugin"); request.setName("cloud-quota-plugin");