nashorn

How to remove java apis from Nashorn-engine?

百般思念 提交于 2019-11-27 06:00:35
问题 Is it possible to hide or remove java api's from nashorn-engine? So that it could only see or use "default" ECMAScript 262 Edition 5.1 with some especially exposed functions / variables? I would like to let my endusers create some specific logic for their own without worrying they could hack the whole system. Of course there might be some security holes in the nashorn engine etc. but that is the different topic. Edit: Sorry I forgot to mention that I am running nashorn inside my java

Java8 Lambda【简】

馋奶兔 提交于 2019-11-27 05:04:51
Java8 Lambda Lambda是一个表达式,也可以说它是一个匿名函数。然而在使用它或是阅读Lambda代码的时候,却显得并不那么容易。因为它匿名,因为它删减了一些必要的说明信息(比如方法名)。 一、为什么要用Lambda表达式 1.更加紧凑的代码 比如Java中现有的匿名内部类以及监听器(listeners)和事件处理器(handlers)都显得很冗长 2.修改方法的能力 (我个人理解为代码注入,或者有点类似JavaScript中传一个回调函数给另外一个函数) 比如Collection接口的contains方法,当且仅当传入的元素真正包含在集合中,才返回true。而假如我们想对一个字符串集合,传入一个字符串,只要这个字符串出现在集合中(忽略大小写)就返回true。 3.更好地支持多核处理 例如,通过Java 8新增的Lambda表达式,我们可以很方便地并行操作大集合,充分发挥多核CPU的潜能。并行处理函数如filter、map和reduce。 二、变量作用域说明 关于变量在Lambda中的作用域,主要表现在以下几点: 对局部变量可见 对全局变量可见 对当前层传入的参数可见 对上层函数传入的参数可见 对上层Lambda传入的参数可见 三、实例 实例1 FileFilter File dir = new File("/an/dir/"); FileFilter

Secure Nashorn JS Execution

北城以北 提交于 2019-11-27 04:13:06
问题 How can I securely execute some user supplied JS code using Java8 Nashorn? The script extends some computations for some servlet based reports. The app has many different (untrusted) users. The scripts should only be able to access a Java Object and those returned by the defined members. By default the scripts could instantiate any class using Class.forName() (using .getClass() of my supplied object). Is there any way to prohibit access to any java class not explicitly specified by me? 回答1: I

Reuse Nashorn ScriptEngine in Servlet [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-11-27 02:42:47
问题 This question already has an answer here: Should I use a separate ScriptEngine and CompiledScript instances per each thread? 4 answers I want to execute a JavaScript within a servlet. Is it possible to reuse the same Scripting Engine across all servlet invocations? Servlet instances are shared by multiple threads. Does this require to create a new Scripting Engine per request? That would be a unacceptable performance penalty. As an example, is the following code save? public class MyServlet

Java 8 ScriptEngine across ClassLoaders

人盡茶涼 提交于 2019-11-26 18:30:05
问题 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? 回答1: 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: Nashorn

Should I use a separate ScriptEngine and CompiledScript instances per each thread?

强颜欢笑 提交于 2019-11-26 17:22:43
My program uses Java Scripting API and can eval some scripts concurrently. They don't use shared script objects, Bindings or Context, but can use same ScriptEngine and CompiledScript objects. I see that Oracle Nashorn implementation in Java 8 is not multithreaded, ScriptEngineFactory.getParameter('THREADING') returns null about which the documentation says: The engine implementation is not thread safe, and cannot be used to execute scripts concurrently on multiple threads. Does it mean that I should create a separate instance of ScriptEngine for each thread? Besides, documentation says nothing

Should I use a separate ScriptEngine and CompiledScript instances per each thread?

…衆ロ難τιáo~ 提交于 2019-11-26 05:22:48
问题 My program uses Java Scripting API and can eval some scripts concurrently. They don\'t use shared script objects, Bindings or Context, but can use same ScriptEngine and CompiledScript objects. I see that Oracle Nashorn implementation in Java 8 is not multithreaded, ScriptEngineFactory.getParameter(\'THREADING\') returns null about which the documentation says: The engine implementation is not thread safe, and cannot be used to execute scripts concurrently on multiple threads. Does it mean