From 6452a0b512de72a2beb6da4a3f50fdc788a0f90f Mon Sep 17 00:00:00 2001
From: kenneth_suter <kenneth_suter@localhost>
Date: Thu, 12 Jul 2007 14:41:36 +0000
Subject: [PATCH] This commit addresses issue 1851 "upgrader does not handle new config entries also present in current configuration".  This issue is somewhat of a corner case but this code will allow a user to better deal with problems that might arise migrating the configuration or schema of a server.

---
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/InProcessServerController.java |  192 ++++++++++++++++++++++--------------------------
 1 files changed, 88 insertions(+), 104 deletions(-)

diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/InProcessServerController.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/InProcessServerController.java
index 6863f20..eb6dd0c 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/InProcessServerController.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/InProcessServerController.java
@@ -38,7 +38,6 @@
 import org.opends.server.loggers.AccessLogger;
 import org.opends.server.types.Modification;
 import org.opends.server.types.ResultCode;
-import org.opends.server.types.LDIFImportConfig;
 import org.opends.server.types.ByteStringFactory;
 import org.opends.server.types.ByteString;
 import org.opends.server.types.InitializationException;
@@ -48,7 +47,6 @@
 import org.opends.server.api.ErrorLogPublisher;
 import org.opends.server.api.AccessLogPublisher;
 import org.opends.server.util.LDIFException;
-import org.opends.server.util.LDIFReader;
 import org.opends.server.util.ModifyChangeRecordEntry;
 import org.opends.server.util.ChangeRecordEntry;
 import org.opends.server.util.AddChangeRecordEntry;
@@ -64,7 +62,6 @@
 import java.util.logging.Level;
 import java.util.List;
 import java.util.ArrayList;
-import java.io.File;
 import java.io.IOException;
 
 /**
@@ -361,113 +358,100 @@
   /**
    * Applies modifications contained in an LDIF file to the server.
    *
-   * @param ldifFile LDIF file to apply
+   * @param cre changes to apply to the directory data
    * @throws IOException if there is an IO Error
    * @throws LDIFException if there is an LDIF error
    * @throws ApplicationException if there is an application specific error
    */
-  public void modify(File ldifFile)
-          throws IOException, LDIFException,
-          ApplicationException {
-    try {
-      InternalClientConnection cc =
-              InternalClientConnection.getRootConnection();
-      LDIFImportConfig importCfg =
-              new LDIFImportConfig(
-                      Utils.getPath(ldifFile));
-      LDIFReader ldifReader =
-              new LDIFReader(importCfg);
-      ChangeRecordEntry cre;
-      while (null != (cre = ldifReader.readChangeRecord(false))) {
-        ByteString dnByteString =
-                ByteStringFactory.create(
-                        cre.getDN().toString());
-        ResultCode rc;
-        switch(cre.getChangeOperationType()) {
-        case MODIFY:
-          LOG.log(Level.INFO, "proparing to modify " + dnByteString);
-          ModifyChangeRecordEntry mcre =
-                  (ModifyChangeRecordEntry) cre;
-          ModifyOperation op =
-                  cc.processModify(dnByteString, mcre.getModifications());
-          rc = op.getResultCode();
-          if (rc.equals(ResultCode.
-                  SUCCESS)) {
-            LOG.log(Level.INFO, "processed server modification " +
-                            modListToString(op.getModifications()));
-          } else if (rc.equals(
-                  ResultCode.
-                          ATTRIBUTE_OR_VALUE_EXISTS)) {
-            // ignore this error
-            LOG.log(Level.INFO, "ignoring attribute that already exists: " +
-                    modListToString(op.getModifications()));
-          } else if (rc.equals(ResultCode.NO_SUCH_ATTRIBUTE)) {
-            // This can happen if for instance the old configuration was
-            // changed so that the value of an attribute matches the default
-            // value of the attribute in the new configuration.
-            // Just log it and move on.
-            LOG.log(Level.INFO, "Ignoring attribute not found: " +
-                    modListToString(op.getModifications()));
-          } else {
-            // report the error to the user
-            StringBuilder error = op.getErrorMessage();
-            throw new ApplicationException(
-                    ApplicationException.Type.IMPORT_ERROR,
-                    getMsg("error-apply-ldif-modify", dnByteString.toString(),
-                            error != null ? error.toString() : ""),
-                    null);
-          }
-          break;
-        case ADD:
-          LOG.log(Level.INFO, "proparing to add " + dnByteString);
-          AddChangeRecordEntry acre = (AddChangeRecordEntry) cre;
-          List<Attribute> attrs = acre.getAttributes();
-          ArrayList<RawAttribute> rawAttrs =
-                  new ArrayList<RawAttribute>(attrs.size());
-          for (Attribute a : attrs) {
-            rawAttrs.add(new LDAPAttribute(a));
-          }
-          AddOperation addOp = cc.processAdd(dnByteString, rawAttrs);
-          rc = addOp.getResultCode();
-          if (rc.equals(ResultCode.SUCCESS)) {
-            LOG.log(Level.INFO, "processed server add " + addOp.getEntryDN());
-          } else {
-            // report the error to the user
-            StringBuilder error = addOp.getErrorMessage();
-            throw new ApplicationException(
-                    ApplicationException.Type.IMPORT_ERROR,
-                    getMsg("error-apply-ldif-add", dnByteString.toString(),
-                            error != null ? error.toString() : ""),
-                    null);
-          }
-          break;
-        case DELETE:
-          LOG.log(Level.INFO, "proparing to delete " + dnByteString);
-          DeleteOperation delOp = cc.processDelete(dnByteString);
-          rc = delOp.getResultCode();
-          if (rc.equals(ResultCode.SUCCESS)) {
-            LOG.log(Level.INFO, "processed server delete " +
-                    delOp.getEntryDN());
-          } else {
-            // report the error to the user
-            StringBuilder error = delOp.getErrorMessage();
-            throw new ApplicationException(
-                    ApplicationException.Type.IMPORT_ERROR,
-                    getMsg("error-apply-ldif-delete", dnByteString.toString(),
-                            error != null ? error.toString() : ""),
-                    null);
-          }
-          break;
-        default:
-          LOG.log(Level.SEVERE, "Unexpected record type " + cre.getClass());
-          throw new ApplicationException(ApplicationException.Type.BUG,
-                  getMsg("bug-msg"),
+  public void modify(ChangeRecordEntry cre)
+          throws IOException, LDIFException, ApplicationException
+  {
+    InternalClientConnection cc =
+            InternalClientConnection.getRootConnection();
+    ByteString dnByteString =
+            ByteStringFactory.create(
+                    cre.getDN().toString());
+    ResultCode rc;
+    switch (cre.getChangeOperationType()) {
+      case MODIFY:
+        LOG.log(Level.INFO, "proparing to modify " + dnByteString);
+        ModifyChangeRecordEntry mcre =
+                (ModifyChangeRecordEntry) cre;
+        ModifyOperation op =
+                cc.processModify(dnByteString, mcre.getModifications());
+        rc = op.getResultCode();
+        if (rc.equals(ResultCode.
+                SUCCESS)) {
+          LOG.log(Level.INFO, "processed server modification " +
+                  modListToString(op.getModifications()));
+        } else if (rc.equals(
+                ResultCode.
+                        ATTRIBUTE_OR_VALUE_EXISTS)) {
+          // ignore this error
+          LOG.log(Level.INFO, "ignoring attribute that already exists: " +
+                  modListToString(op.getModifications()));
+        } else if (rc.equals(ResultCode.NO_SUCH_ATTRIBUTE)) {
+          // This can happen if for instance the old configuration was
+          // changed so that the value of an attribute matches the default
+          // value of the attribute in the new configuration.
+          // Just log it and move on.
+          LOG.log(Level.INFO, "Ignoring attribute not found: " +
+                  modListToString(op.getModifications()));
+        } else {
+          // report the error to the user
+          StringBuilder error = op.getErrorMessage();
+          throw new ApplicationException(
+                  ApplicationException.Type.IMPORT_ERROR,
+                  getMsg("error-apply-ldif-modify", dnByteString.toString(),
+                          error != null ? error.toString() : ""),
                   null);
         }
-      }
-    } catch (Throwable t) {
-      throw new ApplicationException(ApplicationException.Type.BUG,
-              getMsg("bug-msg"), t);
+        break;
+      case ADD:
+        LOG.log(Level.INFO, "preparing to add " + dnByteString);
+        AddChangeRecordEntry acre = (AddChangeRecordEntry) cre;
+        List<Attribute> attrs = acre.getAttributes();
+        ArrayList<RawAttribute> rawAttrs =
+                new ArrayList<RawAttribute>(attrs.size());
+        for (Attribute a : attrs) {
+          rawAttrs.add(new LDAPAttribute(a));
+        }
+        AddOperation addOp = cc.processAdd(dnByteString, rawAttrs);
+        rc = addOp.getResultCode();
+        if (rc.equals(ResultCode.SUCCESS)) {
+          LOG.log(Level.INFO, "processed server add " + addOp.getEntryDN());
+        } else {
+          // report the error to the user
+          StringBuilder error = addOp.getErrorMessage();
+          throw new ApplicationException(
+                  ApplicationException.Type.IMPORT_ERROR,
+                  getMsg("error-apply-ldif-add", dnByteString.toString(),
+                          error != null ? error.toString() : ""),
+                  null);
+        }
+        break;
+      case DELETE:
+        LOG.log(Level.INFO, "preparing to delete " + dnByteString);
+        DeleteOperation delOp = cc.processDelete(dnByteString);
+        rc = delOp.getResultCode();
+        if (rc.equals(ResultCode.SUCCESS)) {
+          LOG.log(Level.INFO, "processed server delete " +
+                  delOp.getEntryDN());
+        } else {
+          // report the error to the user
+          StringBuilder error = delOp.getErrorMessage();
+          throw new ApplicationException(
+                  ApplicationException.Type.IMPORT_ERROR,
+                  getMsg("error-apply-ldif-delete", dnByteString.toString(),
+                          error != null ? error.toString() : ""),
+                  null);
+        }
+        break;
+      default:
+        LOG.log(Level.SEVERE, "Unexpected record type " + cre.getClass());
+        throw new ApplicationException(ApplicationException.Type.BUG,
+                getMsg("bug-msg"),
+                null);
     }
   }
 

--
Gitblit v1.10.0