3. 接口。它表示“命令链”,要在其中执行的命令,需要先添加到Chain中。Chain的父接口是Command,ChainBase实现了它。 Chain
public class Command1 implements Command {
public boolean execute(Context arg0) throws Exception {
System.out.println("Command1 is done!");
return false;
}
}
|
public class Command2 implements Command {
public boolean execute(Context arg0) throws Exception {
System.out.println("Command2 is done!");
return false;
}
}
|
public class Command3 implements Command {
public boolean execute(Context arg0) throws Exception {
System.out.println("Command3 is done!");
return true;
}
}
|
public class CommandChain extends ChainBase {
//增加命令的顺序也决定了执行命令的顺序
public CommandChain(){
addCommand( new Command1());
addCommand( new Command2());
addCommand( new Command3());
}
public static void main(String[] args) throws Exception{
Command process = new CommandChain();
Context ctx= new ContextBase();
process.execute( ctx);
}
}
|
<?xml version="1.0" encoding="gb2312"?>
<catalog>
<chain name="CommandChain">
<!-- 定义的顺序决定执行的顺序 -->
<command id="command1" className= "chain.Command1"/>
<command id="command2" className= "chain.Command2"/>
<command id="command3" className= "chain.Command3"/>
</chain>
<command name="command4" className="chain.Command1"/>
</catalog>
|
public class CatalogLoader {
static final String cfgFile= "/chain/chain-cfg.xml";
public static void main(String[] args) throws Exception{
CatalogLoader loader= new CatalogLoader();
ConfigParser parser= new ConfigParser();
parser.parse( loader.getClass().getResource( cfgFile));
Catalog catalog= CatalogFactoryBase.getInstance().getCatalog();
//加载Chain
Command cmd= catalog.getCommand("CommandChain");
Context ctx= new ContextBase();
cmd.execute( ctx);
//加载Command
cmd= catalog.getCommand( "command4");
cmd.execute( ctx);
}
}
|
注意:使用配置文件的话,需要使用Commons Digester。而Digester则依赖:Commons Collections、Commons Logging和Commons BeanUtils。
<context-param> <param-name>org.apache.commons.chain.CONFIG_CLASS_RESOURCE</param-name> <param-value>resources/catalog.xml</param-value> </context-param> <listener> <listener-class>org.apache.commons.chain.web.ChainListener</listener-class>
</listener>
|
.getServletContext().getAttribute("catalog");
public class Filter1 implements Filter {
public boolean postprocess(Context arg0, Exception arg1) {
System.out.println("Filter1 is after done!");
return false;
}
public boolean execute(Context arg0) throws Exception {
System.out.println("Filter1 is done!");
return false;
}
}
|
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<chain name="CommandChain">
<command id="command1" className= "chain.Command1"/>
<command id="filter1" className= "chain.Filter1"/>
<command className="org.apache.commons.chain.generic.LookupCommand" name="chain_command3"
optional="true"/>
<command id="command2" className= "chain.Command2"/>
</chain>
<chain name="chain_command3"> <command id="command3" className= "chain.Command3"/>
</chain>
</catalog>
|
<?xml version="1.0" encoding="gb2312"?>
<catalog>
<!-- Command的别名,以后直接使用即可 -->
<define name="command1" className="chain.Command1"/>
<define name="command2" className="chain.Command2"/>
<define name="command3" className="chain.Command3"/>
<define name="filter1" className="chain.Filter1"/>
<define name="lookupCommand"
className="org.apache.commons.chain.generic.LookupCommand"/>
<chain name="CommandChain">
<command1 id="1"/>
<filter1 id="2"/>
<lookupCommand name="chain_command3" optional="true"/>
<command2 id="3"/>
</chain>
<chain name="chain_command3">
<command3 id="3"/>
</chain>
<command1 name="command4"/>
</catalog>
|
- 使用<define>定义别名,简化书写。