From 9d6ecd358f1b1c2d32b3a209d0fabe8bb6d4b4a7 Mon Sep 17 00:00:00 2001
From: Gaetan Boismal <gaetan.boismal@forgerock.com>
Date: Thu, 16 Apr 2015 14:17:37 +0000
Subject: [PATCH] OPENDJ-1714 Create a LDIF context management
---
opendj-config/src/main/java/org/forgerock/opendj/config/client/ldap/LDAPManagementContext.java | 95 +++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 95 insertions(+), 0 deletions(-)
diff --git a/opendj-config/src/main/java/org/forgerock/opendj/config/client/ldap/LDAPManagementContext.java b/opendj-config/src/main/java/org/forgerock/opendj/config/client/ldap/LDAPManagementContext.java
index f352842..6c2be14 100644
--- a/opendj-config/src/main/java/org/forgerock/opendj/config/client/ldap/LDAPManagementContext.java
+++ b/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;
--
Gitblit v1.10.0