From 764c819f288e77bbe3c299042288a96ca06a5f56 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Mon, 02 Jun 2008 20:44:47 +0000
Subject: [PATCH] Fix for issue 3311 (import-ldif --templateFile does not work when the server is  running) The fix consists on not assuming that the ldif file path is required to launch an import (the ds-task-import task definition has been modified for this) and allowing to create a task that imports contents based on a  template.

---
 opendj-sdk/opends/src/server/org/opends/server/tasks/ImportTask.java        |   64 ++++++++++++++++++++
 opendj-sdk/opends/src/messages/messages/task.properties                     |    2 
 opendj-sdk/opends/src/server/org/opends/server/util/cli/CommandBuilder.java |    6 +-
 opendj-sdk/opends/src/server/org/opends/server/config/ConfigConstants.java  |   14 ++++
 opendj-sdk/opends/src/server/org/opends/server/tools/ImportLDIF.java        |   30 ++++++++-
 opendj-sdk/opends/resource/schema/02-config.ldif                            |   12 +++
 6 files changed, 117 insertions(+), 11 deletions(-)

diff --git a/opendj-sdk/opends/resource/schema/02-config.ldif b/opendj-sdk/opends/resource/schema/02-config.ldif
index 3646c9d..68f5dee 100644
--- a/opendj-sdk/opends/resource/schema/02-config.ldif
+++ b/opendj-sdk/opends/resource/schema/02-config.ldif
@@ -846,6 +846,14 @@
   NAME 'ds-task-import-ldif-file'
   SYNTAX 1.3.6.1.4.1.1466.115.121.1.15
   X-ORIGIN 'OpenDS Directory Server' )
+attributeTypes: ( 1.3.6.1.4.1.26027.1.1.459
+  NAME 'ds-task-import-template-file'
+  SYNTAX 1.3.6.1.4.1.1466.115.121.1.15
+  X-ORIGIN 'OpenDS Directory Server' )
+attributeTypes: ( 1.3.6.1.4.1.26027.1.1.460
+  NAME 'ds-task-import-random-seed'
+  SYNTAX 1.3.6.1.4.1.1466.115.121.1.27
+  X-ORIGIN 'OpenDS Directory Server' )
 attributeTypes: ( 1.3.6.1.4.1.26027.1.1.170
   NAME 'ds-task-import-append'
   SYNTAX 1.3.6.1.4.1.1466.115.121.1.7
@@ -2849,7 +2857,6 @@
   NAME 'ds-task-import'
   SUP ds-task
   STRUCTURAL
-  MUST ds-task-import-ldif-file
   MAY ( ds-task-import-append $
         ds-task-import-replace-existing $
         ds-task-import-include-branch $
@@ -2858,6 +2865,9 @@
         ds-task-import-exclude-attribute $
         ds-task-import-include-filter $
         ds-task-import-exclude-filter $
+        ds-task-import-ldif-file $
+        ds-task-import-template-file $
+        ds-task-import-random-seed $
         ds-task-import-reject-file $
         ds-task-import-overwrite-rejects $
         ds-task-import-skip-schema-validation $
diff --git a/opendj-sdk/opends/src/messages/messages/task.properties b/opendj-sdk/opends/src/messages/messages/task.properties
index da565b4..7ccaf46 100644
--- a/opendj-sdk/opends/src/messages/messages/task.properties
+++ b/opendj-sdk/opends/src/messages/messages/task.properties
@@ -189,3 +189,5 @@
 INFO_TASK_STOPPED_BY_ADMIN_102=Task was stopped by an administrator:  %s
 SEVERE_ERR_TASK_INITIALIZE_INVALID_GENERATION_ID_103=Invalid generation ID provided with the \
  task
+INFO_IMPORT_ARG_TEMPLATE_FILE_104=Template File
+INFO_IMPORT_ARG_RANDOM_SEED_105=Random Seed
diff --git a/opendj-sdk/opends/src/server/org/opends/server/config/ConfigConstants.java b/opendj-sdk/opends/src/server/org/opends/server/config/ConfigConstants.java
index 1be0c38..a2482c2 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/config/ConfigConstants.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/config/ConfigConstants.java
@@ -3828,6 +3828,20 @@
        NAME_PREFIX_TASK + "import-ldif-file";
 
 
+  /**
+   * The name of the attribute in an import task definition that specifies the
+   * path to the file containing the template data to import.
+   */
+  public static final String ATTR_IMPORT_TEMPLATE_FILE =
+       NAME_PREFIX_TASK + "import-template-file";
+
+
+  /**
+   * The name of the attribute in an import task definition that specifies the
+   * random seed to be used when we pass a template file to the import.
+   */
+  public static final String ATTR_IMPORT_RANDOM_SEED =
+       NAME_PREFIX_TASK + "import-random-seed";
 
   /**
    * The name of the attribute in an import task definition that specifies
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tasks/ImportTask.java b/opendj-sdk/opends/src/server/org/opends/server/tasks/ImportTask.java
index 76a882b..6a54a24 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tasks/ImportTask.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tasks/ImportTask.java
@@ -30,8 +30,10 @@
 
 import static org.opends.messages.TaskMessages.*;
 import static org.opends.messages.ToolMessages.*;
+import static org.opends.server.loggers.ErrorLogger.logError;
 import static org.opends.server.loggers.debug.DebugLogger.*;
 import org.opends.server.loggers.debug.DebugTracer;
+import org.opends.server.tools.makeldif.TemplateFile;
 import org.opends.server.types.DebugLogLevel;
 import static org.opends.server.util.StaticUtils.*;
 import static org.opends.server.config.ConfigConstants.*;
@@ -57,11 +59,13 @@
 import org.opends.server.types.ResultCode;
 import org.opends.server.types.SearchFilter;
 
+import java.io.File;
 import java.util.HashSet;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.HashMap;
+import java.util.Random;
 
 /**
  * This class provides an implementation of a Directory Server task that can
@@ -87,6 +91,14 @@
             INFO_IMPORT_ARG_LDIF_FILE.get());
 
     argDisplayMap.put(
+        ATTR_IMPORT_TEMPLATE_FILE,
+        INFO_IMPORT_ARG_TEMPLATE_FILE.get());
+
+    argDisplayMap.put(
+        ATTR_IMPORT_RANDOM_SEED,
+        INFO_IMPORT_ARG_RANDOM_SEED.get());
+
+    argDisplayMap.put(
             ATTR_IMPORT_APPEND,
             INFO_IMPORT_ARG_APPEND.get());
 
@@ -169,6 +181,8 @@
   ArrayList<String>  includeBranchStrings    = null;
   ArrayList<String>  includeFilterStrings    = null;
   ArrayList<String>  ldifFiles               = null;
+  String templateFile = null;
+  int randomSeed = 0;
 
   private LDIFImportConfig importConfig;
 
@@ -209,6 +223,7 @@
     Entry taskEntry = getTaskEntry();
 
     AttributeType typeLdifFile;
+    AttributeType typeTemplateFile;
     AttributeType typeAppend;
     AttributeType typeReplaceExisting;
     AttributeType typeBackendID;
@@ -225,9 +240,12 @@
     AttributeType typeIsCompressed;
     AttributeType typeIsEncrypted;
     AttributeType typeClearBackend;
+    AttributeType typeRandomSeed;
 
     typeLdifFile =
          getAttributeType(ATTR_IMPORT_LDIF_FILE, true);
+    typeTemplateFile =
+         getAttributeType(ATTR_IMPORT_TEMPLATE_FILE, true);
     typeAppend =
          getAttributeType(ATTR_IMPORT_APPEND, true);
     typeReplaceExisting =
@@ -260,12 +278,17 @@
          getAttributeType(ATTR_IMPORT_IS_ENCRYPTED, true);
     typeClearBackend =
          getAttributeType(ATTR_IMPORT_CLEAR_BACKEND, true);
+    typeRandomSeed =
+         getAttributeType(ATTR_IMPORT_RANDOM_SEED, true);
 
     List<Attribute> attrList;
 
     attrList = taskEntry.getAttribute(typeLdifFile);
     ldifFiles = TaskUtils.getMultiValueString(attrList);
 
+    attrList = taskEntry.getAttribute(typeTemplateFile);
+    templateFile = TaskUtils.getSingleValueString(attrList);
+
     attrList = taskEntry.getAttribute(typeAppend);
     append = TaskUtils.getBoolean(attrList, false);
 
@@ -314,6 +337,9 @@
     attrList = taskEntry.getAttribute(typeClearBackend);
     clearBackend = TaskUtils.getBoolean(attrList, false);
 
+    attrList = taskEntry.getAttribute(typeRandomSeed);
+    randomSeed = TaskUtils.getSingleValueInteger(attrList, 0);
+
     // Make sure that either the "includeBranchStrings" argument or the
     // "backendID" argument was provided.
     if(includeBranchStrings.isEmpty() && backendID == null)
@@ -756,8 +782,42 @@
     }
 
     // Create the LDIF import configuration to use when reading the LDIF.
-    ArrayList<String> fileList = new ArrayList<String>(ldifFiles);
-    importConfig = new LDIFImportConfig(fileList);
+    if (templateFile != null)
+    {
+      Random random;
+      try
+      {
+        random = new Random(randomSeed);
+      }
+      catch (Exception e)
+      {
+        random = new Random();
+      }
+
+      String resourcePath = DirectoryServer.getServerRoot() + File.separator +
+                            PATH_MAKELDIF_RESOURCE_DIR;
+      TemplateFile tf = new TemplateFile(resourcePath, random);
+
+      ArrayList<Message> warnings = new ArrayList<Message>();
+      try
+      {
+        tf.parse(templateFile, warnings);
+      }
+      catch (Exception e)
+      {
+        Message message = ERR_LDIFIMPORT_CANNOT_PARSE_TEMPLATE_FILE.get(
+            templateFile, e.getMessage());
+        logError(message);
+        return TaskState.STOPPED_BY_ERROR;
+      }
+
+      importConfig = new LDIFImportConfig(tf);
+    }
+    else
+    {
+      ArrayList<String> fileList = new ArrayList<String>(ldifFiles);
+      importConfig = new LDIFImportConfig(fileList);
+    }
     importConfig.setAppendToExistingData(append);
     importConfig.setReplaceExistingEntries(replaceExisting);
     importConfig.setCompressed(isCompressed);
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/ImportLDIF.java b/opendj-sdk/opends/src/server/org/opends/server/tools/ImportLDIF.java
index f242747..a6ebe3f 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/ImportLDIF.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/ImportLDIF.java
@@ -465,13 +465,33 @@
     // Required attributes
     //
     ArrayList<ASN1OctetString> values;
+
     List<String> fileList = ldifFiles.getValues();
-    if (fileList != null && fileList.size() > 0) {
-      values = new ArrayList<ASN1OctetString>(fileList.size());
-      for (String file : fileList) {
-        values.add(new ASN1OctetString(file));
+    if ((fileList != null) && (fileList.size() > 0))
+    {
+      if (fileList != null && fileList.size() > 0) {
+        values = new ArrayList<ASN1OctetString>(fileList.size());
+        for (String file : fileList) {
+          values.add(new ASN1OctetString(file));
+        }
+        attributes.add(new LDAPAttribute(ATTR_IMPORT_LDIF_FILE, values));
       }
-      attributes.add(new LDAPAttribute(ATTR_IMPORT_LDIF_FILE, values));
+    }
+
+    String templateFileValue = templateFile.getValue();
+    if (templateFileValue != null)
+    {
+      values = new ArrayList<ASN1OctetString>(1);
+      values.add(new ASN1OctetString(templateFileValue));
+      attributes.add(new LDAPAttribute(ATTR_IMPORT_TEMPLATE_FILE, values));
+    }
+
+    String randomSeedValue = randomSeed.getValue();
+    if (randomSeedValue != null)
+    {
+      values = new ArrayList<ASN1OctetString>(1);
+      values.add(new ASN1OctetString(randomSeedValue));
+      attributes.add(new LDAPAttribute(ATTR_IMPORT_RANDOM_SEED, values));
     }
 
     //
diff --git a/opendj-sdk/opends/src/server/org/opends/server/util/cli/CommandBuilder.java b/opendj-sdk/opends/src/server/org/opends/server/util/cli/CommandBuilder.java
index 3c9bf11..a75b3aa 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/util/cli/CommandBuilder.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/util/cli/CommandBuilder.java
@@ -249,15 +249,15 @@
   }
 
   // Chars that require special treatment when passing them to command-line.
-  private final char[] charsToEscape = {' ', '\t', '\n', '|', ';', '<', '>',
-      '(', ')', '$', '`', '\\', '"', '\''};
+  private final static char[] charsToEscape = {' ', '\t', '\n', '|', ';', '<',
+    '>', '(', ')', '$', '`', '\\', '"', '\''};
   /**
    * This method simply takes a value and tries to transform it (with escape or
    * '"') characters so that it can be used in a command line.
    * @param value the String to be treated.
    * @return the transformed value.
    */
-  private String escapeValue(String value)
+  public static String escapeValue(String value)
   {
     StringBuilder b = new StringBuilder();
     if (SetupUtils.isUnix())

--
Gitblit v1.10.0