Previous | Next | Trail Map | Beyond the Basics | URLs

As Names Returned by Naming Enumerations

You can enumerate the contents of a context by using list()(in the API reference documentation) , listBindings()(in the API reference documentation) and overload forms of the search()(in the API reference documentation) method. When you invoke one of these methods, you get back a NamingEnumeration(in the API reference documentation). Each item of the enumeration is an instance of NameClassPair(in the API reference documentation) or one of its subclasses. To get the name of the item, that is, the name of the object relative to the target context--the context that you're listing or searching--you use NameClassPair.getName()(in the API reference documentation). The string name returned by this method is a composite name. For instance, you should be able to feed this name back into one of the Context(in the API reference documentation) methods of the target context.

However, sometimes it is not possible for the underlying service or service provider to return a name relative to the target context. This can happen, for instance, if the item was retrieved by following a referral or an alias. When a relative name cannot be returned, the service provider returns a URL string. You use this URL string by passing it to the InitialContext(in the API reference documentation) methods, as described in the previous section.

To determine whether the name returned by getName() is relative, you use NameClassPair.isRelative()(in the API reference documentation) .

Here is an example that searches a context for entries whose cn attribute starts with the letter S. It then retrieves the telephonenumber attribute of the item by using DirContext.getAttributes()(in the API reference documentation). This could have been done much easier by requesting the attributes by using the SearchControls(in the API reference documentation) argument. We separate the attribute retrieval just so that we could illustrate the use of isRelative(). When the example gets an item containing a URL string as a name (that is, isRelative() returns false), it uses the InitialContext to process the URL string.

// Set up environment for creating initial context
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://icdev:7489/o=JNDItutorial");

// Enable referrals so that we get some nonrelative names
env.put(Context.REFERRAL, "follow");

// Create the initial context
DirContext initCtx = new InitialDirContext(env);

// Get the target context
DirContext targetCtx = (DirContext)initCtx.lookup("ou=All");

SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

// Perform search on target context
NamingEnumeration enum = targetCtx.search("", "(cn=S*)", constraints);
Attributes attrs;
NameClassPair item;
String[] attrIds = new String[]{"telephonenumber"};
	    
// For each answer found, get its telephonenumber attribute
// If relative, resolve relative to target context
// If not relative, resolve relative to initial context
while (enum.hasMore()) {
    item = (NameClassPair)enum.next();
    System.out.println(">>>>>" + item.getName() + " ");
    if (item.isRelative()) {
        attrs = targetCtx.getAttributes(item.getName(), attrIds);
    } else {
        attrs = initCtx.getAttributes(item.getName(), attrIds);
    }
    System.out.println(attrs);
}
Here is the output from running this program:
>>>>>ldap://icdev:7389/cn=Scott Seligman, ou=People, o=JNDITutorial 
{telephonenumber=telephonenumber: +1 408 555 5252}
>>>>>ldap://icdev:7389/cn=Samuel Clemens, ou=People, o=JNDITutorial 
{telephonenumber=telephonenumber: +1 408 555 0186}
>>>>>ldap://icdev:7389/cn=Spuds Mackenzie, ou=People, o=JNDITutorial 
{telephonenumber=telephonenumber: +1 408 555 4420}
>>>>>ldap://icdev:7389/cn=S. User,ou=NewHires,o=JNDITutorial 
No attributes

Note: The 1.2 release of the LDAP provider returns the URL string with an extra set of quotes. This prevents this example from working. This bug has been fixed in 1.2.1 and later releases.

See also the Referrals (in the Tips for LDAP Users trail) lesson and Dereferencing Aliases (in the Tips for LDAP Users trail) section for more examples and descriptions.


Previous | Next | Trail Map | Beyond the Basics | URLs