[ Pobierz całość w formacie PDF ]
.You also will use the javax.naming.directory.Attributes[5]class to hold the various attributes.In other words, an Attributes instance holdsAttribute instances, which as a group are supplied when creating a new entry in thedirectory server.To create these, you will need to instantiate implementations of the twointerfaces; the BasicAttributes and BasicAttribute classes (also in thejavax.naming.directory package) fit the bill perfectly.So you will need to create a new BasicAttribute for all of the attributes used in the newobject class.This includes the cn, sn, givenName, userPassword, and uid attributes.Theother attribute you will need to worry about is the objectClass attribute.It specifies theobject class hierarchy that the new object will have; we discussed directory hierarchies andobject class hierarchies in Chapter 3.Creating this attribute and adding the object classes tothe hierarchy also reveals something important about the Attribute class: it can havemultiple values.This will also be important when looking at adding users to groups, whichinvolves assigning multiple values (user DNs) to a group's uniqueMember attribute.Once allof the individual Attribute objects are created, they must be assigned to the Attributesobject.Finally, this container is passed on to the createSubcontext( ) method, and theresult is a new entry in the LDAP tree.You should also note that the addUser( ) method, aswell as almost all of the methods in the LDAPManager class, throws a NamingException.Thisexception can occur when connections have failed, and also when an object already existswith the supplied DN.Later, you'll code business objects that create users, and handle theseerrors and report problems back to the user in a more meaningful format.For now, just throwthe error back to the client component.Add this method to the LDAPManager source file, andthe manager will be equipped to add new users to the directory:public void addUser(String username, String firstName,String lastName, String password)throws NamingException {// Create a container set of attributesAttributes container = new BasicAttributes( );5Actually, both the Attribute and Attributes classes are interfaces, but you will see that this is not a problem, as the manager code will useimplementations of these as needed.104 Building Java"! Enterprise Applications Volume I: Architecture// Create the objectclass to addAttribute objClasses = new BasicAttribute("objectClass");objClasses.add("top");objClasses.add("person");objClasses.add("organizationalPerson");objClasses.add("inetOrgPerson");// Assign the username, first name, and last nameString cnValue = new StringBuffer(firstName).append(" ").append(lastName).toString( );Attribute cn = new BasicAttribute("cn", cnValue);Attribute givenName = new BasicAttribute("givenName", firstName);Attribute sn = new BasicAttribute("sn", lastName);Attribute uid = new BasicAttribute("uid", username);// Add passwordAttribute userPassword =new BasicAttribute("userpassword", password);// Add these to the containercontainer.put(objClasses);container.put(cn);container.put(sn);container.put(givenName);container.put(uid);container.put(userPassword);// Create the entrycontext.createSubcontext(getUserDN(username), container);}Deleting users, or any type of subcontext, is a much simpler task.All you need to do isidentify the name that the subcontext is bound to (in this case, the user's DN), and invoke thedestroySubcontext( ) method on the manager's DirContext object.Additionally, whilethe method still throws a NamingException, it should trap one specific problem, theNameNotFoundException.This exception is thrown when the requested subcontext does notexist within the directory; however, because ensuring that the DN for the user specifieddoesn't exist is the point of the deleteUser( ) method, this problem is ignored.Whether thespecified user is deleted, or did not exist prior to the method call, is irrelevant to the client.Add the deleteUser( ) method shown here to your source code:public void deleteUser(String username) throws NamingException {try {context.destroySubcontext(getUserDN(username));} catch (NameNotFoundException e) {// If the user is not found, ignore the error}}Any other exceptions that might result, such as connection failures, are still reported throughthe NamingException that can be thrown in the method.With these two methods in place, all user manipulation can be handled [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • szamanka888.keep.pl