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.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    import org.sonar.api.BatchComponent;
026    import org.sonar.api.batch.Sensor;
027    import org.sonar.api.batch.SensorContext;
028    import org.sonar.api.batch.maven.DependsUponMavenPlugin;
029    import org.sonar.api.batch.maven.MavenPluginHandler;
030    import org.sonar.api.database.DatabaseSession;
031    import org.sonar.api.resources.Project;
032    import org.sonar.api.utils.TimeProfiler;
033    import org.sonar.batch.bootstrap.BatchExtensionDictionnary;
034    import org.sonar.batch.events.EventBus;
035    import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
036    import org.sonar.batch.scan.maven.MavenPluginExecutor;
037    
038    import java.util.Collection;
039    
040    public class SensorsExecutor implements BatchComponent {
041      private static final Logger LOG = LoggerFactory.getLogger(SensorsExecutor.class);
042    
043      private MavenPluginExecutor mavenExecutor;
044      private EventBus eventBus;
045      private Project module;
046      private DefaultModuleFileSystem fs;
047      private BatchExtensionDictionnary selector;
048      private final DatabaseSession session;
049      private final SensorMatcher sensorMatcher;
050    
051      public SensorsExecutor(BatchExtensionDictionnary selector, Project project, DefaultModuleFileSystem fs, MavenPluginExecutor mavenExecutor, EventBus eventBus,
052        DatabaseSession session, SensorMatcher sensorMatcher) {
053        this.selector = selector;
054        this.mavenExecutor = mavenExecutor;
055        this.eventBus = eventBus;
056        this.module = project;
057        this.fs = fs;
058        this.session = session;
059        this.sensorMatcher = sensorMatcher;
060      }
061    
062      public void execute(SensorContext context) {
063        Collection<Sensor> sensors = selector.select(Sensor.class, module, true, sensorMatcher);
064        eventBus.fireEvent(new SensorsPhaseEvent(Lists.newArrayList(sensors), true));
065    
066        for (Sensor sensor : sensors) {
067          // SONAR-2965 In case the sensor takes too much time we close the session to not face a timeout
068          session.commitAndClose();
069    
070          executeSensor(context, sensor);
071        }
072    
073        eventBus.fireEvent(new SensorsPhaseEvent(Lists.newArrayList(sensors), false));
074      }
075    
076      private void executeSensor(SensorContext context, Sensor sensor) {
077        eventBus.fireEvent(new SensorExecutionEvent(sensor, true));
078        executeMavenPlugin(sensor);
079        sensor.analyse(module, context);
080        eventBus.fireEvent(new SensorExecutionEvent(sensor, false));
081      }
082    
083      private void executeMavenPlugin(Sensor sensor) {
084        if (sensor instanceof DependsUponMavenPlugin) {
085          MavenPluginHandler handler = ((DependsUponMavenPlugin) sensor).getMavenPluginHandler(module);
086          if (handler != null) {
087            TimeProfiler profiler = new TimeProfiler(LOG).start("Execute maven plugin " + handler.getArtifactId());
088            mavenExecutor.execute(module, fs, handler);
089            profiler.stop();
090          }
091        }
092      }
093    }