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