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@d61f4e1da2
This commit is contained in:
Ryan Ernst 2017-05-26 00:03:00 -07:00 committed by GitHub
parent 77f441b1a0
commit d9816fac58
6 changed files with 22 additions and 23 deletions

View File

@ -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<TimeValue> MAX_STOP_TIMEOUT_SETTING =
Setting.timeSetting("xpack.watcher.stop.timeout", TimeValue.timeValueSeconds(30), Setting.Property.NodeScope);
public static final ScriptContext<SearchScript.Compiled> SCRIPT_SEARCH_CONTEXT =
new ScriptContext<>("xpack", SearchScript.Compiled.class);
public static final ScriptContext<SearchScript.Factory> 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<ExecutableScript.Compiled> SCRIPT_EXECUTABLE_CONTEXT
= new ScriptContext<>("xpack_executable", ExecutableScript.Compiled.class);
public static final ScriptContext<ExecutableScript.Factory> SCRIPT_EXECUTABLE_CONTEXT
= new ScriptContext<>("xpack_executable", ExecutableScript.Factory.class);
private static final Logger logger = Loggers.getLogger(Watcher.class);
private WatcherIndexingListener listener;

View File

@ -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;

View File

@ -52,8 +52,8 @@ public class ExecutableScriptTransform extends ExecutableTransform<ScriptTransfo
model.putAll(script.getParams());
}
model.putAll(createCtxModel(ctx, payload));
ExecutableScript.Compiled compiledScript = scriptService.compile(script, Watcher.SCRIPT_EXECUTABLE_CONTEXT);
ExecutableScript executable = compiledScript.newInstance(model);
ExecutableScript.Factory factory = scriptService.compile(script, Watcher.SCRIPT_EXECUTABLE_CONTEXT);
ExecutableScript executable = factory.newInstance(model);
Object value = executable.run();
// TODO: deprecate one of these styles (returning a map or returning an opaque value below)
if (value instanceof Map) {

View File

@ -47,6 +47,6 @@ public class MockMustacheScriptEngine extends MockScriptEngine {
if (context.instanceClazz.equals(ExecutableScript.class) == false) {
throw new IllegalArgumentException("mock mustache only understands template scripts, not [" + context.name + "]");
}
return context.compiledClazz.cast((ExecutableScript.Compiled) vars -> new MockExecutableScript(vars, p -> script));
return context.factoryClazz.cast((ExecutableScript.Factory) vars -> new MockExecutableScript(vars, p -> script));
}
}

View File

@ -45,9 +45,9 @@ public class MockPainlessScriptEngine extends MockScriptEngine {
@Override
public <T> T compile(String name, String script, ScriptContext<T> context, Map<String, String> 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 + "]");
}

View File

@ -60,8 +60,8 @@ public class ScriptTransformTests extends ESTestCase {
ScriptType type = randomFrom(ScriptType.values());
Map<String, Object> 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<String, Object> 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<String, Object> 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());