From d9816fac586a8012ea6baae198ef29fc6622211e Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Fri, 26 May 2017 00:03:00 -0700 Subject: [PATCH] Update uses of script factory types to new names (elastic/x-pack-elasticsearch#1560) This is the xpack side of https://github.com/elastic/elasticsearch/pull/24897 Original commit: elastic/x-pack-elasticsearch@d61f4e1da2c84cebef5c74372f3366972f7c44ba --- .../elasticsearch/xpack/watcher/Watcher.java | 9 ++++----- .../watcher/condition/ScriptCondition.java | 8 ++++---- .../script/ExecutableScriptTransform.java | 4 ++-- .../script/MockMustacheScriptEngine.java | 2 +- .../test/MockPainlessScriptEngine.java | 4 ++-- .../transform/script/ScriptTransformTests.java | 18 +++++++++--------- 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java index 59f470d71a3..6af74d909fa 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java @@ -34,7 +34,6 @@ import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.index.IndexModule; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.plugins.ActionPlugin; -import org.elasticsearch.plugins.ScriptPlugin; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestHandler; import org.elasticsearch.script.ExecutableScript; @@ -182,11 +181,11 @@ public class Watcher implements ActionPlugin { public static final Setting MAX_STOP_TIMEOUT_SETTING = Setting.timeSetting("xpack.watcher.stop.timeout", TimeValue.timeValueSeconds(30), Setting.Property.NodeScope); - public static final ScriptContext SCRIPT_SEARCH_CONTEXT = - new ScriptContext<>("xpack", SearchScript.Compiled.class); + public static final ScriptContext SCRIPT_SEARCH_CONTEXT = + new ScriptContext<>("xpack", SearchScript.Factory.class); // TODO: remove this context when each xpack script use case has their own contexts - public static final ScriptContext SCRIPT_EXECUTABLE_CONTEXT - = new ScriptContext<>("xpack_executable", ExecutableScript.Compiled.class); + public static final ScriptContext SCRIPT_EXECUTABLE_CONTEXT + = new ScriptContext<>("xpack_executable", ExecutableScript.Factory.class); private static final Logger logger = Loggers.getLogger(Watcher.class); private WatcherIndexingListener listener; diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/condition/ScriptCondition.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/condition/ScriptCondition.java index c0b804ad8d5..3b7c5be1079 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/condition/ScriptCondition.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/condition/ScriptCondition.java @@ -30,20 +30,20 @@ public final class ScriptCondition extends Condition { private final ScriptService scriptService; private final Script script; - private final ExecutableScript.Compiled compiledScript; + private final ExecutableScript.Factory scriptFactory; public ScriptCondition(Script script) { super(TYPE); this.script = script; scriptService = null; - compiledScript = null; + scriptFactory = null; } ScriptCondition(Script script, ScriptService scriptService) { super(TYPE); this.scriptService = scriptService; this.script = script; - compiledScript = scriptService.compile(script, Watcher.SCRIPT_EXECUTABLE_CONTEXT); + scriptFactory = scriptService.compile(script, Watcher.SCRIPT_EXECUTABLE_CONTEXT); } public Script getScript() { @@ -70,7 +70,7 @@ public final class ScriptCondition extends Condition { if (script.getParams() != null && !script.getParams().isEmpty()) { parameters.putAll(script.getParams()); } - ExecutableScript executable = compiledScript.newInstance(parameters); + ExecutableScript executable = scriptFactory.newInstance(parameters); Object value = executable.run(); if (value instanceof Boolean) { return (Boolean) value ? MET : UNMET; diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ExecutableScriptTransform.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ExecutableScriptTransform.java index 38d0786bce4..2dde57f4a82 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ExecutableScriptTransform.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ExecutableScriptTransform.java @@ -52,8 +52,8 @@ public class ExecutableScriptTransform extends ExecutableTransform new MockExecutableScript(vars, p -> script)); + return context.factoryClazz.cast((ExecutableScript.Factory) vars -> new MockExecutableScript(vars, p -> script)); } } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/test/MockPainlessScriptEngine.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/test/MockPainlessScriptEngine.java index 4812a308bf4..5ff59ac30ae 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/test/MockPainlessScriptEngine.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/test/MockPainlessScriptEngine.java @@ -45,9 +45,9 @@ public class MockPainlessScriptEngine extends MockScriptEngine { @Override public T compile(String name, String script, ScriptContext context, Map params) { if (context.instanceClazz.equals(ExecutableScript.class)) { - return context.compiledClazz.cast((ExecutableScript.Compiled) vars -> new MockExecutableScript(vars, p -> script)); + return context.factoryClazz.cast((ExecutableScript.Factory) vars -> new MockExecutableScript(vars, p -> script)); } else if (context.instanceClazz.equals(SearchScript.class)) { - return context.compiledClazz.cast((SearchScript.Compiled) (vars, lookup) -> new MockSearchScript(lookup, vars, p -> script)); + return context.factoryClazz.cast((SearchScript.Factory) (vars, lookup) -> new MockSearchScript(lookup, vars, p -> script)); } throw new IllegalArgumentException("mock painless does not know how to handle context [" + context.name + "]"); } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java b/plugin/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java index a4ac34e45e6..49642046cf7 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java @@ -60,8 +60,8 @@ public class ScriptTransformTests extends ESTestCase { ScriptType type = randomFrom(ScriptType.values()); Map params = Collections.emptyMap(); Script script = new Script(type, "_lang", "_script", params); - ExecutableScript.Compiled compiledScript = mock(ExecutableScript.Compiled.class); - when(service.compile(script, Watcher.SCRIPT_EXECUTABLE_CONTEXT)).thenReturn(compiledScript); + ExecutableScript.Factory factory = mock(ExecutableScript.Factory.class); + when(service.compile(script, Watcher.SCRIPT_EXECUTABLE_CONTEXT)).thenReturn(factory); ExecutableScriptTransform transform = new ExecutableScriptTransform(new ScriptTransform(script), logger, service); WatchExecutionContext ctx = mockExecutionContext("_name", EMPTY_PAYLOAD); @@ -74,7 +74,7 @@ public class ScriptTransformTests extends ESTestCase { ExecutableScript executable = mock(ExecutableScript.class); when(executable.run()).thenReturn(transformed); - when(compiledScript.newInstance(model)).thenReturn(executable); + when(factory.newInstance(model)).thenReturn(executable); Transform.Result result = transform.execute(ctx, payload); assertThat(result, notNullValue()); @@ -88,8 +88,8 @@ public class ScriptTransformTests extends ESTestCase { ScriptType type = randomFrom(ScriptType.values()); Map params = Collections.emptyMap(); Script script = new Script(type, "_lang", "_script", params); - ExecutableScript.Compiled compiledScript = mock(ExecutableScript.Compiled.class); - when(service.compile(script, Watcher.SCRIPT_EXECUTABLE_CONTEXT)).thenReturn(compiledScript); + ExecutableScript.Factory factory = mock(ExecutableScript.Factory.class); + when(service.compile(script, Watcher.SCRIPT_EXECUTABLE_CONTEXT)).thenReturn(factory); ExecutableScriptTransform transform = new ExecutableScriptTransform(new ScriptTransform(script), logger, service); WatchExecutionContext ctx = mockExecutionContext("_name", EMPTY_PAYLOAD); @@ -100,7 +100,7 @@ public class ScriptTransformTests extends ESTestCase { ExecutableScript executable = mock(ExecutableScript.class); when(executable.run()).thenThrow(new RuntimeException("_error")); - when(compiledScript.newInstance(model)).thenReturn(executable); + when(factory.newInstance(model)).thenReturn(executable); Transform.Result result = transform.execute(ctx, payload); assertThat(result, notNullValue()); @@ -114,8 +114,8 @@ public class ScriptTransformTests extends ESTestCase { ScriptType type = randomFrom(ScriptType.values()); Map params = Collections.emptyMap(); Script script = new Script(type, "_lang", "_script", params); - ExecutableScript.Compiled compiledScript = mock(ExecutableScript.Compiled.class); - when(service.compile(script, Watcher.SCRIPT_EXECUTABLE_CONTEXT)).thenReturn(compiledScript); + ExecutableScript.Factory factory = mock(ExecutableScript.Factory.class); + when(service.compile(script, Watcher.SCRIPT_EXECUTABLE_CONTEXT)).thenReturn(factory); ExecutableScriptTransform transform = new ExecutableScriptTransform(new ScriptTransform(script), logger, service); WatchExecutionContext ctx = mockExecutionContext("_name", EMPTY_PAYLOAD); @@ -127,7 +127,7 @@ public class ScriptTransformTests extends ESTestCase { ExecutableScript executable = mock(ExecutableScript.class); Object value = randomFrom("value", 1, new String[] { "value" }, Arrays.asList("value"), singleton("value")); when(executable.run()).thenReturn(value); - when(compiledScript.newInstance(model)).thenReturn(executable); + when(factory.newInstance(model)).thenReturn(executable); Transform.Result result = transform.execute(ctx, payload); assertThat(result, notNullValue());