1 package org.apache.maven.continuum.configuration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import javax.annotation.Resource;
28
29 import org.apache.continuum.buildqueue.BuildQueueService;
30 import org.apache.continuum.buildqueue.BuildQueueServiceException;
31 import org.apache.continuum.configuration.BuildAgentConfiguration;
32 import org.apache.continuum.configuration.BuildAgentGroupConfiguration;
33 import org.apache.continuum.configuration.ContinuumConfiguration;
34 import org.apache.continuum.configuration.ContinuumConfigurationException;
35 import org.apache.continuum.configuration.GeneralConfiguration;
36 import org.apache.continuum.dao.ScheduleDao;
37 import org.apache.continuum.dao.SystemConfigurationDao;
38 import org.apache.maven.continuum.model.project.BuildQueue;
39 import org.apache.maven.continuum.model.project.Schedule;
40 import org.apache.maven.continuum.model.system.SystemConfiguration;
41 import org.apache.maven.continuum.store.ContinuumStoreException;
42 import org.codehaus.plexus.util.FileUtils;
43 import org.codehaus.plexus.util.StringUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48
49
50
51 public class DefaultConfigurationService
52 implements ConfigurationService
53 {
54 private static final Logger log = LoggerFactory.getLogger( DefaultConfigurationService.class );
55
56
57
58
59
60
61 private File applicationHome;
62
63 @Resource
64 private ScheduleDao scheduleDao;
65
66 @Resource
67 private SystemConfigurationDao systemConfigurationDao;
68
69 @Resource
70 private BuildQueueService buildQueueService;
71
72 @Resource
73 private ContinuumConfiguration configuration;
74
75 private GeneralConfiguration generalConfiguration;
76
77
78
79
80
81 private SystemConfiguration systemConf;
82
83 private boolean loaded = false;
84
85
86
87
88
89 public void initialize()
90 throws ConfigurationLoadingException, ContinuumConfigurationException
91 {
92 loadData();
93 }
94
95 public ScheduleDao getScheduleDao()
96 {
97 return scheduleDao;
98 }
99
100 public void setScheduleDao( ScheduleDao scheduleDao )
101 {
102 this.scheduleDao = scheduleDao;
103 }
104
105 public BuildQueueService getBuildQueueService()
106 {
107 return buildQueueService;
108 }
109
110 public void setBuildQueueService( BuildQueueService buildQueueService )
111 {
112 this.buildQueueService = buildQueueService;
113 }
114
115 public SystemConfigurationDao getSystemConfigurationDao()
116 {
117 return systemConfigurationDao;
118 }
119
120 public void setSystemConfigurationDao( SystemConfigurationDao systemConfigurationDao )
121 {
122 this.systemConfigurationDao = systemConfigurationDao;
123 }
124
125 public ContinuumConfiguration getConfiguration()
126 {
127 return configuration;
128 }
129
130 public void setConfiguration( ContinuumConfiguration configuration )
131 {
132 this.configuration = configuration;
133 }
134
135 public File getApplicationHome()
136 {
137 return applicationHome;
138 }
139
140 public void setApplicationHome( File applicationHome )
141 {
142 this.applicationHome = applicationHome;
143 }
144
145 public void setInitialized( boolean initialized )
146 {
147 systemConf.setInitialized( initialized );
148 }
149
150 public boolean isInitialized()
151 {
152 return systemConf.isInitialized();
153 }
154
155 public String getUrl()
156 {
157 String baseUrl = generalConfiguration.getBaseUrl();
158 if ( StringUtils.isEmpty( baseUrl ) )
159 {
160 baseUrl = systemConf.getBaseUrl();
161 setUrl( baseUrl );
162 }
163 return baseUrl != null ? baseUrl : "";
164 }
165
166 public void setUrl( String url )
167 {
168 generalConfiguration.setBaseUrl( url );
169 }
170
171
172
173
174 public File getBuildOutputDirectory()
175 {
176 File buildOutputDirectory = generalConfiguration.getBuildOutputDirectory();
177 if ( buildOutputDirectory == null )
178 {
179 buildOutputDirectory = getFile( systemConf.getBuildOutputDirectory() );
180 setBuildOutputDirectory( buildOutputDirectory );
181 }
182 return buildOutputDirectory;
183 }
184
185 public void setBuildOutputDirectory( File buildOutputDirectory )
186 {
187 File f = buildOutputDirectory;
188 try
189 {
190 f = f.getCanonicalFile();
191 }
192 catch ( IOException e )
193 {
194 }
195 generalConfiguration.setBuildOutputDirectory( f );
196 }
197
198 public File getWorkingDirectory()
199 {
200 File workingDirectory = generalConfiguration.getWorkingDirectory();
201 if ( workingDirectory == null )
202 {
203 workingDirectory = getFile( systemConf.getWorkingDirectory() );
204 setWorkingDirectory( workingDirectory );
205 }
206 return workingDirectory;
207 }
208
209 public void setWorkingDirectory( File workingDirectory )
210 {
211 File f = workingDirectory;
212 try
213 {
214 f = f.getCanonicalFile();
215 }
216 catch ( IOException e )
217 {
218 }
219
220 generalConfiguration.setWorkingDirectory( f );
221 }
222
223 public File getDeploymentRepositoryDirectory()
224 {
225 File deploymentDirectory = generalConfiguration.getDeploymentRepositoryDirectory();
226 if ( deploymentDirectory == null )
227 {
228 deploymentDirectory = getFile( systemConf.getDeploymentRepositoryDirectory() );
229 setDeploymentRepositoryDirectory( deploymentDirectory );
230 }
231 return deploymentDirectory;
232 }
233
234 public void setDeploymentRepositoryDirectory( File deploymentRepositoryDirectory )
235 {
236 generalConfiguration.setDeploymentRepositoryDirectory( deploymentRepositoryDirectory );
237 }
238
239 public String getBuildOutput( int buildId, int projectId )
240 throws ConfigurationException
241 {
242 File file = getBuildOutputFile( buildId, projectId );
243
244 try
245 {
246 if ( file.exists() )
247 {
248 return FileUtils.fileRead( file.getAbsolutePath() );
249 }
250 else
251 {
252 return "There are no output for this build.";
253 }
254 }
255 catch ( IOException e )
256 {
257 log.warn( "Error reading build output for build '" + buildId + "'.", e );
258
259 return null;
260 }
261 }
262
263 public File getReleaseOutputDirectory()
264 {
265 File releaseOutputDirectory = generalConfiguration.getReleaseOutputDirectory();
266
267 if ( releaseOutputDirectory == null )
268 {
269 releaseOutputDirectory = getFile( systemConf.getReleaseOutputDirectory() );
270 setReleaseOutputDirectory( releaseOutputDirectory );
271 }
272 return releaseOutputDirectory;
273 }
274
275 public void setReleaseOutputDirectory( File releaseOutputDirectory )
276 {
277 if ( releaseOutputDirectory == null )
278 {
279 generalConfiguration.setReleaseOutputDirectory( releaseOutputDirectory );
280 return;
281 }
282
283 File f = releaseOutputDirectory;
284 try
285 {
286 f = f.getCanonicalFile();
287 }
288 catch ( IOException e )
289 {
290 }
291 generalConfiguration.setReleaseOutputDirectory( f );
292 }
293
294 public List<BuildAgentConfiguration> getBuildAgents()
295 {
296 return generalConfiguration.getBuildAgents();
297 }
298
299 public void addBuildAgent( BuildAgentConfiguration buildAgent )
300 throws ConfigurationException
301 {
302 List<BuildAgentConfiguration> buildAgents = generalConfiguration.getBuildAgents();
303 if ( buildAgents == null )
304 {
305 buildAgents = new ArrayList<BuildAgentConfiguration>();
306 }
307
308 for ( BuildAgentConfiguration agent : buildAgents )
309 {
310 if ( agent.getUrl().equals( buildAgent.getUrl() ) )
311 {
312 throw new ConfigurationException( "Unable to add build agent: build agent already exist" );
313 }
314 }
315
316 buildAgents.add( buildAgent );
317 generalConfiguration.setBuildAgents( buildAgents );
318 }
319
320 public void removeBuildAgent( BuildAgentConfiguration buildAgent )
321 {
322 List<BuildAgentConfiguration> buildAgents = getBuildAgents();
323 if ( buildAgents != null )
324 {
325 for ( BuildAgentConfiguration agent : buildAgents )
326 {
327 if ( agent.getUrl().equals( buildAgent.getUrl() ) )
328 {
329 buildAgents.remove( agent );
330 break;
331 }
332 }
333 generalConfiguration.setBuildAgents( buildAgents );
334 }
335 }
336
337 public void updateBuildAgent( BuildAgentConfiguration buildAgent )
338 {
339 List<BuildAgentConfiguration> buildAgents = getBuildAgents();
340 if ( buildAgents != null )
341 {
342 for ( BuildAgentConfiguration agent : buildAgents )
343 {
344 if ( agent.getUrl().equals( buildAgent.getUrl() ) )
345 {
346 agent.setDescription( buildAgent.getDescription() );
347 agent.setEnabled( buildAgent.isEnabled() );
348
349 return;
350 }
351 }
352 }
353 }
354
355 public boolean isDistributedBuildEnabled()
356 {
357 return systemConf.isDistributedBuildEnabled();
358 }
359
360 public void setDistributedBuildEnabled( boolean distributedBuildEnabled )
361 {
362 systemConf.setDistributedBuildEnabled( distributedBuildEnabled );
363 }
364
365 public void addBuildAgentGroup( BuildAgentGroupConfiguration buildAgentGroup )
366 throws ConfigurationException
367 {
368 List<BuildAgentGroupConfiguration> buildAgentGroups = generalConfiguration.getBuildAgentGroups();
369
370 if ( buildAgentGroups == null )
371 {
372 buildAgentGroups = new ArrayList<BuildAgentGroupConfiguration>();
373 }
374
375 for ( BuildAgentGroupConfiguration groups : buildAgentGroups )
376 {
377 if ( groups.getName().equals( buildAgentGroup.getName() ) )
378 {
379 throw new ConfigurationException( "Unable to add build agent group: build agent group already exist" );
380 }
381 }
382
383 buildAgentGroups.add( buildAgentGroup );
384 generalConfiguration.setBuildAgentGroups( buildAgentGroups );
385 }
386
387 public void removeBuildAgentGroup( BuildAgentGroupConfiguration buildAgentGroup )
388 throws ConfigurationException
389 {
390 List<BuildAgentGroupConfiguration> buildAgentGroups = generalConfiguration.getBuildAgentGroups();
391 if ( buildAgentGroups != null )
392 {
393 for ( BuildAgentGroupConfiguration groups : buildAgentGroups )
394 {
395 if ( groups.getName().equals( buildAgentGroup.getName() ) )
396 {
397 buildAgentGroups.remove( groups );
398 break;
399 }
400 }
401 generalConfiguration.setBuildAgentGroups( buildAgentGroups );
402 }
403 }
404
405 public void updateBuildAgentGroup( BuildAgentGroupConfiguration buildAgentGroup )
406 throws ConfigurationException
407 {
408 List<BuildAgentGroupConfiguration> buildAgentGroups = generalConfiguration.getBuildAgentGroups();
409 if ( buildAgentGroups != null )
410 {
411 for ( BuildAgentGroupConfiguration groups : buildAgentGroups )
412 {
413 if ( groups.getName().equals( buildAgentGroup.getName() ) )
414 {
415 groups.setName( buildAgentGroup.getName() );
416 groups.setBuildAgents( buildAgentGroup.getBuildAgents() );
417
418 return;
419 }
420 }
421 }
422
423 }
424
425 public void addBuildAgent( BuildAgentGroupConfiguration buildAgentGroup, BuildAgentConfiguration buildAgent )
426 throws ConfigurationException
427 {
428 List<BuildAgentGroupConfiguration> buildAgentGroupConfiguration = generalConfiguration.getBuildAgentGroups();
429 if ( buildAgentGroupConfiguration != null )
430 {
431 for ( BuildAgentGroupConfiguration group : buildAgentGroupConfiguration )
432 {
433 if ( group.getName().equals( buildAgentGroup.getName() ) )
434 {
435 List<BuildAgentConfiguration> agents = group.getBuildAgents();
436
437 for ( BuildAgentConfiguration agent : agents )
438 {
439 if ( agent.getUrl().equals( buildAgent.getUrl() ) )
440 {
441 throw new ConfigurationException( "Unable to add build agent : build agent already exist" );
442 }
443 }
444 group.addBuildAgent( buildAgent );
445 break;
446 }
447 }
448 generalConfiguration.setBuildAgentGroups( buildAgentGroupConfiguration );
449 }
450 }
451
452 public void removeBuildAgent( BuildAgentGroupConfiguration buildAgentGroup, BuildAgentConfiguration buildAgent )
453 throws ConfigurationException
454 {
455 List<BuildAgentGroupConfiguration> buildAgentGroupConfiguration = generalConfiguration.getBuildAgentGroups();
456 if ( buildAgentGroupConfiguration != null )
457 {
458 for ( BuildAgentGroupConfiguration group : buildAgentGroupConfiguration )
459 {
460 if ( group.getName().equals( buildAgentGroup.getName() ) )
461 {
462 List<BuildAgentConfiguration> agents = group.getBuildAgents();
463
464 for ( BuildAgentConfiguration agent : agents )
465 {
466 if ( agent.getUrl().equals( buildAgent.getUrl() ) )
467 {
468 group.removeBuildAgent( agent );
469 break;
470 }
471 }
472 }
473 }
474 generalConfiguration.setBuildAgentGroups( buildAgentGroupConfiguration );
475 }
476 }
477
478 public BuildAgentGroupConfiguration getBuildAgentGroup( String name )
479 {
480 List<BuildAgentGroupConfiguration> buildAgentGroupConfiguration = generalConfiguration.getBuildAgentGroups();
481 if ( buildAgentGroupConfiguration != null )
482 {
483 for ( BuildAgentGroupConfiguration buildAgentGroup : buildAgentGroupConfiguration )
484 {
485 if ( buildAgentGroup.getName().equals( name ) )
486 {
487 return buildAgentGroup;
488 }
489 }
490 }
491
492 return null;
493 }
494
495 public BuildAgentConfiguration getBuildAgent( String url )
496 {
497 List<BuildAgentConfiguration> buildAgents = generalConfiguration.getBuildAgents();
498 if ( buildAgents == null )
499 {
500 buildAgents = new ArrayList<BuildAgentConfiguration>();
501 }
502
503 for ( BuildAgentConfiguration agent : buildAgents )
504 {
505 if ( agent.getUrl().equals( url ) )
506 {
507 return agent;
508 }
509 }
510 return null;
511 }
512
513 public List<BuildAgentGroupConfiguration> getBuildAgentGroups()
514 {
515 return generalConfiguration.getBuildAgentGroups();
516 }
517
518 public boolean containsBuildAgentUrl( String buildAgentUrl, BuildAgentGroupConfiguration buildAgentGroup )
519 {
520 BuildAgentGroupConfiguration group = this.getBuildAgentGroup( buildAgentGroup.getName() );
521 List<BuildAgentConfiguration> buildAgents = group.getBuildAgents();
522 if ( buildAgents == null )
523 {
524 buildAgents = new ArrayList<BuildAgentConfiguration>();
525 }
526
527 for ( BuildAgentConfiguration agent : buildAgents )
528 {
529 if ( agent.getUrl().equals( buildAgentUrl ) )
530 {
531 return true;
532 }
533 }
534 return false;
535 }
536
537
538
539
540
541
542 public File getBuildOutputDirectory( int projectId )
543 {
544 File dir = new File( getBuildOutputDirectory(), Integer.toString( projectId ) );
545
546 try
547 {
548 dir = dir.getCanonicalFile();
549 }
550 catch ( IOException e )
551 {
552 }
553
554 return dir;
555 }
556
557 public File getTestReportsDirectory( int buildId, int projectId )
558 throws ConfigurationException
559 {
560 File ouputDirectory = getBuildOutputDirectory( projectId );
561
562 return new File(
563 ouputDirectory.getPath() + File.separatorChar + buildId + File.separatorChar + "surefire-reports" );
564
565 }
566
567 public File getBuildOutputFile( int buildId, int projectId )
568 throws ConfigurationException
569 {
570 File dir = getBuildOutputDirectory( projectId );
571
572 if ( !dir.exists() && !dir.mkdirs() )
573 {
574 throw new ConfigurationException(
575 "Could not make the build output directory: " + "'" + dir.getAbsolutePath() + "'." );
576 }
577
578 return new File( dir, buildId + ".log.txt" );
579 }
580
581 public File getReleaseOutputDirectory( int projectGroupId )
582 {
583 if ( getReleaseOutputDirectory() == null )
584 {
585 return null;
586 }
587
588 File dir = new File( getReleaseOutputDirectory(), Integer.toString( projectGroupId ) );
589
590 try
591 {
592 dir = dir.getCanonicalFile();
593 }
594 catch ( IOException e )
595 {
596 }
597
598 return dir;
599 }
600
601 public File getReleaseOutputFile( int projectGroupId, String name )
602 throws ConfigurationException
603 {
604 File dir = getReleaseOutputDirectory( projectGroupId );
605
606 if ( dir == null )
607 {
608 return null;
609 }
610
611 if ( !dir.exists() && !dir.mkdirs() )
612 {
613 throw new ConfigurationException(
614 "Could not make the release output directory: " + "'" + dir.getAbsolutePath() + "'." );
615 }
616
617 return new File( dir, name + ".log.txt" );
618 }
619
620 public String getReleaseOutput( int projectGroupId, String name )
621 throws ConfigurationException
622 {
623 File file = getReleaseOutputFile( projectGroupId, name );
624
625 try
626 {
627 if ( file.exists() )
628 {
629 return FileUtils.fileRead( file.getAbsolutePath() );
630 }
631 else
632 {
633 return "There are no output for this release.";
634 }
635 }
636 catch ( IOException e )
637 {
638 log.warn( "Error reading release output for release '" + name + "'.", e );
639 return null;
640 }
641 }
642
643 public int getNumberOfBuildsInParallel()
644 {
645 return generalConfiguration.getNumberOfBuildsInParallel();
646 }
647
648 public void setNumberOfBuildsInParallel( int num )
649 {
650 generalConfiguration.setNumberOfBuildsInParallel( num );
651 }
652
653
654
655
656
657 public File getFile( String filename )
658 {
659 if ( filename == null )
660 {
661 return null;
662 }
663
664 File f = null;
665
666 if ( filename.length() != 0 )
667 {
668 f = new File( filename );
669
670 if ( !f.isAbsolute() )
671 {
672 f = new File( applicationHome, filename );
673 }
674 }
675
676 try
677 {
678 if ( f != null )
679 {
680 return f.getCanonicalFile();
681 }
682 return null;
683 }
684 catch ( IOException e )
685 {
686 return f;
687 }
688 }
689
690
691
692
693
694 public boolean isLoaded()
695 {
696 return loaded;
697 }
698
699
700 private void loadData()
701 throws ConfigurationLoadingException, ContinuumConfigurationException
702 {
703 generalConfiguration = configuration.getGeneralConfiguration();
704
705 try
706 {
707 systemConf = getSystemConfigurationDao().getSystemConfiguration();
708
709 if ( systemConf == null )
710 {
711 systemConf = new SystemConfiguration();
712
713 systemConf = getSystemConfigurationDao().addSystemConfiguration( systemConf );
714 }
715
716 loaded = true;
717 }
718 catch ( ContinuumStoreException e )
719 {
720 throw new ConfigurationLoadingException( "Error reading configuration from database.", e );
721 }
722 }
723
724 public void reload()
725 throws ConfigurationLoadingException, ContinuumConfigurationException
726 {
727 configuration.reload();
728 loadData();
729 }
730
731 public void store()
732 throws ConfigurationStoringException, ContinuumConfigurationException
733 {
734 configuration.setGeneralConfiguration( generalConfiguration );
735 configuration.save();
736 try
737 {
738 getSystemConfigurationDao().updateSystemConfiguration( systemConf );
739 }
740 catch ( ContinuumStoreException e )
741 {
742 throw new ConfigurationStoringException( "Error writting configuration to database.", e );
743 }
744 }
745
746 public Schedule getDefaultSchedule()
747 throws ContinuumStoreException, ConfigurationLoadingException, ContinuumConfigurationException,
748 BuildQueueServiceException
749 {
750
751 Schedule defaultSchedule = scheduleDao.getScheduleByName( DEFAULT_SCHEDULE_NAME );
752
753 if ( defaultSchedule == null )
754 {
755 defaultSchedule = createDefaultSchedule();
756
757 defaultSchedule = scheduleDao.addSchedule( defaultSchedule );
758 }
759
760 return defaultSchedule;
761 }
762
763 public BuildQueue getDefaultBuildQueue()
764 throws BuildQueueServiceException
765 {
766 BuildQueue defaultBuildQueue = buildQueueService.getBuildQueueByName( DEFAULT_BUILD_QUEUE_NAME );
767
768 if ( defaultBuildQueue == null )
769 {
770 defaultBuildQueue = createDefaultBuildQueue();
771
772 defaultBuildQueue = buildQueueService.addBuildQueue( defaultBuildQueue );
773 }
774
775 return defaultBuildQueue;
776 }
777
778
779
780
781
782 private Schedule createDefaultSchedule()
783 throws ConfigurationLoadingException, ContinuumConfigurationException, ContinuumStoreException,
784 BuildQueueServiceException
785 {
786
787 log.info( "create Default Schedule" );
788
789 Schedule schedule = new Schedule();
790
791 schedule.setName( DEFAULT_SCHEDULE_NAME );
792
793
794 if ( systemConf == null )
795 {
796 this.reload();
797 }
798
799 schedule.setDescription( systemConf.getDefaultScheduleDescription() );
800
801 schedule.setCronExpression( systemConf.getDefaultScheduleCronExpression() );
802
803 schedule.setActive( true );
804
805 BuildQueue buildQueue = getDefaultBuildQueue();
806
807 schedule.addBuildQueue( buildQueue );
808
809 return schedule;
810 }
811
812 private BuildQueue createDefaultBuildQueue()
813 {
814 log.info( "create Default Build Queue" );
815
816 BuildQueue buildQueue = new BuildQueue();
817
818 buildQueue.setName( DEFAULT_BUILD_QUEUE_NAME );
819
820 return buildQueue;
821 }
822 }