import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class SearchScopeExample {
public static void main(String[] argc) {
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample");
try {
DirContext dctx = new InitialDirContext(env);
String filter = "(&(cn=S*)(account>1000))";
String[] attrIDs = { "cn", "email" };
SearchControls sc = new SearchControls();
sc.setReturningAttributes(attrIDs);
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration result = dctx.search("ou=People", filter, sc);
while (result.hasMore()) {
SearchResult sr = (SearchResult) result.next();
System.out.println("Result = " + sr.getName());
}
} catch (NamingException e) {
System.out.println(e);
}
}
}
|