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-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS
026 */
027package org.opends.server.tasks;
028
029import static org.opends.messages.BackendMessages.*;
030import static org.opends.server.config.ConfigConstants.*;
031import static org.opends.server.core.DirectoryServer.*;
032import static org.opends.server.util.StaticUtils.*;
033
034import java.util.List;
035
036import org.forgerock.i18n.LocalizableMessage;
037import org.forgerock.i18n.LocalizableMessageBuilder;
038import org.forgerock.i18n.slf4j.LocalizedLogger;
039import org.forgerock.opendj.ldap.ResultCode;
040import org.opends.messages.TaskMessages;
041import org.opends.server.backends.task.Task;
042import org.opends.server.backends.task.TaskState;
043import org.opends.server.replication.plugin.LDAPReplicationDomain;
044import org.opends.server.types.Attribute;
045import org.opends.server.types.AttributeType;
046import org.opends.server.types.DN;
047import org.opends.server.types.DirectoryException;
048import org.opends.server.types.Entry;
049
050/**
051 * This class provides an implementation of a Directory Server task that can
052 * be used to import data from an LDIF file into a backend.
053 */
054public class InitializeTargetTask extends Task
055{
056  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
057
058  // Config properties
059  private String domainString;
060  private LDAPReplicationDomain domain;
061  private int target;
062  private long total;
063
064  /** {@inheritDoc} */
065  @Override
066  public LocalizableMessage getDisplayName() {
067    return TaskMessages.INFO_TASK_INITIALIZE_TARGET_NAME.get();
068  }
069
070  /** {@inheritDoc} */
071  @Override
072  public void initializeTask() throws DirectoryException
073  {
074    if (TaskState.isDone(getTaskState()))
075    {
076      return;
077    }
078
079    // FIXME -- Do we need any special authorization here?
080    Entry taskEntry = getTaskEntry();
081
082    AttributeType typeDomainBase = getAttributeTypeOrDefault(ATTR_TASK_INITIALIZE_TARGET_DOMAIN_DN);
083    AttributeType typeScope = getAttributeTypeOrDefault(ATTR_TASK_INITIALIZE_TARGET_SCOPE);
084
085    List<Attribute> attrList = taskEntry.getAttribute(typeDomainBase);
086    domainString = TaskUtils.getSingleValueString(attrList);
087
088    try
089    {
090      DN dn = DN.valueOf(domainString);
091      // We can assume that this is an LDAP replication domain
092      domain = LDAPReplicationDomain.retrievesReplicationDomain(dn);
093    }
094    catch(DirectoryException e)
095    {
096      LocalizableMessageBuilder mb = new LocalizableMessageBuilder();
097      mb.append(TaskMessages.ERR_TASK_INITIALIZE_INVALID_DN.get());
098      mb.append(" ");
099      mb.append(stackTraceToSingleLineString(e));
100      throw new DirectoryException(ResultCode.INVALID_DN_SYNTAX, e);
101    }
102
103    attrList = taskEntry.getAttribute(typeScope);
104    String targetString = TaskUtils.getSingleValueString(attrList);
105    target = domain.decodeTarget(targetString);
106
107    setTotal(0);
108  }
109
110  /** {@inheritDoc} */
111  @Override
112  protected TaskState runTask()
113  {
114    if (logger.isTraceEnabled())
115    {
116      logger.trace("[IE] InitializeTargetTask is starting on domain: " + domain.getBaseDN());
117    }
118
119    try
120    {
121      domain.initializeRemote(target, this);
122    }
123    catch (DirectoryException e)
124    {
125      logger.traceException(e);
126
127      // This log will go to the task log message
128      logger.error(ERR_TASK_EXECUTE_FAILED, getTaskEntryDN(), stackTraceToSingleLineString(e));
129
130      return TaskState.STOPPED_BY_ERROR;
131    }
132    return TaskState.COMPLETED_SUCCESSFULLY;
133  }
134
135  /**
136   * Set the total number of entries expected to be exported.
137   * @param total The total number of entries.
138   * @throws DirectoryException when a problem occurs
139   */
140  public void setTotal(long total) throws DirectoryException
141  {
142    this.total = total;
143    replaceAttributeValue(ATTR_TASK_INITIALIZE_LEFT, String.valueOf(total));
144    replaceAttributeValue(ATTR_TASK_INITIALIZE_DONE, String.valueOf(0));
145  }
146
147  /**
148   * Set the total number of entries still to be exported.
149   * @param left The total number of entries to be exported.
150   * @throws DirectoryException when a problem occurs
151   */
152  public void setLeft(long left)  throws DirectoryException
153  {
154    replaceAttributeValue(ATTR_TASK_INITIALIZE_LEFT, String.valueOf(left));
155    replaceAttributeValue(ATTR_TASK_INITIALIZE_DONE,String.valueOf(total-left));
156  }
157}