001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 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; 021 022 import org.apache.commons.lang.StringUtils; 023 import org.slf4j.Logger; 024 import org.slf4j.LoggerFactory; 025 import org.sonar.api.batch.BatchExtensionDictionnary; 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 034 import java.util.Collection; 035 036 public class SensorsExecutor implements CoreJob { 037 private static final Logger logger = LoggerFactory.getLogger(SensorsExecutor.class); 038 039 private Collection<Sensor> sensors; 040 private DatabaseSession session; 041 private MavenPluginExecutor mavenExecutor; 042 043 public SensorsExecutor(BatchExtensionDictionnary selector, Project project, DatabaseSession session, MavenPluginExecutor mavenExecutor) { 044 this.sensors = selector.select(Sensor.class, project, true); 045 this.session = session; 046 this.mavenExecutor = mavenExecutor; 047 } 048 049 public void execute(Project project, SensorContext context) { 050 if (logger.isDebugEnabled()) { 051 logger.debug("Sensors : {}", StringUtils.join(sensors, " -> ")); 052 } 053 054 for (Sensor sensor : sensors) { 055 executeMavenPlugin(project, sensor); 056 057 TimeProfiler profiler = new TimeProfiler(logger).start("Sensor "+ sensor); 058 sensor.analyse(project, context); 059 session.commit(); 060 profiler.stop(); 061 } 062 } 063 064 private void executeMavenPlugin(Project project, Sensor sensor) { 065 if (sensor instanceof DependsUponMavenPlugin) { 066 MavenPluginHandler handler = ((DependsUponMavenPlugin) sensor).getMavenPluginHandler(project); 067 if (handler != null) { 068 TimeProfiler profiler = new TimeProfiler(logger).start("Execute maven plugin " + handler.getArtifactId()); 069 mavenExecutor.execute(project, handler); 070 profiler.stop(); 071 } 072 } 073 } 074 }