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.batch.BatchExtensionDictionnary;
026import org.sonar.api.batch.Initializer;
027import org.sonar.api.batch.bootstrap.ProjectDefinition;
028import org.sonar.api.batch.maven.DependsUponMavenPlugin;
029import org.sonar.api.batch.maven.MavenPluginHandler;
030import org.sonar.api.resources.Project;
031import org.sonar.api.utils.TimeProfiler;
032import org.sonar.batch.MavenPluginExecutor;
033
034import java.util.Collection;
035
036public class InitializersExecutor {
037
038  private static final Logger LOG = LoggerFactory.getLogger(SensorsExecutor.class);
039
040  private MavenPluginExecutor mavenExecutor;
041
042  private ProjectDefinition projectDef;
043  private Project project;
044  private BatchExtensionDictionnary selector;
045
046  public InitializersExecutor(BatchExtensionDictionnary selector, Project project, ProjectDefinition projectDef, MavenPluginExecutor mavenExecutor) {
047    this.selector = selector;
048    this.mavenExecutor = mavenExecutor;
049    this.project = project;
050    this.projectDef = projectDef;
051  }
052
053  public void execute() {
054    Collection<Initializer> initializers = selector.select(Initializer.class, project, true);
055    if (LOG.isDebugEnabled()) {
056      LOG.debug("Initializers : {}", StringUtils.join(initializers, " -> "));
057    }
058
059    for (Initializer initializer : initializers) {
060      executeMavenPlugin(initializer);
061
062      TimeProfiler profiler = new TimeProfiler(LOG).start("Initializer " + initializer);
063      initializer.execute(project);
064      profiler.stop();
065    }
066  }
067
068  private void executeMavenPlugin(Initializer sensor) {
069    if (sensor instanceof DependsUponMavenPlugin) {
070      MavenPluginHandler handler = ((DependsUponMavenPlugin) sensor).getMavenPluginHandler(project);
071      if (handler != null) {
072        TimeProfiler profiler = new TimeProfiler(LOG).start("Execute maven plugin " + handler.getArtifactId());
073        mavenExecutor.execute(project, projectDef, handler);
074        profiler.stop();
075      }
076    }
077  }
078
079}