由上面的代码看到,
EjbDelegate
只是命令的传递者而已,真是执行这个操作的地方是
SLSB
的
execute
方法。现在看看在
SLSB
的
execute
方法中到底做了些什么,如下面的代码所示:
public
Response execute(Request request)
throws
Exception {
<o:p></o:p>
logger
.debug(
"SessionFacade execute"
);
<o:p></o:p>
Response resp =
new
Response();
<o:p></o:p>
if
(request ==
null
) {
<o:p></o:p>
resp.setReturnCode(Response.
APPLICATION_LEVEL_ERROR
);
<o:p></o:p>
resp.getState().setErrCode(
"request is Null"
);
<o:p></o:p>
return
resp;
<o:p></o:p>
}
<o:p></o:p>
String serviceName = request.getServiceName();
<o:p></o:p>
if
(serviceName ==
null
) {
<o:p></o:p>
resp.setReturnCode(Response.
APPLICATION_LEVEL_ERROR
);
<o:p></o:p>
resp.getState().setErrCode(
"serviceName is Null"
);
<o:p></o:p>
return
resp;
<o:p></o:p>
}
<o:p></o:p>
BaseProcessor processor = (BaseProcessor) getBeanFactory().getBean(serviceName);
<o:p></o:p>
try
{
<o:p></o:p>
processor.doActivities(request, resp);
<o:p></o:p>
}
catch
(GoOnException e) {
<o:p></o:p>
e.printStackTrace();
<o:p></o:p>
}
catch
(Exception e) {
<o:p></o:p>
e.printStackTrace();
<o:p></o:p>
//
抛出特定的异常。
<o:p></o:p>
try
{
<o:p></o:p>
super
.getSessionContext().setRollbackOnly();
<o:p></o:p>
}
catch
(Exception e1) {
<o:p></o:p>
e1.printStackTrace();
<o:p></o:p>
throw
new
EJBException(
"
事务回滚错误
,
抛出
EJBException."
);
<o:p></o:p>
}
<o:p></o:p>
}
<o:p></o:p>
if
(resp.getState().isOK()) {
<o:p></o:p>
resp.setReturnCode(Response.
SUCCESS
);
<o:p></o:p>
}
<o:p></o:p>
return
resp;
<o:p></o:p>
}
|
SLSB
的
execute
方法
<o:p> </o:p>
可见
SLSB
也不是最终调用
Command
的地方,它只是获得了
Spring
的上下文,在
Spring
的上下文中查找名字叫
serviceName
的
Processor
,在
SLSB
中如何获得
Spring
的上下文在
Spring
对
EJB
的支持一节中再做介绍。
BaseProcessor
是
Processor
的基类,它实现了
InitializingBean, BeanNameAware, BeanFactoryAware, Processor
接口,在
Processor
接口中有个方法
doActivities
,在这里调用
Command
的
execute
方法。如果在调用
Processor
的过程中发生了异常,则要调用
SLSB
的
setRollbackOnly
方法进行回滚。
Command
实现了
Component
接口,
Component
代表了
Spring
管理的所有的组件。
Init
方法是在执行
execute
之前做一些相关的初始化工作,
fini
方法在
execute
方法执行后做相关的收尾工作。
public
interface
Component
extends
Serializable,BeanNameAware {
<o:p></o:p>
//
初始化接口
<o:p></o:p>
public
abstract
void
init(String parameter)
throws
Exception;
<o:p></o:p>
public
abstract
void
execute(Map params, Map response)
throws
Exception;
<o:p></o:p>
//
析构接口
<o:p></o:p>
public
abstract
void
fini()
throws
Exception;
<o:p></o:p>
public
ErrorHandler getErrorHandler();
<o:p></o:p>
public
String getBeanName();
<o:p></o:p>
}
|
Component
接口
<o:p> </o:p>
在
POJODelegate
中,由
BizDelegate
传过来的请求就在
POJODelegate
内完成。
public
Response execute(Request request)
throws
Exception {
<o:p></o:p>
logger
.debug(
"POJODelegate execute"
);
<o:p></o:p>
Response resp =
new
Response();
<o:p></o:p>
if
(request ==
null
) {
<o:p></o:p>
resp.setReturnCode(Response.
APPLICATION_LEVEL_ERROR
);
<o:p></o:p>
resp.getState().setErrCode(
"POJODelegate REQUEST IS NULL"
);
<o:p></o:p>
return
resp;
<o:p></o:p>
}
<o:p></o:p>
String serviceName = request.getServiceName();
<o:p></o:p>
if
(serviceName ==
null
) {
<o:p></o:p>
resp.setReturnCode(Response.
APPLICATION_LEVEL_ERROR
);
<o:p></o:p>
resp.getState().setErrCode(
"POJODelegate SERVICENAME IS NULL"
);
<o:p></o:p>
return
resp;
<o:p></o:p>
}
<o:p></o:p>
BaseProcessor processor = (BaseProcessor) ContextServiceLocator
<o:p></o:p>
.getInstance().getBean(serviceName);
<o:p></o:p>
processor.doActivities(request, resp);
<o:p></o:p>
if
(resp.getState().isOK()) {
<o:p></o:p>
resp.setReturnCode(Response.
SUCCESS
);
<o:p></o:p>
}
<o:p></o:p>
return
resp;
<o:p></o:p>
}
|
POJODelegate
的
execute
方法
POJODelegate
在
Spring
的上下文中得到相应的
Processor
,然后交给
Processor
去执行。采用
POJODelegate
的方式,在系统启动的时候,要对
Spring
做初始化设置,如在得到
Spring
的配置文件的地址中,可以用
ClassPathXmlApplicationContext
,
FileSystemXmlApplicationContext
等进行初始化。