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
053 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
054 *   for proprietary functionality or for external specifications that are not
055 *   considered stable or mature enough to be guaranteed to work in an
056 *   interoperable way with 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    this(taskID, scheduledStartTime, dependencyIDs, failedDependencyAction,
239         null, notifyOnCompletion, null, notifyOnError, null, null, null,
240         paths);
241  }
242
243
244
245  /**
246   * Creates a new rotate log task with the provided information.
247   *
248   * @param  taskID                  The task ID to use for this task.  If it is
249   *                                 {@code null} then a UUID will be generated
250   *                                 for use as the task ID.
251   * @param  scheduledStartTime      The time that this task should start
252   *                                 running.
253   * @param  dependencyIDs           The list of task IDs that will be required
254   *                                 to complete before this task will be
255   *                                 eligible to start.
256   * @param  failedDependencyAction  Indicates what action should be taken if
257   *                                 any of the dependencies for this task do
258   *                                 not complete successfully.
259   * @param  notifyOnStart           The list of e-mail addresses of individuals
260   *                                 that should be notified when this task
261   *                                 starts running.
262   * @param  notifyOnCompletion      The list of e-mail addresses of individuals
263   *                                 that should be notified when this task
264   *                                 completes.
265   * @param  notifyOnSuccess         The list of e-mail addresses of individuals
266   *                                 that should be notified if this task
267   *                                 completes successfully.
268   * @param  notifyOnError           The list of e-mail addresses of individuals
269   *                                 that should be notified if this task does
270   *                                 not complete successfully.
271   * @param  alertOnStart            Indicates whether the server should send an
272   *                                 alert notification when this task starts.
273   * @param  alertOnSuccess          Indicates whether the server should send an
274   *                                 alert notification if this task completes
275   *                                 successfully.
276   * @param  alertOnError            Indicates whether the server should send an
277   *                                 alert notification if this task fails to
278   *                                 complete successfully.
279   * @param  paths                   The paths (on the server filesystem) of the
280   *                                 log files to rotate.  The paths may be
281   *                                 either absolute or relative to the server
282   *                                 root.  This may be {@code null} or empty if
283   *                                 the server should rotate all appropriate
284   *                                 log files.
285   */
286  public RotateLogTask(final String taskID, final Date scheduledStartTime,
287                       final List<String> dependencyIDs,
288                       final FailedDependencyAction failedDependencyAction,
289                       final List<String> notifyOnStart,
290                       final List<String> notifyOnCompletion,
291                       final List<String> notifyOnSuccess,
292                       final List<String> notifyOnError,
293                       final Boolean alertOnStart, final Boolean alertOnSuccess,
294                       final Boolean alertOnError,
295                      final Collection<String> paths)
296  {
297    super(taskID, ROTATE_LOG_TASK_CLASS, scheduledStartTime, dependencyIDs,
298         failedDependencyAction, notifyOnStart, notifyOnCompletion,
299         notifyOnSuccess, notifyOnError, alertOnStart, alertOnSuccess,
300         alertOnError);
301
302    if (paths == null)
303    {
304      this.paths = Collections.emptyList();
305    }
306    else
307    {
308      this.paths = Collections.unmodifiableList(new ArrayList<>(paths));
309    }
310  }
311
312
313
314  /**
315   * Creates a new rotate log task from the provided entry.
316   *
317   * @param  entry  The entry to use to create this rotate log task.
318   *
319   * @throws  TaskException  If the provided entry cannot be parsed as a rotate
320   *                         log task entry.
321   */
322  public RotateLogTask(final Entry entry)
323         throws TaskException
324  {
325    super(entry);
326
327    // Get the log file paths, if present.
328    final String[] pathValues = entry.getAttributeValues(ATTR_PATH);
329    if (pathValues == null)
330    {
331      paths = Collections.emptyList();
332    }
333    else
334    {
335      paths = Collections.unmodifiableList(new ArrayList<>(
336           Arrays.asList(pathValues)));
337    }
338  }
339
340
341
342  /**
343   * Creates a new rotate log task from the provided set of task properties.
344   *
345   * @param  properties  The set of task properties and their corresponding
346   *                     values to use for the task.  It must not be
347   *                     {@code null}.
348   *
349   * @throws  TaskException  If the provided set of properties cannot be used to
350   *                         create a valid rotate log task.
351   */
352  public RotateLogTask(final Map<TaskProperty,List<Object>> properties)
353         throws TaskException
354  {
355    super(ROTATE_LOG_TASK_CLASS, properties);
356
357    String[] pathArray = StaticUtils.NO_STRINGS;
358    for (final Map.Entry<TaskProperty,List<Object>> entry :
359         properties.entrySet())
360    {
361      final TaskProperty p = entry.getKey();
362      final String attrName = p.getAttributeName();
363      final List<Object> values = entry.getValue();
364
365      if (attrName.equalsIgnoreCase(ATTR_PATH))
366      {
367        pathArray = parseStrings(p, values, pathArray);
368      }
369    }
370
371    paths = Collections.unmodifiableList(Arrays.asList(pathArray));
372  }
373
374
375
376  /**
377   * {@inheritDoc}
378   */
379  @Override()
380  public String getTaskName()
381  {
382    return INFO_TASK_NAME_ROTATE_LOG.get();
383  }
384
385
386
387  /**
388   * {@inheritDoc}
389   */
390  @Override()
391  public String getTaskDescription()
392  {
393    return INFO_TASK_DESCRIPTION_ROTATE_LOG.get();
394  }
395
396
397
398  /**
399   * Retrieves the paths of the log files to rotate.  The paths may be
400   * absolute or relative to the server root.
401   *
402   * @return  The paths of the log files to rotate, or an empty list if no
403   *          paths were specified and the server should rotate the log files
404   *          for all applicable loggers.
405   */
406  public List<String> getPaths()
407  {
408    return paths;
409  }
410
411
412
413  /**
414   * {@inheritDoc}
415   */
416  @Override()
417  protected List<String> getAdditionalObjectClasses()
418  {
419    return Collections.singletonList(OC_ROTATE_LOG_TASK);
420  }
421
422
423
424  /**
425   * {@inheritDoc}
426   */
427  @Override()
428  protected List<Attribute> getAdditionalAttributes()
429  {
430    if (paths.isEmpty())
431    {
432      return Collections.emptyList();
433    }
434    else
435    {
436      return Collections.singletonList(new Attribute(ATTR_PATH, paths));
437    }
438  }
439
440
441
442  /**
443   * {@inheritDoc}
444   */
445  @Override()
446  public List<TaskProperty> getTaskSpecificProperties()
447  {
448    return Collections.singletonList(PROPERTY_PATH);
449  }
450
451
452
453  /**
454   * {@inheritDoc}
455   */
456  @Override()
457  public Map<TaskProperty,List<Object>> getTaskPropertyValues()
458  {
459    final LinkedHashMap<TaskProperty,List<Object>> props =
460         new LinkedHashMap<>(10);
461
462
463    if (! paths.isEmpty())
464    {
465      props.put(PROPERTY_PATH, Collections.<Object>unmodifiableList(paths));
466    }
467
468    props.putAll(super.getTaskPropertyValues());
469    return Collections.unmodifiableMap(props);
470  }
471}