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   
9   
10  
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  
33  
34      private volatile int _failures = 0;
35      
36  
37  
38      private volatile int _successes = 0;
39      
40  
41  
42      private volatile int _notRun = 0;
43  
44      private AtomicInteger _reservations = new AtomicInteger( 0 );
45  
46      
47  
48  
49  
50  
51      public TaskExecutionContext( final Task task )
52      {
53          _task = task;
54      }
55  
56      
57  
58  
59  
60  
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  
71  
72  
73  
74      public Task getTask()
75      {
76          return _task;
77      }
78  
79      public TaskConfiguration getTaskConfiguration()
80      {
81          return _taskConfiguration;
82      }
83  
84      
85  
86  
87  
88  
89      public int getFailures()
90      {
91          return _failures;
92      }
93  
94      
95  
96  
97  
98  
99      public int getSuccesses()
100     {
101         return _successes;
102     }
103 
104     
105 
106 
107 
108 
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 
127 
128 
129 
130     public int getInvocations()
131     {
132         return _reservations.get() + _successes + _failures;
133     }
134 
135     
136 
137 
138 
139 
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 }