* cache compiled script snippets since each new instance leaks into PermGen memory

This commit is contained in:
Reinhard Pointner 2013-01-29 09:05:42 +00:00
parent 5683b85d4b
commit 75bd998408
4 changed files with 25 additions and 11 deletions

View File

@ -13,7 +13,9 @@ import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.script.Bindings;
import javax.script.Compilable;
@ -30,6 +32,7 @@ import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory;
public class ExpressionFormat extends Format {
private static ScriptEngine engine;
private static Map<String, CompiledScript> scriptletCache = new HashMap<String, CompiledScript>();
protected static synchronized ScriptEngine getGroovyScriptEngine() throws ScriptException {
@ -41,6 +44,16 @@ public class ExpressionFormat extends Format {
}
protected static synchronized CompiledScript compileScriptlet(String expression) throws ScriptException {
Compilable engine = (Compilable) getGroovyScriptEngine();
CompiledScript scriptlet = scriptletCache.get(expression);
if (scriptlet == null) {
scriptlet = engine.compile(expression);
scriptletCache.put(expression, scriptlet);
}
return scriptlet;
}
private final String expression;
private final Object[] compilation;
@ -50,7 +63,7 @@ public class ExpressionFormat extends Format {
public ExpressionFormat(String expression) throws ScriptException {
this.expression = expression;
this.compilation = secure(compile(expression, (Compilable) getGroovyScriptEngine()));
this.compilation = secure(compile(expression));
}
@ -59,7 +72,7 @@ public class ExpressionFormat extends Format {
}
protected Object[] compile(String expression, Compilable engine) throws ScriptException {
protected Object[] compile(String expression) throws ScriptException {
List<Object> compilation = new ArrayList<Object>();
char open = '{';
@ -87,7 +100,7 @@ public class ExpressionFormat extends Format {
if (level == 1) {
if (token.length() > 0) {
try {
compilation.add(engine.compile(token.toString()));
compilation.add(compileScriptlet(token.toString()));
} catch (ScriptException e) {
// try to extract syntax exception
ScriptException illegalSyntax = e;

View File

@ -36,13 +36,16 @@ public class SecureCompiledScript extends CompiledScript {
permissions.add(new RuntimePermission("getFileSystemAttributes"));
// write permissions for temp and cache folders
permissions.add(new FilePermission(new File(System.getProperty("ehcache.disk.store.dir")).getAbsolutePath() + File.separator + "-", "write, delete"));
permissions.add(new FilePermission(new File(System.getProperty("java.io.tmpdir")).getAbsolutePath() + File.separator + "-", "write, delete"));
try {
permissions.add(new FilePermission(new File(System.getProperty("java.io.tmpdir")).getAbsolutePath() + File.separator + "-", "write, delete"));
permissions.add(new FilePermission(new File(System.getProperty("ehcache.disk.store.dir")).getAbsolutePath() + File.separator + "-", "write, delete"));
} catch (Exception e) {
// ignore
}
return permissions;
}
private final CompiledScript compiledScript;
private final AccessControlContext sandbox;

View File

@ -31,7 +31,6 @@ import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.Compilable;
import javax.script.ScriptException;
import javax.swing.AbstractAction;
import javax.swing.Action;
@ -432,9 +431,9 @@ class BindingDialog extends JDialog {
ExpressionFormat format = new ExpressionFormat(expression) {
@Override
protected Object[] compile(String expression, Compilable engine) throws ScriptException {
protected Object[] compile(String expression) throws ScriptException {
// simple expression format, everything as one expression
return new Object[] { engine.compile(expression) };
return new Object[] { compileScriptlet(expression) };
}
};

View File

@ -5,7 +5,6 @@ package net.sourceforge.filebot.format;
import static org.junit.Assert.*;
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
@ -19,7 +18,7 @@ public class ExpressionFormatTest {
public void compile() throws Exception {
ExpressionFormat format = new TestScriptFormat("");
Object[] expression = format.compile("name: {name}, number: {number}", (Compilable) ExpressionFormat.getGroovyScriptEngine());
Object[] expression = format.compile("name: {name}, number: {number}");
assertTrue(expression[0] instanceof String);
assertTrue(expression[1] instanceof CompiledScript);