Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close searchResults so LDAP context can close. #609

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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