Skip to content

Commit

Permalink
Merge pull request #609 from perry2of5/main
Browse files Browse the repository at this point in the history
Close searchResults so LDAP context can close.
  • Loading branch information
jzheaux authored Jan 18, 2022

Verified

This commit was signed with the committer’s verified signature.
crazy-max CrazyMax
2 parents cb1bdbe + 366c7c9 commit b74bb96
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
import org.springframework.util.Assert;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
@@ -165,9 +166,10 @@ public void setSearchControls(SearchControls searchControls) {
public boolean validateDirContext(DirContextType contextType, DirContext dirContext) {
Assert.notNull(contextType, "contextType may not be null");
Assert.notNull(dirContext, "dirContext may not be null");


NamingEnumeration<SearchResult> searchResults = null;
try {
final NamingEnumeration<SearchResult> searchResults = dirContext.search(this.base, this.filter, this.searchControls);
searchResults = dirContext.search(this.base, this.filter, this.searchControls);

if (searchResults.hasMore()) {
this.logger.debug("DirContext '{}' passed validation.", dirContext);
@@ -179,6 +181,14 @@ public boolean validateDirContext(DirContextType contextType, DirContext dirCont
this.logger.debug("DirContext '{}' failed validation with an exception.", dirContext, e);
return false;
}
finally {
if (searchResults != null) {
try {
searchResults.close();
} catch (NamingException namingException) {
}
}
}

this.logger.debug("DirContext '{}' failed validation.", dirContext);
return false;
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
import org.springframework.util.Assert;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
@@ -166,19 +167,28 @@ public boolean validateDirContext(DirContextType contextType, DirContext dirCont
Assert.notNull(contextType, "contextType may not be null");
Assert.notNull(dirContext, "dirContext may not be null");

NamingEnumeration<SearchResult> searchResults = null;
try {
final NamingEnumeration<SearchResult> searchResults = dirContext.search(this.base, this.filter, this.searchControls);
searchResults = dirContext.search(this.base, this.filter, this.searchControls);

if (searchResults.hasMore()) {
this.logger.debug("DirContext '{}' passed validation.", dirContext);

return true;
}
}
catch (Exception e) {
this.logger.debug("DirContext '{}' failed validation with an exception.", dirContext, e);
return false;
}
finally {
if (searchResults != null) {
try {
searchResults.close();
} catch (NamingException namingException) {
}
}
}

this.logger.debug("DirContext '{}' failed validation.", dirContext);
return false;

0 comments on commit b74bb96

Please sign in to comment.