int totalResults = 0;
try {
ctx = getConnection();
if (ctx == null) {
log.info("Failed to get a directory context object");
throw new LoginException("Failed to get a directory context object");
}
searchCtls.setReturningAttributes(returnedAtts);
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Search for objects using the filter
NamingEnumeration<SearchResult> answer = ctx.search(searchBase, searchFilter, searchCtls);
// Loop through the search results
while (answer.hasMoreElements()) {
SearchResult sr = answer.next();
totalResults++;
Attributes attrs = sr.getAttributes();
if (attrs != null) {
try {
byte[] userSid = (byte[]) attrs.get("objectSid;binary").get();
Integer primaryGroupId = new Integer((String) attrs.get("primaryGroupID").get());
byte[] groupRid = integerToFourBytes(primaryGroupId);
byte[] groupSid = userSid.clone();
// Replace the last four bytes to construct
// groupSid
for (int i = 0; i < 4; ++i) {
groupSid[groupSid.length - 1 - i] = groupRid[i];
}
groupSearchFilter = "(&(objectSid=" + binaryToStringSID(groupSid) + "))";
Attribute answer1 = attrs.get("memberOf");
for (int i = 0; i < answer1.size(); i++) {
String str = answer1.get(i).toString();
String str1[] = str.split("CN=");
allPrincipals.add(new GeronimoGroupPrincipal(str1[1].substring(0, str1[1].indexOf(","))));
}
} catch (NullPointerException e) {
throw new LoginException("Errors listing attributes: " + e);
}
}
}
// Search for objects using the group search filter
NamingEnumeration<SearchResult> answer2 = ctx.search(searchBase, groupSearchFilter, searchCtls);
// Loop through the search results
while (answer2.hasMoreElements()) {
SearchResult sr = answer2.next();
String str1[] = sr.getName().split("CN=");
allPrincipals.add(new GeronimoGroupPrincipal(str1[1].substring(0, str1[1].indexOf(","))));
}
} catch (NamingException e) {
throw (LoginException) new LoginException().initCause(e);
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {