![]() ![]() ![]() ![]() |
URLs |
You can enumerate the contents of a context by using list(), listBindings()
and overload forms of the search()
method. When you invoke one of these methods, you get back a NamingEnumeration
. Each item of the enumeration is an instance of NameClassPair
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()
. 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
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
methods, as described in the previous section.
To determine whether the name returned by getName() is relative, you use NameClassPair.isRelative()
.
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()
. This could have been done much easier by requesting the attributes by using the SearchControls
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.
Here is the output from running this program:// 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); }>>>>>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
lesson and Dereferencing Aliases
section for more examples and descriptions.
![]() ![]() ![]() ![]() |
URLs |