mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Gaetan Boismal
16.17.2015 9d6ecd358f1b1c2d32b3a209d0fabe8bb6d4b4a7
opendj-config/src/main/java/org/forgerock/opendj/config/client/ldap/LDAPManagementContext.java
@@ -27,16 +27,39 @@
package org.forgerock.opendj.config.client.ldap;
import static org.forgerock.opendj.ldap.Connections.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.config.LDAPProfile;
import org.forgerock.opendj.config.client.ManagementContext;
import org.forgerock.opendj.config.client.spi.Driver;
import org.forgerock.opendj.ldap.AbstractConnectionWrapper;
import org.forgerock.opendj.ldap.Connection;
import org.forgerock.opendj.ldap.Entry;
import org.forgerock.opendj.ldap.MemoryBackend;
import org.forgerock.opendj.ldap.requests.UnbindRequest;
import org.forgerock.opendj.ldif.LDIF;
import org.forgerock.opendj.ldif.LDIFEntryReader;
import org.forgerock.opendj.ldif.LDIFEntryWriter;
import org.forgerock.util.Reject;
/**
 * An LDAP management connection context.
 */
public final class LDAPManagementContext extends ManagementContext {
    private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
    /**
     * Create a new LDAP management context using the provided LDAP connection.
     *
@@ -54,6 +77,78 @@
        return context;
    }
    /**
     * Returns a LDIF management context on the provided LDIF file.
     *
     * @param ldifFile
     *            The LDIF file to manage
     * @param profile
     *            The LDAP profile
     * @param exceptions
     *            Contains {@code IOException} that may occurred during
     *            management context close. Could be {@code null}
     * @return A LDIF file management context
     * @throws IOException
     *             If problems occurs while reading the file.
     */
    public static ManagementContext newLDIFManagementContext(
        final File ldifFile, final LDAPProfile profile, final List<IOException> exceptions) throws IOException {
        final BufferedReader configReader = new BufferedReader(new FileReader(ldifFile));
        try {
            final MemoryBackend memoryBackend = new MemoryBackend(new LDIFEntryReader(configReader));
            final Connection co = new AbstractConnectionWrapper<Connection>(newInternalConnection(memoryBackend)) {
                @Override
                public void close() {
                    try {
                        final BufferedWriter configWriter = new BufferedWriter(new FileWriter(ldifFile));
                        try {
                            final Iterator<Entry> entries = memoryBackend.getAll().iterator();
                            entries.next(); // skip RootDSE
                            LDIF.copyTo(LDIF.newEntryIteratorReader(entries), new LDIFEntryWriter(configWriter));
                        } finally {
                            configWriter.close();
                        }
                    } catch (IOException e) {
                        if (exceptions != null) {
                            exceptions.add(e);
                        } else {
                            logger.error(LocalizableMessage.raw(
                                "IOException occured during LDIF context management close:" + e));
                        }
                    }
                }
                @Override
                public void close(UnbindRequest request, String reason) {
                    close();
                }
            };
            // We need to add the root dse entry to make the configuration
            // framework work.
            co.add(LDIFEntryReader.valueOfLDIFEntry("dn:", "objectClass:top", "objectClass:ds-root-dse"));
            return LDAPManagementContext.newManagementContext(co, LDAPProfile.getInstance());
        } finally {
            configReader.close();
        }
    }
    /**
     * Returns a LDIF management context on the provided LDIF file.
     *
     * @param ldifFile
     *            The LDIF file to manage
     * @param profile
     *            The LDAP profile
     * @return A LDIF file management context
     * @throws IOException
     *             If problems occurs while reading the file.
     */
    public static ManagementContext newLDIFManagementContext(
        final File ldifFile, final LDAPProfile profile) throws IOException {
        return newLDIFManagementContext(ldifFile, profile, null);
    }
    /** The LDAP management context driver. */
    private final LDAPDriver driver;