Skip to content

Commit

Permalink
Polish LdapTemplate Logging
Browse files Browse the repository at this point in the history
Issue gh-345
  • Loading branch information
jzheaux committed Feb 2, 2024
1 parent 8adf061 commit 95da09c
Showing 1 changed file with 24 additions and 41 deletions.
65 changes: 24 additions & 41 deletions core/src/main/java/org/springframework/ldap/core/LdapTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,15 @@ public void search(String base, String filter, int searchScope, boolean returnin
@Override
public void search(final Name base, final String filter, final SearchControls controls,
NameClassPairCallbackHandler handler) {

SearchExecutor se = (ctx) -> {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
return ctx.search(base, filter, controls);
};
// Create a SearchExecutor to perform the search.
if (handler instanceof ContextMapperCallbackHandler) {
assureReturnObjFlagSet(controls);
}
search(searchExecutorWithLogging(base, filter, controls), handler);
search(se, handler);
}

/**
Expand All @@ -266,12 +269,15 @@ public void search(final Name base, final String filter, final SearchControls co
@Override
public void search(final String base, final String filter, final SearchControls controls,
NameClassPairCallbackHandler handler) {

SearchExecutor se = (ctx) -> {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
return ctx.search(base, filter, controls);
};
// Create a SearchExecutor to perform the search.
if (handler instanceof ContextMapperCallbackHandler) {
assureReturnObjFlagSet(controls);
}
search(searchExecutorWithLogging(base, filter, controls), handler);
search(se, handler);
}

/**
Expand All @@ -280,12 +286,15 @@ public void search(final String base, final String filter, final SearchControls
@Override
public void search(final Name base, final String filter, final SearchControls controls,
NameClassPairCallbackHandler handler, DirContextProcessor processor) {

SearchExecutor se = (ctx) -> {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
return ctx.search(base, filter, controls);
};
// Create a SearchExecutor to perform the search.
if (handler instanceof ContextMapperCallbackHandler) {
assureReturnObjFlagSet(controls);
}
search(searchExecutorWithLogging(base, filter, controls), handler, processor);
search(se, handler, processor);
}

/**
Expand All @@ -294,12 +303,15 @@ public void search(final Name base, final String filter, final SearchControls co
@Override
public void search(final String base, final String filter, final SearchControls controls,
NameClassPairCallbackHandler handler, DirContextProcessor processor) {

SearchExecutor se = (ctx) -> {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
return ctx.search(base, filter, controls);
};
// Create a SearchExecutor to perform the search.
if (handler instanceof ContextMapperCallbackHandler) {
assureReturnObjFlagSet(controls);
}
search(searchExecutorWithLogging(base, filter, controls), handler, processor);
search(se, handler, processor);
}

/**
Expand Down Expand Up @@ -1552,7 +1564,10 @@ <T> Stream<T> searchForStream(LdapQuery query, Function<SearchResult, T> mapper)

assureReturnObjFlagSet(searchControls);

NamingEnumeration<SearchResult> results = unchecked(searchSupplier(ctx, base, encodedFilter, searchControls));
NamingEnumeration<SearchResult> results = unchecked(() -> {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
return ctx.search(base, encodedFilter, searchControls);
});
if (results == null) {
return Stream.empty();
}
Expand Down Expand Up @@ -1781,38 +1796,6 @@ public <T> Stream<T> findForStream(LdapQuery query, Class<T> clazz) {
return searchForStream(builder.filter(includeClass), contextMapper);
}

private SearchExecutor searchExecutorWithLogging(final Name base, final String filter,
final SearchControls controls) {
return ctx -> {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
}
return ctx.search(base, filter, controls);
};
}

private SearchExecutor searchExecutorWithLogging(final String base, final String filter,
final SearchControls controls) {
return ctx -> {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
}
return ctx.search(base, filter, controls);
};
}

private CheckedSupplier<NamingEnumeration<SearchResult>> searchSupplier(final DirContext ctx,
final Name base,
final String filter,
final SearchControls controls) {
return () -> {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
}
return ctx.search(base, filter, controls);
};
}

private <T> T unchecked(CheckedSupplier<T> supplier) {
try {
return supplier.get();
Expand Down

0 comments on commit 95da09c

Please sign in to comment.