Skip to content

Commit

Permalink
Make CloudEventValidatorProvider thread safe (#650) (#651)
Browse files Browse the repository at this point in the history
Signed-off-by: Pierangelo Di Pilato <[email protected]>
  • Loading branch information
pierDipi authored Jul 2, 2024
1 parent d01caf0 commit e4105a4
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -30,10 +30,14 @@ public class CloudEventValidatorProvider {

private static final CloudEventValidatorProvider cloudEventValidatorProvider = new CloudEventValidatorProvider();

private final ServiceLoader<CloudEventValidator> loader;
private final Collection<CloudEventValidator> validators;

private CloudEventValidatorProvider(){
loader = ServiceLoader.load(CloudEventValidator.class);
private CloudEventValidatorProvider() {
final ServiceLoader<CloudEventValidator> loader = ServiceLoader.load(CloudEventValidator.class);
this.validators = new ArrayList<>(2);
for (CloudEventValidator cloudEventValidator : loader) {
validators.add(cloudEventValidator);
}
}

public static CloudEventValidatorProvider getInstance() {
Expand All @@ -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);
}
}
Expand Down

0 comments on commit e4105a4

Please sign in to comment.