Skip to content

Commit

Permalink
Improved ScriptEngine handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechhabarta committed Apr 20, 2017
1 parent 6a8aee4 commit 80424eb
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.*;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

Expand Down Expand Up @@ -162,8 +163,16 @@ private static boolean isUndefined(Object variable) {

private CustomTypeNamingFunction getCustomTypeNamingFunction() throws ScriptException {
if (customTypeNamingFunction == null) {
final String engineMimeType = "application/javascript";
final ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine engine = manager.getEngineByName("javascript");
final ScriptEngine engine = manager.getEngineByMimeType(engineMimeType);
if (engine == null) {
System.out.println(String.format("Error: Script engine for '%s' MIME type not found. Available engines: %s", engineMimeType, manager.getEngineFactories().size()));
for (ScriptEngineFactory factory : manager.getEngineFactories()) {
System.out.println(String.format(" %s %s - MIME types: %s", factory.getEngineName(), factory.getEngineVersion(), factory.getMimeTypes()));
}
throw new RuntimeException("Cannot evaluate function specified using 'customTypeNamingFunction' parameter. See log for details.");
}
engine.eval("var getName = " + settings.customTypeNamingFunction);
final Invocable invocable = (Invocable) engine;
customTypeNamingFunction = invocable.getInterface(CustomTypeNamingFunction.class);
Expand Down

0 comments on commit 80424eb

Please sign in to comment.