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); ...
Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.