Adding Directory Support
Attribute Updates
The DirContext interface contains the following methods (plus their java.lang.String overloads) for retrieving the attributes of an object in a directory:The form that accepts a modification operation (modOp) and an Attributes is typically used by an API user to specify the same operation to be applied to a bunch of attributes, such as adding several attributes. The API user can also use the form that accepts an array of ModificationItem for the same purpose, but the other form is more convenient. The form that accepts an array of ModificationItem is typically used by the API user to specify a series of different modifications to the same object.
- modifyAttributes(Name name, int modOp, Attributes attrs)
- modifyAttributes(Name name, ModificationItem[] mods)
A context implementation commonly implements the Attributes form by using the ModificationItem form. Here's how the example does this.
The implementation of both methods should guarantee that the series of modifications encapsulated by a single modifyAttributes() invocation is atomic. Whether atomicity is guaranteed is highly dependent on the underlying directory service, as is, in fact, the entire implementation of these methods. Other characteristics of the implementation include whether attribute names or identifiers are case-sensitive and whether the updates are sanctioned by any schema-checking features of the underlying directory service. Typically a context implementation does not need to take any special action to support or disallow these features. These are usually server-side features that are enforced when the modification requests reach the underlying service.public void modifyAttributes(Name name, int mod_op, Attributes attrs) throws NamingException { if (attrs == null || attrs.size() == 0) { throw new IllegalArgumentException( "Cannot modify without an attribute"); } // Turn it into a modification list and pass it on NamingEnumeration attrEnum = attrs.getAll(); ModificationItem[] mods = new ModificationItem[attrs.size()]; for (int i = 0; i < mods.length && attrEnum.hasMoreElements(); i++) { mods[i] = new ModificationItem(mod_op, (Attribute)attrEnum.next()); } modifyAttributes(name, mods); }
Adding Directory Support