001/*
002 * Copyright 2016-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2016-2018 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.sdk.unboundidds.tasks;
022
023
024
025import java.util.ArrayList;
026import java.util.Arrays;
027import java.util.Collection;
028import java.util.Collections;
029import java.util.Date;
030import java.util.LinkedHashMap;
031import java.util.List;
032import java.util.Map;
033
034import com.unboundid.ldap.sdk.Attribute;
035import com.unboundid.ldap.sdk.Entry;
036import com.unboundid.util.NotMutable;
037import com.unboundid.util.StaticUtils;
038import com.unboundid.util.ThreadSafety;
039import com.unboundid.util.ThreadSafetyLevel;
040
041import static com.unboundid.ldap.sdk.unboundidds.tasks.TaskMessages.*;
042
043
044
045/**
046 * This class defines a Directory Server task that can be used to trigger the
047 * rotation of one or more log files.
048 * <BR>
049 * <BLOCKQUOTE>
050 *   <B>NOTE:</B>  This class, and other classes within the
051 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
052 *   supported for use against Ping Identity, UnboundID, and Alcatel-Lucent 8661
053 *   server products.  These classes provide support for proprietary
054 *   functionality or for external specifications that are not considered stable
055 *   or mature enough to be guaranteed to work in an interoperable way with
056 *   other types of LDAP servers.
057 * </BLOCKQUOTE>
058 * <BR>
059 * The properties that are available for use with this type of task include:
060 * <UL>
061 *   <LI>The path to the log file to be rotated.  It may be either an absolute
062 *       path or a path that is relative to the server root.  Multiple log files
063 *       may be targeted by specifying multiple paths, and if no paths are given
064 *       then the server will rotate all log files.</LI>
065 * </UL>
066 */
067@NotMutable()
068@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
069public final class RotateLogTask
070       extends Task
071{
072  /**
073   * The fully-qualified name of the Java class that is used for the rotate log
074   * task.
075   */
076  static final String ROTATE_LOG_TASK_CLASS =
077       "com.unboundid.directory.server.tasks.RotateLogTask";
078
079
080
081  /**
082   * The name of the attribute used to specify the path to a log file to rotate.
083   */
084  private static final String ATTR_PATH = "ds-task-rotate-log-path";
085
086
087
088  /**
089   * The name of the object class used in rotate log task entries.
090   */
091  private static final String OC_ROTATE_LOG_TASK = "ds-task-rotate-log";
092
093
094
095  /**
096   * The task property that will be used for the log file path.
097   */
098  private static final TaskProperty PROPERTY_PATH = new TaskProperty(ATTR_PATH,
099       INFO_ROTATE_LOG_DISPLAY_NAME_PATH.get(),
100       INFO_ROTATE_LOG_DESCRIPTION_PATH.get(), String.class, false, true,
101       false);
102
103
104
105  /**
106   * The serial version UID for this serializable class.
107   */
108  private static final long serialVersionUID = -7737121245254808139L;
109
110
111
112  // The paths of the log files to rotate.
113  private final List<String> paths;
114
115
116
117  /**
118   * Creates a new uninitialized rotate log task instance that should only be
119   * used for obtaining general information about this task, including the task
120   * name, description, and supported properties.  Attempts to use a task
121   * created with this constructor for any other reason will likely fail.
122   */
123  public RotateLogTask()
124  {
125    paths = null;
126  }
127
128
129
130  /**
131   * Creates a new rotate log task with the provided information.
132   *
133   * @param  taskID  The task ID to use for this task.  If it is {@code null}
134   *                 then a UUID will be generated for use as the task ID.
135   * @param  paths   The paths (on the server filesystem) of the log files to
136   *                 rotate.  The paths may be either absolute or relative to
137   *                 the server root.  This may be {@code null} or empty if the
138   *                 server should rotate all appropriate log files.
139   */
140  public RotateLogTask(final String taskID, final String... paths)
141  {
142    this(taskID, null, null, null, null, null, paths);
143  }
144
145
146
147  /**
148   * Creates a new rotate log task with the provided information.
149   *
150   * @param  taskID  The task ID to use for this task.  If it is {@code null}
151   *                 then a UUID will be generated for use as the task ID.
152   * @param  paths   The paths (on the server filesystem) of the log files to
153   *                 rotate.  The paths may be either absolute or relative to
154   *                 the server root.  This may be {@code null} or empty if the
155   *                 server should rotate all appropriate log files.
156   */
157  public RotateLogTask(final String taskID, final Collection<String> paths)
158  {
159    this(taskID, null, null, null, null, null, paths);
160  }
161
162
163
164  /**
165   * Creates a new rotate log task with the provided information.
166   *
167   * @param  taskID                  The task ID to use for this task.  If it is
168   *                                 {@code null} then a UUID will be generated
169   *                                 for use as the task ID.
170   * @param  scheduledStartTime      The time that this task should start
171   *                                 running.
172   * @param  dependencyIDs           The list of task IDs that will be required
173   *                                 to complete before this task will be
174   *                                 eligible to start.
175   * @param  failedDependencyAction  Indicates what action should be taken if
176   *                                 any of the dependencies for this task do
177   *                                 not complete successfully.
178   * @param  notifyOnCompletion      The list of e-mail addresses of individuals
179   *                                 that should be notified when this task
180   *                                 completes.
181   * @param  notifyOnError           The list of e-mail addresses of individuals
182   *                                 that should be notified if this task does
183   *                                 not complete successfully.
184   * @param  paths                   The paths (on the server filesystem) of the
185   *                                 log files to rotate.  The paths may be
186   *                                 either absolute or relative to the server
187   *                                 root.  This may be {@code null} or empty if
188   *                                 the server should rotate all appropriate
189   *                                 log files.
190   */
191  public RotateLogTask(final String taskID, final Date scheduledStartTime,
192                       final List<String> dependencyIDs,
193                       final FailedDependencyAction failedDependencyAction,
194                       final List<String> notifyOnCompletion,
195                          final List<String> notifyOnError,
196                          final String... paths)
197  {
198    this(taskID, scheduledStartTime, dependencyIDs, failedDependencyAction,
199         notifyOnCompletion, notifyOnError, StaticUtils.toList(paths));
200  }
201
202
203
204  /**
205   * Creates a new rotate log task with the provided information.
206   *
207   * @param  taskID                  The task ID to use for this task.  If it is
208   *                                 {@code null} then a UUID will be generated
209   *                                 for use as the task ID.
210   * @param  scheduledStartTime      The time that this task should start
211   *                                 running.
212   * @param  dependencyIDs           The list of task IDs that will be required
213   *                                 to complete before this task will be
214   *                                 eligible to start.
215   * @param  failedDependencyAction  Indicates what action should be taken if
216   *                                 any of the dependencies for this task do
217   *                                 not complete successfully.
218   * @param  notifyOnCompletion      The list of e-mail addresses of individuals
219   *                                 that should be notified when this task
220   *                                 completes.
221   * @param  notifyOnError           The list of e-mail addresses of individuals
222   *                                 that should be notified if this task does
223   *                                 not complete successfully.
224   * @param  paths                   The paths (on the server filesystem) of the
225   *                                 log files to rotate.  The paths may be
226   *                                 either absolute or relative to the server
227   *                                 root.  This may be {@code null} or empty if
228   *                                 the server should rotate all appropriate
229   *                                 log files.
230   */
231  public RotateLogTask(final String taskID, final Date scheduledStartTime,
232                       final List<String> dependencyIDs,
233                       final FailedDependencyAction failedDependencyAction,
234                       final List<String> notifyOnCompletion,
235                       final List<String> notifyOnError,
236                       final Collection<String> paths)
237  {
238    super(taskID, ROTATE_LOG_TASK_CLASS, scheduledStartTime, dependencyIDs,
239         failedDependencyAction, notifyOnCompletion, notifyOnError);
240
241    if (paths == null)
242    {
243      this.paths = Collections.emptyList();
244    }
245    else
246    {
247      this.paths = Collections.unmodifiableList(new ArrayList<String>(paths));
248    }
249  }
250
251
252
253  /**
254   * Creates a new rotate log task from the provided entry.
255   *
256   * @param  entry  The entry to use to create this rotate log task.
257   *
258   * @throws  TaskException  If the provided entry cannot be parsed as a rotate
259   *                         log task entry.
260   */
261  public RotateLogTask(final Entry entry)
262         throws TaskException
263  {
264    super(entry);
265
266    // Get the log file paths, if present.
267    final String[] pathValues = entry.getAttributeValues(ATTR_PATH);
268    if (pathValues == null)
269    {
270      paths = Collections.emptyList();
271    }
272    else
273    {
274      paths = Collections.unmodifiableList(new ArrayList<String>(
275           Arrays.asList(pathValues)));
276    }
277  }
278
279
280
281  /**
282   * Creates a new rotate log task from the provided set of task properties.
283   *
284   * @param  properties  The set of task properties and their corresponding
285   *                     values to use for the task.  It must not be
286   *                     {@code null}.
287   *
288   * @throws  TaskException  If the provided set of properties cannot be used to
289   *                         create a valid rotate log task.
290   */
291  public RotateLogTask(final Map<TaskProperty,List<Object>> properties)
292         throws TaskException
293  {
294    super(ROTATE_LOG_TASK_CLASS, properties);
295
296    String[] pathArray = StaticUtils.NO_STRINGS;
297    for (final Map.Entry<TaskProperty,List<Object>> entry :
298         properties.entrySet())
299    {
300      final TaskProperty p = entry.getKey();
301      final String attrName = p.getAttributeName();
302      final List<Object> values = entry.getValue();
303
304      if (attrName.equalsIgnoreCase(ATTR_PATH))
305      {
306        pathArray = parseStrings(p, values, pathArray);
307      }
308    }
309
310    paths = Collections.unmodifiableList(Arrays.asList(pathArray));
311  }
312
313
314
315  /**
316   * {@inheritDoc}
317   */
318  @Override()
319  public String getTaskName()
320  {
321    return INFO_TASK_NAME_ROTATE_LOG.get();
322  }
323
324
325
326  /**
327   * {@inheritDoc}
328   */
329  @Override()
330  public String getTaskDescription()
331  {
332    return INFO_TASK_DESCRIPTION_ROTATE_LOG.get();
333  }
334
335
336
337  /**
338   * Retrieves the paths of the log files to rotate.  The paths may be
339   * absolute or relative to the server root.
340   *
341   * @return  The paths of the log files to rotate, or an empty list if no
342   *          paths were specified and the server should rotate the log files
343   *          for all applicable loggers.
344   */
345  public List<String> getPaths()
346  {
347    return paths;
348  }
349
350
351
352  /**
353   * {@inheritDoc}
354   */
355  @Override()
356  protected List<String> getAdditionalObjectClasses()
357  {
358    return Arrays.asList(OC_ROTATE_LOG_TASK);
359  }
360
361
362
363  /**
364   * {@inheritDoc}
365   */
366  @Override()
367  protected List<Attribute> getAdditionalAttributes()
368  {
369    if (paths.isEmpty())
370    {
371      return Collections.emptyList();
372    }
373    else
374    {
375      return Collections.singletonList(new Attribute(ATTR_PATH, paths));
376    }
377  }
378
379
380
381  /**
382   * {@inheritDoc}
383   */
384  @Override()
385  public List<TaskProperty> getTaskSpecificProperties()
386  {
387    return Collections.singletonList(PROPERTY_PATH);
388  }
389
390
391
392  /**
393   * {@inheritDoc}
394   */
395  @Override()
396  public Map<TaskProperty,List<Object>> getTaskPropertyValues()
397  {
398    final LinkedHashMap<TaskProperty,List<Object>> props =
399         new LinkedHashMap<TaskProperty,List<Object>>(10);
400
401
402    if (! paths.isEmpty())
403    {
404      props.put(PROPERTY_PATH, Collections.<Object>unmodifiableList(paths));
405    }
406
407    props.putAll(super.getTaskPropertyValues());
408    return Collections.unmodifiableMap(props);
409  }
410}