3. 接口。它表示「命令鏈」,要在其中執行的命令,須要先添加到Chain中。Chain的父接口是Command,ChainBase實現了它。 Chainweb
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"/> apache
</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。 post
<context-param> spa <param-name>org.apache.commons.chain.CONFIG_CLASS_RESOURCE</param-name> xml <param-value>resources/catalog.xml</param-value> 對象 </context-param> 索引 <listener> 接口 <listener-class>org.apache.commons.chain.web.ChainListener</listener-class> ci
</listener>
|
.getServletContext().getAttribute("catalog"); get
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>定義別名,簡化書寫。