RoleBean.java
/* * 创建日期 2005-1-25 * * 作者:javamxj(分享java快乐) */ package javamxj.ejb.cmp; /** * * <!-- begin-user-doc --> You can insert your documentation for '<em><b>RoleBean</b></em>'. <!-- end-user-doc --> * <!-- begin-lomboz-definition --> <?xml version="1.0" encoding="UTF-8"?> <lomboz:EJB xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:lomboz="http://lomboz.objectlearn.com/xml/lomboz"> <lomboz:entity> <lomboz:entityEjb> <j2ee:display-name>Role</j2ee:display-name> <j2ee:ejb-name>Role</j2ee:ejb-name> <j2ee:ejb-class>javamxj.ejb.cmp.RoleBean</j2ee:ejb-class> <j2ee:persistence-type>Container</j2ee:persistence-type> <j2ee:prim-key-class>java.lang.String</j2ee:prim-key-class> <j2ee:cmp-version>2.x</j2ee:cmp-version> <j2ee:abstract-schema-name>RoleSchema</j2ee:abstract-schema-name> <j2ee:primkey-field>name</j2ee:primkey-field> </lomboz:entityEjb> <lomboz:fieldMappings> <lomboz:fieldName>name</lomboz:fieldName> <lomboz:fieldType>java.lang.String</lomboz:fieldType> <lomboz:columnName>权限名称</lomboz:columnName> <lomboz:jdbcType>VARCHAR</lomboz:jdbcType> <lomboz:sqlType>varchar</lomboz:sqlType> <lomboz:readOnly>false</lomboz:readOnly> <lomboz:primaryKey>true</lomboz:primaryKey> </lomboz:fieldMappings> <lomboz:fieldMappings> <lomboz:fieldName>description</lomboz:fieldName> <lomboz:fieldType>java.lang.String</lomboz:fieldType> <lomboz:columnName>权限描述</lomboz:columnName> <lomboz:jdbcType>VARCHAR</lomboz:jdbcType> <lomboz:sqlType>varchar</lomboz:sqlType> <lomboz:readOnly>false</lomboz:readOnly> <lomboz:primaryKey>false</lomboz:primaryKey> </lomboz:fieldMappings> <lomboz:tableName>RoleTable</lomboz:tableName> <lomboz:dataSourceName></lomboz:dataSourceName> </lomboz:entity> </lomboz:EJB> <!-- end-lomboz-definition --> * * <!-- begin-xdoclet-definition --> * @ejb.bean name="Role" * jndi-name="Role" * type="CMP" * primkey-field="name" * schema="RoleSchema" * cmp-version="2.x" * view-type = "local" * data-source="" * * @ejb.persistence * table-name="RoleTable" * * @jboss.persistence * create-table = "true" * remove-table = "false" * * @ejb.finder * query="SELECT OBJECT(a) FROM RoleSchema as a" * signature="java.util.Collection findAll()" * * @ejb.pk class="java.lang.String" * <!-- end-xdoclet-definition --> * @generated **/ public abstract class RoleBean implements javax.ejb.EntityBean { /** * @ejb.create-method */ public java.lang.String ejbCreate(String name, String description) throws javax.ejb.CreateException { // EJB 2.0 spec says return null for CMP ejbCreate methods. setName(name); setDescription(description); return null; } /** * The container invokes this method immediately after it calls ejbCreate. */ public void ejbPostCreate(String name, String description) throws javax.ejb.CreateException { } /** * CMP Field name * @return the name * @ejb.persistent-field * @ejb.persistence * column-name="权限名称" * jdbc-type="VARCHAR" * sql-type="varchar(12)" * read-only="false" * @ejb.pk-field * * @ejb.interface-method */ public abstract java.lang.String getName(); /** * @param java.lang.String the new name value * @ejb.interface-method */ public abstract void setName(java.lang.String name); /** * CMP Field description * @return the description * @ejb.persistent-field * @ejb.persistence * column-name="权限描述" * jdbc-type="VARCHAR" * sql-type="varchar(30)" * read-only="false" * * * @ejb.interface-method */ public abstract java.lang.String getDescription(); /** * @param java.lang.String the new description value * @ejb.interface-method */ public abstract void setDescription(java.lang.String description); }
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; } |
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]", color:
评论