001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
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 */
020package org.sonar.batch.phases;
021
022import org.apache.commons.lang.StringUtils;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.sonar.api.BatchComponent;
026import org.sonar.api.batch.BatchExtensionDictionnary;
027import org.sonar.api.batch.PostJob;
028import org.sonar.api.batch.SensorContext;
029import org.sonar.api.batch.bootstrap.ProjectDefinition;
030import org.sonar.api.batch.maven.DependsUponMavenPlugin;
031import org.sonar.api.batch.maven.MavenPluginHandler;
032import org.sonar.api.resources.Project;
033import org.sonar.batch.MavenPluginExecutor;
034
035import java.util.Collection;
036
037public class PostJobsExecutor implements BatchComponent {
038  private static final Logger LOG = LoggerFactory.getLogger(PostJobsExecutor.class);
039
040  private MavenPluginExecutor mavenExecutor;
041  private ProjectDefinition projectDefinition;
042  private Project project;
043  private BatchExtensionDictionnary selector;
044
045  public PostJobsExecutor(BatchExtensionDictionnary selector, Project project, ProjectDefinition projectDefinition, MavenPluginExecutor mavenExecutor) {
046    this.selector = selector;
047    this.mavenExecutor = mavenExecutor;
048    this.project = project;
049    this.projectDefinition = projectDefinition;
050  }
051
052  public void execute(SensorContext context) {
053    Collection<PostJob> postJobs = selector.select(PostJob.class, project, true);
054    execute(context, postJobs);
055  }
056
057  void execute(SensorContext context, Collection<PostJob> postJobs) {
058    logPostJobs(postJobs);
059
060    for (PostJob postJob : postJobs) {
061      LOG.info("Executing post-job {}", postJob.getClass());
062      executeMavenPlugin(postJob);
063      postJob.executeOn(project, context);
064    }
065  }
066
067  private void logPostJobs(Collection<PostJob> postJobs) {
068    if (LOG.isDebugEnabled()) {
069      LOG.debug("Post-jobs : {}", StringUtils.join(postJobs, " -> "));
070    }
071  }
072
073  private void executeMavenPlugin(PostJob job) {
074    if (job instanceof DependsUponMavenPlugin) {
075      MavenPluginHandler handler = ((DependsUponMavenPlugin) job).getMavenPluginHandler(project);
076      if (handler != null) {
077        mavenExecutor.execute(project, projectDefinition, handler);
078      }
079    }
080  }
081}