From 5c8fe598a6bcb11bf0e674ef59c2726eeb8cfbf8 Mon Sep 17 00:00:00 2001 From: kimchy Date: Wed, 27 Apr 2011 00:38:51 +0300 Subject: [PATCH] Scripting - mvel: Fix thread safety misuse when compiling scripts, first go it so use ParserConfiguration, second is a fix in mvel itself, relates to #886. --- .../script/mvel/MvelScriptEngineService.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/script/mvel/MvelScriptEngineService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/script/mvel/MvelScriptEngineService.java index 43fc2b00424..dc6cbadff42 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/script/mvel/MvelScriptEngineService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/script/mvel/MvelScriptEngineService.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.math.UnboxedMathUtils; import org.elasticsearch.common.mvel2.MVEL; +import org.elasticsearch.common.mvel2.ParserConfiguration; import org.elasticsearch.common.mvel2.ParserContext; import org.elasticsearch.common.mvel2.compiler.ExecutableStatement; import org.elasticsearch.common.mvel2.integration.impl.MapVariableResolverFactory; @@ -45,20 +46,20 @@ import java.util.Map; */ public class MvelScriptEngineService extends AbstractComponent implements ScriptEngineService { - private final ParserContext parserContext; + private final ParserConfiguration parserConfiguration; @Inject public MvelScriptEngineService(Settings settings) { super(settings); - parserContext = new ParserContext(); - parserContext.addPackageImport("java.util"); - parserContext.addPackageImport("org.elasticsearch.common.trove"); - parserContext.addPackageImport("org.elasticsearch.common.joda"); - parserContext.addImport("time", MVEL.getStaticMethod(System.class, "currentTimeMillis", new Class[0])); + parserConfiguration = new ParserConfiguration(); + parserConfiguration.addPackageImport("java.util"); + parserConfiguration.addPackageImport("org.elasticsearch.common.trove"); + parserConfiguration.addPackageImport("org.elasticsearch.common.joda"); + parserConfiguration.addImport("time", MVEL.getStaticMethod(System.class, "currentTimeMillis", new Class[0])); // unboxed version of Math, better performance since conversion from boxed to unboxed my mvel is not needed for (Method m : UnboxedMathUtils.class.getMethods()) { if ((m.getModifiers() & Modifier.STATIC) > 0) { - parserContext.addImport(m.getName(), m); + parserConfiguration.addImport(m.getName(), m); } } } @@ -76,7 +77,7 @@ public class MvelScriptEngineService extends AbstractComponent implements Script } @Override public Object compile(String script) { - return MVEL.compileExpression(script, parserContext); + return MVEL.compileExpression(script, new ParserContext(parserConfiguration)); } @Override public Object execute(Object compiledScript, Map vars) {