Java Reference
In-Depth Information
L ISTING 11.2
Continued
public Vector search(String[] returnAttributes, String searchFilter)
throws NamingException {
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;
}
/**
* Updates the given object in the LDAP server.
*/
Search WWH ::




Custom Search