1 package org.apache.maven.continuum.builddefinition;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.continuum.buildqueue.BuildQueueServiceException;
23 import org.apache.continuum.configuration.ContinuumConfigurationException;
24 import org.apache.continuum.dao.BuildDefinitionDao;
25 import org.apache.continuum.dao.BuildDefinitionTemplateDao;
26 import org.apache.continuum.dao.ProjectDao;
27 import org.apache.continuum.dao.ProjectGroupDao;
28 import org.apache.maven.continuum.configuration.ConfigurationLoadingException;
29 import org.apache.maven.continuum.configuration.ConfigurationService;
30 import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
31 import org.apache.maven.continuum.model.project.BuildDefinition;
32 import org.apache.maven.continuum.model.project.BuildDefinitionTemplate;
33 import org.apache.maven.continuum.model.project.Project;
34 import org.apache.maven.continuum.model.project.ProjectGroup;
35 import org.apache.maven.continuum.model.project.Schedule;
36 import org.apache.maven.continuum.store.ContinuumObjectNotFoundException;
37 import org.apache.maven.continuum.store.ContinuumStoreException;
38 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
39 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
46
47
48
49
50
51
52
53 public class DefaultBuildDefinitionService
54 implements BuildDefinitionService, Initializable
55 {
56 private static final Logger log = LoggerFactory.getLogger( DefaultBuildDefinitionService.class );
57
58
59
60
61 private String defaultAntGoals;
62
63
64
65
66 private String defaultAntArguments;
67
68
69
70
71 private String defaultM1Goals;
72
73
74
75
76 private String defaultM1Arguments;
77
78
79
80
81 private String defaultM2Goals;
82
83
84
85
86 private String defaultM2Arguments;
87
88
89
90
91 private BuildDefinitionDao buildDefinitionDao;
92
93
94
95
96 private BuildDefinitionTemplateDao buildDefinitionTemplateDao;
97
98
99
100
101 private ProjectDao projectDao;
102
103
104
105
106 private ProjectGroupDao projectGroupDao;
107
108
109
110
111 private ConfigurationService configurationService;
112
113
114
115
116
117 public void initialize()
118 throws InitializationException
119 {
120 try
121 {
122 initializeDefaultContinuumBuildDefintions();
123 }
124 catch ( BuildDefinitionServiceException e )
125 {
126 throw new InitializationException( e.getMessage(), e );
127 }
128 }
129
130 private void initializeDefaultContinuumBuildDefintions()
131 throws BuildDefinitionServiceException
132 {
133 this.getDefaultAntBuildDefinitionTemplate();
134 this.getDefaultMavenOneBuildDefinitionTemplate();
135 this.getDefaultMavenTwoBuildDefinitionTemplate();
136 this.getDefaultShellBuildDefinitionTemplate();
137 }
138
139 public BuildDefinition getBuildDefinition( int buildDefinitionId )
140 throws BuildDefinitionServiceException
141 {
142 try
143 {
144 return buildDefinitionDao.getBuildDefinition( buildDefinitionId );
145 }
146 catch ( ContinuumObjectNotFoundException e )
147 {
148 return null;
149 }
150 catch ( ContinuumStoreException e )
151 {
152 throw new BuildDefinitionServiceException( e.getMessage(), e );
153 }
154 }
155
156 public BuildDefinition addBuildDefinition( BuildDefinition buildDefinition )
157 throws BuildDefinitionServiceException
158 {
159 try
160 {
161 return buildDefinitionDao.addBuildDefinition( buildDefinition );
162 }
163 catch ( ContinuumStoreException e )
164 {
165 throw new BuildDefinitionServiceException( e.getMessage(), e );
166 }
167 }
168
169
170 public void removeBuildDefinition( BuildDefinition buildDefinition )
171 throws BuildDefinitionServiceException
172 {
173 try
174 {
175 buildDefinitionDao.removeBuildDefinition( buildDefinition );
176 }
177 catch ( ContinuumStoreException e )
178 {
179 throw new BuildDefinitionServiceException( e.getMessage(), e );
180 }
181 }
182
183 public void updateBuildDefinition( BuildDefinition buildDefinition )
184 throws BuildDefinitionServiceException
185 {
186 try
187 {
188 BuildDefinition storedBuildDefinition = buildDefinitionDao.getBuildDefinition( buildDefinition.getId() );
189 storedBuildDefinition.setBuildFresh( buildDefinition.isBuildFresh() );
190 storedBuildDefinition.setAlwaysBuild( buildDefinition.isAlwaysBuild() );
191 storedBuildDefinition.setArguments( buildDefinition.getArguments() );
192 storedBuildDefinition.setBuildFile( buildDefinition.getBuildFile() );
193 storedBuildDefinition.setDefaultForProject( buildDefinition.isDefaultForProject() );
194 storedBuildDefinition.setDescription( buildDefinition.getDescription() );
195 storedBuildDefinition.setGoals( buildDefinition.getGoals() );
196 storedBuildDefinition.setProfile( buildDefinition.getProfile() );
197 storedBuildDefinition.setSchedule( buildDefinition.getSchedule() );
198 storedBuildDefinition.setType( buildDefinition.getType() );
199 storedBuildDefinition.setUpdatePolicy( buildDefinition.getUpdatePolicy() );
200 buildDefinitionDao.storeBuildDefinition( storedBuildDefinition );
201 }
202 catch ( ContinuumStoreException e )
203 {
204 throw new BuildDefinitionServiceException( e.getMessage(), e );
205 }
206
207 }
208
209 public List<BuildDefinition> getAllBuildDefinitions()
210 throws BuildDefinitionServiceException
211 {
212 try
213 {
214 return buildDefinitionDao.getAllBuildDefinitions();
215 }
216 catch ( ContinuumStoreException e )
217 {
218 throw new BuildDefinitionServiceException( e.getMessage(), e );
219 }
220 }
221
222
223 public List<BuildDefinition> getAllTemplates()
224 throws BuildDefinitionServiceException
225 {
226 try
227 {
228 return buildDefinitionDao.getAllTemplates();
229 }
230 catch ( ContinuumStoreException e )
231 {
232 throw new BuildDefinitionServiceException( e.getMessage(), e );
233 }
234 }
235
236
237
238
239 public BuildDefinition cloneBuildDefinition( BuildDefinition buildDefinition )
240 {
241 BuildDefinition cloned = new BuildDefinition();
242 cloned.setAlwaysBuild( buildDefinition.isAlwaysBuild() );
243 cloned.setArguments( buildDefinition.getArguments() );
244 cloned.setBuildFile( buildDefinition.getBuildFile() );
245 cloned.setBuildFresh( buildDefinition.isBuildFresh() );
246 cloned.setDefaultForProject( buildDefinition.isDefaultForProject() );
247 cloned.setDescription( buildDefinition.getDescription() );
248 cloned.setGoals( buildDefinition.getGoals() );
249 cloned.setProfile( buildDefinition.getProfile() );
250 cloned.setSchedule( buildDefinition.getSchedule() );
251 cloned.setType( buildDefinition.getType() );
252 cloned.setTemplate( buildDefinition.isTemplate() );
253 cloned.setUpdatePolicy( buildDefinition.getUpdatePolicy() );
254 return cloned;
255 }
256
257
258 public BuildDefinitionTemplate getContinuumDefaultWithType( String type )
259 throws BuildDefinitionServiceException
260 {
261 try
262 {
263 return buildDefinitionTemplateDao.getContinuumBuildDefinitionTemplateWithType( type );
264 }
265 catch ( ContinuumStoreException e )
266 {
267 throw new BuildDefinitionServiceException( e.getMessage(), e );
268 }
269 }
270
271 public BuildDefinitionTemplate getDefaultAntBuildDefinitionTemplate()
272 throws BuildDefinitionServiceException
273 {
274 BuildDefinitionTemplate template = getContinuumDefaultWithType(
275 ContinuumBuildExecutorConstants.ANT_BUILD_EXECUTOR );
276 if ( template != null )
277 {
278 return template;
279 }
280 log.info( "create default AntBuildDefinitionTemplate" );
281 template = new BuildDefinitionTemplate();
282 template.setContinuumDefault( true );
283 template.setName( "Default Ant Template" );
284 template.setType( ContinuumBuildExecutorConstants.ANT_BUILD_EXECUTOR );
285
286 template = addBuildDefinitionTemplate( template );
287
288 BuildDefinition bd = new BuildDefinition();
289
290 bd.setDefaultForProject( true );
291
292 bd.setGoals( defaultAntGoals );
293
294 bd.setArguments( defaultAntArguments );
295
296 bd.setBuildFile( "build.xml" );
297
298 bd.setSchedule( getDefaultSchedule() );
299
300 bd.setDescription( "Default Ant Build Definition" );
301
302 bd.setTemplate( true );
303
304 bd.setType( ContinuumBuildExecutorConstants.ANT_BUILD_EXECUTOR );
305 return addBuildDefinitionInTemplate( template, bd, true );
306 }
307
308 public BuildDefinitionTemplate getDefaultMavenOneBuildDefinitionTemplate()
309 throws BuildDefinitionServiceException
310 {
311 BuildDefinitionTemplate template = getContinuumDefaultWithType(
312 ContinuumBuildExecutorConstants.MAVEN_ONE_BUILD_EXECUTOR );
313 if ( template != null )
314 {
315 log.debug( "found default maven template " + template.getType() );
316 return template;
317 }
318 log.info( "create default MavenOneBuildDefinitionTemplate" );
319 template = new BuildDefinitionTemplate();
320 template.setContinuumDefault( true );
321 template.setName( "Default Maven 1 Template" );
322 template.setType( ContinuumBuildExecutorConstants.MAVEN_ONE_BUILD_EXECUTOR );
323
324 template = addBuildDefinitionTemplate( template );
325
326 BuildDefinition bd = new BuildDefinition();
327
328 bd.setDefaultForProject( true );
329
330 bd.setArguments( defaultM1Arguments );
331
332 bd.setGoals( defaultM1Goals );
333
334 bd.setBuildFile( "project.xml" );
335
336 bd.setSchedule( getDefaultSchedule() );
337
338 bd.setType( ContinuumBuildExecutorConstants.MAVEN_ONE_BUILD_EXECUTOR );
339
340 bd.setDescription( "Default Maven 1 Build Definition" );
341
342 bd.setTemplate( true );
343
344 return addBuildDefinitionInTemplate( template, bd, true );
345 }
346
347 public BuildDefinitionTemplate getDefaultMavenTwoBuildDefinitionTemplate()
348 throws BuildDefinitionServiceException
349 {
350 BuildDefinitionTemplate template = getContinuumDefaultWithType(
351 ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
352 if ( template != null )
353 {
354 return template;
355 }
356 log.info( "create default MavenTwoBuildDefinitionTemplate" );
357 template = new BuildDefinitionTemplate();
358 template.setContinuumDefault( true );
359 template.setName( "Default Maven Template" );
360 template.setType( ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
361
362 template = addBuildDefinitionTemplate( template );
363
364 BuildDefinition bd = new BuildDefinition();
365
366 bd.setDefaultForProject( true );
367
368 bd.setGoals( this.defaultM2Goals );
369
370 bd.setArguments( this.defaultM2Arguments );
371
372 bd.setBuildFile( "pom.xml" );
373
374 bd.setSchedule( getDefaultSchedule() );
375
376 bd.setType( ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
377
378 bd.setDescription( "Default Maven Build Definition" );
379
380 bd.setTemplate( true );
381
382 return addBuildDefinitionInTemplate( template, bd, true );
383 }
384
385 public BuildDefinitionTemplate getDefaultShellBuildDefinitionTemplate()
386 throws BuildDefinitionServiceException
387 {
388 BuildDefinitionTemplate template = getContinuumDefaultWithType(
389 ContinuumBuildExecutorConstants.SHELL_BUILD_EXECUTOR );
390 if ( template != null )
391 {
392 return template;
393 }
394 log.info( "create default ShellBuildDefinitionTemplate" );
395 template = new BuildDefinitionTemplate();
396 template.setContinuumDefault( true );
397 template.setName( "Default Shell Template" );
398 template.setType( ContinuumBuildExecutorConstants.SHELL_BUILD_EXECUTOR );
399
400 template = addBuildDefinitionTemplate( template );
401
402 BuildDefinition bd = new BuildDefinition();
403
404 bd.setDefaultForProject( true );
405
406 bd.setSchedule( getDefaultSchedule() );
407
408 bd.setType( ContinuumBuildExecutorConstants.SHELL_BUILD_EXECUTOR );
409
410 bd.setTemplate( true );
411
412 bd.setDescription( "Default Shell Build Definition" );
413
414 return addBuildDefinitionInTemplate( template, bd, true );
415 }
416
417 private Schedule getDefaultSchedule()
418 throws BuildDefinitionServiceException
419 {
420 try
421 {
422 return configurationService.getDefaultSchedule();
423 }
424 catch ( ContinuumStoreException e )
425 {
426 throw new BuildDefinitionServiceException( e.getMessage(), e );
427 }
428 catch ( ConfigurationLoadingException e )
429 {
430 throw new BuildDefinitionServiceException( e.getMessage(), e );
431 }
432 catch ( ContinuumConfigurationException e )
433 {
434 throw new BuildDefinitionServiceException( e.getMessage(), e );
435 }
436 catch ( BuildQueueServiceException e )
437 {
438 throw new BuildDefinitionServiceException( e.getMessage(), e );
439 }
440 }
441
442
443
444
445
446 public List<BuildDefinitionTemplate> getAllBuildDefinitionTemplate()
447 throws BuildDefinitionServiceException
448 {
449 try
450 {
451 return buildDefinitionTemplateDao.getAllBuildDefinitionTemplate();
452 }
453 catch ( ContinuumStoreException e )
454 {
455 throw new BuildDefinitionServiceException( e.getMessage(), e );
456 }
457 }
458
459 public BuildDefinitionTemplate getBuildDefinitionTemplate( int id )
460 throws BuildDefinitionServiceException
461 {
462 try
463 {
464 return buildDefinitionTemplateDao.getBuildDefinitionTemplate( id );
465 }
466 catch ( ContinuumStoreException e )
467 {
468 throw new BuildDefinitionServiceException( e.getMessage(), e );
469 }
470 }
471
472 public void removeBuildDefinitionTemplate( BuildDefinitionTemplate buildDefinitionTemplate )
473 throws BuildDefinitionServiceException
474 {
475 try
476 {
477
478
479 buildDefinitionTemplate.setBuildDefinitions( null );
480 buildDefinitionTemplate = buildDefinitionTemplateDao.updateBuildDefinitionTemplate(
481 buildDefinitionTemplate );
482 buildDefinitionTemplateDao.removeBuildDefinitionTemplate( buildDefinitionTemplate );
483 }
484 catch ( ContinuumStoreException e )
485 {
486 throw new BuildDefinitionServiceException( e.getMessage(), e );
487 }
488 }
489
490 public BuildDefinitionTemplate updateBuildDefinitionTemplate( BuildDefinitionTemplate buildDefinitionTemplate )
491 throws BuildDefinitionServiceException
492 {
493 try
494 {
495 if ( !hasDuplicateTemplateName( buildDefinitionTemplate ) )
496 {
497 BuildDefinitionTemplate stored = getBuildDefinitionTemplate( buildDefinitionTemplate.getId() );
498 stored.setName( buildDefinitionTemplate.getName() );
499 stored.setBuildDefinitions( buildDefinitionTemplate.getBuildDefinitions() );
500 return buildDefinitionTemplateDao.updateBuildDefinitionTemplate( stored );
501 }
502 }
503 catch ( ContinuumStoreException e )
504 {
505 throw new BuildDefinitionServiceException( e.getMessage(), e );
506 }
507
508 return null;
509 }
510
511 public BuildDefinitionTemplate addBuildDefinitionTemplate( BuildDefinitionTemplate buildDefinitionTemplate )
512 throws BuildDefinitionServiceException
513 {
514 try
515 {
516 if ( !hasDuplicateTemplateName( buildDefinitionTemplate ) )
517 {
518 return buildDefinitionTemplateDao.addBuildDefinitionTemplate( buildDefinitionTemplate );
519 }
520 }
521 catch ( ContinuumStoreException e )
522 {
523 throw new BuildDefinitionServiceException( e.getMessage(), e );
524 }
525
526 return null;
527 }
528
529 public BuildDefinitionTemplate addBuildDefinitionInTemplate( BuildDefinitionTemplate buildDefinitionTemplate,
530 BuildDefinition buildDefinition, boolean template )
531 throws BuildDefinitionServiceException
532 {
533 try
534 {
535 BuildDefinitionTemplate stored = getBuildDefinitionTemplate( buildDefinitionTemplate.getId() );
536 stored.setName( buildDefinitionTemplate.getName() );
537 BuildDefinition storedBuildDefinition = getBuildDefinition( buildDefinition.getId() );
538 if ( storedBuildDefinition != null )
539 {
540 buildDefinition = storedBuildDefinition;
541 }
542 buildDefinition.setTemplate( template );
543
544 stored.addBuildDefinition( buildDefinition );
545 return buildDefinitionTemplateDao.updateBuildDefinitionTemplate( stored );
546 }
547 catch ( ContinuumStoreException e )
548 {
549 throw new BuildDefinitionServiceException( e.getMessage(), e );
550 }
551 }
552
553 public BuildDefinitionTemplate removeBuildDefinitionFromTemplate( BuildDefinitionTemplate buildDefinitionTemplate,
554 BuildDefinition buildDefinition )
555 throws BuildDefinitionServiceException
556 {
557 try
558 {
559 BuildDefinitionTemplate stored = getBuildDefinitionTemplate( buildDefinitionTemplate.getId() );
560 stored.setName( buildDefinitionTemplate.getName() );
561 List<BuildDefinition> buildDefinitions = new ArrayList<BuildDefinition>();
562 for ( int i = 0, size = stored.getBuildDefinitions().size(); i < size; i++ )
563 {
564 BuildDefinition buildDef = (BuildDefinition) stored.getBuildDefinitions().get( i );
565 if ( buildDef.getId() != buildDefinition.getId() )
566 {
567 buildDefinitions.add( getBuildDefinition( buildDef.getId() ) );
568 }
569 }
570 stored.setBuildDefinitions( buildDefinitions );
571 return buildDefinitionTemplateDao.updateBuildDefinitionTemplate( stored );
572 }
573 catch ( ContinuumStoreException e )
574 {
575 throw new BuildDefinitionServiceException( e.getMessage(), e );
576 }
577
578 }
579
580 public void addTemplateInProject( int buildDefinitionTemplateId, Project project )
581 throws BuildDefinitionServiceException
582 {
583 try
584 {
585 BuildDefinitionTemplate template = getBuildDefinitionTemplate( buildDefinitionTemplateId );
586 if ( template.getBuildDefinitions().isEmpty() )
587 {
588 return;
589 }
590 project = projectDao.getProjectWithBuildDetails( project.getId() );
591
592 for ( BuildDefinition bd : (List<BuildDefinition>) template.getBuildDefinitions() )
593 {
594 bd = cloneBuildDefinition( bd );
595 bd.setTemplate( false );
596 bd = buildDefinitionDao.addBuildDefinition( bd );
597 project.addBuildDefinition( bd );
598 }
599 projectDao.updateProject( project );
600
601 }
602 catch ( ContinuumStoreException e )
603 {
604 throw new BuildDefinitionServiceException( e.getMessage(), e );
605 }
606 }
607
608 public ProjectGroup addBuildDefinitionTemplateToProjectGroup( int projectGroupId, BuildDefinitionTemplate template )
609 throws BuildDefinitionServiceException, ContinuumObjectNotFoundException
610 {
611 try
612 {
613 ProjectGroup projectGroup = projectGroupDao.getProjectGroupWithBuildDetailsByProjectGroupId(
614 projectGroupId );
615 if ( template.getBuildDefinitions().isEmpty() )
616 {
617 return null;
618 }
619
620 for ( BuildDefinition bd : (List<BuildDefinition>) template.getBuildDefinitions() )
621 {
622 bd.setTemplate( false );
623 bd = buildDefinitionDao.addBuildDefinition( cloneBuildDefinition( bd ) );
624 projectGroup.addBuildDefinition( bd );
625 }
626 projectGroupDao.updateProjectGroup( projectGroup );
627 return projectGroup;
628
629 }
630 catch ( ContinuumStoreException e )
631 {
632 throw new BuildDefinitionServiceException( e.getMessage(), e );
633 }
634 }
635
636 public List<BuildDefinitionTemplate> getBuildDefinitionTemplatesWithType( String type )
637 throws BuildDefinitionServiceException
638 {
639 try
640 {
641 return buildDefinitionTemplateDao.getBuildDefinitionTemplatesWithType( type );
642 }
643 catch ( ContinuumStoreException e )
644 {
645 throw new BuildDefinitionServiceException( e.getMessage(), e );
646 }
647 }
648
649 public List<BuildDefinitionTemplate> getContinuumBuildDefinitionTemplates()
650 throws BuildDefinitionServiceException
651 {
652 try
653 {
654 return buildDefinitionTemplateDao.getContinuumBuildDefinitionTemplates();
655 }
656 catch ( ContinuumStoreException e )
657 {
658 throw new BuildDefinitionServiceException( e.getMessage(), e );
659 }
660 }
661
662 private boolean hasDuplicateTemplateName( BuildDefinitionTemplate buildDefinitionTemplate )
663 throws BuildDefinitionServiceException
664 {
665 boolean isDuplicate = false;
666 List<BuildDefinitionTemplate> allBuildDefinitionTemplate = this.getAllBuildDefinitionTemplate();
667
668 for ( BuildDefinitionTemplate template : allBuildDefinitionTemplate )
669 {
670 String name = buildDefinitionTemplate.getName();
671 if ( ( template.getId() != buildDefinitionTemplate.getId() ) && ( template.getName().equals( name ) ) )
672 {
673 isDuplicate = true;
674 break;
675 }
676 }
677 return isDuplicate;
678 }
679 }