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 @@ -41,6 +41,7 @@
import io.serverlessworkflow.impl.scheduler.WorkflowScheduler;
import io.serverlessworkflow.impl.schema.SchemaValidator;
import io.serverlessworkflow.impl.schema.SchemaValidatorFactory;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -78,6 +79,7 @@ public class WorkflowApplication implements AutoCloseable {
private final SchedulerListener schedulerListener;
private final Optional<URITemplateResolver> templateResolver;
private final Optional<FunctionReader> functionReader;
private final URI defaultCatalogURI;

private WorkflowApplication(Builder builder) {
this.taskFactory = builder.taskFactory;
Expand All @@ -102,6 +104,7 @@ private WorkflowApplication(Builder builder) {
this.secretManager = builder.secretManager;
this.templateResolver = builder.templateResolver;
this.functionReader = builder.functionReader;
this.defaultCatalogURI = builder.defaultCatalogURI;
}

public TaskExecutorFactory taskFactory() {
Expand Down Expand Up @@ -184,6 +187,7 @@ public SchemaValidator getValidator(SchemaInline inline) {
private SchedulerListener schedulerListener;
private Optional<URITemplateResolver> templateResolver;
private Optional<FunctionReader> functionReader;
private URI defaultCatalogURI;

private Builder() {
ServiceLoader.load(NamedWorkflowAdditionalObject.class)
Expand Down Expand Up @@ -281,6 +285,15 @@ public Builder withContextFactory(WorkflowModelFactory contextFactory) {
return this;
}

public Builder withDefaultCatalogURI(String defaultCatalogURI) {
return withDefaultCatalogURI(URI.create(defaultCatalogURI));
}

public Builder withDefaultCatalogURI(URI defaultCatalogURI) {
this.defaultCatalogURI = defaultCatalogURI;
return this;
}

public WorkflowApplication build() {
if (modelFactory == null) {
modelFactory =
Expand Down Expand Up @@ -344,6 +357,9 @@ public WorkflowApplication build() {
}
templateResolver = ServiceLoader.load(URITemplateResolver.class).findFirst();
functionReader = ServiceLoader.load(FunctionReader.class).findFirst();
if (defaultCatalogURI == null) {
defaultCatalogURI = URI.create("https://github.com/serverlessworkflow/catalog");
}
return new WorkflowApplication(this);
}
}
Expand Down Expand Up @@ -429,6 +445,10 @@ public Optional<FunctionReader> functionReader() {
return functionReader;
}

public URI defaultCatalogURI() {
return defaultCatalogURI;
}

public <T> Optional<T> additionalObject(
String name, WorkflowContext workflowContext, TaskContext taskContext) {
return Optional.ofNullable(additionalObjects.get(name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import io.serverlessworkflow.api.types.Task;
import io.serverlessworkflow.api.types.TaskBase;
import io.serverlessworkflow.api.types.Use;
import io.serverlessworkflow.api.types.UseCatalogs;
import io.serverlessworkflow.api.types.UseFunctions;
import io.serverlessworkflow.impl.WorkflowDefinition;
import io.serverlessworkflow.impl.WorkflowMutablePosition;
import io.serverlessworkflow.impl.WorkflowUtils;
Expand All @@ -43,33 +41,43 @@ public void init(
CallFunction task, WorkflowDefinition definition, WorkflowMutablePosition position) {
String functionName = task.getCall();
Use use = definition.workflow().getUse();
int indexOf = functionName.indexOf('@');
Task function = null;
if (use != null) {
UseFunctions functions = use.getFunctions();
if (functions != null) {
function = functions.getAdditionalProperties().get(functionName);
}
if (function == null) {
int indexOf = functionName.indexOf('@');
if (indexOf > 0) {
String catalogName = functionName.substring(indexOf + 1);
UseCatalogs catalogs = use.getCatalogs();
if (catalogs != null) {
Catalog catalog = catalogs.getAdditionalProperties().get(catalogName);
ResourceLoader loader = definition.resourceLoader();
function =
definition
.resourceLoader()
.loadURI(
WorkflowUtils.concatURI(
loader.uri(catalog.getEndpoint()),
pathFromFunctionName(functionName.substring(0, indexOf))),
h -> from(definition, h));
}
if (indexOf > 0) {
// Catalog function
URI catalogEndpoint;
String catalogName = functionName.substring(indexOf + 1);
ResourceLoader loader = definition.resourceLoader();
if (catalogName.equalsIgnoreCase("default")) {
catalogEndpoint = definition.application().defaultCatalogURI();
} else {
if (use == null || use.getCatalogs() == null) {
throw new IllegalStateException(
"Using catalog " + catalogName + ", but there is not catalog definition");
}
Catalog catalog = use.getCatalogs().getAdditionalProperties().get(catalogName);
if (catalog == null) {
throw new IllegalStateException(
"Catalog "
+ catalogName
+ " is not included in Catalog dictionary: "
+ use.getCatalogs().getAdditionalProperties());
}
catalogEndpoint = loader.uri(catalog.getEndpoint());
}
function =
definition
.resourceLoader()
.loadURI(
WorkflowUtils.concatURI(
catalogEndpoint, pathFromFunctionName(functionName.substring(0, indexOf))),
h -> from(definition, h));
} else if (use != null && use.getFunctions() != null) {
// search for inline function definition
function = use.getFunctions().getAdditionalProperties().get(functionName);
}
if (function == null) {
// try to load function if function name is an uri
function =
definition.resourceLoader().loadURI(URI.create(functionName), h -> from(definition, h));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ void testCustomFunction() {
@ValueSource(
strings = {
"workflows-samples/call-custom-function-cataloged.yaml",
"workflows-samples/call-custom-function-cataloged-global.yaml"
"workflows-samples/call-custom-function-cataloged-global.yaml",
"workflows-samples/call-custom-function-cataloged-default.yaml"
})
void testCustomCatalogFunction(String fileName) throws IOException {
assertThat(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
document:
dsl: '1.0.2'
namespace: test
name: call-custom-function-cataloged-default
version: '0.1.0'
do:
- log:
call: log:1.0.0@default
with:
message: Hello, world!
level: information
timestamp: true
format: '{TIMESTAMP} [{LEVEL}] ({CONTEXT}): {MESSAGE}'