From 6a779fc730c8e09258a3799005759604a92b5429 Mon Sep 17 00:00:00 2001 From: Chris Earle Date: Tue, 15 Sep 2015 10:56:33 -0400 Subject: [PATCH] Adding support for invokedynamic with Groovy scripts. --- .../groovy/GroovyScriptEngineService.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java b/core/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java index 9c3cf4f86cc..7b73cdbc5fd 100644 --- a/core/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java +++ b/core/src/main/java/org/elasticsearch/script/groovy/GroovyScriptEngineService.java @@ -57,19 +57,52 @@ import java.util.Map; */ public class GroovyScriptEngineService extends AbstractComponent implements ScriptEngineService { + /** + * The name of the scripting engine/language. + */ public static final String NAME = "groovy"; + /** + * The setting to enable or disable invokedynamic instruction support in Java 7+. + *

+ * This should only be used with Java 7u60 or later because of issues related to the instruction. + * The invokedynamic instruction allows near-Java performance from many of Groovy's + * dynamic features, which is why it is enabled by default. + *

+ * Note: If this is disabled because invokedynamic is causing issues, then the Groovy + * indy jar needs to be replaced by the non-indy variant of it on the classpath (e.g., + * groovy-all-2.4.4-indy.jar should be replaced by groovy-all-2.4.4.jar). + *

+ * Defaults to {@code true}. + */ + public static final String GROOVY_INDY_ENABLED = "script.groovy.indy"; + /** + * The name of the Groovy compiler setting to use associated with activating invokedynamic support. + */ + public static final String GROOVY_INDY_SETTING_NAME = "indy"; + private final GroovyClassLoader loader; @Inject public GroovyScriptEngineService(Settings settings) { super(settings); + ImportCustomizer imports = new ImportCustomizer(); imports.addStarImports("org.joda.time"); imports.addStaticStars("java.lang.Math"); + CompilerConfiguration config = new CompilerConfiguration(); + config.addCompilationCustomizers(imports); // Add BigDecimal -> Double transformer config.addCompilationCustomizers(new GroovyBigDecimalTransformer(CompilePhase.CONVERSION)); + + // Implicitly requires Java 7u60 or later to get valid support + if (settings.getAsBoolean(GROOVY_INDY_ENABLED, true)) { + // maintain any default optimizations + config.getOptimizationOptions().put(GROOVY_INDY_SETTING_NAME, true); + } + + // Groovy class loader to isolate Groovy-land code this.loader = new GroovyClassLoader(getClass().getClassLoader(), config); }