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

neil_a_wilson
05.16.2007 e0c5b184252862e29ca271955bcd15eebd752830
Update the import-ldif utility so that it includes a new "--countRejects"
option that can be used to count the number of rejected entries. That value
will be used as the exit code, and can be used in scripts to determine if there
were any failures during the import.

OpenDS Issue Number: 1836
1 files added
12 files modified
255 ■■■■■ changed files
opends/src/server/org/opends/server/api/Backend.java 6 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/backends/BackupBackend.java 5 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/backends/MemoryBackend.java 7 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/backends/MonitorBackend.java 3 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/backends/RootDSEBackend.java 3 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/backends/SchemaBackend.java 5 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/backends/jeb/BackendImpl.java 4 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/backends/jeb/ImportJob.java 10 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/backends/task/TaskBackend.java 3 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/extensions/ConfigFileHandler.java 5 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/messages/ToolMessages.java 21 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/ImportLDIF.java 26 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/types/LDIFImportResult.java 157 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/api/Backend.java
@@ -51,6 +51,7 @@
import org.opends.server.types.InitializationException;
import org.opends.server.types.LDIFExportConfig;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFImportResult;
import org.opends.server.types.LockManager;
import org.opends.server.types.RestoreConfig;
import org.opends.server.types.WritabilityMode;
@@ -492,10 +493,13 @@
   * @param  importConfig  The configuration to use when performing
   *                       the import.
   *
   * @return  Information about the result of the import processing.
   *
   * @throws  DirectoryException  If a problem occurs while performing
   *                              the LDIF import.
   */
  public abstract void importLDIF(LDIFImportConfig importConfig)
  public abstract LDIFImportResult importLDIF(
                                        LDIFImportConfig importConfig)
         throws DirectoryException;
opends/src/server/org/opends/server/backends/BackupBackend.java
@@ -53,8 +53,9 @@
import org.opends.server.types.DN;
import org.opends.server.types.Entry;
import org.opends.server.types.InitializationException;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFExportConfig;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFImportResult;
import org.opends.server.types.ObjectClass;
import org.opends.server.types.RDN;
import org.opends.server.types.RestoreConfig;
@@ -1104,7 +1105,7 @@
  /**
   * {@inheritDoc}
   */
  public void importLDIF(LDIFImportConfig importConfig)
  public LDIFImportResult importLDIF(LDIFImportConfig importConfig)
         throws DirectoryException
  {
    // This backend does not support LDIF imports.
opends/src/server/org/opends/server/backends/MemoryBackend.java
@@ -50,6 +50,7 @@
import org.opends.server.types.InitializationException;
import org.opends.server.types.LDIFExportConfig;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFImportResult;
import org.opends.server.types.RestoreConfig;
import org.opends.server.types.ResultCode;
import org.opends.server.types.SearchScope;
@@ -743,7 +744,7 @@
  /**
   * {@inheritDoc}
   */
  public synchronized void importLDIF(LDIFImportConfig importConfig)
  public synchronized LDIFImportResult importLDIF(LDIFImportConfig importConfig)
         throws DirectoryException
  {
    clearMemoryBackend();
@@ -800,6 +801,10 @@
          reader.rejectLastEntry(de.getErrorMessage());
        }
      }
      return new LDIFImportResult(reader.getEntriesRead(),
                                  reader.getEntriesRejected(),
                                  reader.getEntriesIgnored());
    }
    catch (DirectoryException de)
    {
opends/src/server/org/opends/server/backends/MonitorBackend.java
@@ -59,6 +59,7 @@
import org.opends.server.types.InitializationException;
import org.opends.server.types.LDIFExportConfig;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFImportResult;
import org.opends.server.types.ObjectClass;
import org.opends.server.types.RDN;
import org.opends.server.types.RestoreConfig;
@@ -1100,7 +1101,7 @@
  /**
   * {@inheritDoc}
   */
  public void importLDIF(LDIFImportConfig importConfig)
  public LDIFImportResult importLDIF(LDIFImportConfig importConfig)
         throws DirectoryException
  {
    // This backend does not support LDIF imports.
opends/src/server/org/opends/server/backends/RootDSEBackend.java
@@ -65,6 +65,7 @@
import org.opends.server.types.InitializationException;
import org.opends.server.types.LDIFExportConfig;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFImportResult;
import org.opends.server.types.ObjectClass;
import org.opends.server.types.RestoreConfig;
import org.opends.server.types.ResultCode;
@@ -1209,7 +1210,7 @@
  /**
   * {@inheritDoc}
   */
  public void importLDIF(LDIFImportConfig importConfig)
  public LDIFImportResult importLDIF(LDIFImportConfig importConfig)
         throws DirectoryException
  {
    // This backend does not support LDIF imports.
opends/src/server/org/opends/server/backends/SchemaBackend.java
@@ -93,8 +93,9 @@
import org.opends.server.types.ErrorLogSeverity;
import org.opends.server.types.ExistingFileBehavior;
import org.opends.server.types.InitializationException;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFExportConfig;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFImportResult;
import org.opends.server.types.MatchingRuleUse;
import org.opends.server.types.Modification;
import org.opends.server.types.ModificationType;
@@ -4230,7 +4231,7 @@
  /**
   * {@inheritDoc}
   */
  public void importLDIF(LDIFImportConfig importConfig)
  public LDIFImportResult importLDIF(LDIFImportConfig importConfig)
         throws DirectoryException
  {
    // This backend does not support LDIF imports.
opends/src/server/org/opends/server/backends/jeb/BackendImpl.java
@@ -1043,7 +1043,7 @@
  /**
   * {@inheritDoc}
   */
  public void importLDIF(LDIFImportConfig importConfig)
  public LDIFImportResult importLDIF(LDIFImportConfig importConfig)
      throws DirectoryException
  {
    // If the backend already has the root container open, we must use the same
@@ -1100,7 +1100,7 @@
      initializeRootContainer(envConfig);
      ImportJob importJob = new ImportJob(importConfig);
      importJob.importLDIF(rootContainer);
      return importJob.importLDIF(rootContainer);
    }
    catch (IOException ioe)
    {
opends/src/server/org/opends/server/backends/jeb/ImportJob.java
@@ -39,6 +39,7 @@
import org.opends.server.types.ErrorLogCategory;
import org.opends.server.types.ErrorLogSeverity;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFImportResult;
import org.opends.server.types.ResultCode;
import org.opends.server.util.LDIFException;
import org.opends.server.util.LDIFReader;
@@ -138,12 +139,15 @@
   * files to load the index databases.
   *
   * @param rootContainer The root container to import into.
   *
   * @return  Information about the result of the import.
   *
   * @throws DatabaseException If an error occurs in the JE database.
   * @throws IOException  If a problem occurs while opening the LDIF file for
   *                      reading, or while reading from the LDIF file.
   * @throws JebException If an error occurs in the JE backend.
   */
  public void importLDIF(RootContainer rootContainer)
  public LDIFImportResult importLDIF(RootContainer rootContainer)
      throws DatabaseException, IOException, JebException
  {
@@ -304,6 +308,10 @@
    message = getMessage(msgID, getEntryLimitExceededCount());
    logError(ErrorLogCategory.BACKEND, ErrorLogSeverity.NOTICE,
             message, msgID);
    return new LDIFImportResult(reader.getEntriesRead(),
                                reader.getEntriesRejected(),
                                reader.getEntriesIgnored());
  }
  /**
opends/src/server/org/opends/server/backends/task/TaskBackend.java
@@ -53,6 +53,7 @@
import org.opends.server.types.InitializationException;
import org.opends.server.types.LDIFExportConfig;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFImportResult;
import org.opends.server.types.RestoreConfig;
import org.opends.server.types.ResultCode;
import org.opends.server.types.SearchFilter;
@@ -972,7 +973,7 @@
  /**
   * {@inheritDoc}
   */
  public void importLDIF(LDIFImportConfig importConfig)
  public LDIFImportResult importLDIF(LDIFImportConfig importConfig)
         throws DirectoryException
  {
    // This backend does not support LDIF imports.
opends/src/server/org/opends/server/extensions/ConfigFileHandler.java
@@ -88,8 +88,9 @@
import org.opends.server.types.ErrorLogSeverity;
import org.opends.server.types.ExistingFileBehavior;
import org.opends.server.types.InitializationException;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFExportConfig;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFImportResult;
import org.opends.server.types.Modification;
import org.opends.server.types.Privilege;
import org.opends.server.types.ResultCode;
@@ -2326,7 +2327,7 @@
  /**
   * {@inheritDoc}
   */
  public void importLDIF(LDIFImportConfig importConfig)
  public LDIFImportResult importLDIF(LDIFImportConfig importConfig)
         throws DirectoryException
  {
    int msgID     =  MSGID_CONFIG_FILE_UNWILLING_TO_IMPORT;
opends/src/server/org/opends/server/messages/ToolMessages.java
@@ -8979,6 +8979,16 @@
    CATEGORY_MASK_TOOLS | SEVERITY_MASK_INFORMATIONAL | 1194;
  /**
   * The message ID for the message that will be used as the description of the
   * countRejects argument.  This does not take any arguments.
   */
  public static final int MSGID_LDIFIMPORT_DESCRIPTION_COUNT_REJECTS =
       CATEGORY_MASK_TOOLS | SEVERITY_MASK_INFORMATIONAL | 1195;
  /**
   * Associates a set of generic messages with the message IDs defined in this
   * class.
@@ -9231,6 +9241,10 @@
                    "Seed for the MakeLDIF random number generator");
    registerMessage(MSGID_LDIFIMPORT_DESCRIPTION_SKIP_SCHEMA_VALIDATION,
                    "Skip schema validation during the LDIF import");
    registerMessage(MSGID_LDIFIMPORT_DESCRIPTION_COUNT_REJECTS,
                    "Count the number of entries rejected by the server and " +
                    "return that value as the exit code (values > 255 will " +
                    "be reduced to 255 due to exit code restrictions)");
    registerMessage(MSGID_LDIFIMPORT_DESCRIPTION_IS_COMPRESSED,
                    "LDIF file is compressed");
    registerMessage(MSGID_LDIFIMPORT_DESCRIPTION_IS_ENCRYPTED,
@@ -9263,11 +9277,8 @@
                    "None of the Directory Server backends are configured " +
                    "with the requested backend ID \"%s\"");
    registerMessage(MSGID_LDIFIMPORT_CANNOT_IMPORT,
                    "The Directory Server backend for base DN %s does " +
                    "not provide a mechanism for performing LDIF imports. " +
                    "The replication will proceed but it will not be " +
                    "possible to perform total update on this " +
                    "ReplicationDomain");
                    "The Directory Server backend with backend ID %s does " +
                    "not provide a mechanism for performing LDIF imports");
    registerMessage(MSGID_LDIFIMPORT_CANNOT_DECODE_EXCLUDE_BASE,
                    "Unable to decode exclude branch string \"%s\" as a " +
                    "valid distinguished name:  %s");
opends/src/server/org/opends/server/tools/ImportLDIF.java
@@ -34,6 +34,7 @@
import java.util.List;
import java.util.Random;
import org.opends.server.admin.std.server.BackendCfg;
import org.opends.server.api.Backend;
import org.opends.server.api.ErrorLogPublisher;
import org.opends.server.api.plugin.PluginType;
@@ -54,6 +55,7 @@
import org.opends.server.types.ExistingFileBehavior;
import org.opends.server.types.InitializationException;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFImportResult;
import org.opends.server.types.SearchFilter;
import org.opends.server.util.args.ArgumentException;
import org.opends.server.util.args.ArgumentParser;
@@ -67,9 +69,7 @@
import static org.opends.server.messages.ToolMessages.*;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
import org.opends.server.util.StaticUtils;
import static org.opends.server.tools.ToolConstants.*;
import org.opends.server.admin.std.server.BackendCfg;
/**
@@ -135,6 +135,7 @@
    // Define the command-line arguments that may be used with this program.
    BooleanArgument append                  = null;
    BooleanArgument countRejects            = null;
    BooleanArgument displayUsage            = null;
    BooleanArgument isCompressed            = null;
    BooleanArgument isEncrypted             = null;
@@ -290,6 +291,12 @@
      argParser.addArgument(skipSchemaValidation);
      countRejects =
           new BooleanArgument("countrejects", null, "countRejects",
                               MSGID_LDIFIMPORT_DESCRIPTION_COUNT_REJECTS);
      argParser.addArgument(countRejects);
      isCompressed =
           new BooleanArgument("iscompressed", 'c', "isCompressed",
                               MSGID_LDIFIMPORT_DESCRIPTION_IS_COMPRESSED);
@@ -510,7 +517,7 @@
        catch(Exception e)
        {
          System.err.println("Error installing the custom error logger: " +
              StaticUtils.stackTraceToSingleLineString(e));
                             stackTraceToSingleLineString(e));
        }
      }
@@ -1023,7 +1030,18 @@
    int retCode = 0;
    try
    {
      backend.importLDIF(importConfig);
      LDIFImportResult importResult = backend.importLDIF(importConfig);
      if (countRejects.isPresent())
      {
        if (importResult.getEntriesRejected() > Integer.MAX_VALUE)
        {
          retCode = Integer.MAX_VALUE;
        }
        else
        {
          retCode = (int) importResult.getEntriesRejected();
        }
      }
    }
    catch (DirectoryException de)
    {
opends/src/server/org/opends/server/types/LDIFImportResult.java
New file
@@ -0,0 +1,157 @@
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2006-2007 Sun Microsystems, Inc.
 */
package org.opends.server.types;
/**
 * This class defines a data structure for providing information about
 * the state of a completed LDIF import, including the total number of
 * entries read, skipped, and rejected.
 */
public class LDIFImportResult
{
  // The total number of entries read during the import.
  private final long entriesRead;
  // The total number of entries rejected during the import.
  private final long entriesRejected;
  // The total number of entries skipped during the import.
  private final long entriesSkipped;
  /**
   * Creates a new LDIF import result object with the provided
   * information.
   *
   * @param  entriesRead      The total number of entries read
   *                          during the import, including those that
   *                          were later rejected or skipped.
   * @param  entriesRejected  The total number of entries rejected
   *                          during the import.
   * @param  entriesSkipped   The total number of entries skipped
   *                          during the import.
   */
  public LDIFImportResult(long entriesRead, long entriesRejected,
                          long entriesSkipped)
  {
    this.entriesRead     = entriesRead;
    this.entriesRejected = entriesRejected;
    this.entriesSkipped  = entriesSkipped;
  }
  /**
   * Retrieves the total number of entries read during the import,
   * including those that were later rejected or skipped.
   *
   * @return  The total number of entries read during the import,
   *          including those that were later rejected or skipped.
   */
  public long getEntriesRead()
  {
    return entriesRead;
  }
  /**
   * Retrieves the total number of entries that were successfully
   * imported.
   *
   * @return  The total number of entries that were successfully
   *          imported.
   */
  public long getEntriesImported()
  {
    return entriesRead - entriesRejected - entriesSkipped;
  }
  /**
   * Retrieves the total number of entries rejected during the import.
   *
   * @return  The total number of entries rejected during the import.
   */
  public long getEntriesRejected()
  {
    return entriesRejected;
  }
  /**
   * Retrieves the total number of entries skipped during the import.
   *
   * @return  The total number of entries skipped during the import.
   */
  public long getEntriesSkipped()
  {
    return entriesSkipped;
  }
  /**
   * Retrieves a string representation of this LDIF import result
   * object.
   *
   * @return  A string representation of this LDIF import result
   *          object.
   */
  public String toString()
  {
    StringBuilder buffer = new StringBuilder();
    toString(buffer);
    return buffer.toString();
  }
  /**
   * Appends a string representation of this LDIF import result object
   * to the provided buffer.
   *
   * @param  buffer  The buffer to which the information should be
   *                 appended.
   */
  public void toString(StringBuilder buffer)
  {
    buffer.append("LDIFImportResult(entriesRead=");
    buffer.append(entriesRead);
    buffer.append(", entriesRejected=");
    buffer.append(entriesRejected);
    buffer.append(", entriesSkipped=");
    buffer.append(entriesSkipped);
    buffer.append(")");
  }
}