/** * 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. * <p>By default, the {@link org.springframework.ldap.core.ContextSource#getReadWriteContext()} method * will be used to create the DirContext instance to operate on.</p> * * @param contextSource The target ContextSource to retrieve a DirContext from. * @param callback the callback to perform the Ldap operations. * @return the result returned from the callback. * @see #doWithSingleContext(org.springframework.ldap.core.ContextSource, LdapOperationsCallback, boolean, boolean, boolean) * @since 2.0 */ public static <T> T doWithSingleContext(ContextSource contextSource, LdapOperationsCallback<T> callback) { return doWithSingleContext(contextSource, callback, DONT_USE_READ_ONLY, DONT_IGNORE_PARTIAL_RESULT, DONT_IGNORE_NAME_NOT_FOUND); }
/** * 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. * <p>By default, the {@link org.springframework.ldap.core.ContextSource#getReadWriteContext()} method * will be used to create the DirContext instance to operate on.</p> * * @param contextSource The target ContextSource to retrieve a DirContext from. * @param callback the callback to perform the Ldap operations. * @return the result returned from the callback. * @see #doWithSingleContext(org.springframework.ldap.core.ContextSource, LdapOperationsCallback, boolean, boolean, boolean) * @since 2.0 */ public static <T> T doWithSingleContext(ContextSource contextSource, LdapOperationsCallback<T> callback) { return doWithSingleContext(contextSource, callback, DONT_USE_READ_ONLY, DONT_IGNORE_PARTIAL_RESULT, DONT_IGNORE_NAME_NOT_FOUND); }
/** * 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. * <p>By default, the {@link org.springframework.ldap.core.ContextSource#getReadWriteContext()} method * will be used to create the DirContext instance to operate on.</p> * * @param contextSource The target ContextSource to retrieve a DirContext from. * @param callback the callback to perform the Ldap operations. * @return the result returned from the callback. * @see #doWithSingleContext(org.springframework.ldap.core.ContextSource, LdapOperationsCallback, boolean, boolean, boolean) * @since 2.0 */ public static <T> T doWithSingleContext(ContextSource contextSource, LdapOperationsCallback<T> callback) { return doWithSingleContext(contextSource, callback, DONT_USE_READ_ONLY, DONT_IGNORE_PARTIAL_RESULT, DONT_IGNORE_NAME_NOT_FOUND); }
/** * 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. * <p>By default, the {@link org.springframework.ldap.core.ContextSource#getReadWriteContext()} method * will be used to create the DirContext instance to operate on.</p> * * @param contextSource The target ContextSource to retrieve a DirContext from. * @param callback the callback to perform the Ldap operations. * @return the result returned from the callback. * @see #doWithSingleContext(org.springframework.ldap.core.ContextSource, LdapOperationsCallback, boolean, boolean, boolean) * @since 2.0 */ public static <T> T doWithSingleContext(ContextSource contextSource, LdapOperationsCallback<T> callback) { return doWithSingleContext(contextSource, callback, DONT_USE_READ_ONLY, DONT_IGNORE_PARTIAL_RESULT, DONT_IGNORE_NAME_NOT_FOUND); }
@Test public void testDoWithSingleContext() { when(contextSourceMock.getReadWriteContext()).thenReturn(dirContextMock); verifyNoMoreInteractions(contextSourceMock); SingleContextSource.doWithSingleContext(contextSourceMock, new LdapOperationsCallback<Object>() { @Override public Object doWithLdapOperations(LdapOperations operations) { operations.executeReadOnly(new ContextExecutor<Object>() { @Override public Object executeWithContext(DirContext ctx) throws NamingException { Object targetContex = Whitebox.getInternalState(Proxy.getInvocationHandler(ctx), "target"); assertThat(targetContex).isSameAs(dirContextMock); return false; } }); // Second operation will have retrieved new DirContext from the SingleContextSource. // It should be the same instance. operations.executeReadOnly(new ContextExecutor<Object>() { @Override public Object executeWithContext(DirContext ctx) throws NamingException { Object targetContex = Whitebox.getInternalState(Proxy.getInvocationHandler(ctx), "target"); assertThat(targetContex).isSameAs(dirContextMock); return false; } }); return null; } }); }
SingleContextSource.doWithSingleContext(contextSource, new LdapOperationsCallback<Object>() { @Override public Object doWithLdapOperations(LdapOperations operations) {