修改属性
原文: https://docs.oracle.com/javase/tutorial/jndi/ops/modattrs.html
DirContext 接口包含用于修改目录中对象的属性和属性值的方法。
ModificationItem )。每个ModificationItem包含一个数字常量,表示要进行的修改类型, 属性 描述要进行的修改。以下是三种类型的修改:
修改按它们在列表中出现的顺序应用。要么执行所有修改,要么都不执行。
以下代码创建了一个修改列表。它将“mail”属性的值替换为值“geisel@wizards.com”,为“telephonenumber”属性添加一个附加值,并且删除“jpegphoto”属性。
// Specify the changes to makeModificationItem[] mods = new ModificationItem[3];// Replace the "mail" attribute with a new valuemods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("mail", "geisel@wizards.com"));// Add an additional value to "telephonenumber"mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,new BasicAttribute("telephonenumber", "+1 555 555 5555"));// Remove the "jpegphoto" attributemods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,new BasicAttribute("jpegphoto"));
Windows Active Directory: Active Directory 将“telephonenumber”定义为单值属性,与 RFC 2256 相反。要使此示例针对 Active Directory 工作,您必须使用“telephonenumber”以外的属性,或者将DirContext.ADD_ATTRIBUTE更改为DirContext.REPLACE_ATTRIBUTE 。
创建此修改列表后,您可以将其提供给 modifyAttributes() ,如下所示。
// Perform the requested modifications on the named objectctx.modifyAttributes(name, mods);
或者,您可以通过指定修改类型和要应用修改的属性来执行修改。
例如,以下行将与名称相关联的属性(在orig中标识)替换为orig中的属性:
ctx.modifyAttributes(name, DirContext.REPLACE_ATTRIBUTE, orig);
名称的任何其他属性保持不变。
在 the sample program中证明了modifyAttributes()的这两种用途。此程序通过使用修改列表修改属性,然后使用modifyAttributes()的第二种形式来恢复原始属性。
