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 */ 020 package org.sonar.batch.phases; 021 022 import com.google.common.collect.Lists; 023 import org.slf4j.Logger; 024 import org.slf4j.LoggerFactory; 025 import org.sonar.api.BatchComponent; 026 import org.sonar.api.batch.BatchExtensionDictionnary; 027 import org.sonar.api.batch.Sensor; 028 import org.sonar.api.batch.SensorContext; 029 import org.sonar.api.batch.bootstrap.ProjectDefinition; 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.api.utils.TimeProfiler; 034 import org.sonar.batch.MavenPluginExecutor; 035 import org.sonar.batch.events.EventBus; 036 037 import java.util.Collection; 038 039 public class SensorsExecutor implements BatchComponent { 040 private static final Logger LOG = LoggerFactory.getLogger(SensorsExecutor.class); 041 042 private MavenPluginExecutor mavenExecutor; 043 private EventBus eventBus; 044 private Project project; 045 private ProjectDefinition projectDefinition; 046 private BatchExtensionDictionnary selector; 047 048 public SensorsExecutor(BatchExtensionDictionnary selector, Project project, ProjectDefinition projectDefinition, MavenPluginExecutor mavenExecutor, EventBus eventBus) { 049 this.selector = selector; 050 this.mavenExecutor = mavenExecutor; 051 this.eventBus = eventBus; 052 this.project = project; 053 this.projectDefinition = projectDefinition; 054 } 055 056 public void execute(SensorContext context) { 057 Collection<Sensor> sensors = selector.select(Sensor.class, project, true); 058 eventBus.fireEvent(new SensorsPhaseEvent(Lists.newArrayList(sensors), true)); 059 060 for (Sensor sensor : sensors) { 061 executeMavenPlugin(sensor); 062 063 eventBus.fireEvent(new SensorExecutionEvent(sensor, true)); 064 sensor.analyse(project, context); 065 eventBus.fireEvent(new SensorExecutionEvent(sensor, false)); 066 } 067 068 eventBus.fireEvent(new SensorsPhaseEvent(Lists.newArrayList(sensors), false)); 069 } 070 071 private void executeMavenPlugin(Sensor sensor) { 072 if (sensor instanceof DependsUponMavenPlugin) { 073 MavenPluginHandler handler = ((DependsUponMavenPlugin) sensor).getMavenPluginHandler(project); 074 if (handler != null) { 075 TimeProfiler profiler = new TimeProfiler(LOG).start("Execute maven plugin " + handler.getArtifactId()); 076 mavenExecutor.execute(project, projectDefinition, handler); 077 profiler.stop(); 078 } 079 } 080 } 081 }