Suspected title party
This article does not involve steps, conditions, loops, spilts, joins, and roles in a workflow.
Workflow is not involved.
To put it bluntly, add script control to the responsibility chain.
Extended from Apache common chain:
Http://commons.apache.org/chain/
For example, the following chain describes my work day life: breakfast, go to company, work, lunch, work, go home
<Catalog> <br/> <chain name = "My working day life"> <br/> <command ID = "Breakfast" classname = "Co. command. breakfastcommand "/> <br/> <command ID =" go-to-company "classname =" Co. command. gotocompanycommand "/> <br/> <command ID =" work "classname =" Co. command. workcommand "/> <br/> <command ID =" lunch "classname =" Co. command. lunchcommand "/> <br/> <command ID =" work "classname =" Co. command. workcommand "/> <br/> <command ID =" go-home "classname =" Co. command. gohomecommand "/> <br/> </chain> <br/> </CATALOG>
If it is a holiday, my life may be like this: breakfast, going out high, going home
<Catalog> <br/> <chain name = "my holiday life"> <br/> <command ID = "Breakfast" classname = "Co. command. breakfastcommand "/> <br/> <command ID =" play "classname =" Co. command. playcommand "/> <br/> <command ID =" go-home "classname =" Co. command. gohomecommand "/> <br/> </chain> <br/> </CATALOG>
Now I have a requirement to add the Script Function in the chain configuration to control whether the command should be executed.
With this function, the two chains above can be merged into one:
<Catalog> <br/> <chain name = "My working day life"> <br/> <command ID = "Breakfast" classname = "Co. command. breakfastcommand "/> <br/> <MCE: script expression =" context. isholiday () "> <! -- <Br/> <command ID = "play" classname = "Co. command. playcommand "/> </P> <p> // --> </MCE: SCRIPT> <br/> <MCE: script expression = "! Context. isholiday () "> <! -- <Br/> <command ID = "go-to-company" classname = "Co. command. gotocompanycommand "/> <br/> <command ID =" work "classname =" Co. command. workcommand "/> <br/> <command ID =" lunch "classname =" Co. command. lunchcommand "/> <br/> <command ID =" work "classname =" Co. command. workcommand "/> </P> <p> // --> </MCE: SCRIPT> <br/> <command ID =" go-home "classname =" Co. command. gohomecommand "/> <br/> </chain> <br/> </CATALOG> <br/>
With process control, the command is reversed.
How to implement it?
I added an expression attribute (script content) to none of the commands ):
Each Command contained in the <SCRIPT>... </SCRIPT> node has the same expression attribute.
For example:
<MCE: script expression = "! Context. isholiday () "> <! -- <Br/> <command ID = "go-to-company" classname = "Co. command. gotocompanycommand "/> <br/> <command ID =" work "classname =" Co. command. workcommand "/> <br/> <command ID =" lunch "classname =" Co. command. lunchcommand "/> <br/> <command ID =" work "classname =" Co. command. workcommand "/> </P> <p> // --> </MCE: SCRIPT> <br/>
This is a command. The expression value of each command is :"! Context. isholiday ()"
To a command base class:
Run the expression before running the business logic (Action). If it is true, execute the action. Otherwise, continue to the next command:
Public abstract class basecommand implements Org. apache. commons. chain. command <br/>{< br/> private string expression; </P> <p> private scriptengine; </P> <p> Public final Boolean execute (context) throws exception <br/>{< br/> If (this. isrunable (context) <br/>{< br/> return this. action (context); <br/>}< br/> return false; <br/>}< br/> private Boolean isrunable (context) <B R/>{< br/> If (! Stringutils. hastext (this. expression) <br/> return true; <br/> return (Boolean) scriptengine. run (this. expression, context )). booleanvalue (); <br/>}< br/> Public void setscriptengine (scriptengine) <br/>{< br/> This. scriptengine = scriptengine; </P> <p >}< br/> Public String getexpression () <br/>{< br/> return expression; <br/>}< br/> Public void setexpression (string expression) <br/>{< br/> This. expression = expression; <br/>}</P> <p> Public Abstract Boolean action (context) throws exception; <br/>}
Scriptengine is a script engine interface:
Public interface scriptengine <br/>{< br/> Public object run (string Express, object context); <br/>}< br/>
To provide a groovy implementation:
Public class policyexpressionengine implements scriptengine <br/>{< br/> Public object run (string Express, object context) <br/>{< br/> classloader Cl = policyexpressionengine. class. getclassloader (); <br/> policyclassloader policycl = new policyclassloader (CL); <br/> class policyclass = policycl. parseclass (Express); <br/> script SC; <br/> try <br/>{< br/> SC = (SCRIPT) policyclass. newinstance (); <br/>}< br/> catch (exception e) <br/>{< br/> throw new runtimeexception (E. getmessage (), e); <br/>}< br/> binding = new binding (); <br/> binding. setvariable ("context", context); <br/> SC. setbinding (binding); <br/> return SC. run (); <br/>}< br/>
Game over.