001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.batch;
021
022 import org.apache.commons.lang.StringUtils;
023 import org.slf4j.Logger;
024 import org.slf4j.LoggerFactory;
025 import org.sonar.api.batch.BatchExtensionDictionnary;
026 import org.sonar.api.batch.PostJob;
027 import org.sonar.api.batch.SensorContext;
028 import org.sonar.api.batch.maven.DependsUponMavenPlugin;
029 import org.sonar.api.batch.maven.MavenPluginHandler;
030 import org.sonar.api.resources.Project;
031
032 import java.util.Collection;
033
034 public class PostJobsExecutor implements CoreJob {
035 private static final Logger LOG = LoggerFactory.getLogger(PostJobsExecutor.class);
036
037 private Collection<PostJob> postJobs;
038 private MavenPluginExecutor mavenExecutor;
039
040 public PostJobsExecutor(Project project, BatchExtensionDictionnary selector, MavenPluginExecutor mavenExecutor) {
041 postJobs = selector.select(PostJob.class, project, true);
042 this.mavenExecutor = mavenExecutor;
043 }
044
045 protected PostJobsExecutor(Collection<PostJob> postJobs, MavenPluginExecutor mavenExecutor) {
046 this.postJobs = postJobs;
047 this.mavenExecutor = mavenExecutor;
048 }
049
050 public void execute(Project project, SensorContext context) {
051 if (shouldExecuteOn(project)) {
052 logPostJobs();
053
054 for (PostJob postJob : postJobs) {
055 LOG.info("Executing post-job {}", postJob.getClass());
056 executeMavenPlugin(project, postJob);
057 postJob.executeOn(project, context);
058 }
059 }
060 }
061
062 private void logPostJobs() {
063 if (LOG.isDebugEnabled()) {
064 LOG.debug("Post-jobs : {}", StringUtils.join(postJobs, " -> "));
065 }
066 }
067
068 private boolean shouldExecuteOn(Project project) {
069 return postJobs != null && project.isRoot();
070 }
071
072
073 private void executeMavenPlugin(Project project, PostJob job) {
074 if (job instanceof DependsUponMavenPlugin) {
075 MavenPluginHandler handler = ((DependsUponMavenPlugin) job).getMavenPluginHandler(project);
076 if (handler != null) {
077 mavenExecutor.execute(project, handler);
078 }
079 }
080 }
081 }