This is an automated email from the ASF dual-hosted git repository.
terrymanu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new adb00290890 Reuse TypedSPILoader for MCP workflow algorithm type
checks (#39918)
adb00290890 is described below
commit adb00290890a50168f75a2f3b68accd5483e8acd
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Sep 22 09:36:43 2026 +0800
Reuse TypedSPILoader for MCP workflow algorithm type checks (#39918)
---
.../workflow/service/WorkflowAlgorithmUtils.java | 23 +---------------------
.../service/WorkflowAlgorithmUtilsTest.java | 14 ++++++++++---
2 files changed, 12 insertions(+), 25 deletions(-)
diff --git
a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowAlgorithmUtils.java
b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowAlgorithmUtils.java
index 60fa77b9a99..e9b79bb1b31 100644
---
a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowAlgorithmUtils.java
+++
b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowAlgorithmUtils.java
@@ -20,7 +20,6 @@ package
org.apache.shardingsphere.mcp.support.workflow.service;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import
org.apache.shardingsphere.infra.exception.external.ShardingSphereExternalException;
-import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.util.json.JsonEngine;
@@ -154,7 +153,7 @@ public final class WorkflowAlgorithmUtils {
return true;
}
if (hasSecretReference(properties)) {
- return containsServiceType(serviceInterface, actualAlgorithmType);
+ return TypedSPILoader.containsService(serviceInterface,
actualAlgorithmType);
}
try {
TypedSPILoader.checkService(serviceInterface, actualAlgorithmType,
createProperties(properties));
@@ -269,24 +268,4 @@ public final class WorkflowAlgorithmUtils {
private static String trimToEmpty(final String value) {
return null == value ? "" : value.trim();
}
-
- private static <T extends TypedSPI> boolean containsServiceType(final
Class<T> serviceInterface, final String algorithmType) {
- for (T each :
ShardingSphereServiceLoader.getServiceInstances(serviceInterface)) {
- if (matchesType(algorithmType, each)) {
- return true;
- }
- }
- return false;
- }
-
- private static boolean matchesType(final String type, final TypedSPI
instance) {
- Object instanceType = instance.getType();
- if (null == instanceType) {
- return false;
- }
- if (instanceType instanceof String) {
- return instanceType.toString().equalsIgnoreCase(type) ||
instance.getTypeAliases().contains(type);
- }
- return instanceType.equals(type) ||
instance.getTypeAliases().contains(type);
- }
}
diff --git
a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowAlgorithmUtilsTest.java
b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowAlgorithmUtilsTest.java
index 4e2090fb06d..d4c95b6dd2c 100644
---
a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowAlgorithmUtilsTest.java
+++
b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowAlgorithmUtilsTest.java
@@ -37,7 +37,13 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
class WorkflowAlgorithmUtilsTest {
@@ -153,12 +159,14 @@ class WorkflowAlgorithmUtilsTest {
@Test
void assertSecretReferenceOnlyRequiresTypeAvailability() {
+ TypedSPIFixture fixture = spy(new TypedSPIFixture());
try (
- MockedStatic<TypedSPILoader> typedSPILoader =
mockStatic(TypedSPILoader.class);
+ MockedStatic<TypedSPILoader> typedSPILoader =
mockStatic(TypedSPILoader.class, CALLS_REAL_METHODS);
MockedStatic<ShardingSphereServiceLoader> serviceLoader =
mockStatic(ShardingSphereServiceLoader.class)) {
- serviceLoader.when(() ->
ShardingSphereServiceLoader.getServiceInstances(TypedSPIFixture.class)).thenReturn(List.of(new
TypedSPIFixture()));
+ serviceLoader.when(() ->
ShardingSphereServiceLoader.getServiceInstances(TypedSPIFixture.class)).thenReturn(List.of(fixture));
assertTrue(WorkflowAlgorithmUtils.isAlgorithmServiceAvailable(TypedSPIFixture.class,
"fixture", Map.of("secret-key", "secret_reference:primary.secret-key")));
- typedSPILoader.verifyNoInteractions();
+ typedSPILoader.verify(() ->
TypedSPILoader.checkService(eq(TypedSPIFixture.class), eq("fixture"), any()),
never());
+ verify(fixture, never()).init(any());
}
}