Microsoft AD, JNDI and LDAP

Hello,

I working to move an application off of Novell eDirectory onto Microsoft AD. The program successfully binds to AD, the search filter executes correctly and the attributes are returned as expected. However, the final call to searchResults.hasMore() is not returning! In other words, the final call, which should return false, is not returning at all.

Any ideas?

-Bryan

try

{

hashtable = null;

hashtable = new Hashtable();

hashtable.put("java.naming.ldap.version", "3");

hashtable.put Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

hashtable.put(Context.SECURITY_AUTHENTICATION, "Simple");

hashtable.put(Context.REFERRAL, "follow");

hashtable.put(Context.PROVIDER_URL, url);

hashtable.put(Context.SECURITY_PRINCIPAL, loginDN);

hashtable.put(Context.SECURITY_CREDENTIALS, passwd);

ctx = new InitialLdapContext(hashtable, null);

SearchControls sc = new SearchControls();

sc.setSearchScope(SearchControls.SUBTREE_SCOPE);

sc.setReturningAttributes(new String[] {"mailNickname", "givenName", "sn", "mail"});

NamingEnumeration results = ctx.search(base, filter, sc);

cns = new ArrayList(25);

givenNames = new ArrayList(25);

mails = new ArrayList(25);

surNames = new ArrayList(25);

while (results.hasMore()) // hangup occurring here when hasMore() should return false

{

SearchResult sr = (SearchResult)results.next();

Attributes attrs = sr.getAttributes();

Enumeration enum = attrs.getAll();

while (enum.hasMoreElements())

System.out.println(enum.nextElement());

cns.add((String)(attrs.get("mailNickname").get()));

givenNames.add((String)(attrs.get("givenName").get()));

surNames.add((String)(attrs.get("sn").get()));

mails.add((String)(attrs.get("mail").get()));

}

}

finally

{

try {ctx.close();}

catch (Exception e) {}

}

[2001 byte] By [bjb1440a] at [2007-10-3 4:33:59]
# 1

I'll take a bit of a stab at this, although your code sample seems to have some typos, missing a few braces and few catches.

Anyway, there are some subtle differences between the behaviour of AD and other LDAP directories. One is that by default AD limits the number of objects that can be returned in a LDAP query to 1000. I think I described this in the post JNDI, Active Directory, Paging and Range Retrieval available at http://forum.java.sun.com/thread.jspa?threadID=578347&tstart=0

Also there are differences in the behaviour of Enumeration.hasMore & Enumeration.hasMoreElements, Enumeration.hasMore throws exception s and will not return false, whereas hasMoreElements does not throw an exception and will return false. Have a look at http://java.sun.com/j2se/1.3/docs/api/javax/naming/NamingEnumeration.html

So I'll assume that AD is causing an exception to be thrown, perhaps a SizeLimitExceededException or PartialResultsException.

Either process the exception, or use hasMoreElements instead.

adler_stevena at 2007-7-14 22:37:36 > top of Java-index,Core,Core APIs...
# 2

Hi,

The culprit was a PartialResultsException.

The server was returning a referral to another machine which did not exist. It's probably a misconfiguration. Since configuring AD is beyond the scope of my job the best I could do was notify those who administer AD and set my program to ignore all referrals.

And sorry about the typos.

And this is sort of a belated reponse.

-Bryan

bjb1440a at 2007-7-14 22:37:36 > top of Java-index,Core,Core APIs...