Skip to content

Commit

Permalink
Avoid creating the eBL scenario list all the time
Browse files Browse the repository at this point in the history
Signed-off-by: Niels Thykier <[email protected]>
  • Loading branch information
nt-gt committed Jan 15, 2024
1 parent 7bd24c9 commit 9d20cbb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import com.fasterxml.jackson.databind.JsonNode;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import lombok.SneakyThrows;
import org.dcsa.conformance.core.AbstractComponentFactory;
import org.dcsa.conformance.core.check.JsonSchemaValidator;
import org.dcsa.conformance.core.party.ConformanceParty;
import org.dcsa.conformance.core.party.CounterpartConfiguration;
import org.dcsa.conformance.core.party.PartyConfiguration;
import org.dcsa.conformance.core.party.PartyWebClient;
import org.dcsa.conformance.core.scenario.ConformanceScenario;
import org.dcsa.conformance.core.scenario.ScenarioListBuilder;
import org.dcsa.conformance.core.state.JsonNodeMap;
import org.dcsa.conformance.core.toolkit.JsonToolkit;
Expand All @@ -25,6 +27,8 @@ public class EblComponentFactory extends AbstractComponentFactory {
private static final String CARRIER_AUTH_HEADER_VALUE = UUID.randomUUID().toString();
private static final String SHIPPER_AUTH_HEADER_VALUE = UUID.randomUUID().toString();

private static final ConcurrentHashMap<String, List<ConformanceScenario>> SCENARIO_CACHE = new ConcurrentHashMap<>();

private final String standardVersion;

public EblComponentFactory(String standardVersion) {
Expand Down Expand Up @@ -82,12 +86,36 @@ public List<ConformanceParty> createParties(
public ScenarioListBuilder<?> createScenarioListBuilder(
PartyConfiguration[] partyConfigurations,
CounterpartConfiguration[] counterpartConfigurations) {
return EblScenarioListBuilder.buildTree(
this,
return new CachedEblScenarioListBuilder<>(standardVersion, () -> EblScenarioListBuilder.buildTree(
this.standardVersion,
_findPartyOrCounterpartName(
partyConfigurations, counterpartConfigurations, EblRole::isCarrier),
_findPartyOrCounterpartName(
partyConfigurations, counterpartConfigurations, EblRole::isShipper));
partyConfigurations, counterpartConfigurations, EblRole::isShipper)));
}


private static class CachedEblScenarioListBuilder<T extends ScenarioListBuilder<T>> extends ScenarioListBuilder<T> {

private final Supplier<EblScenarioListBuilder> realScenarioBuilder;
private final String standardVersion;

protected CachedEblScenarioListBuilder(String standardVersion, Supplier<EblScenarioListBuilder> realScenarioBuilder) {
super(Function.identity());
this.standardVersion = standardVersion;
this.realScenarioBuilder = realScenarioBuilder;
}

@Override
protected List<ConformanceScenario> _buildScenarioList() {
var scenarioList = SCENARIO_CACHE.get(standardVersion);
if (scenarioList != null) {
return scenarioList;
}
scenarioList = Collections.unmodifiableList(realScenarioBuilder.get().buildScenarioList());
SCENARIO_CACHE.put(standardVersion, scenarioList);
return scenarioList;
}
}

@Override
Expand All @@ -112,13 +140,6 @@ public Set<String> getReportRoleNames(
.collect(Collectors.toSet());
}

public JsonSchemaValidator getMessageSchemaValidator(String apiName, String jsonSchema) {
String schemaFilePath = "/standards/ebl/schemas/ebl-%s-v%s0.json"
.formatted(apiName, standardVersion.charAt(0));

return JsonSchemaValidator.getInstance(schemaFilePath, jsonSchema);
}

@SneakyThrows
public JsonNode getJsonSandboxConfigurationTemplate(
String testedPartyRole, boolean isManual, boolean isTestingCounterpartsConfig) {
Expand Down
Loading

0 comments on commit 9d20cbb

Please sign in to comment.