Skip to content
Closed
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
@@ -0,0 +1,32 @@
/*
* 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.events;

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

public interface BuilderDecorator<T> extends ServicePriority {

void decorate(
T builder,
WorkflowContext workflowContext,
TaskContext taskContext,
WorkflowModel workflowModel);

boolean accept(Class<?> clazz);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,23 @@
import io.serverlessworkflow.impl.WorkflowMutablePosition;
import io.serverlessworkflow.impl.WorkflowUtils;
import io.serverlessworkflow.impl.WorkflowValueResolver;
import io.serverlessworkflow.impl.events.BuilderDecorator;
import io.serverlessworkflow.impl.events.CloudEventUtils;
import io.serverlessworkflow.impl.events.EventPublisher;
import io.serverlessworkflow.impl.expressions.ExpressionDescriptor;
import java.net.URI;
import java.time.OffsetDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.concurrent.CompletableFuture;

public class EmitExecutor extends RegularTaskExecutor<EmitTask> {

private final EventPropertiesBuilder props;
private final List<BuilderDecorator> decorators;

public static class EmitExecutorBuilder extends RegularTaskExecutorBuilder<EmitTask> {

Expand All @@ -66,13 +70,19 @@ public EmitExecutor buildInstance() {
private EmitExecutor(EmitExecutorBuilder builder) {
super(builder);
this.props = builder.eventBuilder;
this.decorators =
ServiceLoader.load(BuilderDecorator.class).stream()
.map(ServiceLoader.Provider::get)
.sorted()
.toList();
}

@Override
protected CompletableFuture<WorkflowModel> internalExecute(
WorkflowContext workflow, TaskContext taskContext) {
Collection<EventPublisher> eventPublishers =
workflow.definition().application().eventPublishers();

CloudEvent ce = buildCloudEvent(workflow, taskContext);
return CompletableFuture.allOf(
eventPublishers.stream()
Expand All @@ -83,6 +93,13 @@ protected CompletableFuture<WorkflowModel> internalExecute(

private CloudEvent buildCloudEvent(WorkflowContext workflow, TaskContext taskContext) {
io.cloudevents.core.v1.CloudEventBuilder ceBuilder = CloudEventBuilder.v1();

for (BuilderDecorator decorator : decorators) {
if (decorator.accept(io.cloudevents.core.v1.CloudEventBuilder.class)) {
decorator.decorate(ceBuilder, workflow, taskContext, taskContext.input());
}
}

ceBuilder.withId(
props
.idFilter()
Expand Down