Skip to content

Commit 11501f5

Browse files
committed
[Fix #1121] Ensuring service priority order
Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent 9f56628 commit 11501f5

18 files changed

Lines changed: 135 additions & 28 deletions

File tree

impl/core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@
3333
<artifactId>junit-jupiter-engine</artifactId>
3434
<scope>test</scope>
3535
</dependency>
36+
<dependency>
37+
<groupId>org.assertj</groupId>
38+
<artifactId>assertj-core</artifactId>
39+
<scope>test</scope>
40+
</dependency>
3641
</dependencies>
3742
</project>

impl/core/src/main/java/io/serverlessworkflow/impl/FunctionReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
import io.serverlessworkflow.impl.resources.ExternalResourceHandler;
2020
import java.util.function.Function;
2121

22-
public interface FunctionReader extends Function<ExternalResourceHandler, Task> {}
22+
public interface FunctionReader extends Function<ExternalResourceHandler, Task>, ServicePriority {}

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowApplication.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18+
import static io.serverlessworkflow.impl.WorkflowUtils.loadFirst;
1819
import static io.serverlessworkflow.impl.WorkflowUtils.safeClose;
1920

2021
import io.serverlessworkflow.api.types.SchemaInline;
@@ -305,8 +306,7 @@ public Builder withDefaultCatalogURI(URI defaultCatalogURI) {
305306
public WorkflowApplication build() {
306307
if (modelFactory == null) {
307308
modelFactory =
308-
ServiceLoader.load(WorkflowModelFactory.class)
309-
.findFirst()
309+
loadFirst(WorkflowModelFactory.class)
310310
.orElseThrow(
311311
() ->
312312
new IllegalStateException(
@@ -318,21 +318,17 @@ public WorkflowApplication build() {
318318
ServiceLoader.load(ExpressionFactory.class).forEach(exprFactories::add);
319319
if (schemaValidatorFactory == null) {
320320
schemaValidatorFactory =
321-
ServiceLoader.load(SchemaValidatorFactory.class)
322-
.findFirst()
321+
loadFirst(SchemaValidatorFactory.class)
323322
.orElseGet(() -> EmptySchemaValidatorHolder.instance);
324323
}
325324
if (taskFactory == null) {
326325
taskFactory =
327-
ServiceLoader.load(TaskExecutorFactory.class)
328-
.findFirst()
329-
.orElseGet(() -> DefaultTaskExecutorFactory.get());
326+
loadFirst(TaskExecutorFactory.class).orElseGet(() -> DefaultTaskExecutorFactory.get());
330327
}
331328
ServiceLoader.load(EventPublisher.class).forEach(e -> eventPublishers.add(e));
332329
if (eventConsumer == null) {
333330
eventConsumer =
334-
ServiceLoader.load(EventConsumer.class)
335-
.findFirst()
331+
loadFirst(EventConsumer.class)
336332
.orElseGet(
337333
() -> {
338334
InMemoryEvents inMemory = new InMemoryEvents(executorFactory);
@@ -353,18 +349,14 @@ public WorkflowApplication build() {
353349

354350
if (configManager == null) {
355351
configManager =
356-
ServiceLoader.load(ConfigManager.class)
357-
.findFirst()
358-
.orElseGet(() -> new SystemPropertyConfigManager());
352+
loadFirst(ConfigManager.class).orElseGet(() -> new SystemPropertyConfigManager());
359353
}
360354
if (secretManager == null) {
361355
secretManager =
362-
ServiceLoader.load(SecretManager.class)
363-
.findFirst()
364-
.orElseGet(() -> new ConfigSecretManager(configManager));
356+
loadFirst(SecretManager.class).orElseGet(() -> new ConfigSecretManager(configManager));
365357
}
366-
templateResolver = ServiceLoader.load(URITemplateResolver.class).findFirst();
367-
functionReader = ServiceLoader.load(FunctionReader.class).findFirst();
358+
templateResolver = loadFirst(URITemplateResolver.class);
359+
functionReader = loadFirst(FunctionReader.class);
368360
if (defaultCatalogURI == null) {
369361
defaultCatalogURI = URI.create("https://github.com/serverlessworkflow/catalog");
370362
}

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.Map;
4040
import java.util.Objects;
4141
import java.util.Optional;
42+
import java.util.ServiceLoader;
4243
import org.slf4j.Logger;
4344
import org.slf4j.LoggerFactory;
4445

@@ -304,4 +305,11 @@ public static WorkflowValueResolver<URI> getURISupplier(
304305
}
305306
throw new IllegalArgumentException("Invalid uritemplate definition " + template);
306307
}
308+
309+
public static <T extends ServicePriority> Optional<T> loadFirst(Class<T> serviceClass) {
310+
return ServiceLoader.load(serviceClass).stream()
311+
.map(ServiceLoader.Provider::get)
312+
.sorted()
313+
.findFirst();
314+
}
307315
}

impl/core/src/main/java/io/serverlessworkflow/impl/events/EventConsumer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717

1818
import io.cloudevents.CloudEvent;
1919
import io.serverlessworkflow.api.types.EventFilter;
20+
import io.serverlessworkflow.impl.ServicePriority;
2021
import io.serverlessworkflow.impl.WorkflowApplication;
2122
import java.util.Collection;
2223
import java.util.function.Consumer;
2324

2425
public interface EventConsumer<T extends EventRegistration, V extends EventRegistrationBuilder>
25-
extends AutoCloseable {
26+
extends AutoCloseable, ServicePriority {
2627

2728
V listen(EventFilter filter, WorkflowApplication workflowApplication);
2829

impl/core/src/main/java/io/serverlessworkflow/impl/executors/RunScriptExecutorBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public CallableTask build(RunScript taskConfiguration, WorkflowDefinition defini
6868
ServiceLoader.load(ScriptRunner.class).stream()
6969
.map(ServiceLoader.Provider::get)
7070
.filter(s -> s.identifier().equals(language))
71+
.sorted()
7172
.findFirst()
7273
.orElseThrow(
7374
() ->

impl/core/src/main/java/io/serverlessworkflow/impl/executors/RunTaskExecutor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ protected RunTaskExecutorBuilder(
4444
runnables.stream()
4545
.map(Provider::get)
4646
.filter(r -> r.accept(config.getClass()))
47+
.sorted()
4748
.findFirst()
4849
.map(r -> r.build(config, definition))
4950
.orElseThrow(

impl/core/src/main/java/io/serverlessworkflow/impl/executors/RunnableTaskBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
package io.serverlessworkflow.impl.executors;
1717

1818
import io.serverlessworkflow.api.types.RunTaskConfiguration;
19+
import io.serverlessworkflow.impl.ServicePriority;
1920
import io.serverlessworkflow.impl.WorkflowDefinition;
2021

21-
public interface RunnableTaskBuilder<T extends RunTaskConfiguration> {
22+
public interface RunnableTaskBuilder<T extends RunTaskConfiguration> extends ServicePriority {
2223
boolean accept(Class<? extends RunTaskConfiguration> clazz);
2324

2425
CallableTask build(T taskConfiguration, WorkflowDefinition definition);

impl/core/src/main/java/io/serverlessworkflow/impl/resources/URITemplateResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package io.serverlessworkflow.impl.resources;
1717

18+
import io.serverlessworkflow.impl.ServicePriority;
1819
import io.serverlessworkflow.impl.TaskContext;
1920
import io.serverlessworkflow.impl.WorkflowContext;
2021
import io.serverlessworkflow.impl.WorkflowModel;
2122
import java.net.URI;
2223

23-
public interface URITemplateResolver {
24+
public interface URITemplateResolver extends ServicePriority {
2425
URI resolveTemplates(
2526
String uri, WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel model);
2627
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
public class LowestPriority implements ServicePriority {
19+
public int priority() {
20+
return DEFAULT_PRIORITY + 1;
21+
}
22+
}

0 commit comments

Comments
 (0)