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