Second, add business methods in UserManagementBean
1. Add a GroupLocalHome's variable GroupHome and put it in the EJBCREATE and EJBPassivate methods.
2. Add the following business methods in turn.
Part of GroupBean in UserManagementBean (very simple) // ************************************************************************************************************************************ *************************** One-to-many two-way relationship // ********* *********************************************************** ************ / ** * Create Group * @throws createException * @ Ejb.interface-method view-type = "remote" * / public void creategroup (String Groupname, String Description) Throws CreateException {groupHome.create (groupName, description);} / ** * delete group * @throws RemoveException * @throws EJBException * @ ejb.interface-method view-type = "remote" * / public void removeGroup (String groupName) throws EJBException, RemoveException {groupHome.remove (groupName);} / ** * show all groups * @throws FinderException * @ ejb.interface-method view-type = "remote" * / public ArrayList getGroups () throws FinderException {ArrayList Grouplist = new arraylist (30); iterator it = grouphome.findall (). iterator (); while (ore.hasnext ()) {GroupLocal Group = (GroupLocal) ney.next (); grou PList.add (Group.getName ());} Return GroupList;} / ** * Add users to group * @throws findexception * @ 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);} / ** * verify user's group * @throws FinderException * @ Ejb.interface-method view-type = "remote" * / public boolean INGROUP (String email, string groupname) throws findexception {userinfolocal user = infoHome.FindByPrimaryKey (email);
Return User.getGroup (). getname (); Equals (groupname);} / ** * Give a group of users Add permissions * @throws FinderException * @ ejb.interface-method view-type = "remote" * / public void addroleTouses (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);}} / ** * Output a group of users * @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 (); {userinfolocal element = (userinfolocal); userlist.add (Element.GeteMail ());} Return Userlis t;} / ** * output by the user within a division ejb.select * Business method * @throws FinderException * @ ejb.interface-method view-type = "remote" * / public ArrayList getUserIDsInGroup2 (String groupName) throws FinderException { Return GroupHome.getuserids (Groupname);} 3. Ok, save, Generate EJB Classes, start mysql, start JBoss server, deploy module.
If everything is normal, a table is automatically created in the CMP_SAMPLE library: GroupTable.
Third, create a client
1. Right-click the SRC folder -> New-> Lomboz EJB Test Client Wizard:
Create a cmpclient4.java file (the steps are the same as the first two articles).
2. Modify the generated cmpclient4.java, call the method in the userManagementBean.
Here is a complete source code:
CmpClient4.java
/ * * Create date 2005-1-26 * * Author: javamxj (share the joy of 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 To change this generated type annotation template, go to * window - Preference - Java - code style - Code Template * / 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 / authe ntication // 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 = gethHome (). Create (); createUsers (usermgmt); system.out.println ("Output Group Name in Group:"); PrintList (usermgmt.getgroups ()) System.out.Println ("Before the change, by the EJB.Finder Output Group for User"); PrintList (usermgmt.getuseridsingroup1 ("it"); system.out.println ("Change: Lisi @ BBB .CCC is added to IT group ");
Usermgmt.moveusertogroup ("Lisi@bb.ccc", "it"); system.out.println ("After the change, the user is used by EJB.SELECT Output Group"); PrintList (usermgmt.getuseridsingroup2 ("IT" ); System.out.println ("Add permissions through user group"); usermgmt.addroleTouses ("IT", "manager"); system.out.println ("Verify User Group"); if (usermgmt . InRoup ("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 (createExcection e) {E.PrintStackTrace ();} catch (NamingException e) {e.printStackTrace ();} catch (FinderException e) {e.printStackTrace ();}} // create a user public static void createUsers (UserManagement userMgmt) throws RemoteException, CreateException {System. Out.println (Add data to the Group "); Usermgmt.createGroup (" Marketing "," Market Group "); Usermgmt.createGroup (" Engineering "," Engineering Group "); US Ermgmt.createGroup ("Sales", "Sales Group"); Usermgmt.createGroup ("IT", "Information Technology Group");} // Output 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. Run client
· Console output:
· Changes in the database in the database: