import org.springframework.ldap.core.ContextSource; //導入方法依賴的package包/類
/**
* Construct a SingleContextSource and execute the LdapOperationsCallback using the created instance.
* This makes sure the same connection will be used for all operations inside the LdapOperationsCallback,
* which is particularly useful when working with e.g. Paged Results as these typically require the exact
* same connection to be used for all requests involving the same cookie..
* The SingleContextSource instance will be properly disposed of once the operation has been completed.
*
* @param contextSource The target ContextSource to retrieve a DirContext from
* @param callback the callback to perform the Ldap operations
* @param useReadOnly if true
, use the {@link org.springframework.ldap.core.ContextSource#getReadOnlyContext()}
* method on the target ContextSource to get the actual DirContext instance, if false
,
* use {@link org.springframework.ldap.core.ContextSource#getReadWriteContext()}.
* @param ignorePartialResultException Used for populating this property on the created LdapTemplate instance.
* @param ignoreNameNotFoundException Used for populating this property on the created LdapTemplate instance.
* @return the result returned from the callback.
* @since 2.0
*/
public static T doWithSingleContext(ContextSource contextSource,
LdapOperationsCallback callback,
boolean useReadOnly,
boolean ignorePartialResultException,
boolean ignoreNameNotFoundException) {
SingleContextSource singleContextSource;
if (useReadOnly) {
singleContextSource = new SingleContextSource(contextSource.getReadOnlyContext());
} else {
singleContextSource = new SingleContextSource(contextSource.getReadWriteContext());
}
LdapTemplate ldapTemplate = new LdapTemplate(singleContextSource);
ldapTemplate.setIgnorePartialResultException(ignorePartialResultException);
ldapTemplate.setIgnoreNameNotFoundException(ignoreNameNotFoundException);
try {
return callback.doWithLdapOperations(ldapTemplate);
} finally {
singleContextSource.destroy();
}
}