From c7bf5ed639ff33f7e952567bcae2b8fd01565fd4 Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Wed, 11 Dec 2024 21:40:34 +0100 Subject: [PATCH] Build SIRI loader in factory, inject into source --- .../configure/UpdaterConfigurator.java | 11 ++++-- .../updater/SiriETHttpTripUpdateSource.java | 20 ++--------- .../updater/siri/updater/SiriETUpdater.java | 4 +++ .../siri/updater/SiriLoaderFactory.java | 34 +++++++++++++++++++ .../lite/SiriETLiteHttpTripUpdateSource.java | 21 ++---------- 5 files changed, 51 insertions(+), 39 deletions(-) create mode 100644 application/src/main/java/org/opentripplanner/updater/siri/updater/SiriLoaderFactory.java diff --git a/application/src/main/java/org/opentripplanner/updater/configure/UpdaterConfigurator.java b/application/src/main/java/org/opentripplanner/updater/configure/UpdaterConfigurator.java index 6c628d11a0e..82902f447d3 100644 --- a/application/src/main/java/org/opentripplanner/updater/configure/UpdaterConfigurator.java +++ b/application/src/main/java/org/opentripplanner/updater/configure/UpdaterConfigurator.java @@ -23,6 +23,7 @@ import org.opentripplanner.updater.siri.updater.SiriETHttpTripUpdateSource; import org.opentripplanner.updater.siri.updater.SiriETUpdater; import org.opentripplanner.updater.siri.updater.SiriHttpLoader; +import org.opentripplanner.updater.siri.updater.SiriLoaderFactory; import org.opentripplanner.updater.siri.updater.SiriSXUpdater; import org.opentripplanner.updater.siri.updater.google.SiriETGooglePubsubUpdater; import org.opentripplanner.updater.siri.updater.lite.SiriETLiteHttpTripUpdateSource; @@ -191,7 +192,10 @@ private List createUpdatersFromConfig() { new SiriETUpdater( configItem, provideSiriTimetableSnapshot(), - new SiriETHttpTripUpdateSource(configItem.sourceParameters()), + new SiriETHttpTripUpdateSource( + configItem.sourceParameters(), + SiriLoaderFactory.createLoader(configItem) + ), TripUpdateMetrics.streaming(configItem) ) ); @@ -201,7 +205,10 @@ private List createUpdatersFromConfig() { new SiriETUpdater( configItem, provideSiriTimetableSnapshot(), - new SiriETLiteHttpTripUpdateSource(configItem.sourceParameters()), + new SiriETLiteHttpTripUpdateSource( + configItem.sourceParameters(), + SiriLoaderFactory.createLoader(configItem) + ), TripUpdateMetrics.batch(configItem) ) ); diff --git a/application/src/main/java/org/opentripplanner/updater/siri/updater/SiriETHttpTripUpdateSource.java b/application/src/main/java/org/opentripplanner/updater/siri/updater/SiriETHttpTripUpdateSource.java index 886b338f470..9d0373e1a16 100644 --- a/application/src/main/java/org/opentripplanner/updater/siri/updater/SiriETHttpTripUpdateSource.java +++ b/application/src/main/java/org/opentripplanner/updater/siri/updater/SiriETHttpTripUpdateSource.java @@ -31,7 +31,7 @@ public class SiriETHttpTripUpdateSource implements EstimatedTimetableSource { private UpdateIncrementality updateIncrementality = FULL_DATASET; private ZonedDateTime lastTimestamp = ZonedDateTime.now().minusMonths(1); - public SiriETHttpTripUpdateSource(Parameters parameters) { + public SiriETHttpTripUpdateSource(Parameters parameters, SiriLoader siriLoader) { this.url = parameters.url(); this.requestorRef = @@ -39,7 +39,7 @@ public SiriETHttpTripUpdateSource(Parameters parameters) { ? "otp-" + UUID.randomUUID() : parameters.requestorRef(); - this.siriLoader = createLoader(url, parameters); + this.siriLoader = siriLoader; } @Override @@ -81,22 +81,6 @@ public String toString() { return ToStringBuilder.of(SiriETHttpTripUpdateSource.class).addStr("url", url).toString(); } - private static SiriLoader createLoader(String url, Parameters parameters) { - // Load real-time updates from a file. - if (SiriFileLoader.matchesUrl(url)) { - return new SiriFileLoader(url); - } - // Fallback to default loader - else { - return new SiriHttpLoader( - url, - parameters.timeout(), - parameters.httpRequestHeaders(), - parameters.previewInterval() - ); - } - } - public interface Parameters { String url(); diff --git a/application/src/main/java/org/opentripplanner/updater/siri/updater/SiriETUpdater.java b/application/src/main/java/org/opentripplanner/updater/siri/updater/SiriETUpdater.java index f9a982fb468..50ef0ba245a 100644 --- a/application/src/main/java/org/opentripplanner/updater/siri/updater/SiriETUpdater.java +++ b/application/src/main/java/org/opentripplanner/updater/siri/updater/SiriETUpdater.java @@ -3,6 +3,8 @@ import java.util.List; import java.util.function.Consumer; import org.opentripplanner.updater.siri.SiriTimetableSnapshotSource; +import org.opentripplanner.updater.siri.updater.lite.SiriETLiteHttpTripUpdateSource; +import org.opentripplanner.updater.siri.updater.lite.SiriETLiteUpdaterParameters; import org.opentripplanner.updater.spi.PollingGraphUpdater; import org.opentripplanner.updater.spi.PollingGraphUpdaterParameters; import org.opentripplanner.updater.spi.ResultLogger; @@ -106,6 +108,8 @@ public String toString() { } public interface Parameters extends UrlUpdaterParameters, PollingGraphUpdaterParameters { + String url(); + boolean blockReadinessUntilInitialized(); boolean fuzzyTripMatching(); diff --git a/application/src/main/java/org/opentripplanner/updater/siri/updater/SiriLoaderFactory.java b/application/src/main/java/org/opentripplanner/updater/siri/updater/SiriLoaderFactory.java new file mode 100644 index 00000000000..b9b43d9341e --- /dev/null +++ b/application/src/main/java/org/opentripplanner/updater/siri/updater/SiriLoaderFactory.java @@ -0,0 +1,34 @@ +package org.opentripplanner.updater.siri.updater; + +import org.opentripplanner.updater.siri.updater.lite.SiriETLiteUpdaterParameters; +import org.opentripplanner.updater.siri.updater.lite.SiriLiteHttpLoader; + +/** + * Constructs a SiriLoader from the parameters of the updater. + */ +public class SiriLoaderFactory { + + public static SiriLoader createLoader(SiriETUpdater.Parameters parameters) { + // Load real-time updates from a file. + if (SiriFileLoader.matchesUrl(parameters.url())) { + return new SiriFileLoader(parameters.url()); + } + // Fallback to default loader + else { + return switch (parameters) { + case SiriETUpdaterParameters p -> new SiriHttpLoader( + p.url(), + p.timeout(), + p.httpRequestHeaders(), + p.previewInterval() + ); + case SiriETLiteUpdaterParameters p -> new SiriLiteHttpLoader( + p.uri(), + p.timeout(), + p.httpRequestHeaders() + ); + default -> throw new IllegalArgumentException("Unexpected value: " + parameters); + }; + } + } +} diff --git a/application/src/main/java/org/opentripplanner/updater/siri/updater/lite/SiriETLiteHttpTripUpdateSource.java b/application/src/main/java/org/opentripplanner/updater/siri/updater/lite/SiriETLiteHttpTripUpdateSource.java index d6eef736a33..7a1e8c988f2 100644 --- a/application/src/main/java/org/opentripplanner/updater/siri/updater/lite/SiriETLiteHttpTripUpdateSource.java +++ b/application/src/main/java/org/opentripplanner/updater/siri/updater/lite/SiriETLiteHttpTripUpdateSource.java @@ -7,7 +7,6 @@ import java.util.Optional; import org.opentripplanner.framework.io.OtpHttpClientException; import org.opentripplanner.updater.siri.updater.EstimatedTimetableSource; -import org.opentripplanner.updater.siri.updater.SiriFileLoader; import org.opentripplanner.updater.siri.updater.SiriLoader; import org.opentripplanner.updater.spi.HttpHeaders; import org.opentripplanner.updater.trip.UpdateIncrementality; @@ -31,10 +30,9 @@ public class SiriETLiteHttpTripUpdateSource implements EstimatedTimetableSource private final SiriLoader siriLoader; - public SiriETLiteHttpTripUpdateSource(Parameters parameters) { + public SiriETLiteHttpTripUpdateSource(Parameters parameters, SiriLoader siriLoader) { this.parameters = parameters; - - this.siriLoader = createLoader(parameters); + this.siriLoader = siriLoader; } @Override @@ -66,21 +64,6 @@ public String toString() { .toString(); } - private static SiriLoader createLoader(Parameters parameters) { - // Load real-time updates from a file. - if (SiriFileLoader.matchesUrl(parameters.uri().toString())) { - return new SiriFileLoader(parameters.uri().toString()); - } - // Fallback to default loader - else { - return new SiriLiteHttpLoader( - parameters.uri(), - parameters.timeout(), - parameters.httpRequestHeaders() - ); - } - } - public interface Parameters { URI uri();