|
| 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; |
| 17 | + |
| 18 | +import io.serverlessworkflow.api.types.Document; |
| 19 | +import io.serverlessworkflow.api.types.Workflow; |
| 20 | +import io.serverlessworkflow.impl.executors.DefaultTaskExecutorFactory; |
| 21 | +import io.serverlessworkflow.impl.executors.TaskExecutorFactory; |
| 22 | +import io.serverlessworkflow.impl.expressions.ExpressionFactory; |
| 23 | +import io.serverlessworkflow.impl.expressions.JQExpressionFactory; |
| 24 | +import io.serverlessworkflow.impl.jsonschema.DefaultSchemaValidatorFactory; |
| 25 | +import io.serverlessworkflow.impl.jsonschema.SchemaValidatorFactory; |
| 26 | +import io.serverlessworkflow.resources.DefaultResourceLoaderFactory; |
| 27 | +import io.serverlessworkflow.resources.ResourceLoaderFactory; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.concurrent.ConcurrentHashMap; |
| 33 | + |
| 34 | +public class WorkflowApplication implements AutoCloseable { |
| 35 | + |
| 36 | + private final TaskExecutorFactory taskFactory; |
| 37 | + private final ExpressionFactory exprFactory; |
| 38 | + private final ResourceLoaderFactory resourceLoaderFactory; |
| 39 | + private final SchemaValidatorFactory schemaValidatorFactory; |
| 40 | + private final Collection<WorkflowExecutionListener> listeners; |
| 41 | + private final Map<WorkflowId, WorkflowDefinition> definitions; |
| 42 | + |
| 43 | + public WorkflowApplication( |
| 44 | + TaskExecutorFactory taskFactory, |
| 45 | + ExpressionFactory exprFactory, |
| 46 | + ResourceLoaderFactory resourceLoaderFactory, |
| 47 | + SchemaValidatorFactory schemaValidatorFactory, |
| 48 | + Collection<WorkflowExecutionListener> listeners) { |
| 49 | + this.taskFactory = taskFactory; |
| 50 | + this.exprFactory = exprFactory; |
| 51 | + this.resourceLoaderFactory = resourceLoaderFactory; |
| 52 | + this.schemaValidatorFactory = schemaValidatorFactory; |
| 53 | + this.listeners = listeners; |
| 54 | + this.definitions = new ConcurrentHashMap<>(); |
| 55 | + } |
| 56 | + |
| 57 | + public TaskExecutorFactory taskFactory() { |
| 58 | + return taskFactory; |
| 59 | + } |
| 60 | + |
| 61 | + public static Builder builder() { |
| 62 | + return new Builder(); |
| 63 | + } |
| 64 | + |
| 65 | + public ExpressionFactory expressionFactory() { |
| 66 | + return exprFactory; |
| 67 | + } |
| 68 | + |
| 69 | + public SchemaValidatorFactory validatorFactory() { |
| 70 | + return schemaValidatorFactory; |
| 71 | + } |
| 72 | + |
| 73 | + public ResourceLoaderFactory resourceLoaderFactory() { |
| 74 | + return resourceLoaderFactory; |
| 75 | + } |
| 76 | + |
| 77 | + public Collection<WorkflowExecutionListener> listeners() { |
| 78 | + return listeners; |
| 79 | + } |
| 80 | + |
| 81 | + public static class Builder { |
| 82 | + private TaskExecutorFactory taskFactory = DefaultTaskExecutorFactory.get(); |
| 83 | + private ExpressionFactory exprFactory = JQExpressionFactory.get(); |
| 84 | + private Collection<WorkflowExecutionListener> listeners; |
| 85 | + private ResourceLoaderFactory resourceLoaderFactory = DefaultResourceLoaderFactory.get(); |
| 86 | + private SchemaValidatorFactory schemaValidatorFactory = DefaultSchemaValidatorFactory.get(); |
| 87 | + |
| 88 | + private Builder() {} |
| 89 | + |
| 90 | + public Builder withListener(WorkflowExecutionListener listener) { |
| 91 | + if (listeners == null) { |
| 92 | + listeners = new HashSet<>(); |
| 93 | + } |
| 94 | + listeners.add(listener); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + public Builder withTaskExecutorFactory(TaskExecutorFactory factory) { |
| 99 | + this.taskFactory = factory; |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + public Builder withExpressionFactory(ExpressionFactory factory) { |
| 104 | + this.exprFactory = factory; |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + public Builder withResourceLoaderFactory(ResourceLoaderFactory resourceLoader) { |
| 109 | + this.resourceLoaderFactory = resourceLoader; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + public Builder withSchemaValidatorFactory(SchemaValidatorFactory factory) { |
| 114 | + this.schemaValidatorFactory = factory; |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + public WorkflowApplication build() { |
| 119 | + return new WorkflowApplication( |
| 120 | + taskFactory, |
| 121 | + exprFactory, |
| 122 | + resourceLoaderFactory, |
| 123 | + schemaValidatorFactory, |
| 124 | + listeners == null |
| 125 | + ? Collections.emptySet() |
| 126 | + : Collections.unmodifiableCollection(listeners)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static record WorkflowId(String namespace, String name, String version) { |
| 131 | + static WorkflowId of(Document document) { |
| 132 | + return new WorkflowId(document.getNamespace(), document.getName(), document.getVersion()); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public WorkflowDefinition workflowDefinition(Workflow workflow) { |
| 137 | + return definitions.computeIfAbsent( |
| 138 | + WorkflowId.of(workflow.getDocument()), k -> WorkflowDefinition.of(this, workflow)); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void close() throws Exception { |
| 143 | + for (WorkflowDefinition definition : definitions.values()) { |
| 144 | + definition.close(); |
| 145 | + } |
| 146 | + definitions.clear(); |
| 147 | + } |
| 148 | +} |
0 commit comments