BeanShell

1. Introduction

     BeanShell is a Java scripting language, the official website address is https://github.com/beanshell/beanshell/.

     The commonly used scripting language in XWorker is Groovy, and Beanhell is not very commonly used. BeanShell can run in Android, but Groovy cannot, because Groovy needs to be compiled into classes, but BeanShell does not.

2. Design ideas

     The BeanShell model is relatively simple, it implements the run method and uses the model as an action.

     The Java code of the run method is as follows:

public static Object run(ActionContext actionContext) throws EvalError {
		Bindings bindings = actionContext.getScope(actionContext.getScopesSize() - 2);		
		
		World world = bindings.world;
		Action action = null;
		if(bindings.getCaller() instanceof Thing){
			Thing actionThing = (Thing) bindings.getCaller();
			action = actionThing.getAction();
		}else{
			action = (Action) bindings.getCaller();
		}
		
		if(action == null){
			return null;
		}
		
		Interpreter i = new Interpreter();
		i.set("ac", actionContext);
		i.set("actionContext", actionContext);;
		i.set("world", world);
		
		if(action.getThing().getBoolean("mergeActionContext")) {
			for(String key : actionContext.keySet()) {
				i.set(key, actionContext.get(key));
			}
		}
		
		String code = action.getThing().getStringBlankAsNull("code");
		if(code == null || "".equals(code)) {
			return null;
		}else {
			return i.eval(code);
		}
	}

3.Example

<?xml version='1.0' encoding='utf-8'?>

<BeanShellAction name="BeanShellAction" descriptors="xworker.beanshell.BeanShellAction">
     <code><![CDATA[import java.util.Date;

Date date = new Date();
System.out.println(date);

return date;]]></code>
</BeanShellAction>

 

Copyright ©  2007-2019 XWorker.org  版权所有  沪ICP备08000575号