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