*/
protected long exportBackend(OutputStream output, boolean checksumOutput)
throws DirectoryException
{
long genID = 0;
Backend backend = retrievesBackend(this.baseDn);
long numberOfEntries = backend.numSubordinates(baseDn, true) + 1;
long entryCount = ( (numberOfEntries < 1000 )? numberOfEntries : 1000);
// 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);
throw new DirectoryException(
ResultCode.OTHER, message, null);
}
}
catch (Exception e)
{
Message message =
ERR_LDIFEXPORT_CANNOT_LOCK_BACKEND.get(
backend.getBackendID(), e.getLocalizedMessage());
logError(message);
throw new DirectoryException(
ResultCode.OTHER, message, null);
}
OutputStream os = null;
ReplLDIFOutputStream ros = null;
if (checksumOutput)
{
os = (OutputStream)new ReplLDIFOutputStream(entryCount);
ros = (ReplLDIFOutputStream)os;
try
{
os.write((Long.toString(numberOfEntries)).
getBytes());
}
catch(Exception e)
{
// Should never happen
}
}
else
{
os = output;
}
LDIFExportConfig exportConfig = new LDIFExportConfig(os);
// baseDn branch is the only one included in the export
List<DN> includeBranches = new ArrayList<DN>(1);
includeBranches.add(this.baseDn);
exportConfig.setIncludeBranches(includeBranches);
// For the checksum computing mode, only consider the 'stable' attributes
if (checksumOutput)
{
String includeAttributeStrings[] =
{"objectclass", "sn", "cn", "entryuuid"};
HashSet<AttributeType> includeAttributes;
includeAttributes = new HashSet<AttributeType>();
for (String attrName : includeAttributeStrings)
{
AttributeType attrType = DirectoryServer.getAttributeType(attrName);
if (attrType == null)
{
attrType = DirectoryServer.getDefaultAttributeType(attrName);
}
includeAttributes.add(attrType);
}
exportConfig.setIncludeAttributes(includeAttributes);
}
// Launch the export.
try
{
backend.exportLDIF(exportConfig);
}
catch (DirectoryException de)
{
if ((ros != null) &&
(ros.getNumExportedEntries() >= entryCount))
{
// This is the normal end when computing the generationId
// We can interrupt the export only by an IOException
}
else
{
Message message =
ERR_LDIFEXPORT_ERROR_DURING_EXPORT.get(de.getMessageObject());
logError(message);
throw new DirectoryException(
ResultCode.OTHER, message, null);
}
}
catch (Exception e)
{
Message message = ERR_LDIFEXPORT_ERROR_DURING_EXPORT.get(
stackTraceToSingleLineString(e));
logError(message);
throw new DirectoryException(
ResultCode.OTHER, message, null);
}
finally
{
// Clean up after the export by closing the export config.
// Will also flush the export and export the remaining entries.
exportConfig.close();
if (checksumOutput)
{
genID = ros.getChecksumValue();
}
// 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);
throw new DirectoryException(
ResultCode.OTHER, message, null);
}
}
catch (Exception e)
{
Message message = WARN_LDIFEXPORT_CANNOT_UNLOCK_BACKEND.get(
backend.getBackendID(), stackTraceToSingleLineString(e));
logError(message);
throw new DirectoryException(
ResultCode.OTHER, message, null);
}
}