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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.tasks;
028    import org.opends.messages.MessageBuilder;
029    
030    import static org.opends.server.config.ConfigConstants.*;
031    import static org.opends.server.core.DirectoryServer.getAttributeType;
032    import static org.opends.server.loggers.debug.DebugLogger.*;
033    import org.opends.server.loggers.debug.DebugTracer;
034    
035    import java.util.List;
036    
037    import org.opends.server.backends.task.Task;
038    import org.opends.server.backends.task.TaskState;
039    import org.opends.messages.TaskMessages;
040    import org.opends.messages.Message;
041    import org.opends.server.replication.plugin.ReplicationDomain;
042    import org.opends.server.types.Attribute;
043    import org.opends.server.types.AttributeType;
044    import org.opends.server.types.DN;
045    import org.opends.server.types.DirectoryException;
046    import org.opends.server.types.Entry;
047    
048    
049    import org.opends.server.types.ResultCode;
050    
051    /**
052     * This class provides an implementation of a Directory Server task that can
053     * be used to import data from an LDIF file into a backend.
054     */
055    public class InitializeTargetTask extends Task
056    {
057      /**
058       * The tracer object for the debug logger.
059       */
060      private static final DebugTracer TRACER = getTracer();
061    
062      // Config properties
063      boolean append                  = false;
064      boolean isCompressed            = false;
065      boolean isEncrypted             = false;
066      boolean skipSchemaValidation    = false;
067      String  domainString            = null;
068      ReplicationDomain domain = null;
069      short target;
070      long total;
071      long left;
072    
073      /**
074       * {@inheritDoc}
075       */
076      public Message getDisplayName() {
077        return TaskMessages.INFO_TASK_INITIALIZE_TARGET_NAME.get();
078      }
079    
080      /**
081       * {@inheritDoc}
082       */
083      @Override public void initializeTask() throws DirectoryException
084      {
085        if (TaskState.isDone(getTaskState()))
086        {
087          return;
088        }
089    
090        // FIXME -- Do we need any special authorization here?
091        Entry taskEntry = getTaskEntry();
092    
093        AttributeType typeDomainBase;
094        AttributeType typeScope;
095    
096        typeDomainBase =
097          getAttributeType(ATTR_TASK_INITIALIZE_TARGET_DOMAIN_DN, true);
098        typeScope =
099          getAttributeType(ATTR_TASK_INITIALIZE_TARGET_SCOPE, true);
100    
101        List<Attribute> attrList;
102        attrList = taskEntry.getAttribute(typeDomainBase);
103        domainString = TaskUtils.getSingleValueString(attrList);
104    
105        DN domainDN = DN.nullDN();
106        try
107        {
108          domainDN = DN.decode(domainString);
109        }
110        catch(Exception e)
111        {
112          MessageBuilder mb = new MessageBuilder();
113          mb.append(TaskMessages.ERR_TASK_INITIALIZE_INVALID_DN.get());
114          mb.append(e.getLocalizedMessage());
115          throw new DirectoryException(ResultCode.INVALID_DN_SYNTAX,
116                  mb.toMessage());
117        }
118        domain=ReplicationDomain.retrievesReplicationDomain(domainDN);
119    
120        attrList = taskEntry.getAttribute(typeScope);
121        String targetString = TaskUtils.getSingleValueString(attrList);
122        target = domain.decodeTarget(targetString);
123    
124        setTotal(0);
125      }
126    
127      /**
128       * {@inheritDoc}
129       */
130      protected TaskState runTask()
131      {
132        if (debugEnabled())
133        {
134          TRACER.debugInfo("DebugInfo" + "InitializeTarget Task/runTask ");
135        }
136        try
137        {
138          domain.initializeRemote(target, this);
139        }
140        catch(DirectoryException de)
141        {
142          // This log will go to the task log message
143          MessageBuilder mb = new MessageBuilder();
144          mb.append("Initialize Task stopped by error");
145          mb.append(de.getMessageObject());
146          logError(mb.toMessage());
147    
148          return TaskState.STOPPED_BY_ERROR;
149        }
150        return TaskState.COMPLETED_SUCCESSFULLY;
151      }
152    
153      /**
154       * Set the total number of entries expected to be exported.
155       * @param total The total number of entries.
156       * @throws DirectoryException when a problem occurs
157       */
158      public void setTotal(long total) throws DirectoryException
159      {
160        this.total = total;
161        replaceAttributeValue(ATTR_TASK_INITIALIZE_LEFT,
162            String.valueOf(total));
163        replaceAttributeValue(ATTR_TASK_INITIALIZE_DONE, String.valueOf(0));
164      }
165    
166      /**
167       * Set the total number of entries still to be exported.
168       * @param left The total number of entries to be exported.
169       * @throws DirectoryException when a problem occurs
170       */
171      public void setLeft(long left)  throws DirectoryException
172      {
173        this.left = left;
174        replaceAttributeValue(ATTR_TASK_INITIALIZE_LEFT, String.valueOf(left));
175        replaceAttributeValue(ATTR_TASK_INITIALIZE_DONE,String.valueOf(total-left));
176      }
177    }