• Home
  • Articles
    • 日志
    • 妍小言
    • 舒小书
    • 浩然说
    • 生活日记
  • All Tags

java动态执行groovy脚本

30 Dec 2016

Reading time ~1 minute

背景

  • 可实时按需要编写groovy脚本来做到动态扩展计算能力

问题

  • groovy脚本中去做一些危险不应该被开放的事情,比如: 删表

测试可能性

  • 危险操作
package org.eddy;

/**
 * Created by eddy on 16/12/30.
 */
public class DBOp {

    public static void print() {
        System.out.println("drop a table");
    }
}
  • 脚本
import org.eddy.DBOp
DBOp.print()
  • 执行
    @Test
    public void test() {
        Binding binding = new Binding();
        GroovyShell shell = new GroovyShell(binding);
        shell.evaluate("import org.eddy.DBOp\n" +
                "DBOp.print()");
    }
  • 输出
drop a table

需求

  • 隔离groovy脚本执行环境。


javagroovy集成动态