1 package org.apache.continuum.taskqueue;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.annotation.Resource;
26
27 import org.apache.commons.lang.ArrayUtils;
28 import org.apache.continuum.dao.BuildDefinitionDao;
29 import org.apache.continuum.taskqueueexecutor.ParallelBuildsThreadedTaskQueueExecutor;
30 import org.apache.maven.continuum.model.project.BuildDefinition;
31 import org.apache.maven.continuum.store.ContinuumStoreException;
32 import org.codehaus.plexus.taskqueue.Task;
33 import org.codehaus.plexus.taskqueue.TaskQueue;
34 import org.codehaus.plexus.taskqueue.TaskQueueException;
35 import org.codehaus.plexus.taskqueue.execution.TaskQueueExecutor;
36 import org.codehaus.plexus.util.StringUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
46 public class DefaultOverallBuildQueue
47 implements OverallBuildQueue
48 {
49 private static final Logger log = LoggerFactory.getLogger( DefaultOverallBuildQueue.class );
50
51 @Resource
52 private BuildDefinitionDao buildDefinitionDao;
53
54 private TaskQueueExecutor buildTaskQueueExecutor;
55
56 private TaskQueueExecutor checkoutTaskQueueExecutor;
57
58 private int id;
59
60 private String name;
61
62 public int getId()
63 {
64 return id;
65 }
66
67 public void setId( int id )
68 {
69 this.id = id;
70 }
71
72 public String getName()
73 {
74 return name;
75 }
76
77 public void setName( String name )
78 {
79 this.name = name;
80 }
81
82
83
84
85 public void addToCheckoutQueue( CheckOutTask checkoutTask )
86 throws TaskQueueException
87 {
88 getCheckoutQueue().put( checkoutTask );
89 }
90
91
92
93
94 public void addToCheckoutQueue( List<CheckOutTask> checkoutTasks )
95 throws TaskQueueException
96 {
97 for ( CheckOutTask checkoutTask : checkoutTasks )
98 {
99 getCheckoutQueue().put( checkoutTask );
100 }
101 }
102
103
104
105
106 public List<CheckOutTask> getProjectsInCheckoutQueue()
107 throws TaskQueueException
108 {
109 return getCheckoutQueue().getQueueSnapshot();
110 }
111
112
113
114
115 public boolean isInCheckoutQueue( int projectId )
116 throws TaskQueueException
117 {
118 List<CheckOutTask> queue = getProjectsInCheckoutQueue();
119
120 for ( CheckOutTask task : queue )
121 {
122 if ( task != null && task.getProjectId() == projectId )
123 {
124 return true;
125 }
126 }
127 return false;
128 }
129
130
131
132
133 public boolean removeProjectFromCheckoutQueue( int projectId )
134 throws TaskQueueException
135 {
136 List<CheckOutTask> queue = getProjectsInCheckoutQueue();
137
138 for ( CheckOutTask task : queue )
139 {
140 if ( task != null && task.getProjectId() == projectId )
141 {
142 return getCheckoutQueue().remove( task );
143 }
144 }
145 return false;
146 }
147
148
149
150
151 public boolean removeProjectsFromCheckoutQueue( int[] projectsId )
152 throws TaskQueueException
153 {
154 if ( projectsId == null )
155 {
156 return false;
157 }
158 if ( projectsId.length < 1 )
159 {
160 return false;
161 }
162 List<CheckOutTask> queue = getProjectsInCheckoutQueue();
163
164 List<CheckOutTask> tasks = new ArrayList<CheckOutTask>();
165
166 for ( CheckOutTask task : queue )
167 {
168 if ( task != null )
169 {
170 if ( ArrayUtils.contains( projectsId, task.getProjectId() ) )
171 {
172 tasks.add( task );
173 }
174 }
175 }
176 return !tasks.isEmpty() && getCheckoutQueue().removeAll( tasks );
177 }
178
179
180
181
182 public void removeTasksFromCheckoutQueueWithHashCodes( int[] hashCodes )
183 throws TaskQueueException
184 {
185 List<CheckOutTask> queue = getProjectsInCheckoutQueue();
186
187 for ( CheckOutTask task : queue )
188 {
189 if ( ArrayUtils.contains( hashCodes, task.hashCode() ) )
190 {
191 getCheckoutQueue().remove( task );
192 }
193 }
194 }
195
196
197
198
199 public void addToBuildQueue( BuildProjectTask buildTask )
200 throws TaskQueueException
201 {
202 getBuildQueue().put( buildTask );
203 }
204
205
206
207
208 public void addToBuildQueue( List<BuildProjectTask> buildTasks )
209 throws TaskQueueException
210 {
211 for ( BuildProjectTask buildTask : buildTasks )
212 {
213 getBuildQueue().put( buildTask );
214 }
215 }
216
217
218
219
220 public List<BuildProjectTask> getProjectsInBuildQueue()
221 throws TaskQueueException
222 {
223 return getBuildQueue().getQueueSnapshot();
224 }
225
226
227
228
229 public boolean isInBuildQueue( int projectId )
230 throws TaskQueueException
231 {
232 return isInBuildQueue( projectId, -1 );
233 }
234
235
236
237
238 public boolean isInBuildQueue( int projectId, int buildDefinitionId )
239 throws TaskQueueException
240 {
241 List<BuildProjectTask> queue = getProjectsInBuildQueue();
242 for ( BuildProjectTask buildTask : queue )
243 {
244 if ( buildTask != null )
245 {
246 if ( buildDefinitionId < 0 )
247 {
248 if ( buildTask.getProjectId() == projectId )
249 {
250 return true;
251 }
252 }
253 else
254 {
255 if ( buildTask.getProjectId() == projectId &&
256 buildTask.getBuildDefinitionId() == buildDefinitionId )
257 {
258 return true;
259 }
260 }
261 }
262 }
263 return false;
264 }
265
266
267
268
269 public void cancelBuildTask( int projectId )
270 {
271 BuildProjectTask task = (BuildProjectTask) buildTaskQueueExecutor.getCurrentTask();
272 if ( task != null && task.getProjectId() == projectId )
273 {
274 log.info(
275 "Cancelling build task for project '" + projectId + "' in task executor '" + buildTaskQueueExecutor );
276 buildTaskQueueExecutor.cancelTask( task );
277 }
278 }
279
280
281
282
283 public void cancelCheckoutTask( int projectId )
284 throws TaskQueueException
285 {
286 CheckOutTask task = (CheckOutTask) checkoutTaskQueueExecutor.getCurrentTask();
287 if ( task != null && task.getProjectId() == projectId )
288 {
289 log.info( "Cancelling checkout task for project '" + projectId + "' in task executor '" +
290 checkoutTaskQueueExecutor );
291 checkoutTaskQueueExecutor.cancelTask( task );
292 }
293 }
294
295
296
297
298 public boolean cancelCurrentBuild()
299 {
300 Task task = buildTaskQueueExecutor.getCurrentTask();
301 if ( task != null )
302 {
303 return buildTaskQueueExecutor.cancelTask( task );
304 }
305
306 log.info( "No build task currently executing on build executor: " + buildTaskQueueExecutor );
307 return false;
308 }
309
310
311
312
313 public boolean cancelCurrentCheckout()
314 {
315 Task task = checkoutTaskQueueExecutor.getCurrentTask();
316 if ( task != null )
317 {
318 return checkoutTaskQueueExecutor.cancelTask( task );
319 }
320
321 log.info( "No checkout task currently executing on checkout task executor: " + checkoutTaskQueueExecutor );
322 return false;
323 }
324
325
326
327
328 public boolean removeProjectFromBuildQueue( int projectId, int buildDefinitionId, int trigger, String projectName,
329 int projectGroupId )
330 throws TaskQueueException
331 {
332 BuildDefinition buildDefinition;
333
334
335 try
336 {
337 buildDefinition = buildDefinitionDao.getBuildDefinition( buildDefinitionId );
338 }
339 catch ( ContinuumStoreException e )
340 {
341 throw new TaskQueueException( "Error while removing project from build queue: " + projectName, e );
342 }
343
344 String buildDefinitionLabel = buildDefinition.getDescription();
345 if ( StringUtils.isEmpty( buildDefinitionLabel ) )
346 {
347 buildDefinitionLabel = buildDefinition.getGoals();
348 }
349
350 BuildProjectTask buildProjectTask =
351 new BuildProjectTask( projectId, buildDefinitionId, trigger, projectName,
352 buildDefinitionLabel, null, projectGroupId );
353
354 return getBuildQueue().remove( buildProjectTask );
355 }
356
357
358
359
360 public boolean removeProjectsFromBuildQueue( int[] projectIds )
361 throws TaskQueueException
362 {
363 if ( projectIds == null )
364 {
365 return false;
366 }
367 if ( projectIds.length < 1 )
368 {
369 return false;
370 }
371 List<BuildProjectTask> queue = getProjectsInBuildQueue();
372
373 List<BuildProjectTask> tasks = new ArrayList<BuildProjectTask>();
374
375 for ( BuildProjectTask buildTask : queue )
376 {
377 if ( buildTask != null )
378 {
379 if ( ArrayUtils.contains( projectIds, buildTask.getProjectId() ) )
380 {
381 tasks.add( buildTask );
382 }
383 }
384 }
385
386 for ( BuildProjectTask buildProjectTask : tasks )
387 {
388 log.info( "cancel build for project " + buildProjectTask.getProjectId() );
389 }
390
391 return !tasks.isEmpty() && getBuildQueue().removeAll( tasks );
392 }
393
394
395
396
397 public boolean removeProjectFromBuildQueue( int projectId )
398 throws TaskQueueException
399 {
400 List<BuildProjectTask> queue = getProjectsInBuildQueue();
401
402 for ( BuildProjectTask buildTask : queue )
403 {
404 if ( buildTask != null && buildTask.getProjectId() == projectId )
405 {
406 return getBuildQueue().remove( buildTask );
407 }
408 }
409 return false;
410 }
411
412
413
414
415 public void removeProjectsFromBuildQueueWithHashCodes( int[] hashCodes )
416 throws TaskQueueException
417 {
418 List<BuildProjectTask> queue = getProjectsInBuildQueue();
419 for ( BuildProjectTask task : queue )
420 {
421 if ( ArrayUtils.contains( hashCodes, task.hashCode() ) )
422 {
423 getBuildQueue().remove( task );
424 }
425 }
426 }
427
428
429
430
431 public TaskQueue getCheckoutQueue()
432 {
433 return ( (ParallelBuildsThreadedTaskQueueExecutor) checkoutTaskQueueExecutor ).getQueue();
434 }
435
436
437
438
439 public TaskQueue getBuildQueue()
440 {
441 return ( (ParallelBuildsThreadedTaskQueueExecutor) buildTaskQueueExecutor ).getQueue();
442 }
443
444
445
446
447 public TaskQueueExecutor getBuildTaskQueueExecutor()
448 {
449 return buildTaskQueueExecutor;
450 }
451
452
453
454
455 public TaskQueueExecutor getCheckoutTaskQueueExecutor()
456 {
457 return checkoutTaskQueueExecutor;
458 }
459
460 public void setBuildDefinitionDao( BuildDefinitionDao buildDefinitionDao )
461 {
462 this.buildDefinitionDao = buildDefinitionDao;
463 }
464
465 public void setBuildTaskQueueExecutor( TaskQueueExecutor buildTaskQueueExecutor )
466 {
467 this.buildTaskQueueExecutor = buildTaskQueueExecutor;
468 }
469
470 public void setCheckoutTaskQueueExecutor( TaskQueueExecutor checkoutTaskQueueExecutor )
471 {
472 this.checkoutTaskQueueExecutor = checkoutTaskQueueExecutor;
473 }
474 }