Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import io.serverlessworkflow.api.types.SchemaInline;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.impl.additional.WorkflowAdditionalObject;
import io.serverlessworkflow.impl.events.EventConsumer;
import io.serverlessworkflow.impl.events.EventPublisher;
import io.serverlessworkflow.impl.events.InMemoryEvents;
Expand All @@ -38,6 +39,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.ServiceLoader.Provider;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -61,6 +63,7 @@ public class WorkflowApplication implements AutoCloseable {
private final boolean lifeCycleCEPublishingEnabled;
private final WorkflowModelFactory modelFactory;
private final WorkflowScheduler scheduler;
private final Map<String, WorkflowAdditionalObject<?>> additionalObjects;

private WorkflowApplication(Builder builder) {
this.taskFactory = builder.taskFactory;
Expand All @@ -78,6 +81,7 @@ private WorkflowApplication(Builder builder) {
this.lifeCycleCEPublishingEnabled = builder.lifeCycleCEPublishingEnabled;
this.modelFactory = builder.modelFactory;
this.scheduler = builder.scheduler;
this.additionalObjects = builder.additionalObjects;
}

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

private Builder() {}

Expand Down Expand Up @@ -221,6 +226,15 @@ public Builder withEventPublisher(EventPublisher eventPublisher) {
return this;
}

public <T> Builder withAdditionalObject(
String name, WorkflowAdditionalObject<T> additionalObject) {
if (additionalObjects == null) {
additionalObjects = new ConcurrentHashMap<>();
}
additionalObjects.put(name, additionalObject);
return this;
}

public Builder withModelFactory(WorkflowModelFactory modelFactory) {
this.modelFactory = modelFactory;
return this;
Expand Down Expand Up @@ -269,6 +283,10 @@ public WorkflowApplication build() {
if (scheduler == null) {
scheduler = new DefaultWorkflowScheduler();
}
if (additionalObjects == null) {
additionalObjects = Collections.emptyMap();
}

return new WorkflowApplication(this);
}
}
Expand Down Expand Up @@ -329,4 +347,10 @@ public boolean isLifeCycleCEPublishingEnabled() {
public WorkflowScheduler scheduler() {
return scheduler;
}

public <T> Optional<T> additionalObject(
String name, WorkflowContext workflowContext, TaskContext taskContext) {
return Optional.ofNullable(additionalObjects.get(name))
.map(v -> (T) v.apply(workflowContext, taskContext));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed 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 io.serverlessworkflow.impl.additional;

import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowContext;

public class ConstantAdditionalObject<T> implements WorkflowAdditionalObject<T> {

private final T object;

public ConstantAdditionalObject(T object) {
this.object = object;
}

@Override
public T apply(WorkflowContext t, TaskContext u) {
return object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed 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 io.serverlessworkflow.impl.additional;

import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowContext;
import java.util.function.Supplier;

public class SuppliedAdditionalObject<T> implements WorkflowAdditionalObject<T> {

private final Supplier<T> supplier;

public SuppliedAdditionalObject(Supplier<T> supplier) {
this.supplier = supplier;
}

@Override
public T apply(WorkflowContext t, TaskContext u) {
return supplier.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed 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 io.serverlessworkflow.impl.additional;

import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowContext;
import java.util.function.BiFunction;

public interface WorkflowAdditionalObject<T> extends BiFunction<WorkflowContext, TaskContext, T> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed 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 io.serverlessworkflow.impl.executors.http;

import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowContext;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;

public class HttpClientResolver {

public static final String HTTP_CLIENT_PROVIDER = "httpClientProvider";

private static class DefaultHolder {
private static final Client client = ClientBuilder.newClient();
}

public static Client client(WorkflowContext workflowContext, TaskContext taskContext) {
return workflowContext
.definition()
.application()
.<Client>additionalObject(HTTP_CLIENT_PROVIDER, workflowContext, taskContext)
.orElseGet(() -> DefaultHolder.client);
}

private HttpClientResolver() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import io.serverlessworkflow.impl.expressions.ExpressionDescriptor;
import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Invocation.Builder;
import jakarta.ws.rs.client.WebTarget;
import java.net.URI;
Expand All @@ -46,7 +44,6 @@

public class HttpExecutor implements CallableTask<CallHTTP> {

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

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

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

private static TargetSupplier getTargetSupplier(
WorkflowValueResolver<URI> uriSupplier, WorkflowValueResolver<URI> pathSupplier) {
return (w, t, n) ->
client.target(uriSupplier.apply(w, t, n).resolve(pathSupplier.apply(w, t, n)));
HttpClientResolver.client(w, t)
.target(uriSupplier.apply(w, t, n).resolve(pathSupplier.apply(w, t, n)));
}

private static interface TargetSupplier {
Expand Down