View Javadoc

1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package net.sourceforge.basher.tasks;
16  
17  import java.util.Collections;
18  import java.util.List;
19  import java.util.concurrent.atomic.AtomicInteger;
20  
21  import net.sourceforge.basher.Task;
22  import net.sourceforge.basher.TaskFailedException;
23  import net.sourceforge.basher.TaskNotRunException;
24  import org.apache.commons.logging.Log;
25  
26  /**
27   * A base implementation of the <code>Task</code> interface.  Provides some basic helper methods for dealing with
28   * a task execution.
29   *
30   * @author Johan Lindquist
31   * @version 1.0
32   */
33  public abstract class AbstractTask implements Task
34  {
35      /**
36       * Instance logger
37       */
38      protected Log _log;
39  
40      /**
41       * Convenience method for sub-classes to signal a not run.
42       *
43       * @param reason The reason for not running
44       * @param cause  The (optional) cause of not run
45       * @throws TaskNotRunException Will <b>ALWAYS</b> throw a <code>TaskNotRunException</code> to indicate the task didn't run.
46       */
47      protected void notRun(final String reason, final Throwable cause)
48      {
49          throw new TaskNotRunException(reason);
50      }
51  
52  
53      /**
54       * Convenience method for sub-classes to signal a failure.
55       *
56       * @param reason The reason for the failure
57       * @param cause  The (optional) cause of the failure
58       * @throws TaskFailedException Will <b>ALWAYS</b> throw a <code>TaskFailedException</code> to indicate a failure.
59       */
60      protected void failed(final String reason, final Throwable cause)
61      {
62          throw new TaskFailedException(reason, cause);
63      }
64  
65      /**
66       * Convenience method for sub-classes to signal a failure.
67       *
68       * @param reason The reason for the failure
69       * @throws TaskFailedException Will <b>ALWAYS</b> throw a <code>TaskFailedException</code> to indicate a failure.
70       */
71      protected void failed(final String reason)
72      {
73          failed(reason, null);
74      }
75  
76      /**
77       * Sets the <code>Log</code> instance for this <code>Task</code> instance.
78       *
79       * @param log The log instance to use for logging.
80       */
81      public void setLog(final Log log)
82      {
83          _log = log;
84      }
85  
86      @Override
87      public String toString()
88      {
89          return "AbstractTask{" +
90                  '}';
91      }
92  
93      @Override
94      public boolean equals( final Object o )
95      {
96          if ( this == o )
97          {
98              return true;
99          }
100         if ( !( o instanceof AbstractTask ) )
101         {
102             return false;
103         }
104 
105         final AbstractTask that = ( AbstractTask ) o;
106 
107         if ( _log != null ? !_log.equals( that._log ) : that._log != null )
108         {
109             return false;
110         }
111 
112         return true;
113     }
114 
115     @Override
116     public int hashCode()
117     {
118         int result = _log != null ? _log.hashCode() : 0;
119         return result;
120     }
121 }