001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2016 ForgeRock AS.
016 */
017package org.opends.server.tasks;
018
019import static org.opends.server.config.ConfigConstants.*;
020import static org.opends.server.core.DirectoryServer.*;
021
022import java.util.List;
023
024import org.forgerock.i18n.LocalizableMessage;
025import org.forgerock.i18n.LocalizableMessageBuilder;
026import org.forgerock.i18n.slf4j.LocalizedLogger;
027import org.forgerock.opendj.ldap.ResultCode;
028import org.opends.messages.TaskMessages;
029import org.opends.server.backends.task.Task;
030import org.opends.server.backends.task.TaskState;
031import org.opends.server.replication.plugin.LDAPReplicationDomain;
032import org.opends.server.replication.service.ReplicationDomain;
033import org.opends.server.types.Attribute;
034import org.forgerock.opendj.ldap.schema.AttributeType;
035import org.forgerock.opendj.ldap.DN;
036import org.opends.server.types.DirectoryException;
037import org.opends.server.types.Entry;
038
039/**
040 * This class provides an implementation of a Directory Server task that can
041 * be used to import data over the replication protocol from another
042 * server hosting the same replication domain.
043 */
044public class SetGenerationIdTask extends Task
045{
046  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
047  private String  domainString;
048  private ReplicationDomain domain;
049  private Long generationId;
050
051  /** {@inheritDoc} */
052  @Override
053  public LocalizableMessage getDisplayName() {
054    return TaskMessages.INFO_TASK_SET_GENERATION_ID_NAME.get();
055  }
056
057  /** {@inheritDoc} */
058  @Override
059  public void initializeTask() throws DirectoryException
060  {
061    if (TaskState.isDone(getTaskState()))
062    {
063      return;
064    }
065
066    // FIXME -- Do we need any special authorization here?
067    Entry taskEntry = getTaskEntry();
068
069    // Retrieves the eventual generation-ID
070    AttributeType typeNewValue = getSchema().getAttributeType(ATTR_TASK_SET_GENERATION_ID_NEW_VALUE);
071    List<Attribute> attrList = taskEntry.getAttribute(typeNewValue);
072    if (!attrList.isEmpty())
073    {
074      try
075      {
076        generationId = Long.parseLong(TaskUtils.getSingleValueString(attrList));
077      }
078      catch(Exception e)
079      {
080        LocalizableMessageBuilder mb = new LocalizableMessageBuilder();
081        mb.append(TaskMessages.ERR_TASK_INITIALIZE_INVALID_GENERATION_ID.get());
082        mb.append(e.getMessage());
083        throw new DirectoryException(ResultCode.CLIENT_SIDE_PARAM_ERROR, mb.toMessage());
084      }
085    }
086
087    // Retrieves the replication domain
088    AttributeType typeDomainBase = getSchema().getAttributeType(ATTR_TASK_SET_GENERATION_ID_DOMAIN_DN);
089    attrList = taskEntry.getAttribute(typeDomainBase);
090    domainString = TaskUtils.getSingleValueString(attrList);
091
092    try
093    {
094      DN dn = DN.valueOf(domainString);
095      domain = LDAPReplicationDomain.retrievesReplicationDomain(dn);
096    }
097    catch(DirectoryException e)
098    {
099      LocalizableMessageBuilder mb = new LocalizableMessageBuilder();
100      mb.append(TaskMessages.ERR_TASK_INITIALIZE_INVALID_DN.get());
101      mb.append(e.getMessage());
102      throw new DirectoryException(ResultCode.INVALID_DN_SYNTAX, e);
103    }
104  }
105
106  /** {@inheritDoc} */
107  @Override
108  protected TaskState runTask()
109  {
110    if (logger.isTraceEnabled())
111    {
112      logger.trace("setGenerationIdTask is starting on domain %s" + domain.getBaseDN());
113    }
114
115    try
116    {
117      domain.resetGenerationId(generationId);
118    }
119    catch(DirectoryException de)
120    {
121      logger.error(de.getMessageObject());
122      return TaskState.STOPPED_BY_ERROR;
123    }
124
125    if (logger.isTraceEnabled())
126    {
127      logger.trace("setGenerationIdTask is ending SUCCESSFULLY");
128    }
129    return TaskState.COMPLETED_SUCCESSFULLY;
130  }
131}