二、在UserManagementBean中添加业务方法
1. 增加一个 GroupLocalHome 的变量 groupHome,并将它放入 ejbCreate 和 ejbPassivate 方法中。
2. 依次增加以下几个业务方法。
UserManagementBean中有关 GroupBean 的部分(很简单)
//**********************************************************************
// 一对多的双向关系
//**********************************************************************
/**
* 创建组别
* @throws CreateException
* @ejb.interface-method view-type="remote"
*/
public void createGroup(String groupName, String description)
throws CreateException {
groupHome.create(groupName, description);
}
/**
* 删除组别
* @throws RemoveException
* @throws EJBException
* @ejb.interface-method view-type="remote"
*/
public void removeGroup(String groupName)
throws EJBException,
RemoveException {
groupHome.remove(groupName);
}
/**
* 显示所有组别
* @throws FinderException
* @ejb.interface-method view-type="remote"
*/
public ArrayList getGroups() throws FinderException {
ArrayList groupList = new ArrayList(30);
Iterator iter = groupHome.findAll().iterator();
while (iter.hasNext()) {
GroupLocal group = (GroupLocal) iter.next();
groupList.add(group.getName());
}
return groupList;
}
/**
* 将用户添加到组
* @throws FinderException
* @ejb.interface-method view-type="remote"
*/
public void moveUserToGroup(String email, String groupName)
throws FinderException {
UserInfoLocal user = infoHome.findByPrimaryKey(email);
GroupLocal group = groupHome.findByPrimaryKey(groupName);
user.setGroup(group);
}
/**
* 验证用户所在组
* @throws FinderException
* @ejb.interface-method view-type="remote"
*/
public boolean inGroup(String email, String groupName)
throws FinderException {
UserInfoLocal user = infoHome.findByPrimaryKey(email);
return user.getGroup().getName().equals(groupName);
}
/**
* 给一组用户增加权限
* @throws FinderException
* @ejb.interface-method view-type="remote"
*/
public void addRoleToUsers(String groupName, String roleName)
throws FinderException {
GroupLocal group = groupHome.findByPrimaryKey(groupName);
RoleLocal role = roleHome.findByPrimaryKey(roleName);
Iterator iter = group.getUsers().iterator();
while (iter.hasNext()) {
UserInfoLocal user = (UserInfoLocal) iter.next();
user.getUser().getRoles().add(role);
}
}
/**
* 通过 ejb.finder 输出某组别的用户
* @throws FinderException
* @ejb.interface-method view-type="remote"
*/
public ArrayList getUserIDsInGroup1(String groupName)
throws FinderException {
ArrayList userList = new ArrayList(30);
GroupLocal group = groupHome.findByPrimaryKey(groupName);
Iterator iter = group.getUsers().iterator();
while (iter.hasNext()) {
UserInfoLocal element = (UserInfoLocal) iter.next();
userList.add(element.getEmail());
}
return userList;
}
/**
* 通过 ejb.select 输出某组别的用户
* Business method
* @throws FinderException
* @ejb.interface-method view-type = "remote"
*/
public ArrayList getUserIDsInGroup2(String groupName)
throws FinderException {
return groupHome.getUserIDs(groupName);
}
3. 好了,保存,Generate EJB Classes,启动MySql, 启动JBoss服务器, Deploy Module 。
如果一切正常,则会在 cmp_sample 库中自动建立一个表:grouptable。
三、创建客户端
1. 右击 src 文件夹 ->新建 ->Lomboz EJB Test Client Wizard:
创建一个 CMPClient4.java 文件 (步骤同前两篇文章一样)。
2. 修改生成的 CMPClient4.java,调用UserManagementBean中的方法。
这里给出了完整的源代码:
CMPClient4.java
/*
* 创建日期 2005-1-26
*
* 作者:javamxj(分享java快乐)
*/
package javamxj.ejb.client;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import javamxj.ejb.cmp.UserManagement;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* @author pc
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class CMPClient4 {
private javamxj.ejb.cmp.UserManagementHome getHome() throws NamingException {
return (javamxj.ejb.cmp.UserManagementHome) getContext().lookup(
javamxj.ejb.cmp.UserManagementHome.JNDI_NAME);
}
private InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable();
props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
// This establishes the security for authorization/authentication
// props.put(InitialContext.SECURITY_PRINCIPAL,"username");
// props.put(InitialContext.SECURITY_CREDENTIALS,"password");
InitialContext initialContext = new InitialContext(props);
return initialContext;
}
public void testBean() {
try {
javamxj.ejb.cmp.UserManagement userMgmt = getHome().create();
createUsers(userMgmt);
System.out.println("输出group表中的组名:");
printList(userMgmt.getGroups());
System.out.println("改动前,通过 ejb.finder 输出组别为IT的用户");
printList(userMgmt.getUserIDsInGroup1("IT"));
System.out.println("改动:将lisi@bbb.ccc添加到IT组");
userMgmt.moveUserToGroup("lisi@bbb.ccc", "IT");
System.out.println("改动后,通过 ejb.select 输出组别为IT的用户");
printList(userMgmt.getUserIDsInGroup2("IT"));
System.out.println("通过用户组给用户增加权限");
userMgmt.addRoleToUsers("IT", "manager");
System.out.println("验证用户所在组");
if (userMgmt.inGroup("javamxj@yahoo.com.cn", "IT")) {
System.out.println("javamxj is in the IT group");
} else if (userMgmt.inGroup("javamxj@yahoo.com.cn", "marketing")) {
System.out.println("javamxj is now in the marketing group");
}
} catch (RemoteException e) {
e.printStackTrace();
} catch (CreateException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} catch (FinderException e) {
e.printStackTrace();
}
}
// 创建用户
public static void createUsers(UserManagement userMgmt)
throws RemoteException, CreateException {
System.out.println("向Group表中添加数据");
userMgmt.createGroup("Marketing", "市场组");
userMgmt.createGroup("Engineering", "工程组");
userMgmt.createGroup("Sales", "销售组");
userMgmt.createGroup("IT", "信息科技组");
}
// 输出ArrayList
private static void printList(ArrayList list) {
Iterator i = list.iterator();
while (i.hasNext()) {
Object details = (Object) i.next();
System.out.println(details.toString());
}
System.out.println("");
}
public static void main(String[] args) {
CMPClient4 test = new CMPClient4();
test.testBean();
}
}
3. 运行客户端
·控制台输出:
·数据库中相对应的表的改变: