mirror of
https://github.com/mitb-archive/filebot
synced 2025-03-09 22:09:47 -04:00
* cache compiled script snippets since each new instance leaks into PermGen memory
This commit is contained in:
parent
5683b85d4b
commit
75bd998408
@ -13,7 +13,9 @@ import java.text.FieldPosition;
|
|||||||
import java.text.Format;
|
import java.text.Format;
|
||||||
import java.text.ParsePosition;
|
import java.text.ParsePosition;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.script.Bindings;
|
import javax.script.Bindings;
|
||||||
import javax.script.Compilable;
|
import javax.script.Compilable;
|
||||||
@ -30,6 +32,7 @@ import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory;
|
|||||||
public class ExpressionFormat extends Format {
|
public class ExpressionFormat extends Format {
|
||||||
|
|
||||||
private static ScriptEngine engine;
|
private static ScriptEngine engine;
|
||||||
|
private static Map<String, CompiledScript> scriptletCache = new HashMap<String, CompiledScript>();
|
||||||
|
|
||||||
|
|
||||||
protected static synchronized ScriptEngine getGroovyScriptEngine() throws ScriptException {
|
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 String expression;
|
||||||
|
|
||||||
private final Object[] compilation;
|
private final Object[] compilation;
|
||||||
@ -50,7 +63,7 @@ public class ExpressionFormat extends Format {
|
|||||||
|
|
||||||
public ExpressionFormat(String expression) throws ScriptException {
|
public ExpressionFormat(String expression) throws ScriptException {
|
||||||
this.expression = expression;
|
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>();
|
List<Object> compilation = new ArrayList<Object>();
|
||||||
|
|
||||||
char open = '{';
|
char open = '{';
|
||||||
@ -87,7 +100,7 @@ public class ExpressionFormat extends Format {
|
|||||||
if (level == 1) {
|
if (level == 1) {
|
||||||
if (token.length() > 0) {
|
if (token.length() > 0) {
|
||||||
try {
|
try {
|
||||||
compilation.add(engine.compile(token.toString()));
|
compilation.add(compileScriptlet(token.toString()));
|
||||||
} catch (ScriptException e) {
|
} catch (ScriptException e) {
|
||||||
// try to extract syntax exception
|
// try to extract syntax exception
|
||||||
ScriptException illegalSyntax = e;
|
ScriptException illegalSyntax = e;
|
||||||
|
@ -36,13 +36,16 @@ public class SecureCompiledScript extends CompiledScript {
|
|||||||
permissions.add(new RuntimePermission("getFileSystemAttributes"));
|
permissions.add(new RuntimePermission("getFileSystemAttributes"));
|
||||||
|
|
||||||
// write permissions for temp and cache folders
|
// write permissions for temp and cache folders
|
||||||
permissions.add(new FilePermission(new File(System.getProperty("ehcache.disk.store.dir")).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("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;
|
return permissions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private final CompiledScript compiledScript;
|
private final CompiledScript compiledScript;
|
||||||
private final AccessControlContext sandbox;
|
private final AccessControlContext sandbox;
|
||||||
|
|
||||||
|
@ -31,7 +31,6 @@ import java.util.concurrent.TimeoutException;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.script.Compilable;
|
|
||||||
import javax.script.ScriptException;
|
import javax.script.ScriptException;
|
||||||
import javax.swing.AbstractAction;
|
import javax.swing.AbstractAction;
|
||||||
import javax.swing.Action;
|
import javax.swing.Action;
|
||||||
@ -432,9 +431,9 @@ class BindingDialog extends JDialog {
|
|||||||
ExpressionFormat format = new ExpressionFormat(expression) {
|
ExpressionFormat format = new ExpressionFormat(expression) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object[] compile(String expression, Compilable engine) throws ScriptException {
|
protected Object[] compile(String expression) throws ScriptException {
|
||||||
// simple expression format, everything as one expression
|
// simple expression format, everything as one expression
|
||||||
return new Object[] { engine.compile(expression) };
|
return new Object[] { compileScriptlet(expression) };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ package net.sourceforge.filebot.format;
|
|||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import javax.script.Bindings;
|
import javax.script.Bindings;
|
||||||
import javax.script.Compilable;
|
|
||||||
import javax.script.CompiledScript;
|
import javax.script.CompiledScript;
|
||||||
import javax.script.ScriptException;
|
import javax.script.ScriptException;
|
||||||
import javax.script.SimpleBindings;
|
import javax.script.SimpleBindings;
|
||||||
@ -19,7 +18,7 @@ public class ExpressionFormatTest {
|
|||||||
public void compile() throws Exception {
|
public void compile() throws Exception {
|
||||||
ExpressionFormat format = new TestScriptFormat("");
|
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[0] instanceof String);
|
||||||
assertTrue(expression[1] instanceof CompiledScript);
|
assertTrue(expression[1] instanceof CompiledScript);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user