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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.backends.task;
028    
029    import org.opends.messages.Message;
030    import 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     */
037    public 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 has completed without any
080       * errors.
081       */
082      COMPLETED_SUCCESSFULLY(INFO_TASK_STATE_COMPLETED_SUCCESSFULLY.get()),
083    
084    
085    
086      /**
087       * The task state that indicates that the task was able to complete its
088       * intended goal, but that one or more errors were encountered during the
089       * process.
090       */
091      COMPLETED_WITH_ERRORS(INFO_TASK_STATE_COMPLETED_WITH_ERRORS.get()),
092    
093    
094    
095      /**
096       * The task state that indicates that the task was unable to complete because
097       * it was interrupted by the shutdown of the task backend.
098       */
099      STOPPED_BY_SHUTDOWN(INFO_TASK_STATE_STOPPED_BY_SHUTDOWN.get()),
100    
101    
102    
103      /**
104       * The task state that indicates that one or more errors prevented the task
105       * from completing.
106       */
107      STOPPED_BY_ERROR(INFO_TASK_STATE_STOPPED_BY_ERROR.get()),
108    
109    
110    
111      /**
112       * The task state that indicates that the task was stopped by an administrator
113       * after it had already started but before it was able to complete.
114       */
115      STOPPED_BY_ADMINISTRATOR(INFO_TASK_STATE_STOPPED_BY_ADMINISTRATOR.get()),
116    
117    
118    
119      /**
120       * The task state that indicates that the task was canceled by an
121       * administrator before it started running.
122       */
123      CANCELED_BEFORE_STARTING(INFO_TASK_STATE_CANCELED_BEFORE_STARTING.get());
124    
125    
126    
127    
128    
129    
130      /**
131       * Indicates whether a task with the specified state is currently pending
132       * execution.
133       *
134       * @param  taskState  The task state for which to make the determination.
135       *
136       * @return  <CODE>true</CODE> if the stask tate indicates that the task is
137       *          currently pending, or <CODE>false</CODE> otherwise.
138       */
139      public static boolean isPending(TaskState taskState)
140      {
141        switch (taskState)
142        {
143          case UNSCHEDULED:
144          case WAITING_ON_START_TIME:
145          case WAITING_ON_DEPENDENCY:
146            return true;
147          default:
148            return false;
149        }
150      }
151    
152    
153    
154      /**
155       * Indicates whether a task with the specified state is currently running.
156       *
157       * @param  taskState  The task state for which to make the determination.
158       *
159       * @return  <CODE>true</CODE> if the task state indicates that the task is
160       *          currently running, or <CODE>false</CODE> otherwise.
161       */
162      public static boolean isRunning(TaskState taskState)
163      {
164        switch (taskState)
165        {
166          case RUNNING:
167            return true;
168          default:
169            return false;
170        }
171      }
172    
173    
174    
175      /**
176       * Indicates whether a task with the specified state has completed all the
177       * processing that it will do, regardless of whether it completed its
178       * intended goal.
179       *
180       * @param  taskState  The task state for which to make the determination.
181       *
182       * @return  <CODE>false</CODE> if the task state indicates that the task has
183       *          not yet started or is currently running, or <CODE>true</CODE>
184       *          otherwise.
185       */
186      public static boolean isDone(TaskState taskState)
187      {
188        switch (taskState)
189        {
190          case UNSCHEDULED:
191          case WAITING_ON_START_TIME:
192          case WAITING_ON_DEPENDENCY:
193          case RUNNING:
194            return false;
195          default:
196            return true;
197        }
198      }
199    
200    
201    
202      /**
203       * Indicates whether a task with the specified state has been able to complete
204       * its intended goal.
205       *
206       * @param  taskState  The task state for which to make the determination.
207       *
208       * @return  <CODE>true</CODE> if the task state indicates that the task
209       *          completed successfully or with minor errors that still allowed it
210       *          to achieve its goal, or <CODE>false</CODE> otherwise.
211       */
212      public static boolean isSuccessful(TaskState taskState)
213      {
214        switch (taskState)
215        {
216          case WAITING_ON_START_TIME:
217          case WAITING_ON_DEPENDENCY:
218          case RUNNING:
219          case STOPPED_BY_ERROR:
220          case COMPLETED_WITH_ERRORS:
221            return false;
222          default:
223            return true;
224        }
225      }
226    
227    
228      /**
229       * Indicates whether or not this task has been cancelled.
230       *
231       * @param  taskState  The task state for which to make the determination.
232       *
233       * @return  <CODE>true</CODE> if the task state indicates that the task
234       *          was cancelled either before or during execution, or
235       *          <CODE>false</CODE> otherwise.
236       */
237      public static boolean isCancelled(TaskState taskState)
238      {
239        switch(taskState)
240        {
241          case STOPPED_BY_ADMINISTRATOR:
242          case CANCELED_BEFORE_STARTING:
243            return true;
244          default:
245            return false;
246        }
247      }
248    
249      /**
250       * Retrieves the task state that corresponds to the provided string value.
251       *
252       * @param  s  The string value for which to retrieve the corresponding task
253       *            state.
254       *
255       * @return  The corresponding task state, or <CODE>null</CODE> if none could
256       *          be associated with the provided string.
257       */
258      public static TaskState fromString(String s)
259      {
260        String lowerString = s.toLowerCase();
261        if (lowerString.equals("unscheduled"))
262        {
263          return UNSCHEDULED;
264        }
265        else if (lowerString.equals("disabled"))
266        {
267          return DISABLED;
268        }
269        else if (lowerString.equals("waiting_on_start_time"))
270        {
271          return WAITING_ON_START_TIME;
272        }
273        else if (lowerString.equals("waiting_on_dependency"))
274        {
275          return WAITING_ON_DEPENDENCY;
276        }
277        else if (lowerString.equals("running"))
278        {
279          return RUNNING;
280        }
281        else if (lowerString.equals("completed_successfully"))
282        {
283          return COMPLETED_SUCCESSFULLY;
284        }
285        else if (lowerString.equals("completed_with_errors"))
286        {
287          return COMPLETED_WITH_ERRORS;
288        }
289        else if (lowerString.equals("stopped_by_shutdown"))
290        {
291          return STOPPED_BY_SHUTDOWN;
292        }
293        else if (lowerString.equals("stopped_by_error"))
294        {
295          return STOPPED_BY_ERROR;
296        }
297        else if (lowerString.equals("stopped_by_administrator"))
298        {
299          return STOPPED_BY_ADMINISTRATOR;
300        }
301        else if (lowerString.equals("canceled_before_starting"))
302        {
303          return CANCELED_BEFORE_STARTING;
304        }
305        else
306        {
307          return null;
308        }
309      }
310    
311      private Message displayName;
312    
313      /**
314       * Gets a locale sensitive representation of this state.
315       *
316       * @return Message describing state
317       */
318      public Message getDisplayName() {
319        return displayName;
320      }
321    
322      private TaskState(Message displayName) {
323        this.displayName = displayName;
324      }
325    }
326