UserManagementBean中有关 RoleBean 的部分(很简单)
//********************************************************************** // 多对多的单向关系 //********************************************************************** /** * 创建权限 * Business method * @throws CreateException * @ejb.interface-method view-type = "remote" */ public void createRole(String roleName, String description) throws CreateException { roleHome.create(roleName, description); } /** * 显示所有权限 * @throws FinderException * @ejb.interface-method view-type="remote" */ public ArrayList getRoles() throws FinderException { ArrayList roleList = new ArrayList(30); Iterator iter = roleHome.findAll().iterator(); while (iter.hasNext()) { RoleLocal role = (RoleLocal) iter.next(); roleList.add(role.getName()); } return roleList; } /** * 删除权限 * @throws RemoveException * @throws EJBException * @ejb.interface-method view-type="remote" */ public void removeRole(String name) throws EJBException, RemoveException { roleHome.remove(name); } /** * 给用户增加权限 * @throws FinderException * @ejb.interface-method view-type="remote" */ public void addRole(String email, String roleName) throws FinderException { UserLocal user = userHome.findByPrimaryKey(email); Collection roles = user.getRoles(); RoleLocal role = roleHome.findByPrimaryKey(roleName); roles.add(role); } /** * 得到某个用户拥有的权限 * @throws FinderException * @ejb.interface-method view-type="remote" */ public ArrayList getUserRole(String email) throws FinderException{ ArrayList roleList = new ArrayList(30); UserLocal user = userHome.findByPrimaryKey(email); Iterator iter = user.getRoles().iterator(); while (iter.hasNext()) { RoleLocal role = (RoleLocal) iter.next(); roleList.add(role.getName()); } return roleList; } 3. 好了,保存,Generate EJB Classes,启动MySql, 启动JBoss服务器, Deploy Module 。 如果一切正常,则会在 cmp_sample 库中自动建立两个表:roletable 和 userrole 。 role_fk 和 email_fk 是在 UserBean 中的 getRoles 的方法标记中定义的。 三、创建客户端 1. 右击 src 文件夹 ->新建 ->Lomboz EJB Test Client Wizard: 创建一个 CMPClient3.java 文件 (步骤同前两篇文章一样)。 2. 修改生成的 CMPClient3.java,调用UserManagementBean中的方法。 这里给出了完整的源代码:
CMPClient3.java
/* * 创建日期 2005-1-25 * * 作者: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 CMPClient3 { 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("显示所有权限"); printList(userMgmt.getRoles()); System.out.println("给用户[email protected]增加权限 "); userMgmt.addRole("[email protected]", "manager"); userMgmt.addRole("[email protected]", "user"); System.out.println("给用户javamxj增加权限 "); userMgmt.addRole("[email protected]", "admin"); userMgmt.addRole("[email protected]", "user"); System.out.println("显示javamxj拥有的权限"); printList(userMgmt.getUserRole("[email protected]")); } 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("向Role表中添加数据"); userMgmt.createRole("admin", "系统管理员"); userMgmt.createRole("manager", "内容管理员"); userMgmt.createRole("user", "普通用户"); userMgmt.createRole("guest", "来宾用户"); } // 输出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) { CMPClient3 test = new CMPClient3(); test.testBean(); } } 3. 运行客户端 ·控制台输出: ·数据库中相对应的表的改变:
本文地址:http://com.8s8s.com/it/it11603.htm