Skip to content

Commit 554df0b

Browse files
committed
Adding java lambda support
Signed-off-by: fjtirado <[email protected]>
1 parent 14d25e9 commit 554df0b

File tree

22 files changed

+984
-67
lines changed

22 files changed

+984
-67
lines changed

experimental/lambda/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>io.serverlessworkflow</groupId>
5+
<artifactId>serverlessworkflow-experimental</artifactId>
6+
<version>8.0.0-SNAPSHOT</version>
7+
</parent>
8+
<artifactId>serverlessworkflow-experimental-lambda</artifactId>
9+
<name>ServelessWorkflow:: Experimental:: lambda</name>
10+
<dependencies>
11+
<dependency>
12+
<groupId>io.serverlessworkflow</groupId>
13+
<artifactId>serverlessworkflow-experimental-types</artifactId>
14+
</dependency>
15+
<dependency>
16+
<groupId>io.serverlessworkflow</groupId>
17+
<artifactId>serverlessworkflow-impl-core</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.junit.jupiter</groupId>
21+
<artifactId>junit-jupiter-api</artifactId>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter-engine</artifactId>
27+
<scope>test</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter-params</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.assertj</groupId>
36+
<artifactId>assertj-core</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>ch.qos.logback</groupId>
41+
<artifactId>logback-classic</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.executors;
17+
18+
import io.serverlessworkflow.api.types.CallJava;
19+
import io.serverlessworkflow.api.types.TaskBase;
20+
import io.serverlessworkflow.impl.TaskContext;
21+
import io.serverlessworkflow.impl.WorkflowApplication;
22+
import io.serverlessworkflow.impl.WorkflowContext;
23+
import io.serverlessworkflow.impl.WorkflowModel;
24+
import io.serverlessworkflow.impl.WorkflowModelFactory;
25+
import io.serverlessworkflow.impl.resources.ResourceLoader;
26+
import java.util.concurrent.CompletableFuture;
27+
28+
public class JavaCallExecutor implements CallableTask<CallJava> {
29+
30+
@Override
31+
public void init(CallJava task, WorkflowApplication application, ResourceLoader loader) {}
32+
33+
@Override
34+
public CompletableFuture<WorkflowModel> apply(
35+
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
36+
WorkflowModelFactory modelFactory = workflowContext.definition().application().modelFactory();
37+
if (taskContext.task() instanceof CallJava.CallJavaConsumer consumer) {
38+
consumer.consumer().accept(input.asJavaObject());
39+
} else if (taskContext.task() instanceof CallJava.CallJavaFunction function) {
40+
return CompletableFuture.completedFuture(
41+
modelFactory.fromAny(function.function().apply(input.asJavaObject())));
42+
}
43+
return CompletableFuture.completedFuture(input);
44+
}
45+
46+
@Override
47+
public boolean accept(Class<? extends TaskBase> clazz) {
48+
return CallJava.class.isAssignableFrom(clazz);
49+
}
50+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.expressions;
17+
18+
import io.serverlessworkflow.impl.TaskContext;
19+
import io.serverlessworkflow.impl.WorkflowContext;
20+
import io.serverlessworkflow.impl.WorkflowFilter;
21+
import io.serverlessworkflow.impl.WorkflowModel;
22+
import io.serverlessworkflow.impl.WorkflowModelFactory;
23+
import java.util.function.BiFunction;
24+
import java.util.function.BiPredicate;
25+
import java.util.function.Function;
26+
27+
public class JavaExpressionFactory implements ExpressionFactory {
28+
29+
private final WorkflowModelFactory modelFactory = new JavaModelFactory();
30+
private final Expression dummyExpression =
31+
new Expression() {
32+
@Override
33+
public WorkflowModel eval(
34+
WorkflowContext workflowContext, TaskContext context, WorkflowModel model) {
35+
return model;
36+
}
37+
};
38+
39+
@Override
40+
public Expression buildExpression(String expression) {
41+
return dummyExpression;
42+
}
43+
44+
@Override
45+
public WorkflowFilter buildFilter(String expr, Object value) {
46+
if (value instanceof Function func) {
47+
return (w, t, n) -> modelFactory.fromAny(func.apply(n.asJavaObject()));
48+
} else if (value instanceof BiPredicate pred) {
49+
return (w, t, n) -> modelFactory.from(pred.test(w, t));
50+
} else if (value instanceof BiFunction func) {
51+
return (w, t, n) -> modelFactory.fromAny(func.apply(w, t));
52+
} else if (value instanceof WorkflowFilter filter) {
53+
return filter;
54+
} else {
55+
return (w, t, n) -> modelFactory.fromAny(value);
56+
}
57+
}
58+
59+
@Override
60+
public WorkflowModelFactory modelFactory() {
61+
return modelFactory;
62+
}
63+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.expressions;
17+
18+
import io.cloudevents.CloudEventData;
19+
import io.serverlessworkflow.impl.WorkflowModel;
20+
import java.time.OffsetDateTime;
21+
import java.util.Collection;
22+
import java.util.Collections;
23+
import java.util.Map;
24+
import java.util.Optional;
25+
import java.util.function.BiConsumer;
26+
27+
public class JavaModel implements WorkflowModel {
28+
29+
private Object object;
30+
31+
static final JavaModel TrueModel = new JavaModel(Boolean.TRUE);
32+
static final JavaModel FalseModel = new JavaModel(Boolean.FALSE);
33+
static final JavaModel NullModel = new JavaModel(null);
34+
35+
JavaModel(Object object) {
36+
this.object = object;
37+
}
38+
39+
@Override
40+
public void forEach(BiConsumer<String, WorkflowModel> consumer) {
41+
asMap()
42+
.ifPresent(
43+
m ->
44+
m.forEach(
45+
(k, v) ->
46+
consumer.accept(
47+
k, v instanceof WorkflowModel model ? model : new JavaModel(v))));
48+
}
49+
50+
@Override
51+
public Optional<Boolean> asBoolean() {
52+
return object instanceof Boolean value ? Optional.of(value) : Optional.empty();
53+
}
54+
55+
@Override
56+
public Collection<WorkflowModel> asCollection() {
57+
return object instanceof Collection value
58+
? new JavaModelCollection(value)
59+
: Collections.emptyList();
60+
}
61+
62+
@Override
63+
public Optional<String> asText() {
64+
return object instanceof String value ? Optional.of(value) : Optional.empty();
65+
}
66+
67+
@Override
68+
public Optional<OffsetDateTime> asDate() {
69+
return object instanceof OffsetDateTime value ? Optional.of(value) : Optional.empty();
70+
}
71+
72+
@Override
73+
public Optional<Number> asNumber() {
74+
return object instanceof Number value ? Optional.of(value) : Optional.empty();
75+
}
76+
77+
@Override
78+
public Optional<CloudEventData> asCloudEventData() {
79+
return object instanceof CloudEventData value ? Optional.of(value) : Optional.empty();
80+
}
81+
82+
@Override
83+
public Optional<Map<String, Object>> asMap() {
84+
return object instanceof Map ? Optional.of((Map<String, Object>) object) : Optional.empty();
85+
}
86+
87+
@Override
88+
public Object asJavaObject() {
89+
return object;
90+
}
91+
92+
@Override
93+
public Object asIs() {
94+
return object;
95+
}
96+
97+
@Override
98+
public Class<?> objectClass() {
99+
return object != null ? object.getClass() : Object.class;
100+
}
101+
102+
@Override
103+
public <T> Optional<T> as(Class<T> clazz) {
104+
return object != null && object.getClass().isAssignableFrom(clazz)
105+
? Optional.of(clazz.cast(object))
106+
: Optional.empty();
107+
}
108+
}

0 commit comments

Comments
 (0)