跨类加载器的 Java 8 ScriptEngine [英] Java 8 ScriptEngine across ClassLoaders

查看:23
本文介绍了跨类加载器的 Java 8 ScriptEngine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在内部"不同的类加载器中执行一些 javascript 代码.如果是java,则每个任务将在单独的类加载器中运行.现在我需要这是 javascript.

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.

我是否需要在每个类加载器中创建 ScriptEngine 的新实例,还是可以跨类加载器共享一个实例?

Do I need to create new instance of ScriptEngine in each classloader, or is it ok to share one across class loaders?

推荐答案

从你的问题来看,不清楚你为什么要寻找这样的类加载器隔离.所以,我在这里总结了 nashorn 的类加载器 - 也许,你会得到你想要的.

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 和类加载器:

Nashorn and classloaders:

  1. Nashorn 类 (jdk.nashorn.*) 由 Java 扩展类加载器加载
  2. 生成的脚本类、适配器(子类、脚本的接口实现)由 nashorn 的内部类加载器加载.
  3. 从脚本引用的 Java 类由 Nashorn应用类加载器"加载.

(1) 和 (2) 不能自定义.nashorn 代码假设它是特权代码.还有关于生成脚本类加载器和适配器加载器的假设.

(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) 默认是 nashorn 引擎创建时的线程上下文类加载器.如果线程上下文类加载器为空,则使用 Nashorn 自己的加载器——扩展加载器.

(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.

因此,如果您在通过 Thread.setContextClassLoader API 设置合适的线程上下文加载器后创建 Nashorn 引擎,您就可以控制脚本引擎的应用程序类加载器".

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".

如果您可以使用 NashornScriptEngineFactory(特定于 nashorn 的 API https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/NashornScriptEngineFactory.html) 创建脚本引擎,您也可以以编程方式将任何类加载器作为应用程序类加载器"传递.您可能需要查看接受 ClassLoader 参数的 NashornScriptEngineFactory 方法.

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.

除了应用类加载器"之外,您还可以选择使用另一个在应用类加载器"之前搜索的其他类加载器.这由-cp"或-classpath"nashorn命令行选项指定.请注意,您可以通过nashorn.args"系统属性或使用 NashornScriptEngineFactory 类的 getScriptEngine 方法以编程方式为脚本引擎指定 nashorn 命令行选项.另见:https://wiki.openjdk.java.net/显示/Nashorn/Nashorn+jsr223+engine+notes

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

希望这会有所帮助.

这篇关于跨类加载器的 Java 8 ScriptEngine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆