
Directory Services
• We’ve seen the essentials of naming services we need to cover the essentials of Directory Services as
well.
• Directory services support attributes and lookups based on attributes.
- Classes and Interfaces dealing with directory services are located in the javax.naming.directory package.
- Directories allow content-based lookup, like return all printer objects that support Win2000.
• To work with a directory service you will obtain a reference to a directory context.
javax.naming.directory.DirContext
• When a resource is bound to a directory context, it is also given attributes.
- You can add attributes to an attributes object (java.naming.directory.Attribute) similar to a collection
using the put method.
Attributes attributes = new BasicAttributes(true); //true == ignore case
attributes.put(“color”, “red”);
attributes.put(“leather”, “yes”);
- One key can have multiple values by using the BasicAttribute class.
BasicAttribute att = new BasicAttribute(“style”);
att.add(“Sedan”);
att.add(“Coupe”);
attributes.add(att);
• Binding an object to a context uses the attribute, the name, and the object being bound.
someContext.bind(“name.of.comp”, obj, attributes);
• To search a directory context use the Attributes object again.
Attributes where = new BasicAttributes(true);
where.put(attribute1);
where.put(attribute2);
//javax.naming.
NamingEnumeration nameEnum = someDirContext.search(“subContext”, where);
• The javax.naming.NamingEnumeration class is a familiar iterator type that defines very few methods.
Method Summary
void
close()
Closes this enumeration.
boolean
hasMore()
Determines whether there are any more elements in the enumeration.
Object
next()
Retrieves the next element in the enumeration.
• Sifting through a NamingEnumeration takes a quick while loop.
while(nameEnum.hasMore()){
SearchResult sr = (SearchResult)nameEnum.next();
}
- The NamingEnumeration is a set of SearchResults.
- The SearchResult class is a defined in javax.naming.directory.
- SearchResult is a direct descendant of Binding.
- You can get and set attributes of a SearchResult.
Directory Services
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials