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 @@ -16,7 +16,6 @@
package io.serverlessworkflow.impl;

import com.github.f4b6a3.ulid.UlidCreator;
import io.serverlessworkflow.api.types.Document;
import io.serverlessworkflow.api.types.SchemaInline;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.impl.events.EventConsumer;
Expand Down Expand Up @@ -52,9 +51,9 @@ public class WorkflowApplication implements AutoCloseable {
private final ExpressionFactory exprFactory;
private final ResourceLoaderFactory resourceLoaderFactory;
private final SchemaValidatorFactory schemaValidatorFactory;
private final WorkflowIdFactory idFactory;
private final WorkflowInstanceIdFactory idFactory;
private final Collection<WorkflowExecutionListener> listeners;
private final Map<WorkflowId, WorkflowDefinition> definitions;
private final Map<WorkflowDefinitionId, WorkflowDefinition> definitions;
private final WorkflowPositionFactory positionFactory;
private final ExecutorServiceFactory executorFactory;
private final RuntimeDescriptorFactory runtimeDescriptorFactory;
Expand Down Expand Up @@ -106,7 +105,7 @@ public Collection<EventPublisher> eventPublishers() {
return eventPublishers;
}

public WorkflowIdFactory idFactory() {
public WorkflowInstanceIdFactory idFactory() {
return idFactory;
}

Expand Down Expand Up @@ -142,7 +141,7 @@ public SchemaValidator getValidator(SchemaInline inline) {
private ResourceLoaderFactory resourceLoaderFactory = DefaultResourceLoaderFactory.get();
private SchemaValidatorFactory schemaValidatorFactory;
private WorkflowPositionFactory positionFactory = () -> new QueueWorkflowPosition();
private WorkflowIdFactory idFactory = () -> UlidCreator.getMonotonicUlid().toString();
private WorkflowInstanceIdFactory idFactory = () -> UlidCreator.getMonotonicUlid().toString();
private ExecutorServiceFactory executorFactory = new DefaultExecutorServiceFactory();
private EventConsumer<?, ?> eventConsumer;
private Collection<EventPublisher> eventPublishers = new ArrayList<>();
Expand Down Expand Up @@ -192,7 +191,7 @@ public Builder withSchemaValidatorFactory(SchemaValidatorFactory factory) {
return this;
}

public Builder withIdFactory(WorkflowIdFactory factory) {
public Builder withIdFactory(WorkflowInstanceIdFactory factory) {
this.idFactory = factory;
return this;
}
Expand Down Expand Up @@ -249,15 +248,13 @@ public WorkflowApplication build() {
}
}

private static record WorkflowId(String namespace, String name, String version) {
static WorkflowId of(Document document) {
return new WorkflowId(document.getNamespace(), document.getName(), document.getVersion());
}
public Map<WorkflowDefinitionId, WorkflowDefinition> workflowDefinitions() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Application can now be used as a workflow definition registry

return Collections.unmodifiableMap(definitions);
}

public WorkflowDefinition workflowDefinition(Workflow workflow) {
return definitions.computeIfAbsent(
WorkflowId.of(workflow.getDocument()), k -> WorkflowDefinition.of(this, workflow));
WorkflowDefinitionId.of(workflow), k -> WorkflowDefinition.of(this, workflow));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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;

import io.serverlessworkflow.api.types.Document;
import io.serverlessworkflow.api.types.Workflow;

record WorkflowDefinitionId(String namespace, String name, String version) {

public static final String DEFAULT_VERSION = "0.0.1";
public static final String DEFAULT_NAMESPACE = "org.acme";

public static WorkflowDefinitionId of(Workflow workflow) {
Document document = workflow.getDocument();
return new WorkflowDefinitionId(
document.getNamespace(), document.getName(), document.getVersion());
}

public static WorkflowDefinitionId fromName(String name) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful as syntactic sugar in case you want to identify workflows with a single name.
If not use the record constructor to specify namespace and version

return new WorkflowDefinitionId(DEFAULT_NAMESPACE, name, DEFAULT_VERSION);
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to make explicit that this factory is not related with the definition id, but with the instance id

Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
import java.util.function.Supplier;

@FunctionalInterface
public interface WorkflowIdFactory extends Supplier<String> {}
public interface WorkflowInstanceIdFactory extends Supplier<String> {}