Skip to content

Commit

Permalink
Close searchResults so LDAP context can close.
Browse files Browse the repository at this point in the history
Fixes #489
  • Loading branch information
perry2of5 committed Jan 4, 2022
1 parent 06d01e5 commit 366c7c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 366c7c9

Please sign in to comment.