O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Modifying Directory Entries

Modifying the attribute values of a directory entry involves using the modifyAttributes() method of DirContext. One variant of this method takes the name of a directory entry, a modification type, and an Attributes object that contains modified Attribute objects; another variant takes a name and an array of javax.naming.directory.ModificationItem objects. A ModificationItem encapsulates a modified Attribute object and a modification type.

The only part of this operation that warrants much explanation is the creation of modified Attribute objects. The javax.naming.directory.BasicAttributes and javax.naming.directory.BasicAttribute classes implement the Attributes and Attribute interfaces, respectively. These are the classes you’ll typically use to create modified attribute values.

For example, let’s say we want to remove the phone number "303 444 6633" from a user entry’s "telephonenumber" attribute and replace it with the new number "520 765 4321". In the following code, we create two BasicAttributes objects, newNumber and oldNumber, and use them in calls to modifyAttributes():

DirContext user ... ; // Created somewhere else in the program BasicAttribute newAttr = new BasicAttribute(); newAttr.add("telephonenumber", "520 765 4321"); BasicAttributes newNumber = new BasicAttributes(); newNumber.put(newAttr); BasicAttributes oldNumber = new BasicAttributes("telephonenumber", "303 444 6633"); user.modifyAttributes("", DirContext.REMOVE_ATTRIBUTE, oldNumber); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required