用Cactus来测试J2ee应用
2010-03-19 00:00:00 来源:WEB开发网这里有两个Entity Bean用来创建用户信息。他们之间的关系在xml部署描述文件中描述,他们是1对1的关系。
UserManagerLocal.java
package usersystem;
import javax.ejb.*;
import java.util.*;
public interface UserManagerLocal extends javax.ejb.EJBLocalObject {
public void addUser(String name, String password, String email, String address, String tel);
public Collection findAll() ;
public void delAll();
public void delByName(String name);
public User findByName(String name) ;
}
UserManagerBean.java
package usersystem;
import javax.ejb.*;
import javax.rmi.PortableRemoteObject;
import javax.naming.*;
import java.util.*;
public class UserManagerBean implements SessionBean {
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
/**
* 添加用户
* @param name 用户姓名
* @param password 密码
* @param email 电子邮件
* @param address 地址
* @param tel 电话
*/
public void addUser(String name, String password, String email, String address, String tel) {
try{
UserHome userHome=getUserHome();
User user=userHome.create(name,password) ; //create user entity
UserInfoHome userInfoHome=getUserInfoHome();
UserInfo userInfo=userInfoHome.create(name,email,address,tel) ;// create userinfo
entity
user.setUserInfo(userInfo) ;
}catch(Exception e){
throw new javax.ejb.EJBException (e.toString());
}
}
/**
* 返回UserHome接口
* @return userHome
*/
private UserHome getUserHome(){
try {
javax.naming.InitialContext ctx=new javax.naming.InitialContext ();
Object ref = ctx.lookup("User");
//cast to Home interface
UserHome userHome = (UserHome) PortableRemoteObject.narrow(ref, UserHome.class);
return userHome;
}
catch (ClassCastException ex) {
ex.printStackTrace() ;
return null;
}catch (NamingException ex) {
ex.printStackTrace() ;
return null;
}
}
/**
* 返回UserInfoHome接口
* @return
*/
private UserInfoHome getUserInfoHome(){
try {
javax.naming.InitialContext ctx=new javax.naming.InitialContext ();
Object ref = ctx.lookup("UserInfo");
//cast to Home interface
UserInfoHome userInfoHome = (UserInfoHome) PortableRemoteObject.narrow(ref,
UserInfoHome.class);
return userInfoHome;
}
catch (ClassCastException ex) {
throw new EJBException();
}catch (NamingException ex) {
throw new EJBException(ex.toString());
}
}
/**
* 返回所有用户记录
* @return c
* @throws javax.ejb.FinderException
*/
public java.util.Collection findAll() {
Collection c = null;
try {
UserHome uh=this.getUserHome() ;
c=uh.findAll() ;
}
catch (FinderException ex) {
throw new javax.ejb.EJBException ();
}
return c;
}
/**
* 删除所有记录
*/
public void delAll(){
try {
UserHome u=getUserHome();
java.util.Collection c=u.findAll() ;
java.util.Iterator i=c.iterator() ;
while(i.hasNext() ){
u.remove(((User)i.next()).getName()) ;
}
}
catch (Exception ex) {
throw new EJBException(ex.toString());
}
}
/**
* 根据用户名删除记录
* @param name
*/
public void delByName(String name) {
try {
User user=findByName(name);
UserHome uh=getUserHome();
uh.remove(user.getName()) ;
}
catch (Exception ex) {
throw new javax.ejb.EJBException (ex.toString());
}
}
/**
* 通过用户名查找用户记录
* @param name
* @return
*/
public User findByName(String name) {
try {
UserHome uh=this.getUserHome() ;
User user=(User)uh.findByPrimaryKey(name) ;
UserHome u=this.getUserHome() ;
User uu=u.findByPrimaryKey(name) ;
return user;
}
catch (FinderException ex) {
throw new EJBException(ex.toString());
}
}
}
更多精彩
赞助商链接