001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014 ForgeRock AS
026 */
027package org.opends.server.backends.task;
028
029import org.forgerock.i18n.LocalizableMessage;
030import static org.opends.messages.TaskMessages.*;
031
032
033/**
034 * This enumeration defines the various states that a task can have during its
035 * lifetime.
036 */
037public enum TaskState
038{
039  /**
040   * The task state that indicates that the task has not yet been scheduled,
041   * or possibly that the scheduler is currently not running.
042   */
043  UNSCHEDULED(INFO_TASK_STATE_UNSCHEDULED.get()),
044
045
046
047  /**
048   * The task state that indicates that the task has been disabled by an
049   * administrator.
050   */
051  DISABLED(INFO_TASK_STATE_DISABLED.get()),
052
053
054
055  /**
056   * The task state that indicates that the task's scheduled start time has not
057   * yet arrived.
058   */
059  WAITING_ON_START_TIME(INFO_TASK_STATE_WAITING_ON_START_TIME.get()),
060
061
062
063  /**
064   * The task state that indicates that at least one of the task's defined
065   * dependencies has not yet completed.
066   */
067  WAITING_ON_DEPENDENCY(INFO_TASK_STATE_WAITING_ON_DEPENDENCY.get()),
068
069
070
071  /**
072   * The task state that indicates that the task is currently running.
073   */
074  RUNNING(INFO_TASK_STATE_RUNNING.get()),
075
076
077
078  /**
079   * The task state that indicates that the task is recurring.
080   */
081  RECURRING(INFO_TASK_STATE_RECURRING.get()),
082
083
084
085  /**
086   * The task state that indicates that the task has completed without any
087   * errors.
088   */
089  COMPLETED_SUCCESSFULLY(INFO_TASK_STATE_COMPLETED_SUCCESSFULLY.get()),
090
091
092
093  /**
094   * The task state that indicates that the task was able to complete its
095   * intended goal, but that one or more errors were encountered during the
096   * process.
097   */
098  COMPLETED_WITH_ERRORS(INFO_TASK_STATE_COMPLETED_WITH_ERRORS.get()),
099
100
101
102  /**
103   * The task state that indicates that the task was unable to complete because
104   * it was interrupted by the shutdown of the task backend.
105   */
106  STOPPED_BY_SHUTDOWN(INFO_TASK_STATE_STOPPED_BY_SHUTDOWN.get()),
107
108
109
110  /**
111   * The task state that indicates that one or more errors prevented the task
112   * from completing.
113   */
114  STOPPED_BY_ERROR(INFO_TASK_STATE_STOPPED_BY_ERROR.get()),
115
116
117
118  /**
119   * The task state that indicates that the task was stopped by an administrator
120   * after it had already started but before it was able to complete.
121   */
122  STOPPED_BY_ADMINISTRATOR(INFO_TASK_STATE_STOPPED_BY_ADMINISTRATOR.get()),
123
124
125
126  /**
127   * The task state that indicates that the task was canceled by an
128   * administrator before it started running.
129   */
130  CANCELED_BEFORE_STARTING(INFO_TASK_STATE_CANCELED_BEFORE_STARTING.get());
131
132
133
134
135
136
137  /**
138   * Indicates whether a task with the specified state is currently pending
139   * execution.
140   *
141   * @param  taskState  The task state for which to make the determination.
142   *
143   * @return  <CODE>true</CODE> if the stask tate indicates that the task is
144   *          currently pending, or <CODE>false</CODE> otherwise.
145   */
146  public static boolean isPending(TaskState taskState)
147  {
148    switch (taskState)
149    {
150      case UNSCHEDULED:
151      case WAITING_ON_START_TIME:
152      case WAITING_ON_DEPENDENCY:
153        return true;
154      default:
155        return false;
156    }
157  }
158
159
160
161  /**
162   * Indicates whether a task with the specified state is currently running.
163   *
164   * @param  taskState  The task state for which to make the determination.
165   *
166   * @return  <CODE>true</CODE> if the task state indicates that the task is
167   *          currently running, or <CODE>false</CODE> otherwise.
168   */
169  public static boolean isRunning(TaskState taskState)
170  {
171    switch (taskState)
172    {
173      case RUNNING:
174        return true;
175      default:
176        return false;
177    }
178  }
179
180
181
182  /**
183   * Indicates whether a task with the specified state is recurring.
184   *
185   * @param  taskState  The task state for which to make the determination.
186   *
187   * @return  <CODE>true</CODE> if the task state indicates that the task
188   *          is recurring, or <CODE>false</CODE> otherwise.
189   */
190  public static boolean isRecurring(TaskState taskState)
191  {
192    switch (taskState)
193    {
194      case RECURRING:
195        return true;
196      default:
197        return false;
198    }
199  }
200
201
202
203  /**
204   * Indicates whether a task with the specified state has completed all the
205   * processing that it will do, regardless of whether it completed its
206   * intended goal.
207   *
208   * @param  taskState  The task state for which to make the determination.
209   *
210   * @return  <CODE>false</CODE> if the task state indicates that the task has
211   *          not yet started or is currently running, or <CODE>true</CODE>
212   *          otherwise.
213   */
214  public static boolean isDone(TaskState taskState)
215  {
216    switch (taskState)
217    {
218      case UNSCHEDULED:
219      case WAITING_ON_START_TIME:
220      case WAITING_ON_DEPENDENCY:
221      case RUNNING:
222        return false;
223      default:
224        return true;
225    }
226  }
227
228
229
230  /**
231   * Indicates whether a task with the specified state has been able to complete
232   * its intended goal.
233   *
234   * @param  taskState  The task state for which to make the determination.
235   *
236   * @return  <CODE>true</CODE> if the task state indicates that the task
237   *          completed successfully or with minor errors that still allowed it
238   *          to achieve its goal, or <CODE>false</CODE> otherwise.
239   */
240  public static boolean isSuccessful(TaskState taskState)
241  {
242    switch (taskState)
243    {
244      case WAITING_ON_START_TIME:
245      case WAITING_ON_DEPENDENCY:
246      case RUNNING:
247      case STOPPED_BY_ERROR:
248      case COMPLETED_WITH_ERRORS:
249        return false;
250      default:
251        return true;
252    }
253  }
254
255
256  /**
257   * Indicates whether or not this task has been cancelled.
258   *
259   * @param  taskState  The task state for which to make the determination.
260   *
261   * @return  <CODE>true</CODE> if the task state indicates that the task
262   *          was cancelled either before or during execution, or
263   *          <CODE>false</CODE> otherwise.
264   */
265  public static boolean isCancelled(TaskState taskState)
266  {
267    switch(taskState)
268    {
269      case STOPPED_BY_ADMINISTRATOR:
270      case CANCELED_BEFORE_STARTING:
271        return true;
272      default:
273        return false;
274    }
275  }
276
277  /**
278   * Retrieves the task state that corresponds to the provided string value.
279   *
280   * @param  s  The string value for which to retrieve the corresponding task
281   *            state.
282   *
283   * @return  The corresponding task state, or <CODE>null</CODE> if none could
284   *          be associated with the provided string.
285   */
286  public static TaskState fromString(String s)
287  {
288    String lowerString = s.toLowerCase();
289    if (lowerString.equals("unscheduled"))
290    {
291      return UNSCHEDULED;
292    }
293    else if (lowerString.equals("disabled"))
294    {
295      return DISABLED;
296    }
297    else if (lowerString.equals("waiting_on_start_time"))
298    {
299      return WAITING_ON_START_TIME;
300    }
301    else if (lowerString.equals("waiting_on_dependency"))
302    {
303      return WAITING_ON_DEPENDENCY;
304    }
305    else if (lowerString.equals("running"))
306    {
307      return RUNNING;
308    }
309    else if (lowerString.equals("recurring"))
310    {
311      return RECURRING;
312    }
313    else if (lowerString.equals("completed_successfully"))
314    {
315      return COMPLETED_SUCCESSFULLY;
316    }
317    else if (lowerString.equals("completed_with_errors"))
318    {
319      return COMPLETED_WITH_ERRORS;
320    }
321    else if (lowerString.equals("stopped_by_shutdown"))
322    {
323      return STOPPED_BY_SHUTDOWN;
324    }
325    else if (lowerString.equals("stopped_by_error"))
326    {
327      return STOPPED_BY_ERROR;
328    }
329    else if (lowerString.equals("stopped_by_administrator"))
330    {
331      return STOPPED_BY_ADMINISTRATOR;
332    }
333    else if (lowerString.equals("canceled_before_starting"))
334    {
335      return CANCELED_BEFORE_STARTING;
336    }
337    else
338    {
339      return null;
340    }
341  }
342
343  private LocalizableMessage displayName;
344
345  /**
346   * Gets a locale sensitive representation of this state.
347   *
348   * @return LocalizableMessage describing state
349   */
350  public LocalizableMessage getDisplayName() {
351    return displayName;
352  }
353
354  private TaskState(LocalizableMessage displayName) {
355    this.displayName = displayName;
356  }
357}
358