技术开发 频道

软件体系架构模式在J2EE中的应用(下)

【IT168 技术文档】

3-1 PetStore概述
 
  PetStore是Sun公司在J2EE平台上开发的一个应用样例,是Java软件在J2EE的蓝图程序,它示范了如何利用J2EE1。3平台的性能去开发灵活、可升级的分布式平台企业应用系统。PetStore是一个运行在Web上的,为网络客户提供宠物信息浏览、网上订单和管理等功能的网上宠物店,在技术上使用了J2EE中的大部分企业组件和优秀的设计模式,提供了一套有高灵活性扩展性和可升级的完善J2EE开发框架。
 
PetStore共有4个子系统组成:
 
·petstore Web Site 该部分是petstore中的核心,客户登录系统进行选择,定购,提交定单等
·petstore admin 该部分是系统的管理功能
·Order processing center 定单调度中心,对客户提交的定单进行处理
·PetStore supplier 为产品供应者提供的维护画面
 
在这里主要以WebSite为主进行分析(以下petstore均指该子系统),它是怎样进行分层处理的。
 
3-2 Petstore体系架构
 
我们按照层的架构模式进行分析,主要从Tiers和layer角度进行考虑。
 
3-2-1 Layer层
 
3-2-1-1 概述
  PetStore是基于Web应用系统,它的客户端使用Broswer,然后是Web层的应用,业逻辑的层(由EJB实现),资源管理层(包括数据库,JMS,JavaMail等)。具体又细分为以下几层。
(PetStore分层图)
 
·客户请求浏览页面,一般Web层的View由JSP组成,并且使用了大量Taglib
·把每个请求映射到某个HTMLAction类,来响应它。HTML Action类是一个标准的类, 执行选择的HTML Action
·WCC 是由WebClientController完成(实现类为WebClientControllerImpl),它实际是SUN核心模式的Business Delegate层
·ECC 是EJBClientController,它实际是核心模式Session Facade
·EjbAction层执行EJB Action,传送event参数进来。EJB Action读event里面的参数,并且操纵EJB或者别的数据源来执行相应的商业操作。
·业务集成层,由通过DAO,EntityBean,JMS等访问相应的资源。
 
具体的详细的结构如下:
(PetStore详细结构)
 
3-2-1-2 实现
 
  在Web层它应用了著名的MVC模式,V由JSP来实现,为了业务逻辑和表示的分离,一般结合JSPTagLib。它把请求提交到相应的处理MainServelet,然后准发到RequestProcessor,他根据读取mappings.xml,的配置信息,生成相应的处理类Action如:
 
<url-mapping url="createuser.do" screen="create_customer.screen" isAction="true">
     <action-class>com.sun.j2ee.blueprints.petstore.controller.web.actions.CreateUserHTMLAction</action-class>
</url-mapping>
 
  每个Action一般包括以下几种方法(doStart, perform, doEnd),如果perform只需对请求的处理,如果处理的结果不需调用EJB的业务逻辑,只需返回即可,否则把请求组织成相应的Event,通过WCC转发到后台的业务逻辑层。代码见下:
 
public void processRequest(HttpServletRequest request) throws HTMLActionException, EventException, ServletException {
        Event ev = null;
        String fullURL = request.getRequestURI();
        // get the screen name
        String selectedURL = null;
        ServiceLocator sl = (ServiceLocator)request.getSession().getAttribute(WebKeys.SERVICE_LOCATOR);
        WebClientController wcc = sl.getWebClientController();
 
       HTMLAction action = getAction(selectedURL);
       if (action != null) {
           action.setServletContext(context);
           action.doStart(request);
           ev = action.perform(request);
           EventResponse eventResponse = null;
           if (ev != null) {
               eventResponse = wcc.handleEvent(ev);
           }
           action.doEnd(request, eventResponse);
        }
    }
 
通过WCC把Event转发到EC,这里WC是delegeteBussiness层:
 
public class WebClientControllerImpl implements WebClientController {
    public synchronized EventResponse handleEvent(Event ev)
        throws EventException {
            return ccEjb.processEvent(ev);
    }
}
EC是Fa?ade层,如下,
public class EJBClientControllerEJB implements SessionBean {
protected StateMachine sm;
    public EventResponse processEvent(Event ev)
        throws EventException {
          return (sm.processEvent(ev));
}
}
 
然后根据相应的Event的类型使用不同EJBAction来完成相应的业务逻辑:
 
   public EventResponse processEvent(Event ev) throws EventException {
        String eventName = ev.getEventName();
        String actionName = null;
        EventResponse response = null;
        if (eventName != null) {
            actionName = getActionName(eventName);
            EJBAction action = null;
            action = (EJBAction)Class.forName(actionName).newInstance();
            if (action != null) {
                action.init(this);
                // do the magic
                action.doStart();
                response = action.perform(ev);
                action.doEnd();
            }
        }
        return response;
    }
0
相关文章