Reputation: 5123
I am currently using Nashorn in a project and I would like to create a ScriptEngine
from NashornScriptEngineFactory
with only a ClassFilter
in order to secure my script to avoid unexpected call to some libraries. And I would like also add arguments to the factory (here --strict
in order to execute all javascript function in strict mode).
I found only these prototypes in NashornScriptEngineFactory
:
public ScriptEngine getScriptEngine()
public ScriptEngine getScriptEngine(final String... args)
public ScriptEngine getScriptEngine(final ClassFilter classFilter)
public ScriptEngine getScriptEngine(final ClassLoader appLoader)
public ScriptEngine getScriptEngine(final String[] args, final ClassLoader appLoader)
public ScriptEngine getScriptEngine(final String[] args, final ClassLoader appLoader, final ClassFilter classFilter)
I wonder why there is no prototype :
public ScriptEngine getScriptEngine(final String[] args, final ClassFilter classFilter)
Can anyone help me ?
Upvotes: 1
Views: 465
Reputation: 413757
In a normal, simple situation, you can generally just use the class loader of the class that's making the call to get a ScriptEngine
instance. That is, you can just pass
this.getClass().getClassLoader()
(or explicitly reference the class by name). Just pass that to the three-argument getScriptEngine()
method.
Now, in not-so-normal, not-so-simple situations, where you have multiple class loaders to worry about (sometimes an issue in a servlet container or something like an Ant task for example), then where you get the class loader from might make a difference. If you really are in a situation like that, then (A) good luck and (B) hopefully you'll have enough context to know what to pass.
Upvotes: 2