}
}
}
// Get the backend into which the LDIF should be imported.
Backend backend;
ArrayList<DN> defaultIncludeBranches;
backend = DirectoryServer.getBackend(backendID);
if (backend == null)
{
Message message = ERR_LDIFEXPORT_NO_BACKENDS_FOR_ID.get(backendID);
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
else if (! backend.supportsLDIFExport())
{
Message message = ERR_LDIFEXPORT_CANNOT_EXPORT_BACKEND.get(backendID);
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
defaultIncludeBranches = new ArrayList<DN>(backend.getBaseDNs().length);
for (DN dn : backend.getBaseDNs())
{
defaultIncludeBranches.add(dn);
}
ArrayList<DN> excludeBranches = new ArrayList<DN>();
if (excludeBranchStrings != null)
{
for (String s : excludeBranchStrings)
{
DN excludeBranch;
try
{
excludeBranch = DN.decode(s);
}
catch (DirectoryException de)
{
Message message = ERR_LDIFEXPORT_CANNOT_DECODE_EXCLUDE_BASE.get(
s, de.getMessageObject());
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
catch (Exception e)
{
Message message = ERR_LDIFEXPORT_CANNOT_DECODE_EXCLUDE_BASE.get(
s, getExceptionMessage(e));
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
if (! excludeBranches.contains(excludeBranch))
{
excludeBranches.add(excludeBranch);
}
}
}
ArrayList<DN> includeBranches;
if (!includeBranchStrings.isEmpty())
{
includeBranches = new ArrayList<DN>();
for (String s : includeBranchStrings)
{
DN includeBranch;
try
{
includeBranch = DN.decode(s);
}
catch (DirectoryException de)
{
Message message = ERR_LDIFIMPORT_CANNOT_DECODE_INCLUDE_BASE.get(
s, de.getMessageObject());
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
catch (Exception e)
{
Message message = ERR_LDIFIMPORT_CANNOT_DECODE_INCLUDE_BASE.get(
s, getExceptionMessage(e));
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
if (! Backend.handlesEntry(includeBranch, defaultIncludeBranches,
excludeBranches))
{
Message message =
ERR_LDIFEXPORT_INVALID_INCLUDE_BASE.get(s, backendID);
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
includeBranches.add(includeBranch);
}
}
else
{
includeBranches = defaultIncludeBranches;
}
// Create the LDIF export configuration to use when reading the LDIF.
ExistingFileBehavior existingBehavior;
if (appendToLDIF)
{
existingBehavior = ExistingFileBehavior.APPEND;
}
else
{
existingBehavior = ExistingFileBehavior.OVERWRITE;
}
exportConfig = new LDIFExportConfig(ldifFile, existingBehavior);
exportConfig.setCompressData(compressLDIF);
exportConfig.setEncryptData(encryptLDIF);
exportConfig.setExcludeAttributes(excludeAttributes);
exportConfig.setExcludeBranches(excludeBranches);
exportConfig.setExcludeFilters(excludeFilters);
exportConfig.setIncludeAttributes(includeAttributes);
exportConfig.setIncludeBranches(includeBranches);
exportConfig.setIncludeFilters(includeFilters);
exportConfig.setSignHash(signHash);
exportConfig.setWrapColumn(wrapColumn);
exportConfig.setIncludeOperationalAttributes(includeOperationalAttributes);
// FIXME -- Should this be conditional?
exportConfig.setInvokeExportPlugins(true);
// Get the set of base DNs for the backend as an array.
DN[] baseDNs = new DN[defaultIncludeBranches.size()];
defaultIncludeBranches.toArray(baseDNs);
// From here we must make sure we close the export config.
try
{
// Acquire a shared lock for the backend.
try
{
String lockFile = LockFileManager.getBackendLockFileName(backend);
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.acquireSharedLock(lockFile, failureReason))
{
Message message = ERR_LDIFEXPORT_CANNOT_LOCK_BACKEND.get(
backend.getBackendID(), String.valueOf(failureReason));
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
}
catch (Exception e)
{
Message message = ERR_LDIFEXPORT_CANNOT_LOCK_BACKEND.get(
backend.getBackendID(), getExceptionMessage(e));
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
// From here we must make sure we release the shared backend lock.
try
{
// Launch the export.
try
{
DirectoryServer.notifyExportBeginning(backend, exportConfig);
addLogMessage(INFO_LDIFEXPORT_PATH_TO_LDIF_FILE.get(ldifFile));
backend.exportLDIF(exportConfig);
DirectoryServer.notifyExportEnded(backend, exportConfig, true);
}
catch (DirectoryException de)
{
DirectoryServer.notifyExportEnded(backend, exportConfig, false);
Message message =
ERR_LDIFEXPORT_ERROR_DURING_EXPORT.get(de.getMessageObject());
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
catch (Exception e)
{
DirectoryServer.notifyExportEnded(backend, exportConfig, false);
Message message =
ERR_LDIFEXPORT_ERROR_DURING_EXPORT.get(getExceptionMessage(e));
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
}
finally
{
// Release the shared lock on the backend.
try
{
String lockFile = LockFileManager.getBackendLockFileName(backend);
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.releaseLock(lockFile, failureReason))
{
Message message = WARN_LDIFEXPORT_CANNOT_UNLOCK_BACKEND.get(
backend.getBackendID(), String.valueOf(failureReason));
logError(message);
return TaskState.COMPLETED_WITH_ERRORS;
}
}
catch (Exception e)
{
Message message = WARN_LDIFEXPORT_CANNOT_UNLOCK_BACKEND.get(
backend.getBackendID(), getExceptionMessage(e));
logError(message);
return TaskState.COMPLETED_WITH_ERRORS;
}
}
}