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