Groovy是一个Java的脚本语言,在动态模型中大量使用。
如上图,主要定义GroovyAction的run方法以及code(代码属性)。
下面是GroovyAction的Java静态方法实现的示意代码。
public class GroovyAction { public static Object run(ActionContext context) throws Exception{ //获取Groovy动作实例模型 Bindings bindings = context.getScope(context.getScopesSize() - 2); World world = bindings.world; Action action = null; if(bindings.getCaller() instanceof Thing){ Thing actionThing = (Thing) bindings.getCaller(); action = actionThing.getAction(); action.checkChanged(); }else{ action = (Action) bindings.getCaller(); } if(action.actionClass == null || action.changed){ //查看代码是否需要重新编译 ......省略 } if(action.actionClass != null){ //执行Groovy加拨嗯 Script script = (Script) action.actionClass .newInstance(); Bindings bindings1 = context.push(null); try{ context.pushAction(action); Binding binding = new Binding(context); script.setBinding(binding); Object result = script.run(); return result; }finally{ context.popAction(); bindings1.remove("actionContext"); //回填groovy脚本中定义的全局变量 Bindings varBindings = UtilAction.getVarScope(action.getThing(), context); if(varBindings != null){ varBindings.putAll(bindings1); } context.pop(); } } return null; } }
Copyright © 2007-2019 XWorker.org 版权所有 沪ICP备08000575号