}
}
// 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());
int badFileCount = 0;
for (String pathname : fileList)
{
File f = new File(pathname);
if (!f.canRead())
{
Message message = ERR_LDIFIMPORT_CANNOT_READ_FILE.get(pathname);
logError(message);
badFileCount++;
}
}
if (badFileCount > 0) return 1;
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.getInstanceRoot() + File.separator +
PATH_MAKELDIF_RESOURCE_DIR;
TemplateFile tf = new TemplateFile(resourcePath, random);
ArrayList<Message> warnings = new ArrayList<Message>();
try
{
tf.parse(templateFile.getValue(), warnings);
}
catch (Exception e)
{
Message message = ERR_LDIFIMPORT_CANNOT_PARSE_TEMPLATE_FILE.get(
templateFile.getValue(), e.getMessage());
logError(message);
return 1;
}
importConfig = new LDIFImportConfig(tf);
}
// Create the LDIF import configuration to use when reading the LDIF.
importConfig.setAppendToExistingData(append.isPresent());
importConfig.setReplaceExistingEntries(replaceExisting.isPresent());
importConfig.setCompressed(isCompressed.isPresent());
importConfig.setClearBackend(clearBackend.isPresent());
importConfig.setEncrypted(isEncrypted.isPresent());
importConfig.setExcludeAttributes(excludeAttributes);
importConfig.setExcludeBranches(excludeBranches);
importConfig.setExcludeFilters(excludeFilters);
importConfig.setIncludeAttributes(includeAttributes);
importConfig.setIncludeBranches(includeBranches);
importConfig.setIncludeFilters(includeFilters);
importConfig.setValidateSchema(!skipSchemaValidation.isPresent());
importConfig.setSkipDNValidation(skipDNValidation.isPresent());
importConfig.setTmpDirectory(tmpDirectory.getValue());
try
{
importConfig.setThreadCount(threadCount.getIntValue());
}
catch(Exception e)
{
Message msg = ERR_LDIFIMPORT_CANNOT_PARSE_THREAD_COUNT.get(
threadCount.getValue(), e.getMessage());
logError(msg);
return 1;
}
importConfig.setBufferSize(LDIF_BUFFER_SIZE);
importConfig.setExcludeAllUserAttributes(
excludeAllUserAttributes);
importConfig.setExcludeAllOperationalAttributes(
excludeAllOperationalAttributes);
importConfig.setIncludeAllOpAttributes(
includeAllOperationalAttributes);
importConfig.setIncludeAllUserAttributes(includeAllUserAttributes);
// FIXME -- Should this be conditional?
importConfig.setInvokeImportPlugins(true);
if (rejectFile != null)
{
try
{
ExistingFileBehavior existingBehavior;
if (overwrite.isPresent())
{
existingBehavior = ExistingFileBehavior.OVERWRITE;
}
else
{
existingBehavior = ExistingFileBehavior.APPEND;
}
importConfig.writeRejectedEntries(rejectFile.getValue(),
existingBehavior);
}
catch (Exception e)
{
Message message = ERR_LDIFIMPORT_CANNOT_OPEN_REJECTS_FILE.get(
rejectFile.getValue(), getExceptionMessage(e));
logError(message);
return 1;
}
}
if (skipFile != null)
{
try
{
ExistingFileBehavior existingBehavior;
if (overwrite.isPresent())
{
existingBehavior = ExistingFileBehavior.OVERWRITE;
}
else
{
existingBehavior = ExistingFileBehavior.APPEND;
}
importConfig.writeSkippedEntries(skipFile.getValue(),
existingBehavior);
}
catch (Exception e)
{
Message message = ERR_LDIFIMPORT_CANNOT_OPEN_SKIP_FILE.get(
skipFile.getValue(), getExceptionMessage(e));
logError(message);
return 1;
}
}
// Get the set of base DNs for the backend as an array.
DN[] baseDNs = new DN[defaultIncludeBranches.size()];
defaultIncludeBranches.toArray(baseDNs);
// Acquire an exclusive lock for the backend.
try
{
String lockFile = LockFileManager.getBackendLockFileName(backend);
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.acquireExclusiveLock(lockFile, failureReason))
{
Message message = ERR_LDIFIMPORT_CANNOT_LOCK_BACKEND.get(
backend.getBackendID(), String.valueOf(failureReason));
logError(message);
return 1;
}
}
catch (Exception e)
{
Message message = ERR_LDIFIMPORT_CANNOT_LOCK_BACKEND.get(
backend.getBackendID(), getExceptionMessage(e));
logError(message);
return 1;
}
// Launch the import.
int retCode = 0;
try
{
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)
{
Message message =
ERR_LDIFIMPORT_ERROR_DURING_IMPORT.get(de.getMessageObject());
logError(message);
retCode = 1;
}
catch (Exception e)
{
Message message =
ERR_LDIFIMPORT_ERROR_DURING_IMPORT.get(getExceptionMessage(e));
logError(message);
e.printStackTrace();
retCode = 1;
}
// Release the exclusive lock on the backend.
try
{
String lockFile = LockFileManager.getBackendLockFileName(backend);
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.releaseLock(lockFile, failureReason))
{
Message message = WARN_LDIFIMPORT_CANNOT_UNLOCK_BACKEND.get(
backend.getBackendID(), String.valueOf(failureReason));
logError(message);
retCode = 1;
}
}
catch (Exception e)
{
Message message = WARN_LDIFIMPORT_CANNOT_UNLOCK_BACKEND.get(
backend.getBackendID(), getExceptionMessage(e));
logError(message);
retCode = 1;
}
// Clean up after the import by closing the import config.
importConfig.close();
return retCode;
}