{
rebuildConfig.addRebuildIndex(index);
}
Backend backend =
DirectoryServer.getBackendWithBaseDN(rebuildConfig.getBaseDN());
if(backend == null)
{
Message message = ERR_NO_BACKENDS_FOR_BASE.get(baseDN);
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
if (!(backend instanceof BackendImpl))
{
Message message = ERR_REBUILDINDEX_WRONG_BACKEND_TYPE.get();
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
// Acquire a shared lock for the backend if we are rebuilding attribute
// indexes only. If we are rebuilding one or more system indexes, we have
// to aquire exclusive lock.
String lockFile = LockFileManager.getBackendLockFileName(backend);
StringBuilder failureReason = new StringBuilder();
// Disable the backend.
try
{
TaskUtils.disableBackend(backend.getBackendID());
}
catch (DirectoryException e)
{
if (debugEnabled())
{
TRACER.debugCaught(DebugLogLevel.ERROR, e);
}
logError(e.getMessageObject());
return TaskState.STOPPED_BY_ERROR;
}
try
{
if(! LockFileManager.acquireExclusiveLock(lockFile, failureReason))
{
Message message = ERR_REBUILDINDEX_CANNOT_EXCLUSIVE_LOCK_BACKEND.get(
backend.getBackendID(), String.valueOf(failureReason));
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
}
catch (Exception e)
{
Message message = ERR_REBUILDINDEX_CANNOT_EXCLUSIVE_LOCK_BACKEND.get(
backend.getBackendID(), getExceptionMessage(e));
logError(message);
return TaskState.STOPPED_BY_ERROR;
}
if(tmpDirectory == null)
{
tmpDirectory = "import-tmp";
}
rebuildConfig.setTmpDirectory(tmpDirectory);
rebuildConfig.setRebuildAll(rebuildAll);
TaskState returnCode = TaskState.COMPLETED_SUCCESSFULLY;
// Launch the rebuild process.
try
{
BackendImpl jebBackend = (BackendImpl)backend;
jebBackend.rebuildBackend(rebuildConfig);
}
catch (Exception e)
{
if (debugEnabled())
{
TRACER.debugCaught(DebugLogLevel.ERROR, e);
}
Message message =
ERR_REBUILDINDEX_ERROR_DURING_REBUILD.get(e.getMessage());
logError(message);
returnCode = TaskState.STOPPED_BY_ERROR;
}
// Release the lock on the backend.
finally
{
try
{
lockFile = LockFileManager.getBackendLockFileName(backend);
failureReason = new StringBuilder();
if (! LockFileManager.releaseLock(lockFile, failureReason))
{
Message message = WARN_REBUILDINDEX_CANNOT_UNLOCK_BACKEND.get(
backend.getBackendID(), String.valueOf(failureReason));
logError(message);
returnCode = TaskState.COMPLETED_WITH_ERRORS;
}
}
catch (Throwable t)
{
Message message = WARN_REBUILDINDEX_CANNOT_UNLOCK_BACKEND.get(
backend.getBackendID(), getExceptionMessage(t));
logError(message);
returnCode = TaskState.COMPLETED_WITH_ERRORS;
}
}
if(returnCode == TaskState.COMPLETED_SUCCESSFULLY)
{
// Enable the backend.
try
{
TaskUtils.enableBackend(backend.getBackendID());
}
catch (DirectoryException e)
{
if (debugEnabled())
{