I need to execute some javascript code 'inside' different classloaders. If it is java, each task will run in separate class loader. Now I need this to be javascript.
Do I need to create new instance of ScriptEngine
in each classloader, or is it ok to share one across class loaders?
A class is always identified using its fully qualified name (package. classname). So when a class is loaded into JVM, you have an entry as (package, classname, classloader). Therefore the same class can be loaded twice by two different ClassLoader instances.
Each application might use different versions of the same libraries, and must thus have a different classloader from the others in order to be able to have different versions of the same classes in a single JVM. but the web server has its own loader.it can have several classloaders.
The ClassLoader works based on a set of operations given by the delegation model. They are: ClassLoader always follows the Delegation Hierarchy Principle. Whenever JVM comes across a class, it checks whether that class is already loaded or not.
There are three types of built-in ClassLoader in Java. Bootstrap Class Loader – It loads JDK internal classes. It loads rt. jar and other core classes for example java.
From your question it is not clear why'd you look for such classloader isolation. So, I'm summarizing nashorn's classloader here - may be, you'll get what you're looking for.
Nashorn and classloaders:
(1) and (2) can not be customized. There are assumptions in nashorn code that it's a privileged code. And there are assumptions about genarated script class loaders and adapter loaders.
(3) is by default the thread context classloader at the time of nashorn engine creation. If the thread context class loader is null, then Nashorn's own loader - the extension loader - is used.
So if you create Nashorn engine after setting suitable thread context loader via Thread.setContextClassLoader API, you can control script engine's "app class loader".
If you're okay with using NashornScriptEngineFactory (nashorn specific API https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/NashornScriptEngineFactory.html) to create script engine, you can programmatically pass any classloader as "app class loader" as well. You may want to check out the NashornScriptEngineFactory methods that accept ClassLoader argument.
In addition to the "app class loader", optionally, you can also use another additional classloader that is searched before "app class loader". This is specified by "-cp" or "-classpath" nashorn command line option. Note that you can specify nashorn command line options for script engine via "nashorn.args" System property or programmatically pass using NashornScriptEngineFactory class's getScriptEngine methods. See also: https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With