From c2a2110edf4f03b83e08c009c21005bb758ad0c7 Mon Sep 17 00:00:00 2001 From: Pierangelo Di Pilato Date: Tue, 2 Jul 2024 10:19:23 +0200 Subject: [PATCH] Make CloudEventValidatorProvider thread safe Signed-off-by: Pierangelo Di Pilato --- .../provider/CloudEventValidatorProvider.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/io/cloudevents/core/provider/CloudEventValidatorProvider.java b/core/src/main/java/io/cloudevents/core/provider/CloudEventValidatorProvider.java index 2f6afd61..0945d0b7 100644 --- a/core/src/main/java/io/cloudevents/core/provider/CloudEventValidatorProvider.java +++ b/core/src/main/java/io/cloudevents/core/provider/CloudEventValidatorProvider.java @@ -19,8 +19,8 @@ import io.cloudevents.CloudEvent; import io.cloudevents.core.validator.CloudEventValidator; -import java.util.Iterator; -import java.util.ServiceConfigurationError; +import java.util.ArrayList; +import java.util.Collection; import java.util.ServiceLoader; /** @@ -30,10 +30,14 @@ public class CloudEventValidatorProvider { private static final CloudEventValidatorProvider cloudEventValidatorProvider = new CloudEventValidatorProvider(); - private final ServiceLoader loader; + private final Collection validators; - private CloudEventValidatorProvider(){ - loader = ServiceLoader.load(CloudEventValidator.class); + private CloudEventValidatorProvider() { + final ServiceLoader loader = ServiceLoader.load(CloudEventValidator.class); + this.validators = new ArrayList<>(2); + for (CloudEventValidator cloudEventValidator : loader) { + validators.add(cloudEventValidator); + } } public static CloudEventValidatorProvider getInstance() { @@ -42,10 +46,11 @@ public static CloudEventValidatorProvider getInstance() { /** * iterates through available Cloudevent validators. - * @param cloudEvent + * + * @param cloudEvent event to validate. */ - public void validate(CloudEvent cloudEvent){ - for (final CloudEventValidator validator : loader) { + public void validate(CloudEvent cloudEvent) { + for (final CloudEventValidator validator : validators) { validator.validate(cloudEvent); } }