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 static org.opends.server.config.ConfigConstants.*;
029 import static org.opends.server.core.DirectoryServer.getAttributeType;
030
031 import java.util.List;
032
033 import org.opends.messages.MessageBuilder;
034 import org.opends.messages.TaskMessages;
035 import org.opends.messages.Message;
036 import org.opends.server.backends.task.Task;
037 import org.opends.server.backends.task.TaskState;
038 import static org.opends.server.loggers.debug.DebugLogger.*;
039 import org.opends.server.loggers.debug.DebugTracer;
040 import org.opends.server.replication.plugin.ReplicationDomain;
041 import org.opends.server.types.Attribute;
042 import org.opends.server.types.AttributeType;
043 import org.opends.server.types.DN;
044 import org.opends.server.types.DirectoryException;
045 import org.opends.server.types.Entry;
046 import org.opends.server.types.ResultCode;
047
048 /**
049 * This class provides an implementation of a Directory Server task that can
050 * be used to import data over the replication protocol from another
051 * server hosting the same replication domain.
052 */
053 public class SetGenerationIdTask extends Task
054 {
055 /**
056 * The tracer object for the debug logger.
057 */
058 private static final DebugTracer TRACER = getTracer();
059
060 boolean isCompressed = false;
061 boolean isEncrypted = false;
062 boolean skipSchemaValidation = false;
063 String domainString = null;
064 ReplicationDomain domain = null;
065 TaskState initState;
066 Long generationId = null;
067
068 private static final void debugInfo(String s)
069 {
070 if (debugEnabled())
071 {
072 // System.out.println(Message.raw(Category.SYNC, Severity.NOTICE, s));
073 TRACER.debugInfo(s);
074 }
075 }
076
077 /**
078 * {@inheritDoc}
079 */
080 public Message getDisplayName() {
081 return TaskMessages.INFO_TASK_SET_GENERATION_ID_NAME.get();
082 }
083
084 /**
085 * {@inheritDoc}
086 */
087 @Override public void initializeTask() throws DirectoryException
088 {
089 List<Attribute> attrList;
090
091 if (TaskState.isDone(getTaskState()))
092 {
093 return;
094 }
095
096 // FIXME -- Do we need any special authorization here?
097 Entry taskEntry = getTaskEntry();
098
099 // Retrieves the eventual generation-ID
100 AttributeType typeNewValue;
101 typeNewValue =
102 getAttributeType(ATTR_TASK_SET_GENERATION_ID_NEW_VALUE, true);
103 attrList = taskEntry.getAttribute(typeNewValue);
104 if ((attrList != null) && !attrList.isEmpty())
105 {
106 try
107 {
108 generationId = new Long(TaskUtils.getSingleValueString(attrList));
109 }
110 catch(Exception e)
111 {
112 MessageBuilder mb = new MessageBuilder();
113 mb.append(TaskMessages.ERR_TASK_INITIALIZE_INVALID_GENERATION_ID.get());
114 mb.append(e.getMessage());
115 throw new DirectoryException(ResultCode.CLIENT_SIDE_PARAM_ERROR,
116 mb.toMessage());
117 }
118 }
119
120 // Retrieves the replication domain
121 AttributeType typeDomainBase;
122 typeDomainBase =
123 getAttributeType(ATTR_TASK_SET_GENERATION_ID_DOMAIN_DN, true);
124
125 attrList = taskEntry.getAttribute(typeDomainBase);
126 domainString = TaskUtils.getSingleValueString(attrList);
127 DN domainDN = DN.nullDN();
128 try
129 {
130 domainDN = DN.decode(domainString);
131 }
132 catch(Exception e)
133 {
134 MessageBuilder mb = new MessageBuilder();
135 mb.append(TaskMessages.ERR_TASK_INITIALIZE_INVALID_DN.get());
136 mb.append(e.getMessage());
137 throw new DirectoryException(ResultCode.INVALID_DN_SYNTAX,
138 mb.toMessage());
139 }
140
141 domain = ReplicationDomain.retrievesReplicationDomain(domainDN);
142
143 }
144
145 /**
146 * {@inheritDoc}
147 */
148 protected TaskState runTask()
149 {
150 debugInfo("setGenerationIdTask is starting on domain%s" +
151 domain.getBaseDN());
152
153 domain.resetGenerationId(generationId);
154
155 debugInfo("setGenerationIdTask is ending SUCCESSFULLY");
156 return TaskState.COMPLETED_SUCCESSFULLY;
157 }
158 }