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 2010 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2016 ForgeRock AS. 016 */ 017package org.opends.server.tools.dsreplication; 018 019import java.util.ArrayList; 020import java.util.LinkedList; 021 022import javax.naming.NamingEnumeration; 023import javax.naming.NamingException; 024import javax.naming.directory.Attribute; 025import javax.naming.directory.BasicAttribute; 026import javax.naming.directory.BasicAttributes; 027 028import org.forgerock.opendj.ldap.ByteString; 029import org.opends.server.admin.client.cli.TaskScheduleArgs; 030import org.opends.server.tools.tasks.TaskClient; 031import org.opends.server.tools.tasks.TaskScheduleUserData; 032import org.opends.server.types.HostPort; 033import org.opends.server.types.RawAttribute; 034 035/** This class is used to store the information provided by the user to purge historical data. */ 036public class PurgeHistoricalUserData extends MonoServerReplicationUserData 037{ 038 private int maximumDuration; 039 private boolean online; 040 private TaskScheduleUserData taskSchedule = new TaskScheduleUserData(); 041 042 /** Default constructor. */ 043 public PurgeHistoricalUserData() 044 { 045 } 046 047 /** 048 * Returns the maximum duration that the purge can take in seconds. 049 * @return the maximum duration that the purge can take in seconds. 050 */ 051 public int getMaximumDuration() 052 { 053 return maximumDuration; 054 } 055 056 /** 057 * Sets the maximum duration that the purge can take in seconds. 058 * @param maximumDuration the maximum duration that the purge can take in 059 * seconds. 060 */ 061 public void setMaximumDuration(int maximumDuration) 062 { 063 this.maximumDuration = maximumDuration; 064 } 065 066 /** 067 * Whether the task will be executed on an online server (using an LDAP 068 * connection and the tasks backend) or not. 069 * @return {@code true} if the task will be executed on an online server 070 * and {@code false} otherwise. 071 */ 072 public boolean isOnline() 073 { 074 return online; 075 } 076 077 /** 078 * Sets whether the task will be executed on an online server or not. 079 * @param online {@code true} if the task will be executed on an online server 080 * and {@code false} otherwise. 081 */ 082 public void setOnline(boolean online) 083 { 084 this.online = online; 085 } 086 087 /** 088 * Returns the object describing the schedule of the task. If the operation 089 * is not online, the value returned by this method should not be taken into 090 * account. 091 * @return the object describing the schedule of the task. 092 */ 093 public TaskScheduleUserData getTaskSchedule() 094 { 095 return taskSchedule; 096 } 097 098 /** 099 * Sets the object describing the schedule of the task. 100 * @param taskSchedule the object describing the schedule of the task. 101 */ 102 public void setTaskSchedule(TaskScheduleUserData taskSchedule) 103 { 104 this.taskSchedule = taskSchedule; 105 } 106 107 /** 108 * Initializes the contents of the provided purge historical replication user 109 * data object with what was provided in the command-line without prompting to 110 * the user. 111 * @param uData the purge historical replication user data object to be 112 * initialized. 113 * @param argParser the argument parser with the arguments provided by the 114 * user. 115 */ 116 public static void initializeWithArgParser(PurgeHistoricalUserData uData, 117 ReplicationCliArgumentParser argParser) 118 { 119 uData.setBaseDNs(new LinkedList<String>(argParser.getBaseDNs())); 120 121 if (argParser.connectionArgumentsPresent()) 122 { 123 uData.setAdminUid(argParser.getAdministratorUIDOrDefault()); 124 uData.setAdminPwd(argParser.getBindPasswordAdmin()); 125 uData.setHostPort(new HostPort( 126 argParser.getHostNameToStatusOrDefault(), argParser.getPortToStatusOrDefault())); 127 uData.setOnline(true); 128 TaskScheduleUserData taskSchedule = new TaskScheduleUserData(); 129 TaskScheduleArgs taskArgs = argParser.getTaskArgsList(); 130 taskSchedule.setStartNow(taskArgs.isStartNow()); 131 if (!taskSchedule.isStartNow()) 132 { 133 taskSchedule.setStartDate(taskArgs.getStartDateTime()); 134 taskSchedule.setDependencyIds(taskArgs.getDependencyIds()); 135 taskSchedule.setFailedDependencyAction( 136 taskArgs.getFailedDependencyAction()); 137 taskSchedule.setNotifyUponErrorEmailAddresses( 138 taskArgs.getNotifyUponErrorEmailAddresses()); 139 taskSchedule.setNotifyUponCompletionEmailAddresses( 140 taskArgs.getNotifyUponCompletionEmailAddresses()); 141 taskSchedule.setRecurringDateTime( 142 taskArgs.getRecurringDateTime()); 143 } 144 uData.setTaskSchedule(taskSchedule); 145 } 146 else 147 { 148 uData.setOnline(false); 149 } 150 151 uData.setMaximumDuration(argParser.getMaximumDurationOrDefault()); 152 } 153 154 /** 155 * Commodity method that returns the list of basic task attributes required 156 * to launch a task corresponding to the provided user data. 157 * @param uData the user data describing the purge historical to be executed. 158 * @return the list of basic task attributes required 159 * to launch a task corresponding to the provided user data. 160 */ 161 public static BasicAttributes getTaskAttributes(PurgeHistoricalUserData uData) 162 { 163 PurgeHistoricalScheduleInformation information = 164 new PurgeHistoricalScheduleInformation(uData); 165 return getAttributes(TaskClient.getTaskAttributes(information)); 166 } 167 168 private static BasicAttributes getAttributes(ArrayList<RawAttribute> rawAttrs) 169 { 170 BasicAttributes attrs = new BasicAttributes(); 171 for (RawAttribute rawAttr : rawAttrs) 172 { 173 BasicAttribute attr = new BasicAttribute(rawAttr.getAttributeType()); 174 for (ByteString v : rawAttr.getValues()) 175 { 176 attr.add(v.toString()); 177 } 178 attrs.put(attr); 179 } 180 return attrs; 181 } 182 183 /** 184 * Returns the DN of the task corresponding to the provided list of 185 * attributes. The code assumes that the attributes have been generated 186 * calling the method {@link #getTaskAttributes(PurgeHistoricalUserData)}. 187 * @param attrs the attributes of the task entry. 188 * @return the DN of the task entry. 189 */ 190 public static String getTaskDN(BasicAttributes attrs) 191 { 192 ArrayList<RawAttribute> rawAttrs = getRawAttributes(attrs); 193 return TaskClient.getTaskDN(rawAttrs); 194 } 195 196 /** 197 * Returns the ID of the task corresponding to the provided list of 198 * attributes. The code assumes that the attributes have been generated 199 * calling the method {@link #getTaskAttributes(PurgeHistoricalUserData)}. 200 * @param attrs the attributes of the task entry. 201 * @return the ID of the task entry. 202 */ 203 public static String getTaskID(BasicAttributes attrs) 204 { 205 ArrayList<RawAttribute> rawAttrs = getRawAttributes(attrs); 206 return TaskClient.getTaskID(rawAttrs); 207 } 208 209 private static ArrayList<RawAttribute> getRawAttributes(BasicAttributes attrs) 210 { 211 try 212 { 213 ArrayList<RawAttribute> rawAttrs = new ArrayList<>(); 214 NamingEnumeration<Attribute> nAtt = attrs.getAll(); 215 while (nAtt.hasMore()) 216 { 217 Attribute attr = nAtt.next(); 218 NamingEnumeration<?> values = attr.getAll(); 219 ArrayList<ByteString> rawValues = new ArrayList<>(); 220 while (values.hasMore()) 221 { 222 Object v = values.next(); 223 rawValues.add(ByteString.valueOfUtf8(v.toString())); 224 } 225 RawAttribute rAttr = RawAttribute.create(attr.getID(), rawValues); 226 rawAttrs.add(rAttr); 227 } 228 return rawAttrs; 229 } 230 catch (NamingException ne) 231 { 232 // This is a bug. 233 throw new RuntimeException("Unexpected error: "+ne, ne); 234 } 235 } 236}