From c1a81e552fa014c5654c42bcda002eae7d10236c Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Fri, 13 Jul 2018 11:12:03 -0500 Subject: [PATCH] Watcher: cleanup ensureWatchExists use (#31926) Previously, the ensureWatchExists was overridable. This commit makes it final so that it cannot be overridden, and cleans up some redundant code in the process. --- .../execution/WatchExecutionContext.java | 2 +- .../execution/ManualExecutionContext.java | 21 +++++-------------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionContext.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionContext.java index 4cdd4bb0e3575..62216ff681e82 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionContext.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionContext.java @@ -82,7 +82,7 @@ public Watch watch() { return watch; } - public void ensureWatchExists(CheckedSupplier supplier) throws Exception { + public final void ensureWatchExists(CheckedSupplier supplier) throws Exception { if (watch == null) { watch = supplier.get(); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java index c161b24e85619..abf1e5aec0da4 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.xpack.watcher.execution; -import org.elasticsearch.common.CheckedSupplier; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.actions.ActionWrapper; @@ -29,18 +28,19 @@ public class ManualExecutionContext extends WatchExecutionContext { private final Map actionModes; private final boolean recordExecution; private final boolean knownWatch; - private final Watch watch; ManualExecutionContext(Watch watch, boolean knownWatch, DateTime executionTime, ManualTriggerEvent triggerEvent, TimeValue defaultThrottlePeriod, Input.Result inputResult, Condition.Result conditionResult, - Map actionModes, boolean recordExecution) { + Map actionModes, boolean recordExecution) throws Exception { super(watch.id(), executionTime, triggerEvent, defaultThrottlePeriod); this.actionModes = actionModes; this.recordExecution = recordExecution; this.knownWatch = knownWatch; - this.watch = watch; + + // set the watch early to ensure calls to watch() below succeed. + super.ensureWatchExists(() -> watch); if (inputResult != null) { onInputResult(inputResult); @@ -66,12 +66,6 @@ public class ManualExecutionContext extends WatchExecutionContext { } } - // a noop operation, as the watch is already loaded via ctor - @Override - public void ensureWatchExists(CheckedSupplier supplier) throws Exception { - super.ensureWatchExists(() -> watch); - } - @Override public boolean knownWatch() { return knownWatch; @@ -107,11 +101,6 @@ public final boolean recordExecution() { return recordExecution; } - @Override - public Watch watch() { - return watch; - } - public static Builder builder(Watch watch, boolean knownWatch, ManualTriggerEvent event, TimeValue defaultThrottlePeriod) { return new Builder(watch, knownWatch, event, defaultThrottlePeriod); } @@ -173,7 +162,7 @@ public Builder withCondition(Condition.Result conditionResult) { return this; } - public ManualExecutionContext build() { + public ManualExecutionContext build() throws Exception { if (executionTime == null) { executionTime = DateTime.now(DateTimeZone.UTC); }