001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.batch.phases;
021
022 import com.google.common.collect.Lists;
023 import org.apache.commons.lang.StringUtils;
024 import org.slf4j.Logger;
025 import org.slf4j.LoggerFactory;
026 import org.sonar.api.BatchComponent;
027 import org.sonar.api.batch.BatchExtensionDictionnary;
028 import org.sonar.api.batch.PostJob;
029 import org.sonar.api.batch.SensorContext;
030 import org.sonar.api.batch.maven.DependsUponMavenPlugin;
031 import org.sonar.api.batch.maven.MavenPluginHandler;
032 import org.sonar.api.resources.Project;
033 import org.sonar.batch.events.EventBus;
034 import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
035 import org.sonar.batch.scan.maven.MavenPluginExecutor;
036
037 import java.util.Collection;
038
039 public class PostJobsExecutor implements BatchComponent {
040 private static final Logger LOG = LoggerFactory.getLogger(PostJobsExecutor.class);
041
042 private final BatchExtensionDictionnary selector;
043 private final Project project;
044 private final DefaultModuleFileSystem fs;
045 private final MavenPluginExecutor mavenExecutor;
046 private final EventBus eventBus;
047
048 public PostJobsExecutor(BatchExtensionDictionnary selector, Project project, DefaultModuleFileSystem fs, MavenPluginExecutor mavenExecutor,
049 EventBus eventBus) {
050 this.selector = selector;
051 this.project = project;
052 this.fs = fs;
053 this.mavenExecutor = mavenExecutor;
054 this.eventBus = eventBus;
055 }
056
057 public void execute(SensorContext context) {
058 Collection<PostJob> postJobs = selector.select(PostJob.class, project, true);
059
060 eventBus.fireEvent(new PostJobPhaseEvent(Lists.newArrayList(postJobs), true));
061 execute(context, postJobs);
062 eventBus.fireEvent(new PostJobPhaseEvent(Lists.newArrayList(postJobs), false));
063 }
064
065 private void execute(SensorContext context, Collection<PostJob> postJobs) {
066 logPostJobs(postJobs);
067
068 for (PostJob postJob : postJobs) {
069 LOG.info("Executing post-job {}", postJob.getClass());
070 eventBus.fireEvent(new PostJobExecutionEvent(postJob, true));
071 executeMavenPlugin(postJob);
072 postJob.executeOn(project, context);
073 eventBus.fireEvent(new PostJobExecutionEvent(postJob, false));
074 }
075 }
076
077 private void logPostJobs(Collection<PostJob> postJobs) {
078 if (LOG.isDebugEnabled()) {
079 LOG.debug("Post-jobs : {}", StringUtils.join(postJobs, " -> "));
080 }
081 }
082
083 private void executeMavenPlugin(PostJob job) {
084 if (job instanceof DependsUponMavenPlugin) {
085 MavenPluginHandler handler = ((DependsUponMavenPlugin) job).getMavenPluginHandler(project);
086 if (handler != null) {
087 mavenExecutor.execute(project, fs, handler);
088 }
089 }
090 }
091 }