From 7dc2e6ceaaf74ff2cd8b6a8a3964097cffe1f475 Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Thu, 31 Aug 2006 00:40:49 +0000
Subject: [PATCH] Add initial MakeLDIF support for generating entries based on a template.  This can be used to either write the generated entries to LDIF, or to import them directly into the server.  This code is not yet feature-complete, nor is it 100% compatible with the version of MakeLDIF provided with SLAMD, but it is functional enough for use in a large number of situations.

---
 opends/src/server/org/opends/server/tools/ImportLDIF.java |   97 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 94 insertions(+), 3 deletions(-)

diff --git a/opends/src/server/org/opends/server/tools/ImportLDIF.java b/opends/src/server/org/opends/server/tools/ImportLDIF.java
index 186d248..09926b4 100644
--- a/opends/src/server/org/opends/server/tools/ImportLDIF.java
+++ b/opends/src/server/org/opends/server/tools/ImportLDIF.java
@@ -28,9 +28,11 @@
 
 
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Random;
 
 import org.opends.server.api.Backend;
 import org.opends.server.api.plugin.PluginType;
@@ -45,6 +47,7 @@
 import org.opends.server.core.InitializationException;
 import org.opends.server.core.LockFileManager;
 import org.opends.server.loggers.StartupErrorLogger;
+import org.opends.server.tools.makeldif.TemplateFile;
 import org.opends.server.types.AttributeType;
 import org.opends.server.types.DN;
 import org.opends.server.types.ErrorLogCategory;
@@ -55,6 +58,7 @@
 import org.opends.server.util.args.ArgumentException;
 import org.opends.server.util.args.ArgumentParser;
 import org.opends.server.util.args.BooleanArgument;
+import org.opends.server.util.args.IntegerArgument;
 import org.opends.server.util.args.StringArgument;
 
 import static org.opends.server.config.ConfigConstants.*;
@@ -116,6 +120,7 @@
     BooleanArgument quietMode               = null;
     BooleanArgument replaceExisting         = null;
     BooleanArgument skipSchemaValidation    = null;
+    IntegerArgument randomSeed              = null;
     StringArgument  backendID               = null;
     StringArgument  configClass             = null;
     StringArgument  configFile              = null;
@@ -127,6 +132,7 @@
     StringArgument  includeFilterStrings    = null;
     StringArgument  ldifFiles               = null;
     StringArgument  rejectFile              = null;
+    StringArgument  templateFile            = null;
 
 
     // Create the command-line argument parser for use with this program.
@@ -154,12 +160,19 @@
 
 
       ldifFiles =
-           new StringArgument("ldiffile", 'l', "ldifFile", true, true, true,
+           new StringArgument("ldiffile", 'l', "ldifFile", false, true, true,
                               "{ldifFile}", null, null,
                               MSGID_LDIFIMPORT_DESCRIPTION_LDIF_FILE);
       argParser.addArgument(ldifFiles);
 
 
+      templateFile =
+           new StringArgument("templatefile", 't', "templateFile", false, false,
+                              true, "{templateFile}", null, null,
+                              MSGID_LDIFIMPORT_DESCRIPTION_TEMPLATE_FILE);
+      argParser.addArgument(templateFile);
+
+
       append =
            new BooleanArgument("append", 'a', "append",
                                MSGID_LDIFIMPORT_DESCRIPTION_APPEND);
@@ -234,6 +247,13 @@
       argParser.addArgument(overwriteRejects);
 
 
+      randomSeed =
+           new IntegerArgument("randomseed", 'S', "randomSeed", false, false,
+                               true, "{seed}", 0, null, false, 0, false, 0,
+                               MSGID_LDIFIMPORT_DESCRIPTION_RANDOM_SEED);
+      argParser.addArgument(randomSeed);
+
+
       skipSchemaValidation =
            new BooleanArgument("skipschema", 's', "skipSchemaValidation",
                     MSGID_LDIFIMPORT_DESCRIPTION_SKIP_SCHEMA_VALIDATION);
@@ -296,6 +316,29 @@
     }
 
 
+    // Make sure that either the "ldifFile" argument or the "templateFile"
+    // argument was provided, but not both.
+    if (ldifFiles.isPresent())
+    {
+      if (templateFile.isPresent())
+      {
+        int    msgID   = MSGID_LDIFIMPORT_CONFLICTING_OPTIONS;
+        String message = getMessage(msgID, ldifFiles.getLongIdentifier(),
+                                    templateFile.getLongIdentifier());
+        System.err.println(message);
+        return 1;
+      }
+    }
+    else if (! templateFile.isPresent())
+    {
+      int    msgID   = MSGID_LDIFIMPORT_MISSING_REQUIRED_ARGUMENT;
+      String message = getMessage(msgID, ldifFiles.getLongIdentifier(),
+                                  templateFile.getLongIdentifier());
+      System.err.println(message);
+      return 1;
+    }
+
+
     // Perform the initial bootstrap of the Directory Server and process the
     // configuration.
     DirectoryServer directoryServer = DirectoryServer.getInstance();
@@ -758,9 +801,57 @@
     }
 
 
+    // See if the data should be read from LDIF files or generated via MakeLDIF.
+    LDIFImportConfig importConfig;
+    if (ldifFiles.isPresent())
+    {
+      ArrayList<String> fileList = new ArrayList<String>(ldifFiles.getValues());
+      importConfig = new LDIFImportConfig(fileList);
+    }
+    else
+    {
+      Random random;
+      if (randomSeed.isPresent())
+      {
+        try
+        {
+          random = new Random(randomSeed.getIntValue());
+        }
+        catch (Exception e)
+        {
+          random = new Random();
+        }
+      }
+      else
+      {
+        random = new Random();
+      }
+
+      String resourcePath = DirectoryServer.getServerRoot() + File.separator +
+                            PATH_MAKELDIF_RESOURCE_DIR;
+      TemplateFile tf = new TemplateFile(resourcePath, random);
+
+      ArrayList<String> warnings = new ArrayList<String>();
+      try
+      {
+        tf.parse(templateFile.getValue(), warnings);
+      }
+      catch (Exception e)
+      {
+        int msgID = MSGID_LDIFIMPORT_CANNOT_PARSE_TEMPLATE_FILE;
+        String message = getMessage(msgID, templateFile.getValue(),
+                                    e.getMessage());
+        logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.SEVERE_ERROR,
+                 message, msgID);
+        return 1;
+      }
+
+      importConfig = new LDIFImportConfig(tf);
+    }
+
+
+
     // Create the LDIF import configuration to use when reading the LDIF.
-    ArrayList<String> fileList = new ArrayList<String>(ldifFiles.getValues());
-    LDIFImportConfig importConfig = new LDIFImportConfig(fileList);
     importConfig.setAppendToExistingData(append.isPresent());
     importConfig.setReplaceExistingEntries(replaceExisting.isPresent());
     importConfig.setCompressed(isCompressed.isPresent());

--
Gitblit v1.10.0