Skip to content

Commit

Permalink
[Fix apache#3615] Moving common stuff to common addons
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Aug 13, 2024
1 parent d6af3a5 commit c853229
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 102 deletions.
1 change: 1 addition & 0 deletions addons/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<module>rest-exception-handler</module>
<module>process-svg</module>
<module>process-management</module>
<module>process-dynamic</module>
<module>knative</module>
<module>kubernetes</module>
<module>kubernetes-service-catalog</module>
Expand Down
43 changes: 43 additions & 0 deletions addons/common/process-dynamic/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.kie</groupId>
<artifactId>kogito-addons-common-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>
<artifactId>kie-addons-process-dynamic</artifactId>
<name>KIE Add-On Process Instance Dynamic Calls</name>
<description>Allow performing dynamic calls for existing process instances</description>
<properties>
<java.module.name>org.kie.process.dynamic</java.module.name>
</properties>
<dependencies>

<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-rest-workitem</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.kogito.process.dynamic;

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;
import org.jbpm.process.instance.KogitoProcessContextImpl;
import org.jbpm.process.instance.ProcessInstance;
import org.jbpm.util.ContextFactory;
import org.jbpm.workflow.core.impl.WorkflowProcessImpl;
import org.jbpm.workflow.instance.WorkflowProcessInstance;
import org.jbpm.workflow.instance.node.DynamicUtils;
import org.kie.kogito.Model;
import org.kie.kogito.process.Process;
import org.kie.kogito.process.expr.ExpressionHandlerFactory;
import org.kie.kogito.process.impl.AbstractProcessInstance;
import org.kogito.workitem.rest.RestWorkItemHandler;

public class ProcessInstanceDynamicCallHelper {

private ProcessInstanceDynamicCallHelper() {
}

public static void executeRestCall(RestWorkItemHandler handler, Stream<Process<?>> processes, String processId, String processInstanceId, RestCallInfo input) {
Process<?> processDef = processes.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()) {
process.setDynamic(true);
}
InternalKnowledgeRuntime runtime = pi.getKnowledgeRuntime();
InternalProcessRuntime.asKogitoProcessRuntime(runtime).getKogitoWorkItemManager().registerWorkItemHandler(RestWorkItemHandler.REST_TASK_TYPE, handler);
Map<String, Object> parameters = input.getArguments() == null ? new HashMap<>() : new HashMap<>(input.getArguments());
if (input.getHost() != null) {
parameters.put(RestWorkItemHandler.HOST, input.getHost());
}
if (input.getPort() != null) {
parameters.put(RestWorkItemHandler.PORT, input.getPort());
}
if (input.getMethod() != null) {
parameters.put(RestWorkItemHandler.METHOD, input.getMethod());
}
if (input.getEndpoint() != null) {
parameters.put(RestWorkItemHandler.URL, input.getEndpoint());
}
parameters.put(RestWorkItemHandler.PATH_PARAM_RESOLVER, new DynamicPathParamResolver(processInstanceId));
WorkItemHandlerResultHolder holder = new WorkItemHandlerResultHolder();
parameters.put(RestWorkItemHandler.RESULT_HANDLER, holder);
KogitoProcessContextImpl context = ContextFactory.fromItem(DynamicUtils.addDynamicWorkItem(pi, runtime, RestWorkItemHandler.REST_TASK_TYPE, parameters));
Model model = ((Model) processDef.createModel());
model.update(input.getOutputExpression() != null
? ExpressionHandlerFactory.get(input.getOutputExpressionLang(), input.getOutputExpression()).eval(holder.getResult(), Map.class, context)
: holder.getResult());
((AbstractProcessInstance) pi.unwrap()).updateVariablesPartially(model);
}

private static ProcessInstance findProcessInstance(Process<?> process, String processInstanceId) {
return process.instances()
.findById(processInstanceId).map(AbstractProcessInstance.class::cast)
.map(AbstractProcessInstance::internalGetProcessInstance)
.orElseThrow(() -> new IllegalArgumentException("Cannot find process instance " + processInstanceId + " for process " + process.id()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@

import java.util.Map;

import jakarta.validation.constraints.NotNull;

public class RestCallInfo {

@NotNull
private String endpoint;
private String host;
private Integer port;
@NotNull
private String method;
private Map<String, Object> arguments;
private String outputExpression;
Expand Down
11 changes: 11 additions & 0 deletions kogito-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,17 @@
<classifier>sources</classifier>
</dependency>

<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-addons-process-dynamic</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-addons-process-dynamic</artifactId>
<version>${project.version}</version>
<classifier>sources</classifier>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-addons-quarkus-process-dynamic</artifactId>
Expand Down
32 changes: 0 additions & 32 deletions quarkus/addons/dynamic/integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,38 +56,6 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*Test</include>
</includes>
</configuration>
</plugin>

</plugins>
</build>

<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ mp.messaging.incoming.resume.path=/resume
mp.messaging.outgoing.resume-out.connector=quarkus-http
mp.messaging.outgoing.resume-out.url=http://localhost:8081/resume

quarkus.http.port=8081

quarkus.devservices.enabled = false
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;

import static io.restassured.RestAssured.*;

@QuarkusIntegrationTest
@QuarkusTest
public class DynamicCallResourceTest {

@Test
Expand Down
14 changes: 2 additions & 12 deletions quarkus/addons/dynamic/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,8 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-rest-workitem</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
<groupId>org.kie</groupId>
<artifactId>kie-addons-process-dynamic</artifactId>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,7 @@
*/
package org.kie.kogito.process.dynamic;

import java.util.HashMap;
import java.util.Map;

import org.drools.core.common.InternalKnowledgeRuntime;
import org.jbpm.process.instance.InternalProcessRuntime;
import org.jbpm.process.instance.KogitoProcessContextImpl;
import org.jbpm.process.instance.ProcessInstance;
import org.jbpm.util.ContextFactory;
import org.jbpm.workflow.core.impl.WorkflowProcessImpl;
import org.jbpm.workflow.instance.WorkflowProcessInstance;
import org.jbpm.workflow.instance.node.DynamicUtils;
import org.kie.kogito.Model;
import org.kie.kogito.process.Process;
import org.kie.kogito.process.expr.ExpressionHandlerFactory;
import org.kie.kogito.process.impl.AbstractProcessInstance;
import org.kogito.workitem.rest.RestWorkItemHandler;

import io.vertx.ext.web.client.WebClientOptions;
Expand Down Expand Up @@ -71,43 +57,8 @@ void init() {
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{processId}/{processInstanceId}/rest")
public Response executeRestCall(@PathParam("processId") String processId, @PathParam("processInstanceId") 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()) {
process.setDynamic(true);
}
InternalKnowledgeRuntime runtime = pi.getKnowledgeRuntime();
InternalProcessRuntime.asKogitoProcessRuntime(runtime).getKogitoWorkItemManager().registerWorkItemHandler(RestWorkItemHandler.REST_TASK_TYPE, handler);
Map<String, Object> parameters = input.getArguments() == null ? new HashMap<>() : new HashMap<>(input.getArguments());
if (input.getHost() != null) {
parameters.put(RestWorkItemHandler.HOST, input.getHost());
}
if (input.getPort() != null) {
parameters.put(RestWorkItemHandler.PORT, input.getPort());
}
if (input.getMethod() != null) {
parameters.put(RestWorkItemHandler.METHOD, input.getMethod());
}
if (input.getEndpoint() != null) {
parameters.put(RestWorkItemHandler.URL, input.getEndpoint());
}
parameters.put(RestWorkItemHandler.PATH_PARAM_RESOLVER, new DynamicPathParamResolver(processInstanceId));
WorkItemHandlerResultHolder holder = new WorkItemHandlerResultHolder();
parameters.put(RestWorkItemHandler.RESULT_HANDLER, holder);
KogitoProcessContextImpl context = ContextFactory.fromItem(DynamicUtils.addDynamicWorkItem(pi, runtime, RestWorkItemHandler.REST_TASK_TYPE, parameters));
Model model = ((Model) processDef.createModel());
model.update(input.getOutputExpression() != null
? ExpressionHandlerFactory.get(input.getOutputExpressionLang(), input.getOutputExpression()).eval(holder.getResult(), Map.class, context)
: holder.getResult());
((AbstractProcessInstance) pi.unwrap()).updateVariablesPartially(model);
ProcessInstanceDynamicCallHelper.executeRestCall(handler, processes.stream(), processId, processInstanceId, input);
return Response.status(200).build();
}

private ProcessInstance findProcessInstance(Process<?> process, String processInstanceId) {
return process.instances()
.findById(processInstanceId).map(AbstractProcessInstance.class::cast)
.map(AbstractProcessInstance::internalGetProcessInstance)
.orElseThrow(() -> new IllegalArgumentException("Cannot find process instance " + processInstanceId + " for process " + process.id()));
}
}

0 comments on commit c853229

Please sign in to comment.