View Javadoc

1   package net.sourceforge.basher;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   import java.util.UUID;
6   import java.util.concurrent.atomic.AtomicInteger;
7   
8   /** Holder class for a <code>Task</code> and <code>TaskConfiguration</code> combination.  Class also manages basic
9    * statistics for the invocation of a class, such as invocation counts etc.
10   * @author Johan Lindquist
11   */
12  public class TaskExecutionContext
13  {
14      /**
15       *
16       */
17      private UUID _identifier;
18  
19      /**
20       *
21       */
22      private Task _task;
23  
24      /**
25       *
26       */
27      private TaskConfiguration _taskConfiguration;
28  
29      private Set<TaskExecutionContext> _followers = new HashSet<TaskExecutionContext>( );
30  
31      /**
32       * instance failure counter
33       */
34      private volatile int _failures = 0;
35      /**
36       * instance success counter
37       */
38      private volatile int _successes = 0;
39      /**
40       * instance not run counter
41       */
42      private volatile int _notRun = 0;
43  
44      private AtomicInteger _reservations = new AtomicInteger( 0 );
45  
46      /**
47       *
48       * @param task
49       * @deprecated
50       */
51      public TaskExecutionContext( final Task task )
52      {
53          _task = task;
54      }
55  
56      /** Creates a new <code>TaskExecutionContext</code>, wrapping the specified task and task configuration.
57       *
58       * @param identifier Identifier of the context, used for referencing this context
59       * @param task The task to wrap
60       * @param taskConfiguration The configuration of the task
61       */
62      public TaskExecutionContext( final UUID identifier, final Task task, final TaskConfiguration taskConfiguration )
63      {
64          _identifier = identifier;
65          _task = task;
66          _taskConfiguration = taskConfiguration;
67      }
68  
69      /**
70       * Retrieves the task embedded in this <code>TaskExecutionContext</code>.
71       *
72       * @return The embedded task
73       */
74      public Task getTask()
75      {
76          return _task;
77      }
78  
79      public TaskConfiguration getTaskConfiguration()
80      {
81          return _taskConfiguration;
82      }
83  
84      /**
85       * Retrieves the number of failed invocation of the embedded task.
86       *
87       * @return The number of failed invocations.
88       */
89      public int getFailures()
90      {
91          return _failures;
92      }
93  
94      /**
95       * Retrieves the number of successful invocations of the embedded task.
96       *
97       * @return The number of successful invocations.
98       */
99      public int getSuccesses()
100     {
101         return _successes;
102     }
103 
104     /**
105      * Retrieves the number times the task chose to not run.  The task <strong>must</strong> throw a <code>NotRunException</code>
106      * for this counter to be incremented.
107      *
108      * @return The number of times the embedded task chose to not run.
109      */
110     public int getNotRun()
111     {
112         return _notRun;
113     }
114 
115     public void reserveInvocation()
116     {
117         _reservations.incrementAndGet();
118     }
119 
120     public void releaseInvocation()
121     {
122         _reservations.decrementAndGet();
123     }
124 
125     /**
126      * Retrieves the number of times this task was invoked (successes+failures).
127      *
128      * @return The number of invocations.
129      */
130     public int getInvocations()
131     {
132         return _reservations.get() + _successes + _failures;
133     }
134 
135     /** Main method that should be called by the component that executes tasks.  This method ensures that
136      * counters of failures/successes are kept up to date for the task in question.
137      *
138      * @throws Throwable Any exception thrown by the embedded task when invoking the <code>executeTask</code> method are
139      * re-thrown by this method.
140      */
141     public void executeTask() throws Throwable
142     {
143         try
144         {
145             _task.executeTask();
146             _successes++;
147             _taskConfiguration.reCalculateWeight();
148         }
149         catch (TaskNotRunException e)
150         {
151             _notRun++;
152             throw e;
153         }
154         catch (Throwable throwable)
155         {
156             _failures++;
157             throw throwable;
158         }
159     }
160 
161     public UUID getIdentifier()
162     {
163         return _identifier;
164     }
165 
166     public void setIdentifier( final UUID identifier )
167     {
168         _identifier = identifier;
169     }
170 
171     public void addFollower(final TaskExecutionContext taskExecutionContext)
172     {
173         _followers.add( taskExecutionContext );
174     }
175 
176     public void clearFollowers()
177     {
178         _followers.clear();
179     }
180 
181     public Set<TaskExecutionContext> getFollowers()
182     {
183         return new HashSet<TaskExecutionContext>( _followers );
184     }
185 
186     @Override
187     public String toString()
188     {
189         return "TaskExecutionContext{" +
190                 "_identifier=" + _identifier +
191                 ", _task=" + _task +
192                 ", _taskConfiguration=" + _taskConfiguration +
193                 ", _failures=" + _failures +
194                 ", _successes=" + _successes +
195                 ", _notRun=" + _notRun +
196                 ", _reservations=" + _reservations +
197                 '}';
198     }
199 
200     @Override
201     public boolean equals( final Object o )
202     {
203         if ( this == o )
204         {
205             return true;
206         }
207         if ( !( o instanceof TaskExecutionContext ) )
208         {
209             return false;
210         }
211 
212         final TaskExecutionContext that = ( TaskExecutionContext ) o;
213 
214         if ( _failures != that._failures )
215         {
216             return false;
217         }
218         if ( _notRun != that._notRun )
219         {
220             return false;
221         }
222         if ( _successes != that._successes )
223         {
224             return false;
225         }
226         if ( _identifier != null ? !_identifier.equals( that._identifier ) : that._identifier != null )
227         {
228             return false;
229         }
230         if ( _reservations != null ? !_reservations.equals( that._reservations ) : that._reservations != null )
231         {
232             return false;
233         }
234         if ( _task != null ? !_task.equals( that._task ) : that._task != null )
235         {
236             return false;
237         }
238         if ( _taskConfiguration != null ? !_taskConfiguration.equals( that._taskConfiguration ) : that._taskConfiguration != null )
239         {
240             return false;
241         }
242 
243         return true;
244     }
245 
246     @Override
247     public int hashCode()
248     {
249         int result = _identifier != null ? _identifier.hashCode() : 0;
250         result = 31 * result + ( _task != null ? _task.hashCode() : 0 );
251         result = 31 * result + ( _taskConfiguration != null ? _taskConfiguration.hashCode() : 0 );
252         result = 31 * result + _failures;
253         result = 31 * result + _successes;
254         result = 31 * result + _notRun;
255         result = 31 * result + ( _reservations != null ? _reservations.hashCode() : 0 );
256         return result;
257     }
258 }