设为首页 收藏本站
查看: 568|回复: 0

[经验分享] apache commons chain

[复制链接]

尚未签到

发表于 2016-12-30 09:18:17 | 显示全部楼层 |阅读模式
Chain of Responsibility(CoR)模式也叫职责链模式或者职责连锁模式,是由GoF提出的23种软件设计模式的一种。Chain of Responsibility模式是行为模式之一,该模式构造一系列分别担当不同的职责的类的对象来共同完成一个任务,这些类的对象之间像链条一样紧密相连,所以被称作职责链模式.
将CoR和Command模式结合使用,可以使得客户端在处理的过程不需要关心是使用一个command还是 一系列的command。通过 Liskov 代换原则,chain implement command,在使用command的地方都可以使用chain。
apache commons chain 提供了对CoR模式的基础支持,简化了和促进了实际应用CoR模式。
apache commons chain 核心组件
DSC0000.jpg
1.Context
   command的执行的上下文环境,即包含了应用程序的状态。
   extends map。继承自map接口,没有增加任何其他的接口方法,它只是一个标记接口。ContextBase是它的实现类。
   一般情况下,使用map来记录状态就可以了。例如map.put(state,value),map.get(state).在某些特定的使用时候,若需要明确定义拥有特定状态属性的java bean,例如通过ProfileContext.getXXState()/ProfileContext.setXXState(stateValue)来获得和属性具体的属性值。通过继承ContextBase 来实现,加入特定的属性字段,与此同时获得了两种方式来获取具体的状态。get(state)和getXXState()。通过反射类实现。

public class ContextBase extends HashMap implements Context {
public Object put(Object key, Object value) {
// Case 1 -- no local properties
if (descriptors == null) {
return (super.put(key, value));
}
// Case 2 -- this is a local property
if (key != null) {
PropertyDescriptor descriptor =
(PropertyDescriptor) descriptors.get(key);
if (descriptor != null) {
Object previous = null;
if (descriptor.getReadMethod() != null) {
previous = readProperty(descriptor);
}
writeProperty(descriptor, value);
return (previous);
}
}
// Case 3 -- store or replace value in our underlying map
return (super.put(key, value));
}
public Object get(Object key) {
// Case 1 -- no local properties
if (descriptors == null) {
return (super.get(key));
}
// Case 2 -- this is a local property
if (key != null) {
PropertyDescriptor descriptor =
(PropertyDescriptor) descriptors.get(key);
if (descriptor != null) {
if (descriptor.getReadMethod() != null) {
return (readProperty(descriptor));
} else {
return (null);
}
}
}
// Case 3 -- retrieve value from our underlying Map
return (super.get(key));
}
}


2.Command:职责的最小单元。

public interface Command {
....
boolean execute(Context context) throws Exception;
....
}

return false 会继续执行chain中后续的command,return true就不会了。
3.Chain
chain of command
实现源码

public class ChainBase implements Chain {
public void addCommand(Command command) {
if (command == null) {
throw new IllegalArgumentException();
}
if (frozen) {//execute method will freeze the configuration of the command list
throw new IllegalStateException();
}
Command[] results = new Command[commands.length + 1];
System.arraycopy(commands, 0, results, 0, commands.length);
results[commands.length] = command;
commands = results;
}
public boolean execute(Context context) throws Exception {
// Verify our parameters
if (context == null) {
throw new IllegalArgumentException();
}
// Freeze the configuration of the command list
frozen = true;
// Execute the commands in this list until one returns true
// or throws an exception
boolean saveResult = false;
Exception saveException = null;
int i = 0;
int n = commands.length;
for (i = 0; i < n; i++) {
try {
saveResult = commands.execute(context);
if (saveResult) {//false继续执行chain下面的command,true 就直接返回不再执行了其他command
break;
}
} catch (Exception e) {
saveException = e;
break;
}
}
// Call postprocess methods on Filters in reverse order
if (i >= n) { // Fell off the end of the chain
i--;
}
boolean handled = false;
boolean result = false;
for (int j = i; j >= 0; j--) {
if (commands[j] instanceof Filter) {//filter定义了postprocess方法,用于自定义处理结束执行资源释放的操作
try {
result =
((Filter) commands[j]).postprocess(context,
saveException);
if (result) {
handled = true;
}
} catch (Exception e) {
// Silently ignore
}
}
}
// Return the exception or result state from the last execute()
if ((saveException != null) && !handled) {
throw saveException;
} else {
return (saveResult);
}
}
}


3.filter:是一种特殊的command,加入了postprocess 方法。在chain return之前会去执行postprocess。可以在该方法中释放资源。
4.catalog:command或chain的集合。通过配置文件类加载chain of command 或者command。通过catalog.getCommand(commandName)获取Command。

<catalog>
<chain name="LocaleChange">
<command
className="org.apache.commons.chain.mailreader.commands.ProfileCheck"/>
<command
className="org.apache.commons.chain.mailreader.commands.LocaleChange"/>
</chain>
<command
name="LogonUser"
className="org.apache.commons.chain.mailreader.commands.LogonUser"/>
</catalog>



boolean executeCatalogCommand(Context context,
String name, HttpServletRequest request)
throws Exception {
ServletContext servletContext =
request.getSession().getServletContext();  
Catalog catalog =
(Catalog) servletContext.getAttribute("catalog");
Command command = catalog.getCommand(name);
boolean stop = command.execute(context);
return stop;
}


在web环境使用还需要配置ChainListener

<!-- Commons Chain listener to load catalogs  -->
<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>

在ChainListener监听器init中完成加载catalog的工作,主要是使用Digester解析catalog.xml来得到catalog对象,并放入ServletContext 中去。
commons chain 还提供了ServletWebContext,从名字就可以知道,这是servlet环境下的context实现。另外还有PortletWebContext ,FacesWebContext。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-321382-1-1.html 上篇帖子: Apache 文件上传 下篇帖子: apache的jmeter
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表