代码之家  ›  专栏  ›  技术社区  ›  delux247

如何在沙盒中使用Ryo for Java运行JavaScript?

  •  35
  • delux247  · 技术社区  · 18 年前

    我们的Java应用程序的一部分需要运行由非开发人员编写的JavaScript。这些非开发人员正在使用JavaScript进行数据格式化。(主要是简单的逻辑和字符串连接)。

    我的问题是如何设置这些脚本的执行,以确保脚本错误不会对应用程序的其余部分产生重大的负面影响。

    • 需要防止无限循环
    • 防止生成新线程。
    • 限制对服务和环境的访问
      • 文件系统(例如:如果不满的脚本编写器决定删除文件)
      • 数据库(同样,删除数据库记录)

    基本上,我需要设置JavaScript范围,使其只包含他们需要的内容,而不包含更多内容。

    6 回复  |  直到 18 年前
        1
  •  9
  •   Ates Goral    14 年前

        2
  •  20
  •   Kenster marc_s    11 年前

    example in the Rhino JavaDocs

     protected void observeInstructionCount(Context cx, int instructionCount)
     {
         MyContext mcx = (MyContext)cx;
         long currentTime = System.currentTimeMillis();
         if (currentTime - mcx.startTime > 10*1000) {
             // More then 10 seconds from Context creation time:
             // it is time to stop the script.
             // Throw Error instance to ensure that script will never
             // get control back through catch or finally.
             throw new Error();
         }
     }
    
        6
  •  -9
  •   jliszka    18 年前