Shanzhai workflow: the responsibility chain controlled by groovy

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.