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