Java Reference
In-Depth Information
public Vector search(String[] returnAttributes, String searchFilter)
throws NamingException {
11
Vector ldapObjects = new Vector();
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(returnAttributes);
ctls.setSearchScope(searchScope);
NamingEnumeration results = null;
// bind to the LDAP server.
DirContext ctx = getInitialContext(getEnvironment());
// perform the search on the LDAP server.
results = ctx.search(searchBase, searchFilter, ctls);
while ((results != null) && (results.hasMore())) {
// get the next set of results from the search.
SearchResult sr = (SearchResult)results.next();
// get all of the attributes for this object.
Attributes attributes = sr.getAttributes();
NamingEnumeration attEnum = attributes.getAll();
// create an LDAPObject to hold the results.
LDAPObject tmpObj = new LDAPObject();
// set the Distinguished Name for the object. Append
// the relative dn and the searcb base together.
tmpObj.setDn(sr.getName() + “,” + searchBase);
// iterate through the attributes setting them in the LDAPObject
while (attEnum.hasMore()) {
// get the next attribute
Attribute att = (Attribute)attEnum.next();
// set the attribute in the LDAPObject
tmpObj.setAttribute(att.getID(), att.get());
}
// add the object to the list of return objects
ldapObjects.add(tmpObj);
}
return ldapObjects;
}
You have included the search base as an attribute to the class instead of as a parameter for the
search method. This is because it is common to set the search base and perform several
searches from there. This eliminates the need to set the search base each time you search.
 
Search WWH ::




Custom Search