diff --git a/src/main/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheScriptEngineService.java b/src/main/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheScriptEngineService.java index e41b8804e77..c52c407ffb4 100644 --- a/src/main/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheScriptEngineService.java +++ b/src/main/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheScriptEngineService.java @@ -61,7 +61,7 @@ public class XMustacheScriptEngineService extends AbstractComponent implements S * Execute a compiled template object (as retrieved from the compile method) * and fill potential place holders with the variables given. * - * @param template + * @param compiledScript * compiled template object. * @param vars * map of variables to use during substitution. @@ -69,10 +69,10 @@ public class XMustacheScriptEngineService extends AbstractComponent implements S * @return the processed string with all given variables substitued. * */ @Override - public Object execute(Object template, Map vars) { + public Object execute(CompiledScript compiledScript, Map vars) { BytesStreamOutput result = new BytesStreamOutput(); UTF8StreamWriter writer = utf8StreamWriter().setOutput(result); - ((Mustache) template).execute(writer, vars); + ((Mustache) compiledScript.compiled()).execute(writer, vars); try { writer.flush(); } catch (IOException e) { @@ -103,13 +103,13 @@ public class XMustacheScriptEngineService extends AbstractComponent implements S } @Override - public ExecutableScript executable(Object mustache, + public ExecutableScript executable(CompiledScript compiledScript, @Nullable Map vars) { - return new MustacheExecutableScript((Mustache) mustache, vars); + return new MustacheExecutableScript((Mustache) compiledScript.compiled(), vars); } @Override - public SearchScript search(Object compiledScript, SearchLookup lookup, + public SearchScript search(CompiledScript compiledScript, SearchLookup lookup, @Nullable Map vars) { throw new UnsupportedOperationException(); } diff --git a/src/test/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheScriptEngineTests.java b/src/test/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheScriptEngineTests.java index be009445275..b9997df86ff 100644 --- a/src/test/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheScriptEngineTests.java +++ b/src/test/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheScriptEngineTests.java @@ -8,6 +8,8 @@ package org.elasticsearch.watcher.support.template.xmustache; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.script.CompiledScript; +import org.elasticsearch.script.ScriptService; import org.elasticsearch.test.ElasticsearchTestCase; import org.junit.Before; import org.junit.Test; @@ -37,7 +39,8 @@ public class XMustacheScriptEngineTests extends ElasticsearchTestCase { + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" + "}}, \"negative_boost\": {{boost_val}} } }}"; Map vars = new HashMap<>(); vars.put("boost_val", "0.3"); - BytesReference o = (BytesReference) engine.execute(engine.compile(template), vars); + CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template)); + BytesReference o = (BytesReference) engine.execute(compiledScript, vars); assertEquals("GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.3 } }}", new String(o.toBytes(), Charset.forName("UTF-8"))); @@ -48,7 +51,8 @@ public class XMustacheScriptEngineTests extends ElasticsearchTestCase { Map vars = new HashMap<>(); vars.put("boost_val", "0.3"); vars.put("body_val", "\"quick brown\""); - BytesReference o = (BytesReference) engine.execute(engine.compile(template), vars); + CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template)); + BytesReference o = (BytesReference) engine.execute(compiledScript, vars); assertEquals("GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," + "\"negative\": {\"term\": {\"body\": {\"value\": \"\\\"quick brown\\\"\"}}}, \"negative_boost\": 0.3 } }}", new String(o.toBytes(), Charset.forName("UTF-8"))); @@ -73,7 +77,8 @@ public class XMustacheScriptEngineTests extends ElasticsearchTestCase { vars.put("test_var1", var1Writer.toString()); vars.put("test_var2", var2Writer.toString()); - BytesReference o = (BytesReference) engine.execute(engine.compile(template), vars); + CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template)); + BytesReference o = (BytesReference) engine.execute(compiledScript, vars); String s1 = o.toUtf8(); String s2 = prefix + " " + var1Writer.toString() + " " + var2Writer.toString(); assertEquals(s1, s2); diff --git a/src/test/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheTests.java b/src/test/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheTests.java index 48f9beb50ff..a41d2b9da8e 100644 --- a/src/test/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheTests.java +++ b/src/test/java/org/elasticsearch/watcher/support/template/xmustache/XMustacheTests.java @@ -13,7 +13,9 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.script.CompiledScript; import org.elasticsearch.script.ScriptEngineService; +import org.elasticsearch.script.ScriptService; import org.elasticsearch.test.ElasticsearchTestCase; import org.junit.Before; import org.junit.Test; @@ -40,7 +42,7 @@ public class XMustacheTests extends ElasticsearchTestCase { @Test public void testArrayAccess() throws Exception { String template = "{{data.0}} {{data.1}}"; - Object mustache = engine.compile(template); + CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template)); Map vars = new HashMap<>(); Object data = randomFrom( new String[] { "foo", "bar" }, @@ -57,7 +59,7 @@ public class XMustacheTests extends ElasticsearchTestCase { @Test public void testArrayInArrayAccess() throws Exception { String template = "{{data.0.0}} {{data.0.1}}"; - Object mustache = engine.compile(template); + CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template)); Map vars = new HashMap<>(); Object data = randomFrom( new String[][] { new String[] {"foo", "bar" }}, @@ -75,7 +77,7 @@ public class XMustacheTests extends ElasticsearchTestCase { @Test public void testMapInArrayAccess() throws Exception { String template = "{{data.0.key}} {{data.1.key}}"; - Object mustache = engine.compile(template); + CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template)); Map vars = new HashMap<>(); Object data = randomFrom( new Map[] { ImmutableMap.of("key", "foo"), ImmutableMap.of("key", "bar") }, @@ -132,7 +134,7 @@ public class XMustacheTests extends ElasticsearchTestCase { Map dataMap = new HashMap<>(); dataMap.put("data", unescaped.toString()); - Object mustache = engine.compile(template); + CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template)); Object output = engine.execute(mustache, dataMap); assertThat(output, notNullValue());