From 2ff45a4313861ea48ed2aee937036266604a5247 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti <65240126+fjtirado@users.noreply.github.com> Date: Thu, 22 Aug 2024 18:39:58 +0200 Subject: [PATCH] [Fix #3628] Avoiding concurrency issues with Instance and ServiceLoader (#3632) --- .../dynamic/ProcessInstanceDynamicCallHelper.java | 6 +++--- .../process/expr/ExpressionHandlerFactory.java | 9 ++++++--- .../ProcessInstanceDynamicCallsResource.java | 14 ++++++++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/addons/common/process-dynamic/src/main/java/org/kie/kogito/process/dynamic/ProcessInstanceDynamicCallHelper.java b/addons/common/process-dynamic/src/main/java/org/kie/kogito/process/dynamic/ProcessInstanceDynamicCallHelper.java index f4315135fb7..7c52c174aa2 100644 --- a/addons/common/process-dynamic/src/main/java/org/kie/kogito/process/dynamic/ProcessInstanceDynamicCallHelper.java +++ b/addons/common/process-dynamic/src/main/java/org/kie/kogito/process/dynamic/ProcessInstanceDynamicCallHelper.java @@ -18,9 +18,9 @@ */ package org.kie.kogito.process.dynamic; +import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.stream.Stream; import org.drools.core.common.InternalKnowledgeRuntime; import org.jbpm.process.instance.InternalProcessRuntime; @@ -41,8 +41,8 @@ public class ProcessInstanceDynamicCallHelper { private ProcessInstanceDynamicCallHelper() { } - public static void executeRestCall(RestWorkItemHandler handler, Stream> processes, String processId, String processInstanceId, RestCallInfo input) { - Process processDef = processes.filter(p -> p.id().equals(processId)).findAny().orElseThrow(() -> new IllegalArgumentException("Cannot find process " + processId)); + public static void executeRestCall(RestWorkItemHandler handler, Collection> processes, String processId, String processInstanceId, RestCallInfo input) { + Process processDef = processes.stream().filter(p -> p.id().equals(processId)).findAny().orElseThrow(() -> new IllegalArgumentException("Cannot find process " + processId)); WorkflowProcessInstance pi = (WorkflowProcessInstance) findProcessInstance(processDef, processInstanceId); WorkflowProcessImpl process = (WorkflowProcessImpl) pi.getProcess(); if (!process.isDynamic()) { diff --git a/jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/expr/ExpressionHandlerFactory.java b/jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/expr/ExpressionHandlerFactory.java index af66edf6e99..3becf03a8ad 100644 --- a/jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/expr/ExpressionHandlerFactory.java +++ b/jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/expr/ExpressionHandlerFactory.java @@ -18,15 +18,18 @@ */ package org.kie.kogito.process.expr; +import java.util.Collection; import java.util.Optional; import java.util.ServiceLoader; +import java.util.stream.Collectors; public class ExpressionHandlerFactory { private ExpressionHandlerFactory() { } - private static final ServiceLoader serviceLoader = ServiceLoader.load(ExpressionHandler.class); + private static final Collection expressionHandlers = ServiceLoader.load(ExpressionHandler.class).stream() + .map(ServiceLoader.Provider::get).collect(Collectors.toUnmodifiableList()); public static Expression get(String lang, String expr) { return getExpressionHandler(lang).orElseThrow( @@ -34,10 +37,10 @@ public static Expression get(String lang, String expr) { } public static boolean isSupported(String lang) { - return serviceLoader.stream().anyMatch(p -> p.get().lang().equals(lang)); + return expressionHandlers.stream().map(ExpressionHandler::lang).anyMatch(lang::equals); } private static Optional getExpressionHandler(String lang) { - return serviceLoader.stream().filter(p -> p.get().lang().equals(lang)).findFirst().map(p -> p.get()); + return expressionHandlers.stream().filter(p -> p.lang().equals(lang)).findFirst(); } } diff --git a/quarkus/addons/dynamic/runtime/src/main/java/org/kie/kogito/process/dynamic/ProcessInstanceDynamicCallsResource.java b/quarkus/addons/dynamic/runtime/src/main/java/org/kie/kogito/process/dynamic/ProcessInstanceDynamicCallsResource.java index 6729a5c1ac0..7ff60f52487 100644 --- a/quarkus/addons/dynamic/runtime/src/main/java/org/kie/kogito/process/dynamic/ProcessInstanceDynamicCallsResource.java +++ b/quarkus/addons/dynamic/runtime/src/main/java/org/kie/kogito/process/dynamic/ProcessInstanceDynamicCallsResource.java @@ -18,6 +18,9 @@ */ package org.kie.kogito.process.dynamic; +import java.util.Collection; +import java.util.stream.Collectors; + import org.kie.kogito.process.Process; import org.kogito.workitem.rest.RestWorkItemHandler; @@ -39,13 +42,17 @@ @Path("/_dynamic") public class ProcessInstanceDynamicCallsResource { - @Inject - Instance> processes; @Inject Vertx vertx; @Inject WebClientOptions sslOptions; private RestWorkItemHandler handler; + private Collection> processes; + + @Inject + ProcessInstanceDynamicCallsResource(Instance> processes) { + this.processes = processes.stream().collect(Collectors.toUnmodifiableList()); + } @PostConstruct void init() { @@ -57,8 +64,7 @@ void init() { @Consumes(MediaType.APPLICATION_JSON) @Path("/{processId}/{processInstanceId}/rest") public Response executeRestCall(@PathParam("processId") String processId, @PathParam("processInstanceId") String processInstanceId, RestCallInfo input) { - ProcessInstanceDynamicCallHelper.executeRestCall(handler, processes.stream(), processId, processInstanceId, input); + ProcessInstanceDynamicCallHelper.executeRestCall(handler, processes, processId, processInstanceId, input); return Response.status(200).build(); } - }