Skip to content

Commit accba1b

Browse files
authored
[Fix #925] Allow association of additional objects to application (#927)
* [Fix #925] Allow association of additional objects to application Signed-off-by: fjtirado <[email protected]> * [Fix #925] Review comments Signed-off-by: fjtirado <[email protected]> --------- Signed-off-by: fjtirado <[email protected]>
1 parent 63b3814 commit accba1b

File tree

6 files changed

+156
-5
lines changed

6 files changed

+156
-5
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowApplication.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import io.serverlessworkflow.api.types.SchemaInline;
2121
import io.serverlessworkflow.api.types.Workflow;
22+
import io.serverlessworkflow.impl.additional.WorkflowAdditionalObject;
2223
import io.serverlessworkflow.impl.events.EventConsumer;
2324
import io.serverlessworkflow.impl.events.EventPublisher;
2425
import io.serverlessworkflow.impl.events.InMemoryEvents;
@@ -38,6 +39,7 @@
3839
import java.util.Collections;
3940
import java.util.HashSet;
4041
import java.util.Map;
42+
import java.util.Optional;
4143
import java.util.ServiceLoader;
4244
import java.util.ServiceLoader.Provider;
4345
import java.util.concurrent.ConcurrentHashMap;
@@ -61,6 +63,7 @@ public class WorkflowApplication implements AutoCloseable {
6163
private final boolean lifeCycleCEPublishingEnabled;
6264
private final WorkflowModelFactory modelFactory;
6365
private final WorkflowScheduler scheduler;
66+
private final Map<String, WorkflowAdditionalObject<?>> additionalObjects;
6467

6568
private WorkflowApplication(Builder builder) {
6669
this.taskFactory = builder.taskFactory;
@@ -78,6 +81,7 @@ private WorkflowApplication(Builder builder) {
7881
this.lifeCycleCEPublishingEnabled = builder.lifeCycleCEPublishingEnabled;
7982
this.modelFactory = builder.modelFactory;
8083
this.scheduler = builder.scheduler;
84+
this.additionalObjects = builder.additionalObjects;
8185
}
8286

8387
public TaskExecutorFactory taskFactory() {
@@ -153,6 +157,7 @@ public SchemaValidator getValidator(SchemaInline inline) {
153157
() -> new RuntimeDescriptor("reference impl", "1.0.0_alpha", Collections.emptyMap());
154158
private boolean lifeCycleCEPublishingEnabled = true;
155159
private WorkflowModelFactory modelFactory;
160+
private Map<String, WorkflowAdditionalObject<?>> additionalObjects;
156161

157162
private Builder() {}
158163

@@ -221,6 +226,15 @@ public Builder withEventPublisher(EventPublisher eventPublisher) {
221226
return this;
222227
}
223228

229+
public <T> Builder withAdditionalObject(
230+
String name, WorkflowAdditionalObject<T> additionalObject) {
231+
if (additionalObjects == null) {
232+
additionalObjects = new ConcurrentHashMap<>();
233+
}
234+
additionalObjects.put(name, additionalObject);
235+
return this;
236+
}
237+
224238
public Builder withModelFactory(WorkflowModelFactory modelFactory) {
225239
this.modelFactory = modelFactory;
226240
return this;
@@ -269,6 +283,10 @@ public WorkflowApplication build() {
269283
if (scheduler == null) {
270284
scheduler = new DefaultWorkflowScheduler();
271285
}
286+
if (additionalObjects == null) {
287+
additionalObjects = Collections.emptyMap();
288+
}
289+
272290
return new WorkflowApplication(this);
273291
}
274292
}
@@ -329,4 +347,10 @@ public boolean isLifeCycleCEPublishingEnabled() {
329347
public WorkflowScheduler scheduler() {
330348
return scheduler;
331349
}
350+
351+
public <T> Optional<T> additionalObject(
352+
String name, WorkflowContext workflowContext, TaskContext taskContext) {
353+
return Optional.ofNullable(additionalObjects.get(name))
354+
.map(v -> (T) v.apply(workflowContext, taskContext));
355+
}
332356
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.additional;
17+
18+
import io.serverlessworkflow.impl.TaskContext;
19+
import io.serverlessworkflow.impl.WorkflowContext;
20+
21+
public class ConstantAdditionalObject<T> implements WorkflowAdditionalObject<T> {
22+
23+
private final T object;
24+
25+
public ConstantAdditionalObject(T object) {
26+
this.object = object;
27+
}
28+
29+
@Override
30+
public T apply(WorkflowContext t, TaskContext u) {
31+
return object;
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.additional;
17+
18+
import io.serverlessworkflow.impl.TaskContext;
19+
import io.serverlessworkflow.impl.WorkflowContext;
20+
import java.util.function.Supplier;
21+
22+
public class SuppliedAdditionalObject<T> implements WorkflowAdditionalObject<T> {
23+
24+
private final Supplier<T> supplier;
25+
26+
public SuppliedAdditionalObject(Supplier<T> supplier) {
27+
this.supplier = supplier;
28+
}
29+
30+
@Override
31+
public T apply(WorkflowContext t, TaskContext u) {
32+
return supplier.get();
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.additional;
17+
18+
import io.serverlessworkflow.impl.TaskContext;
19+
import io.serverlessworkflow.impl.WorkflowContext;
20+
import java.util.function.BiFunction;
21+
22+
public interface WorkflowAdditionalObject<T> extends BiFunction<WorkflowContext, TaskContext, T> {}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.http;
17+
18+
import io.serverlessworkflow.impl.TaskContext;
19+
import io.serverlessworkflow.impl.WorkflowContext;
20+
import jakarta.ws.rs.client.Client;
21+
import jakarta.ws.rs.client.ClientBuilder;
22+
23+
public class HttpClientResolver {
24+
25+
public static final String HTTP_CLIENT_PROVIDER = "httpClientProvider";
26+
27+
private static class DefaultHolder {
28+
private static final Client client = ClientBuilder.newClient();
29+
}
30+
31+
public static Client client(WorkflowContext workflowContext, TaskContext taskContext) {
32+
return workflowContext
33+
.definition()
34+
.application()
35+
.<Client>additionalObject(HTTP_CLIENT_PROVIDER, workflowContext, taskContext)
36+
.orElseGet(() -> DefaultHolder.client);
37+
}
38+
39+
private HttpClientResolver() {}
40+
}

impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/HttpExecutor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
import io.serverlessworkflow.impl.expressions.ExpressionDescriptor;
3535
import jakarta.ws.rs.HttpMethod;
3636
import jakarta.ws.rs.WebApplicationException;
37-
import jakarta.ws.rs.client.Client;
38-
import jakarta.ws.rs.client.ClientBuilder;
3937
import jakarta.ws.rs.client.Invocation.Builder;
4038
import jakarta.ws.rs.client.WebTarget;
4139
import java.net.URI;
@@ -46,7 +44,6 @@
4644

4745
public class HttpExecutor implements CallableTask<CallHTTP> {
4846

49-
private static final Client client = ClientBuilder.newClient();
5047
// TODO allow changing default converter
5148
private static final HttpModelConverter defaultConverter = new HttpModelConverter() {};
5249

@@ -262,13 +259,14 @@ public boolean accept(Class<? extends TaskBase> clazz) {
262259
}
263260

264261
private static TargetSupplier getTargetSupplier(WorkflowValueResolver<URI> uriSupplier) {
265-
return (w, t, n) -> client.target(uriSupplier.apply(w, t, n));
262+
return (w, t, n) -> HttpClientResolver.client(w, t).target(uriSupplier.apply(w, t, n));
266263
}
267264

268265
private static TargetSupplier getTargetSupplier(
269266
WorkflowValueResolver<URI> uriSupplier, WorkflowValueResolver<URI> pathSupplier) {
270267
return (w, t, n) ->
271-
client.target(uriSupplier.apply(w, t, n).resolve(pathSupplier.apply(w, t, n)));
268+
HttpClientResolver.client(w, t)
269+
.target(uriSupplier.apply(w, t, n).resolve(pathSupplier.apply(w, t, n)));
272270
}
273271

274272
private static interface TargetSupplier {

0 commit comments

Comments
 (0)