ImportLDIF.java revision d20053afa42700e3ec7a6f6fb474622e8680f4cc
/*
* 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
* 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.
*/
/**
* This program provides a utility that may be used to import the contents of an
* LDIF file into a Directory Server backend. This will be a process that is
* intended to run separate from Directory Server and not internally within the
* server process (e.g., via the tasks interface).
*/
public class ImportLDIF
{
/**
* The buffer size that should be used when reading data from LDIF.
*/
public static final int LDIF_BUFFER_SIZE = 1048576;
/**
* The main method for ImportLDIF tool.
*
* @param args The command-line arguments provided to this program.
*/
{
if(retCode != 0)
{
}
}
/**
* Processes the command-line arguments and invokes the import process.
*
* @param args The command-line arguments provided to thisprogram.
*
* @return The error code.
*/
{
return mainImportLDIF(args, true);
}
/**
* Processes the command-line arguments and invokes the import process.
*
* @param args The command-line arguments provided to this
* program.
* @param initializeServer Indicates whether to initialize the server.
*
* @return The error code.
*/
{
// FIXME -- Need to add a mechanism for verifying the file signature.
// Define the command-line arguments that may be used with this program.
// Create the command-line argument parser for use with this program.
new ArgumentParser("org.opends.server.tools.ImportLDIF",
toolDescription, false);
// Initialize all the command-line argument types and register them with the
// parser.
try
{
true, "{configClass}",
configClass.setHidden(true);
configFile.setHidden(true);
append =
}
catch (ArgumentException ae)
{
return 1;
}
// Parse the command-line arguments provided to this program.
try
{
}
catch (ArgumentException ae)
{
return 1;
}
// If we should just display usage information, then print it and exit.
if (argParser.usageDisplayed())
{
return 0;
}
// Make sure that either the "ldifFile" argument or the "templateFile"
// argument was provided, but not both.
{
if (templateFile.isPresent())
{
return 1;
}
}
else if (! templateFile.isPresent())
{
return 1;
}
// Perform the initial bootstrap of the Directory Server and process the
// configuration.
if (initializeServer)
{
try
{
}
catch (Exception e)
{
return 1;
}
try
{
configFile.getValue());
}
catch (InitializationException ie)
{
return 1;
}
catch (Exception e)
{
return 1;
}
// Initialize the Directory Server schema elements.
try
{
}
catch (ConfigException ce)
{
return 1;
}
catch (InitializationException ie)
{
return 1;
}
catch (Exception e)
{
return 1;
}
// Initialize the Directory Server core configuration.
try
{
}
catch (ConfigException ce)
{
return 1;
}
catch (InitializationException ie)
{
return 1;
}
catch (Exception e)
{
return 1;
}
// Initialize the Directory Server crypto manager.
try
{
}
catch (ConfigException ce)
{
return 1;
}
catch (InitializationException ie)
{
return 1;
}
catch (Exception e)
{
return 1;
}
{
// FIXME -- Install a custom logger to capture information about the
// state of the import.
}
// Initialize all the password policy information.
try
{
}
catch (ConfigException ce)
{
return 1;
}
catch (InitializationException ie)
{
return 1;
}
catch (Exception e)
{
return 1;
}
// Make sure that the Directory Server plugin initialization is performed.
try
{
}
catch (ConfigException ce)
{
return 1;
}
catch (InitializationException ie)
{
return 1;
}
catch (Exception e)
{
return 1;
}
}
// filters. If so, then process them.
boolean excludeAllUserAttributes = false;
boolean excludeAllOperationalAttributes = false;
if (excludeAttributeStrings == null)
{
}
else
{
{
{
excludeAllUserAttributes = true;
}
{
excludeAllOperationalAttributes = true;
}
else
{
{
}
}
}
}
boolean includeAllUserAttributes = false;
boolean includeAllOperationalAttributes = false;
if (includeAttributeStrings == null)
{
}
else
{
{
{
includeAllUserAttributes = true;
}
{
includeAllOperationalAttributes = true;
}
else
{
{
}
}
}
}
if (excludeFilterStrings == null)
{
}
else
{
{
try
{
}
catch (DirectoryException de)
{
de.getErrorMessage());
return 1;
}
catch (Exception e)
{
return 1;
}
}
}
if (includeFilterStrings == null)
{
}
else
{
{
try
{
}
catch (DirectoryException de)
{
de.getErrorMessage());
return 1;
}
catch (Exception e)
{
return 1;
}
}
}
// Get information about the backends defined in the server. Iterate
// through them, finding the one backend into which the LDIF should be
// imported and finding backends with subordinate base DNs that should be
// excluded from the import.
if (code != 0)
{
return code;
}
for (int i=0; i < numBackends; i++)
{
{
continue;
}
{
backend = b;
}
else
{
return 1;
}
}
{
msgID);
return 1;
}
else if (! backend.supportsLDIFImport())
{
msgID);
return 1;
}
{
{
{
{
{
}
break;
}
}
}
}
{
try
{
}
catch (DirectoryException de)
{
return 1;
}
catch (Exception e)
{
return 1;
}
{
}
}
if (! includeBranchStrings.isPresent())
{
}
else
{
{
try
{
}
catch (DirectoryException de)
{
return 1;
}
catch (Exception e)
{
return 1;
}
{
return 1;
}
}
}
// See if the data should be read from LDIF files or generated via MakeLDIF.
{
}
else
{
if (randomSeed.isPresent())
{
try
{
}
catch (Exception e)
{
}
}
else
{
}
try
{
}
catch (Exception e)
{
e.getMessage());
return 1;
}
}
// Create the LDIF import configuration to use when reading the LDIF.
// FIXME -- Should this be conditional?
if (rejectFile != null)
{
try
{
if (overwriteRejects.isPresent())
{
}
else
{
}
}
catch (Exception e)
{
return 1;
}
}
// Get the set of base DNs for the backend as an array.
// Acquire an exclusive lock for the backend.
try
{
{
return 1;
}
}
catch (Exception e)
{
return 1;
}
// Launch the import.
int retCode = 0;
try
{
}
catch (DirectoryException de)
{
msgID);
retCode = 1;
}
catch (Exception e)
{
msgID);
retCode = 1;
}
// Release the exclusive lock on the backend.
try
{
{
retCode = 1;
}
}
catch (Exception e)
{
retCode = 1;
}
// Clean up after the import by closing the import config.
return retCode;
}
/**
* Retrieves information about the backends defined in the Directory Server
* configuration.
*
* @param backendList A list into which instantiated (but not initialized)
* backend instances will be placed.
* @param entryList A list into which the config entries associated with
* the backends will be placed.
* @param dnList A list into which the set of base DNs for each backend
* will be placed.
*
* @return 0 if everything went fine. 1 if an error occurred.
*
*/
{
// Get the base entry for all backend configuration.
try
{
}
catch (DirectoryException de)
{
msgID);
return 1;
}
catch (Exception e)
{
msgID);
return 1;
}
try
{
}
catch (ConfigException ce)
{
msgID);
return 1;
}
catch (Exception e)
{
msgID);
return 1;
}
// Iterate through the immediate children, attempting to parse them as
// backends.
{
// Get the backend ID attribute from the entry. If there isn't one, then
// skip the entry.
try
{
true, false, true);
{
continue;
}
else
{
}
}
catch (ConfigException ce)
{
ce.getMessage());
return 1;
}
catch (Exception e)
{
return 1;
}
// Get the backend class name attribute from the entry. If there isn't
// one, then just skip the entry.
try
{
true, false, false);
{
continue;
}
else
{
}
}
catch (ConfigException ce)
{
ce.getMessage());
return 1;
}
catch (Exception e)
{
return 1;
}
try
{
}
catch (Exception e)
{
return 1;
}
try
{
}
catch (Exception e)
{
return 1;
}
// Get the base DN attribute from the entry. If there isn't one, then
// just skip this entry.
try
{
true, true, true);
if (baseDNAttr == null)
{
}
else
{
}
}
catch (Exception e)
{
return 1;
}
}
return 0;
}
}