您所在的位置: 首页>>读书频道>>设计开发>>Java系列>>

7.11.3 实现Session Bean和CMP的集成

http://book.51cto.com  2007-04-25 20:33  梁爱虎  电子工业出版社  我要评论(0)
  • 摘要:本节主讲在RAD中实现Session Bean和CMP的集成。上面完成了Session Bean和CMP的创建工作,这里将介绍如何在RAD中实现Session Bean和CMP的集成,它是通过Session Bean对CMP的调用来实现的。
  • 标签:CMP  集成  RAD  Session  Entity  Bean  Web Service

下面是本章例子在RAD中所实现的Session Bean和CMP的集成。为了让读者尽快掌握RAD的技术,本章例子所实现的业务逻辑和前几章是一样的,包括:

◆checkUserLogin:检查输入的用户名和密码是否正确。
◆saveUserInfo:存储用户信息。
◆getUserList:取得所有用户信息。

处理的异常主要包括以下几个方面。

(1)当通过JNDI找不到相应的Home接口时,程序会抛出相应的NamingException异常。
(2)当通过finder方法找不到相应的本地接口时(本质上是数据库表没有相应记录),程序会抛出相应的FinderException异常。
(3)当CMP Bean的create方法出现异常时,例如数据的类型不对,必添字段为空等,会导致相应数据不能插入到数据库表,程序会抛出CreateException异常。
(4)当业务逻辑出现错误时,如本方法输入的用户名和密码不匹配时,将会抛出ApplicationException异常。

下面进一步对代码UserAccountMgrBean.java进行介绍。

◆mySessionCtx:Session Bean的环境变量。
◆getSessionContext:取得Session Bean的环境变量。
◆setSessionContext:设置Session Bean的环境变量,以便EJB容器以后能找到。
◆ejbCreate:创建Session Bean实例的方法。
◆ejbActivate:调用Session Bean前,激活相应的资源。
◆ejbPassivate:调用Session Bean后,释放相应的资源。
◆ejbRemove:删除Session Bean实例,释放相应的资源。

package com.sample.ejb.sessionbean;
……
/**
* Bean implementation class for Enterprise Bean: UserAccountMgr
*/
public class UserAccountMgrBean implements javax.ejb.SessionBean {

/** Session Bean的环境变量 */
private javax.ejb.SessionContext mySessionCtx;
private Context context;

/**
* getSessionContext
*/
public javax.ejb.SessionContext getSessionContext() {
return mySessionCtx;
}
/**
* setSessionContext
*/
public void setSessionContext(javax.ejb.SessionContext ctx) {
mySessionCtx = ctx;
}
/**
* ejbCreate
*/
public void ejbCreate() throws javax.ejb.CreateException {
}
/**
* ejbActivate
*/
public void ejbActivate() {
}
/**
* ejbPassivate
*/
public void ejbPassivate() {
}
/**
* ejbRemove
*/
public void ejbRemove() {
}

下面是checkUserLogin的方法:检查输入的用户名和密码是否正确。

◆通过lookup方法找到User CMP的本地Home接口。
◆通过findByLoginName的方法找到相应的User CMP的本地接口的集合。
◆如果返回的集合数量为0,那么抛出没有此用户名的异常。
◆对集合作Iterator循环。
◆得到User CMP的本地接口。
◆如果User CMP的本地接口取得的密码等于用户输入的密码,返回真实true,否则,抛出用户名和密码不一致的异常。

CheckUserLogin的代码如下所示。

/**
* @throws NamingException
* @throws FinderException
* @throws RemoteException
* @ejb.interface-method
* view-type="remote"
**/
public boolean checkUserLogin(String loginName, String password)
throws ApplicationException,
NamingException, FinderException, RemoteException{

UserLocalHome userLocalHome =
(UserLocalHome) lookup("ejb/User");
Collection c = userLocalHome.findByLoginName(loginName);

if(c.size()==0)
throw new ApplicationException("noSuchLoginName");
Iterator it = c.iterator();
UserLocal user = (UserLocal) it.next();
if(user.getPassword().equals(password))
return true;
else
{
throw new ApplicationException("loginNameNotMatched");
}
}

下面是saveUserInfo的方法:存储用户信息。

◆通过lookup方法找到 SequenceNum CMP的本地Home接口。
◆通过findByPrimaryKey的方法找到SequenceNum CMP的本地接口。
◆通过SequenceNum CMP的本地接口,得到当前最大的account序列号,加1,得到下一个account的序列号作为account表的主键。
◆通过SequenceNum CMP的本地接口,得到当前最大的user序列号,加1,得到下一个user的序列号作为account表的主键。
◆更新SequenceNum CMP中的maxAccountID和maxUserID。
◆通过lookup方法得到User CMP的本地Home接口。
◆通过create方法将用户信息插入数据库表User。
◆通过lookup方法得到Account CMP的本地Home接口。
◆通过create方法将用户资金信息插入数据库表account。

SaveUserInfo的代码如下所示。

/**
* @throws NamingException
* @throws FinderException
* @throws CreateException
* @ejb.interface-method
* view-type="remote"
**/
public void saveUserInfo(UserAccountDTO userAccountDTO) throws
NamingException, FinderException, CreateException{
SequenceNumLocalHome seqNumLocalHome = (SequenceNumLocalHome)
lookup("ejb/SequenceNum");
SequenceNumLocal seqNumLocal = seqNumLocalHome.findByPrimaryKey
(new SequenceNumKey(new Integer(0)));
Integer accountID =
new Integer(seqNumLocal.getMaxAccountID().intValue()+1);
Integer userID =
new Integer(seqNumLocal.getMaxUserID().intValue()+1);
seqNumLocal.setMaxAccountID(accountID);
seqNumLocal.setMaxUserID(userID);
UserLocalHome userLocalHome = (UserLocalHome) lookup("ejb/User");
userLocalHome.create(userID,userAccountDTO);
AccountLocalHome accountLocalHome =
(AccountLocalHome)lookup("ejb/Account");
accountLocalHome.create(accountID,userID,
userAccountDTO.getRegistrationFee()); 
}

下面是getUserList的方法:取得所有用户信息。

◆创建存储用户信息的ArrayList。
◆通过lookup方法得到User CMP的本地Home接口。
◆通过lookup方法得到Account CMP的本地Home接口。
◆通过findAll方法找到所有User本地接口的集合。
◆取得上面集合的Iterator。
◆对User本地接口的Iterator作循环。
◆创建UserAccountDTO实例,以便存储用户信息。
◆得到User CMP的本地接口。
◆通过User CMP的本地接口得到其主键类。
◆通过findByUserID的方法得到Account接口的集合。
◆取得Account CMP的本地接口。
◆通过本地接口,取得Account CMP的主键类。
◆通过主键类,得到accountID。
◆得到Accout的本地接口,得到registrationFee。
◆通过本地接口,取得相应信息,存入UserAccountDTO类。
◆将UserAccountDTO类加入到本方法前面的ArrayList。
◆返回ArrayList。

getUserList的代码如下所示。

/**
* @throws NamingException
* @throws FinderException
* @throws RemoteException
* @ejb.interface-method
* view-type="remote"
**/
public List getUserList() throws NamingException, FinderException,
RemoteException{

ArrayList list = new ArrayList();
UserLocalHome userLocalHome =
(UserLocalHome) lookup("ejb/User");

AccountLocalHome accountLocalHome =
(AccountLocalHome)lookup("ejb/Account");
Collection c = userLocalHome.findAll();
Iterator it = c.iterator();
while(it.hasNext())
{
UserAccountDTO userAccountDTO = new UserAccountDTO();

UserLocal user = (UserLocal) it.next();

UserKey userKey = (UserKey) user.getPrimaryKey();
Collection c1 =
accountLocalHome.findByUserID(userKey.userID);

Iterator it1 = c1.iterator();
AccountLocal account = (AccountLocal) it1.next();
AccountKey accountKey =
(AccountKey) account.getPrimaryKey();
Integer accountID = accountKey.accountID;
BigDecimal registrationFee =
account.getRegistrationFee();
userAccountDTO.setUserAccountID(accountID);
userAccountDTO.setRegistrationFee(registrationFee);
userAccountDTO.setLoginName(user.getLoginName());
userAccountDTO.setName(user.getName());
userAccountDTO.setPassword(user.getPassword());
userAccountDTO.setEmail(user.getEmail());
list.add(userAccountDTO);
}
return list; 
}
……
}

【责任编辑:火凤凰 TEL:(010)68476606-8007】

回书目   上一节   下一节
NetBeans开发工具应用专题
深入Vista应用程序开发
Ruby on Rails 社区网站开发
ASP.NET从入门到精通
Java完全自学宝典
 
 验证码: (点击刷新验证码)   匿名发表
  • Visual C++ 完全自学宝典

  • 作者:强锋科技,朱洪波
  • Visual C++ 6.0是微软公司为程序人员提供的Visual Studio 6.0工具套件中的重要组成部分。本书由浅入深地介绍使用Visual C++ 6.0..
Copyright©2005-2008 51CTO.COM 版权所有