端到端J2ME应用开发实例——介绍Smart Ticket

来源: 作者: 2006-10-12 出处:pcdog.com

access  j2me  java  xml  解决方案  
上一页 1 2 3 4 5 6 下一页 

客户端界面

对于大多数应用程序动作,指向模型层的控制器条目是 ModelFacade 类。为符合 MVC 模式,ModelFacade 类包含一个响应模型层中每个事件的方法。

根据动作的本质,界面将它委托给以下的一个或多个模型类:

package com.sun.j2me.blueprints.smartticket.client.midp.model;
public class ModelFacade {
  private SynchronizationAgent syncAgent;
  private RemoteModelProxy remoteModel;
  private LocalModel localModel;
  // Action methods ...
  public Reservation reserveSeats(String theaterKey,
                         String movieKey,
                         int[] showTime, Seat[] seats)
                         throws ApplicationException {
    try {
      return remoteModel.reserveSeats(theaterKey, 
          movieKey, showTime, seats);
    } catch (ModelException me) {
      // ...
    } 
  } 
  public void purchaseTickets(Reservation reservation) 
                          throws ApplicationException {
    try {
      remoteModel.purchaseTickets(reservation.getId());
      localModel.addMovieRating(
        new MovieRating(remoteModel.getMovie(reservation.getMovieId()), 
                        reservation.getShowTime()));
    } catch (ModelException me) {
      // ...
    }
    return;
  }
  public void synchronizeMovieRatings(int 
     conflictResolutionStrategyId) 
         throws ApplicationException {
    try {
      syncAgent.synchronizeMovieRatings(conflictResolutionStrategyId);
      return;
    } catch (ModelException me) {
      // ...
    }
  }
  // ...
}
            

服务端界面

应用程序的服务端使用了很多 Enterprise JavaBeans 组件 (EJB) 来封装业务逻辑和管理与关系数据库的交互。当客户端的 RemoteModelProxy 向服务器端发出 RPC 调用时,HTTP servlet SmartTicketServlet 通过业务代理对象 SmartTicketBD 调用会话 EJB 中合适的动作方法 SmartTicketFacadeBean。根据请求性质,它进一步委托两个其他会话 bean 中的一个,TicketingBeanSynchronizingBean。一组实体 bean 在需要时使用 EJB 2.0 的容器托管的持久性来更新数据库。

package com.sun.j2me.blueprints.smartticket.server.web.midp;
public class SmartTicketServlet extends HttpServlet {
  public static final String SESSION_ATTRIBUTE_SMART_TICKET_BD =
    "com.sun.j2me.blueprints.smartticket.server.web.midp.SmartTicketBD";
  protected void doPost(HttpServletRequest request,
                        HttpServletResponse response)
                        throws ServletException, IOException {
    HttpSession session = request.getSession(true);
    SmartTicketBD smartTicketBD = (SmartTicketBD)
      session.getAttribute(SESSION_ATTRIBUTE_SMART_TICKET_BD);
    // Calls handleCall() method and encodes the URL for
    // session tracking
  }
  public int handleCall(SmartTicketBD smartTicketBD, 
     InputStream in, OutputStream out)
         throws IOException, ApplicationException {
    // Identifies the requested action method
    // Executes the method, as selected in a switch statement
    switch (method) {
    // cases ...
    case MessageConstants.OPERATION_GET_MOVIE: {
      getMovie(smartTicketBD, call, successfulResult);
      break;
    }
    // more cases ...
    }
  }
}
package com.sun.j2me.blueprints.smartticket.server.web.midp;
public class SmartTicketBD implements RemoteModel {
  public static final String EJB_REF_FACADE = 
      "ejb/SmartTicketFacade";
  private SmartTicketFacadeLocal facade;
  private ServletContext servletContext = null;
  public SmartTicketBD(ServletContext servletContext) 
                       throws ApplicationException {
    this.servletContext = servletContext;
    try {
      Context context = (Context)
        new InitialContext().lookup("java:comp/env");
      facade = ((SmartTicketFacadeLocalHome)
        context.lookup(EJB_REF_FACADE)).create();
      return;
    } catch (Exception e) {
      throw new ApplicationException(e);
    } 
  }
  public Movie getMovie(String movieKey) 
      throws ModelException, ApplicationException {
    try {
      MovieLocal movieLocal = facade.getMovie(movieKey);
      Movie movie = new Movie(movieLocal.getId(), 
                              movieLocal.getTitle(), 
                              movieLocal.getSummary(), 
                              movieLocal.getRating());
      return movie;
    } catch (SmartTicketFacadeException stfe) {
      throw new 
          ModelException(ModelException.CAUSE_MOVIE_NOT_FOUND);
    } catch (Exception e) {
      throw new ApplicationException(e);
    } 
  }
  // Other action methods in RemoteModel interface ...
}
package com.sun.j2me.blueprints.smartticket.server.ejb;
public class SmartTicketFacadeBean implements SessionBean {
  // ...
  public MovieLocal getMovie(String movieId) 
                    throws SmartTicketFacadeException {
    try {
      return movieHome.findByPrimaryKey(movieId);
    } catch (FinderException fe) {
      throw new 
          SmartTicketFacadeException("No matching movie.");
    } 
  }
  // ...
}
            

更多内容请看PCdog.com--J2ME安装配置  j2me开发  J2ME开发基础专题
上一页 1 2 3 4 5 6 下一页 
上一篇:如何组建J2EE开发团队
下一篇:minij2ee常见问题(FAQ)