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