Java Code Examples for org.springframework.ldap.core.ContextSource#getReadWriteContext()

The following examples show how to use org.springframework.ldap.core.ContextSource#getReadWriteContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SingleContextSource.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
/**
 * 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 <code>true</code>, use the {@link org.springframework.ldap.core.ContextSource#getReadOnlyContext()}
 *                    method on the target ContextSource to get the actual DirContext instance, if <code>false</code>,
 *                    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> T doWithSingleContext(ContextSource contextSource,
                                        LdapOperationsCallback<T> 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();
    }
}
 
Example 2
Source File: LdapTestUtils.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
 * Clear the directory sub-tree starting with the node represented by the
 * supplied distinguished name.
 *
 * @param contextSource the ContextSource to use for getting a DirContext.
 * @param name          the distinguished name of the root node.
 * @throws NamingException if anything goes wrong removing the sub-tree.
 */
public static void clearSubContexts(ContextSource contextSource, Name name) throws NamingException {
    DirContext ctx = null;
    try {
        ctx = contextSource.getReadWriteContext();
        clearSubContexts(ctx, name);
    } finally {
        try {
            ctx.close();
        } catch (Exception e) {
            // Never mind this
        }
    }
}
 
Example 3
Source File: LdapTestUtils.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
 * Load an Ldif file into an LDAP server.
 *
 * @param contextSource ContextSource to use for getting a DirContext to
 *                      interact with the LDAP server.
 * @param ldifFile      a Resource representing a valid LDIF file.
 * @throws IOException if the Resource cannot be read.
 */
public static void loadLdif(ContextSource contextSource, Resource ldifFile) throws IOException {
    DirContext context = contextSource.getReadWriteContext();
    try {
        loadLdif(context, ldifFile);
    } finally {
        try {
            context.close();
        } catch (Exception e) {
            // This is not the exception we are interested in.
        }
    }
}
 
Example 4
Source File: LdapTestUtils.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
 * Clear the directory sub-tree starting with the node represented by the
 * supplied distinguished name.
 *
 * @param contextSource the ContextSource to use for getting a DirContext.
 * @param name          the distinguished name of the root node.
 * @throws NamingException if anything goes wrong removing the sub-tree.
 */
public static void clearSubContexts(ContextSource contextSource, Name name) throws NamingException {
    DirContext ctx = null;
    try {
        ctx = contextSource.getReadWriteContext();
        clearSubContexts(ctx, name);
    } finally {
        try {
            ctx.close();
        } catch (Exception e) {
            // Never mind this
        }
    }
}
 
Example 5
Source File: LdapTestUtils.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
 * Load an Ldif file into an LDAP server.
 *
 * @param contextSource ContextSource to use for getting a DirContext to
 *                      interact with the LDAP server.
 * @param ldifFile      a Resource representing a valid LDIF file.
 * @throws IOException if the Resource cannot be read.
 */
public static void loadLdif(ContextSource contextSource, Resource ldifFile) throws IOException {
    DirContext context = contextSource.getReadWriteContext();
    try {
        loadLdif(context, ldifFile);
    } finally {
        try {
            context.close();
        } catch (Exception e) {
            // This is not the exception we are interested in.
        }
    }
}